Debian Codegraph @

R/pak.R

raw
1
2 # the minimum-required version of 'pak' for renv integration
3 the$pak_minver <- numeric_version("0.9.0")
4
5 renv_pak_init <- function(stream = NULL, force = FALSE) {
6
7 if (force || !renv_pak_available()) {
8 stream <- stream %||% renv_pak_stream()
9 renv_pak_init_impl(stream)
10 }
11
12 renv_namespace_load("pak")
13
14 }
15
16 renv_pak_stream <- function() {
17
18 # check if stable is new enough
19 streams <- c("stable", "rc", "devel")
20 for (stream in streams) {
21 repos <- renv_pak_repos(stream)
22 latest <- renv_available_packages_latest("pak", repos = repos)
23 version <- numeric_version(latest$Version)
24 if (version >= the$pak_minver)
25 return(stream)
26 }
27
28 fmt <- "internal error: pak (>= %s) is not available"
29 stopf(fmt, format(the$pak_minver))
30
31 }
32
33 renv_pak_available <- function() {
34 tryCatch(
35 packageVersion("pak") >= the$pak_minver,
36 error = function(e) FALSE
37 )
38 }
39
40 renv_pak_repos <- function(stream) {
41
42 # on macOS, we can only use pak binaries with CRAN R
43 if (renv_platform_macos() && .Platform$pkgType == "source")
44 return(getOption("repos"))
45
46 # otherwise, use pre-built pak binaries
47 fmt <- "https://r-lib.github.io/p/pak/%s/%s/%s/%s"
48 sprintf(fmt, stream, .Platform$pkgType, version$os, version$arch)
49
50 }
51
52 renv_pak_init_impl <- function(stream) {
53
54 renv_scope_options(
55 renv.config.pak.enabled = FALSE,
56 renv.config.ppm.enabled = FALSE,
57 repos = c("r-lib" = renv_pak_repos(stream))
58 )
59
60 library <- renv_libpaths_active()
61 install("pak", library = library)
62 loadNamespace("pak", lib.loc = library)
63
64 }
65
66 renv_pak_update <- function(project, library, prompt) {
67
68 pak <- renv_namespace_load("pak")
69
70 # if this project contains a DESCRIPTION file, use it when
71 # determining which packages to update
72 if (file.exists(file.path(project, "DESCRIPTION"))) {
73
74 result <- pak$local_install_dev_deps(
75 root = project,
76 lib = library[[1L]],
77 ask = prompt
78 )
79
80 return(result)
81 }
82
83 # read description files for all installed packages
84 # TODO: do we want to also update packages in other library paths,
85 # or just packages installed in the project library?
86 records <- renv_snapshot_libpaths(library[[1L]], project = project)
87 remotes <- map_chr(records, renv_record_format_remote, versioned = FALSE, pak = TRUE)
88 if (length(remotes) == 0L) {
89 caution("- There are no packages to update.")
90 return(invisible(NULL))
91 }
92
93 # update those packages
94 pak$pkg_install(
95 pkg = unname(remotes),
96 lib = library[[1L]],
97 upgrade = TRUE,
98 ask = prompt
99 )
100
101 }
102
103 renv_pak_install <- function(packages,
104 library,
105 type,
106 rebuild,
107 prompt,
108 project,
109 include = NULL,
110 exclude = NULL)
111 {
112 pak <- renv_namespace_load("pak")
113
114 # transform repositories
115 if (renv_ppm_enabled()) {
116 repos <- getOption("repos")
117 renv_scope_options(repos = renv_ppm_transform(repos))
118 }
119
120 # make sure pak::pkg_install() still works even if we're
121 # running in renv with devtools::load_all()
122 name <- Sys.getenv("_R_CHECK_PACKAGE_NAME_", unset = NA)
123 if (identical(name, "renv"))
124 renv_scope_envvars("_R_CHECK_PACKAGE_NAME_" = NULL)
125
126 # if we received a named list of remotes, use the names
127 packages <- if (any(nzchar(names(packages))))
128 names(packages)
129 else
130 as.character(packages)
131
132 # remember whether the caller explicitly scoped this install so we can
133 # distinguish "no scope at all" (let pak update the project) from "explicit
134 # scope filtered to empty" (no-op, matching the non-pak path)
135 explicit <- length(packages) > 0L || length(include) > 0L || length(exclude) > 0L
136
137 # apply include / exclude consistently with the non-pak install path,
138 # so the semantics of these arguments don't depend on the installer.
139 # if no packages were specified positionally but include was, treat it
140 # as the request set (e.g. install(include = ...))
141 # https://github.com/rstudio/renv/issues/2281
142 if (length(packages) == 0L && length(include))
143 packages <- as.character(include)
144
145 if (length(exclude))
146 packages <- setdiff(packages, exclude)
147
148 if (length(include))
149 packages <- intersect(packages, include)
150
151 # if no packages remain, fall through to a project-wide update only when
152 # the caller did not provide an explicit scope; otherwise treat this as
153 # a no-op so we don't surprise the user with an unintended update
154 if (length(packages) == 0L) {
155 if (explicit) {
156 writef("- There are no packages to install.")
157 return(invisible(list()))
158 }
159 return(renv_pak_update(project, library, prompt))
160 }
161
162 # pak doesn't support ':' as a sub-directory separator, so try to
163 # repair that here
164 # https://github.com/rstudio/renv/issues/2011
165 pattern <- "(?<!:):([^/#@:]+)"
166 packages <- gsub(pattern, "/\\1", packages, perl = TRUE)
167
168 # build parameters
169 packages <- map_chr(packages, function(package) {
170
171 params <- c(
172 if (identical(type, "source")) "source",
173 if (identical(rebuild, TRUE) || package %in% rebuild) "reinstall"
174 )
175
176 if (length(params))
177 paste(package, paste(params, collapse = "&"), sep = "?")
178 else
179 package
180
181 })
182
183 pak$pkg_install(
184 pkg = packages,
185 lib = library[[1L]],
186 ask = prompt,
187 upgrade = TRUE
188 )
189 }
190
191 renv_pak_restore <- function(lockfile,
192 packages = NULL,
193 exclude = NULL,
194 prompt = FALSE,
195 project = NULL)
196 {
197 pak <- renv_namespace_load("pak")
198
199 # transform repositories
200 if (renv_ppm_enabled()) {
201 repos <- getOption("repos")
202 renv_scope_options(repos = renv_ppm_transform(repos))
203 }
204
205 # make sure pak::pkg_install() still works even if we're
206 # running in renv with devtools::load_all()
207 name <- Sys.getenv("_R_CHECK_PACKAGE_NAME_", unset = NA)
208 if (identical(name, "renv"))
209 renv_scope_envvars("_R_CHECK_PACKAGE_NAME_" = NULL)
210
211 # get records to install
212 records <- renv_lockfile_records(lockfile)
213 packages <- setdiff(packages %||% names(records), c(exclude, "pak", "renv"))
214 records <- records[packages]
215
216 # convert into specs compatible with pak, and install
217 remotes <- map_chr(records, renv_record_format_remote, pak = TRUE)
218
219 # TODO: We previously tried converting version-ed remotes into "plain" remotes
220 # if the package version happened to be current, but then 'pak' would choose
221 # not to install the package if a newer version was available. Hence, we need
222 # to preserve the exact remote we wish to install here.
223
224 # return early if there are zero remotes to restore
225 if (length(remotes) == 0L) {
226 return(invisible(TRUE))
227 }
228
229 # perform installation
230 pak$pkg_install(
231 pkg = remotes,
232 ask = prompt
233 )
234
235 # return installed records
236 records
237
238 }
239
240 renv_pak_restore_clean <- function(lockfile,
241 libpaths,
242 library,
243 project,
244 packages,
245 exclude,
246 prompt)
247 {
248 current <- snapshot(
249 project = project,
250 library = libpaths,
251 lockfile = NULL,
252 type = "all"
253 )
254
255 diff <- renv_lockfile_diff_packages(current, lockfile)
256 removes <- diff[diff == "remove"]
257 if (!length(removes))
258 return(invisible(NULL))
259
260 # only remove packages from the project library
261 ispkg <- map_lgl(names(removes), function(package) {
262 path <- find.package(package, lib.loc = libpaths, quiet = TRUE)
263 identical(dirname(path), library)
264 })
265 removes <- removes[ispkg]
266
267 # don't remove ignored packages
268 ignored <- renv_project_ignored_packages(project = project)
269 removes <- removes[renv_vector_diff(names(removes), ignored)]
270
271 # restrict to user-requested packages. unlike the main restore() path we
272 # don't expand `packages` via renv_graph_init() here, since the install
273 # graph's transitive deps come from the lockfile and so won't appear in
274 # the remove diff anyway.
275 selected <- if (is.null(packages))
276 setdiff(names(removes), exclude)
277 else
278 setdiff(packages, exclude)
279 removes <- removes[intersect(names(removes), selected)]
280
281 if (!length(removes))
282 return(invisible(NULL))
283
284 # report planned removals and confirm before mutating the library
285 if (prompt || renv_verbose()) {
286 renv_restore_report_actions(removes, current, lockfile)
287 cancel_if(prompt && !proceed())
288 }
289
290 enumerate(removes, function(package, action) {
291 renv_restore_remove(project, package, current)
292 })
293
294 invisible(removes)
295 }