Debian Codegraph @

R/upgrade.R

raw
1
2 #' Upgrade renv
3 #'
4 #' @description
5 #' Upgrade the version of renv associated with a project, including using
6 #' a development version from GitHub. Automatically snapshots the updated
7 #' renv, updates the activate script, and restarts R.
8 #'
9 #' If you want to update all packages (including renv) to their latest CRAN
10 #' versions, use [renv::update()].
11 #'
12 #' # Note
13 #'
14 #' `upgrade()` is expected to work for renv versions >= 1.0.1.
15 #' To upgrade from prior versions of renv, users should
16 #'
17 #' `renv::deactivate();`
18 #' `install.packages("renv");`
19 #' `renv::activate();`
20 #' `renv::record("renv")`
21 #'
22 #' @inherit renv-params
23 #'
24 #' @param version The version of renv to be installed.
25 #'
26 #' When `NULL` (the default), the latest version of renv will be installed as
27 #' available from CRAN (or whatever active package repositories are active)
28 #' Alternatively, you can install the latest development version with
29 #' `"main"`, or a specific commit with a SHA, e.g. `"5049cef8a"`.
30 #'
31 #' @param prompt Boolean; prompt upgrade before proceeding?
32 #'
33 #' @param reload Boolean; reload renv after install? When `NULL` (the
34 #' default), renv will be re-loaded only if updating renv for the
35 #' active project. Since it's not possible to guarantee a clean reload
36 #' in the current session, this will attempt to restart your R session.
37 #'
38 #' @return A boolean value, indicating whether the requested version of
39 #' renv was successfully installed. Note that this function is normally
40 #' called for its side effects.
41 #'
42 #' @export
43 #'
44 #' @examples
45 #' \dontrun{
46 #'
47 #' # upgrade to the latest version of renv
48 #' renv::upgrade()
49 #'
50 #' # upgrade to the latest version of renv on GitHub (development version)
51 #' renv::upgrade(version = "main")
52 #'
53 #' }
54 upgrade <- function(project = NULL,
55 version = NULL,
56 reload = NULL,
57 prompt = interactive())
58 {
59 renv_scope_error_handler()
60 renv_scope_verbose_if(prompt)
61 invisible(renv_upgrade_impl(project, version, reload, prompt))
62 }
63
64 renv_upgrade_impl <- function(project, version, reload, prompt) {
65
66 project <- renv_project_resolve(project)
67 renv_project_lock(project = project)
68
69 reload <- reload %||% renv_project_loaded(project)
70
71 lockfile <- renv_lockfile_load(project)
72 old <- lockfile$Packages$renv
73 new <- renv_upgrade_find_record(version)
74
75 # check for some form of change
76 if (renv_records_equal(old, new)) {
77 fmt <- "- renv [%s] is already installed and active for this project."
78 writef(fmt, renv_metadata_version_friendly())
79 return(FALSE)
80 }
81
82 if (prompt || renv_verbose()) {
83 renv_pretty_print_records_pair(
84 "A new version of the renv package will be installed:",
85 list(renv = old),
86 list(renv = new),
87 "This project will use the newly-installed version of renv."
88 )
89 }
90
91 cancel_if(prompt && !proceed())
92
93 renv_scope_restore(
94 project = project,
95 library = renv_libpaths_active(),
96 records = list(renv = new),
97 packages = "renv",
98 recursive = FALSE
99 )
100
101 # retrieve and install renv
102 records <- renv_retrieve_impl("renv")
103 renv_install_impl(records)
104
105 # update the lockfile
106 lockfile <- renv_lockfile_load(project = project)
107 records <- renv_lockfile_records(lockfile) %||% list()
108 records$renv <- new
109 renv_lockfile_records(lockfile) <- records
110 renv_lockfile_save(lockfile, project = project)
111
112 # now update the infrastructure to use this version of renv.
113 # do this in a separate process to avoid issues that could arise
114 # if the old version of renv is still loaded
115 #
116 # https://github.com/rstudio/renv/issues/1546
117 writef("- Updating activate script")
118 record <- records[["renv"]]
119
120 # make sure we forward renv.config.autoloader.enabled if set
121 # https://github.com/rstudio/renv/issues/2027
122 autoload <- config$autoloader.enabled()
123 renv_scope_envvars(RENV_CONFIG_AUTOLOADER_ENABLED = autoload)
124
125 code <- expr({
126 renv <- asNamespace("renv"); renv$summon()
127 renv_infrastructure_write(
128 project = !!project,
129 version = !!renv_metadata_version_create(record)
130 )
131 })
132
133 script <- renv_scope_tempfile("renv-activate-", fileext = ".R")
134 writeLines(deparse(code), con = script)
135
136 args <- c("--vanilla", "-s", "-f", renv_shell_path(script))
137 r(args, stdout = FALSE, stderr = FALSE)
138
139 if (reload) {
140 renv_restart_request(project)
141 }
142
143 invisible(TRUE)
144
145 }
146
147 renv_upgrade_find_record <- function(version) {
148
149 if (is.null(version))
150 renv_upgrade_find_record_default()
151 else
152 renv_upgrade_find_record_dev(version)
153
154 }
155
156 renv_upgrade_find_record_default <- function() {
157
158 # check if the package is available on R repositories.
159 # if not, prefer GitHub
160 record <- catch(renv_available_packages_latest("renv"))
161 if (inherits(record, "error"))
162 return(renv_upgrade_find_record_dev())
163
164 # check the version reported by R repositories.
165 # if it's older than current renv, then prefer GitHub
166 version <- record$Version
167 if (package_version(version) < renv_package_version("renv"))
168 return(renv_upgrade_find_record_dev())
169
170 # ok -- install from repository
171 record
172
173 }
174
175 renv_upgrade_find_record_dev <- function(version = NULL) {
176 version <- version %||% renv_upgrade_find_record_dev_latest()
177 entry <- paste("rstudio/renv", version, sep = "@")
178 renv_remotes_resolve(entry)
179 }
180
181
182 renv_upgrade_find_record_dev_latest <- function() {
183
184 # download tags
185 url <- "https://api.github.com/repos/rstudio/renv/tags"
186 destfile <- tempfile("renv-tags-", fileext = ".json")
187 download(url, destfile = destfile, quiet = TRUE)
188 json <- renv_json_read(destfile)
189
190 # find latest version
191 names <- extract_chr(json, "name")
192 versions <- numeric_version(names, strict = FALSE)
193 latest <- sort(versions, decreasing = TRUE)[[1]]
194 names[versions %in% latest][[1L]]
195
196 }