Debian Codegraph @

R/imbue.R

raw
1
2 #' Imbue an renv Installation
3 #'
4 #' Imbue an renv installation into a project, thereby making the requested
5 #' version of renv available within.
6 #'
7 #' Normally, this function does not need to be called directly by the user; it
8 #' will be invoked as required by [init()] and [activate()].
9 #'
10 #' @inherit renv-params
11 #'
12 #' @param version The version of renv to install. If `NULL`, the version
13 #' of renv currently installed will be used. The requested version of
14 #' renv will be retrieved from the renv public GitHub repository,
15 #' at <https://github.com/rstudio/renv>.
16 #'
17 #' @param quiet Boolean; avoid printing output during install of renv?
18 #'
19 imbue <- function(project = NULL,
20 version = NULL,
21 quiet = FALSE)
22 {
23 renv_scope_error_handler()
24 project <- renv_project_resolve(project)
25
26 renv_scope_options(renv.verbose = !quiet)
27
28 vtext <- version %||% renv_metadata_version()
29 writef("Installing renv [%s] ...", vtext)
30 status <- renv_imbue_impl(project, version)
31 writef("- Done! renv has been successfully installed.")
32
33 invisible(status)
34
35 }
36
37 renv_imbue_impl <- function(project,
38 library = NULL,
39 version = NULL,
40 force = FALSE)
41 {
42 # don't imbue during tests unless explicitly requested
43 if (renv_tests_running() && !force)
44 return(NULL)
45
46 # resolve library path
47 library <- library %||% renv_paths_library(project = project)
48 ensure_directory(library)
49
50 # NULL version means imbue this version of renv
51 if (is.null(version))
52 return(renv_imbue_self(project, library = library))
53
54 # otherwise, try to download and install the requested version
55 # of renv from GitHub
56 remote <- paste("rstudio/renv", version %||% "main", sep = "@")
57 record <- renv_remotes_resolve(remote)
58 records <- list(renv = record)
59
60 renv_scope_restore(
61 project = project,
62 library = library,
63 records = records,
64 packages = "renv",
65 recursive = FALSE
66 )
67
68 records <- renv_retrieve_impl("renv")
69 renv_install_impl(records)
70
71 record <- records[["renv"]]
72 invisible(record)
73 }
74
75 renv_imbue_self <- function(project,
76 library = NULL,
77 source = NULL)
78 {
79 # construct source, target paths
80 # (check if 'renv' is loaded to handle embedded case)
81 source <- source %||% {
82 if ("renv" %in% loadedNamespaces()) {
83 renv_namespace_path("renv")
84 } else {
85 renv_package_find("renv")
86 }
87 }
88
89 if (!file.exists(source))
90 stop("internal error: could not find where 'renv' is installed")
91
92 library <- library %||% renv_paths_library(project = project)
93 target <- file.path(library, "renv")
94 if (renv_file_same(source, target))
95 return(TRUE)
96
97 type <- renv_package_type(source, quiet = TRUE)
98 case(
99 type == "source" ~ renv_imbue_self_source(source, target),
100 type == "binary" ~ renv_imbue_self_binary(source, target)
101 )
102
103 }
104
105 renv_imbue_self_source <- function(source, target) {
106
107 # if the package already exists, just skip
108 if (file.exists(target))
109 return(TRUE)
110
111 # otherwise, install it
112 library <- dirname(target)
113 ensure_directory(library)
114 r_cmd_install("renv", source, library)
115
116 }
117
118 renv_imbue_self_binary <- function(source, target) {
119 ensure_parent_directory(target)
120 renv_file_copy(source, target, overwrite = TRUE)
121 }