| 1 |
|
| 2 |
the$update_errors <- new.env(parent = emptyenv()) |
| 3 |
|
| 4 |
renv_update_find_repos <- function(records) { |
| 5 |
|
| 6 |
results <- lapply(records, function(record) { |
| 7 |
catch(renv_update_find_repos_impl(record)) |
| 8 |
}) |
| 9 |
|
| 10 |
failed <- map_lgl(results, inherits, "error") |
| 11 |
if (any(failed)) |
| 12 |
renv_update_errors_set("repos", results[failed]) |
| 13 |
|
| 14 |
results[!failed] |
| 15 |
|
| 16 |
} |
| 17 |
|
| 18 |
renv_update_find_repos_impl <- function(record) { |
| 19 |
|
| 20 |
# retrieve latest-available package |
| 21 |
package <- record$Package |
| 22 |
latest <- catch(renv_available_packages_latest(package)) |
| 23 |
if (inherits(latest, "error")) |
| 24 |
return(NULL) |
| 25 |
|
| 26 |
# validate our versions |
| 27 |
if (empty(latest$Version) || empty(record$Version)) |
| 28 |
return(NULL) |
| 29 |
|
| 30 |
# compare the versions; return NULL if the 'latest' version |
| 31 |
# is older |
| 32 |
compare <- renv_version_compare(latest$Version, record$Version) |
| 33 |
if (compare != 1L) |
| 34 |
return(NULL) |
| 35 |
|
| 36 |
latest |
| 37 |
|
| 38 |
} |
| 39 |
|
| 40 |
renv_update_find_git <- function(records) { |
| 41 |
renv_parallel_exec(records, renv_update_find_git_impl) |
| 42 |
} |
| 43 |
|
| 44 |
renv_update_find_git_impl <- function(record) { |
| 45 |
|
| 46 |
sha <- renv_remotes_resolve_git_sha_ref(record) |
| 47 |
|
| 48 |
# if sha is empty: |
| 49 |
# `git remote-ls origin ref` expects ref to be a reference, not a sha |
| 50 |
# it is empty if ref isn't a reference on the repo |
| 51 |
# this may be due to record$RemoteRef actually being a sha |
| 52 |
# or it may be because record$RemoteRef is not a real ref |
| 53 |
# but we can't check, so we will try to fetch the ref & see what we get |
| 54 |
oldsha <- record$RemoteSha %||% "" |
| 55 |
if (nzchar(oldsha) && identical(sha, oldsha)) |
| 56 |
return(NULL) |
| 57 |
|
| 58 |
current <- record |
| 59 |
current$RemoteSha <- sha |
| 60 |
|
| 61 |
desc <- renv_remotes_resolve_git_description(current) |
| 62 |
|
| 63 |
current$Version <- desc$Version |
| 64 |
current$Package <- desc$Package |
| 65 |
|
| 66 |
updated <- renv_version_ge(current$Version, record$Version) |
| 67 |
if (updated) |
| 68 |
return(current) |
| 69 |
|
| 70 |
} |
| 71 |
|
| 72 |
renv_update_find_github <- function(records) { |
| 73 |
|
| 74 |
names(records) <- map_chr(records, `[[`, "Package") |
| 75 |
results <- renv_parallel_exec(records, function(record) { |
| 76 |
catch(renv_update_find_github_impl(record)) |
| 77 |
}) |
| 78 |
|
| 79 |
failed <- map_lgl(results, inherits, "error") |
| 80 |
if (any(failed)) |
| 81 |
renv_update_errors_set("github", results[failed]) |
| 82 |
|
| 83 |
results[!failed] |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
renv_update_find_github_impl <- function(record) { |
| 88 |
|
| 89 |
# construct and parse record entry |
| 90 |
host <- record$RemoteHost %||% config$github.host() |
| 91 |
user <- record$RemoteUsername |
| 92 |
repo <- record$RemoteRepo |
| 93 |
subdir <- record$RemoteSubdir |
| 94 |
ref <- record$RemoteRef |
| 95 |
|
| 96 |
# check for changed sha |
| 97 |
sha <- renv_remotes_resolve_github_sha_ref(host, user, repo, ref) |
| 98 |
if (sha == record$RemoteSha) |
| 99 |
return(NULL) |
| 100 |
|
| 101 |
url <- record$RemoteUrl %||% { |
| 102 |
origin <- fsub("api.github.com", "github.com", renv_retrieve_origin(host)) |
| 103 |
parts <- c(origin, user, repo) |
| 104 |
paste(parts, collapse = "/") |
| 105 |
} |
| 106 |
|
| 107 |
# get updated record |
| 108 |
desc <- renv_remotes_resolve_github_description(url, host, user, repo, subdir, sha) |
| 109 |
current <- list( |
| 110 |
Package = desc$Package, |
| 111 |
Version = desc$Version, |
| 112 |
Source = "GitHub", |
| 113 |
RemoteUsername = user, |
| 114 |
RemoteRepo = repo, |
| 115 |
RemoteSubdir = subdir, |
| 116 |
RemoteRef = ref, |
| 117 |
RemoteSha = sha, |
| 118 |
RemoteHost = host |
| 119 |
) |
| 120 |
|
| 121 |
# check that the version has actually updated |
| 122 |
updated <- |
| 123 |
current$RemoteSha != record$RemoteSha && |
| 124 |
numeric_version(current$Version) >= numeric_version(record$Version) |
| 125 |
|
| 126 |
if (updated) |
| 127 |
return(current) |
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
renv_update_find_remote <- function(records, type) { |
| 133 |
|
| 134 |
update <- switch(type, |
| 135 |
"gitlab" = renv_remotes_resolve_gitlab, |
| 136 |
"bitbucket" = renv_remotes_resolve_bitbucket, |
| 137 |
stopf("Unsupported type %s", type) |
| 138 |
) |
| 139 |
|
| 140 |
names(records) <- map_chr(records, `[[`, "Package") |
| 141 |
results <- renv_parallel_exec(records, function(record) { |
| 142 |
catch(renv_update_find_remote_impl(record, update)) |
| 143 |
}) |
| 144 |
|
| 145 |
failed <- map_lgl(results, inherits, "error") |
| 146 |
if (any(failed)) |
| 147 |
renv_update_errors_set(type, results[failed]) |
| 148 |
|
| 149 |
results[!failed] |
| 150 |
|
| 151 |
} |
| 152 |
|
| 153 |
renv_update_find_remote_impl <- function(record, update) { |
| 154 |
|
| 155 |
remote <- list( |
| 156 |
host = record$RemoteHost, |
| 157 |
user = record$RemoteUsername, |
| 158 |
repo = record$RemoteRepo, |
| 159 |
subdir = record$RemoteSubdir, |
| 160 |
ref = record$RemoteRef |
| 161 |
) |
| 162 |
current <- update(remote) |
| 163 |
|
| 164 |
# check that the version has actually updated |
| 165 |
updated <- |
| 166 |
current$RemoteSha != record$RemoteSha && |
| 167 |
numeric_version(current$Version) >= numeric_version(record$Version) |
| 168 |
|
| 169 |
if (updated) |
| 170 |
return(current) |
| 171 |
|
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
renv_update_find <- function(records) { |
| 176 |
|
| 177 |
sources <- extract_chr(records, "Source") |
| 178 |
grouped <- split(records, sources) |
| 179 |
|
| 180 |
# retrieve updates |
| 181 |
results <- enumerate(grouped, function(source, records) { |
| 182 |
case( |
| 183 |
source == "Bioconductor" ~ renv_update_find_repos(records), |
| 184 |
source == "Repository" ~ renv_update_find_repos(records), |
| 185 |
source == "GitHub" ~ renv_update_find_github(records), |
| 186 |
source == "Git" ~ renv_update_find_git(records), |
| 187 |
source == "GitLab" ~ renv_update_find_remote(records, "gitlab"), |
| 188 |
source == "Bitbucket" ~ renv_update_find_remote(records, "bitbucket") |
| 189 |
) |
| 190 |
}) |
| 191 |
|
| 192 |
# remove groupings |
| 193 |
ungrouped <- unlist(results, recursive = FALSE, use.names = FALSE) |
| 194 |
if (empty(ungrouped)) |
| 195 |
return(list()) |
| 196 |
|
| 197 |
# keep non-null results |
| 198 |
updates <- Filter(Negate(is.null), ungrouped) |
| 199 |
if (empty(updates)) |
| 200 |
return(list()) |
| 201 |
|
| 202 |
names(updates) <- extract_chr(updates, "Package") |
| 203 |
renv_records_sort(updates) |
| 204 |
|
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
#' Update packages |
| 210 |
#' |
| 211 |
#' @description |
| 212 |
#' Update packages which are currently out-of-date. Currently supports CRAN, |
| 213 |
#' Bioconductor, other CRAN-like repositories, GitHub, GitLab, Git, and |
| 214 |
#' BitBucket. |
| 215 |
#' |
| 216 |
#' Updates will only be checked from the same source -- for example, |
| 217 |
#' if a package was installed from GitHub, but a newer version is |
| 218 |
#' available on CRAN, that updated version will not be seen. |
| 219 |
#' |
| 220 |
#' @inherit renv-params |
| 221 |
#' |
| 222 |
#' @param packages A character vector of \R packages to update. When `NULL` |
| 223 |
#' (the default), all packages (apart from any listed in the `ignored.packages` |
| 224 |
#' project setting) will be updated. |
| 225 |
#' |
| 226 |
#' @param check Boolean; check for package updates without actually |
| 227 |
#' installing available updates? This is useful when you'd like to determine |
| 228 |
#' what updates are available, without actually installing those updates. |
| 229 |
#' |
| 230 |
#' @param exclude A set of packages to explicitly exclude from updating. |
| 231 |
#' Use `renv::update(exclude = <...>)` to update all packages except for |
| 232 |
#' a specific set of excluded packages. |
| 233 |
#' |
| 234 |
#' @param lock Boolean; update the `renv.lock` lockfile after the successful |
| 235 |
#' installation of the requested packages? |
| 236 |
#' |
| 237 |
#' @param all Boolean; should `renv` check all library paths for out-of-date |
| 238 |
#' packages? When `FALSE` (the default), only the project library will be |
| 239 |
#' checked for out-of-date packages. |
| 240 |
#' |
| 241 |
#' @return A named list of package records which were installed by renv. |
| 242 |
#' |
| 243 |
#' @export |
| 244 |
#' |
| 245 |
#' @examples |
| 246 |
#' \dontrun{ |
| 247 |
#' |
| 248 |
#' # update the 'dplyr' package |
| 249 |
#' renv::update("dplyr") |
| 250 |
#' |
| 251 |
#' } |
| 252 |
update <- function(packages = NULL, |
| 253 |
..., |
| 254 |
exclude = NULL, |
| 255 |
library = NULL, |
| 256 |
type = NULL, |
| 257 |
rebuild = FALSE, |
| 258 |
check = FALSE, |
| 259 |
prompt = interactive(), |
| 260 |
lock = FALSE, |
| 261 |
all = FALSE, |
| 262 |
project = NULL) |
| 263 |
{ |
| 264 |
renv_consent_check() |
| 265 |
renv_scope_error_handler() |
| 266 |
renv_dots_check(...) |
| 267 |
|
| 268 |
project <- renv_project_resolve(project) |
| 269 |
renv_project_lock(project = project) |
| 270 |
renv_scope_verbose_if(prompt) |
| 271 |
|
| 272 |
# resolve library path |
| 273 |
library <- renv_libpaths_resolve(library) |
| 274 |
renv_scope_libpaths(library) |
| 275 |
|
| 276 |
# check for explicitly-provided type -- we handle this specially for PPM |
| 277 |
if (!is.null(type)) { |
| 278 |
renv_scope_binding(the, "install_pkg_type", type) |
| 279 |
renv_scope_options(pkgType = type) |
| 280 |
} |
| 281 |
|
| 282 |
# resolve exclusions |
| 283 |
exclude <- c(exclude, settings$ignored.packages(project = project)) |
| 284 |
|
| 285 |
# if users have requested the use of pak, delegate there |
| 286 |
if (config$pak.enabled() && !recursing()) { |
| 287 |
packages <- setdiff(packages, exclude) |
| 288 |
renv_pak_init() |
| 289 |
return( |
| 290 |
renv_pak_install( |
| 291 |
packages = packages, |
| 292 |
library = library, |
| 293 |
rebuild = rebuild, |
| 294 |
type = type, |
| 295 |
prompt = prompt, |
| 296 |
project = project |
| 297 |
) |
| 298 |
) |
| 299 |
} |
| 300 |
|
| 301 |
# get package records |
| 302 |
renv_scope_binding(the, "snapshot_hash", FALSE) |
| 303 |
libpaths <- if (all) library else library[[1L]] |
| 304 |
records <- renv_snapshot_libpaths(libpaths = libpaths, project = project) |
| 305 |
packages <- packages %||% names(records) |
| 306 |
|
| 307 |
# apply exclusions |
| 308 |
packages <- setdiff(packages, exclude) |
| 309 |
|
| 310 |
# check if the user has requested update for packages not installed |
| 311 |
missing <- renv_vector_diff(packages, names(records)) |
| 312 |
if (!empty(missing)) { |
| 313 |
|
| 314 |
if (prompt || renv_verbose()) { |
| 315 |
bulletin( |
| 316 |
"The following package(s) are not currently installed:", |
| 317 |
missing, |
| 318 |
"The latest available versions of these packages will be installed instead." |
| 319 |
) |
| 320 |
} |
| 321 |
|
| 322 |
cancel_if(prompt && !proceed()) |
| 323 |
|
| 324 |
} |
| 325 |
|
| 326 |
# select records |
| 327 |
selected <- c( |
| 328 |
records[renv_vector_intersect(packages, names(records))], |
| 329 |
named(lapply(missing, renv_available_packages_latest), missing) |
| 330 |
) |
| 331 |
|
| 332 |
# check for usage of cran, bioc |
| 333 |
repo <- FALSE |
| 334 |
bioc <- FALSE |
| 335 |
|
| 336 |
for (record in selected) { |
| 337 |
|
| 338 |
source <- renv_record_source(record, normalize = TRUE) |
| 339 |
|
| 340 |
if (source %in% c("repository")) { |
| 341 |
repo <- TRUE |
| 342 |
next |
| 343 |
} |
| 344 |
|
| 345 |
if (source %in% c("bioconductor")) { |
| 346 |
repo <- bioc <- TRUE |
| 347 |
next |
| 348 |
} |
| 349 |
|
| 350 |
} |
| 351 |
|
| 352 |
# activate bioc repositories if needed |
| 353 |
if (bioc) |
| 354 |
renv_scope_bioconductor(project = project) |
| 355 |
|
| 356 |
# ensure database of available packages is current |
| 357 |
if (repo) { |
| 358 |
for (type in renv_package_pkgtypes()) { |
| 359 |
available_packages(type = type) |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
printf("- Checking for updated packages ... ") |
| 364 |
|
| 365 |
# remove records that appear to be from an R package repository, |
| 366 |
# but are not actually available in the current repositories |
| 367 |
selected <- filter(selected, function(record) { |
| 368 |
|
| 369 |
source <- renv_record_source(record, normalize = TRUE) |
| 370 |
if (!source %in% c("bioconductor", "cran", "repository")) |
| 371 |
return(TRUE) |
| 372 |
|
| 373 |
# check for available package |
| 374 |
package <- record$Package |
| 375 |
entry <- catch(renv_available_packages_latest(package)) |
| 376 |
!inherits(entry, "error") |
| 377 |
|
| 378 |
}) |
| 379 |
|
| 380 |
updates <- renv_update_find(selected) |
| 381 |
writef("Done!") |
| 382 |
|
| 383 |
renv_update_errors_emit() |
| 384 |
|
| 385 |
if (empty(updates)) { |
| 386 |
writef("- All packages appear to be up-to-date.") |
| 387 |
return(invisible(TRUE)) |
| 388 |
} |
| 389 |
|
| 390 |
# perform a diff (for reporting to user) |
| 391 |
old <- selected[names(updates)] |
| 392 |
new <- updates |
| 393 |
diff <- renv_lockfile_diff_packages(old, new) |
| 394 |
|
| 395 |
# if we're only checking for updates, just report and exit |
| 396 |
if (check) { |
| 397 |
|
| 398 |
fmt <- case( |
| 399 |
length(diff) == 1 ~ "- %i package has updates available.", |
| 400 |
length(diff) != 1 ~ "- %i packages have updates available." |
| 401 |
) |
| 402 |
|
| 403 |
preamble <- sprintf(fmt, length(diff)) |
| 404 |
renv_updates_report(preamble, diff, old, new) |
| 405 |
return(invisible(renv_updates_create(diff, old, new))) |
| 406 |
|
| 407 |
} |
| 408 |
|
| 409 |
if (prompt || renv_verbose()) { |
| 410 |
renv_restore_report_actions(diff, old, new) |
| 411 |
cancel_if(prompt && !proceed()) |
| 412 |
} |
| 413 |
|
| 414 |
# perform the install |
| 415 |
install( |
| 416 |
packages = updates, |
| 417 |
library = library, |
| 418 |
rebuild = rebuild, |
| 419 |
prompt = FALSE, |
| 420 |
lock = lock, |
| 421 |
project = project |
| 422 |
) |
| 423 |
|
| 424 |
} |
| 425 |
|
| 426 |
renv_update_errors_set <- function(key, errors) { |
| 427 |
assign(key, errors, envir = the$update_errors) |
| 428 |
} |
| 429 |
|
| 430 |
renv_update_errors_clear <- function() { |
| 431 |
rm( |
| 432 |
list = ls(envir = the$update_errors, all.names = TRUE), |
| 433 |
envir = the$update_errors |
| 434 |
) |
| 435 |
} |
| 436 |
|
| 437 |
renv_update_errors_emit <- function() { |
| 438 |
|
| 439 |
# clear errors when we're done |
| 440 |
defer(renv_update_errors_clear()) |
| 441 |
|
| 442 |
# if we have any errors, start by emitting a single newline |
| 443 |
all <- ls(envir = the$update_errors, all.names = TRUE) |
| 444 |
if (!empty(all)) |
| 445 |
writef() |
| 446 |
|
| 447 |
# then emit errors for each class |
| 448 |
renv_update_errors_emit_repos() |
| 449 |
renv_update_errors_emit_remote("github", "GitHub") |
| 450 |
renv_update_errors_emit_remote("gitlab", "GitLab") |
| 451 |
renv_update_errors_emit_remote("bitbucket", "BitBucket") |
| 452 |
|
| 453 |
} |
| 454 |
|
| 455 |
renv_update_errors_emit_impl <- function(key, preamble, postamble) { |
| 456 |
|
| 457 |
errors <- the$update_errors[[key]] |
| 458 |
if (empty(errors)) |
| 459 |
return() |
| 460 |
|
| 461 |
messages <- enumerate(errors, function(package, error) { |
| 462 |
errmsg <- paste(conditionMessage(error), collapse = "; ") |
| 463 |
sprintf("%s: %s", format(package), errmsg) |
| 464 |
}) |
| 465 |
|
| 466 |
bulletin( |
| 467 |
preamble = preamble, |
| 468 |
values = messages, |
| 469 |
postamble = postamble |
| 470 |
) |
| 471 |
|
| 472 |
} |
| 473 |
|
| 474 |
renv_update_errors_emit_repos <- function() { |
| 475 |
|
| 476 |
renv_update_errors_emit_impl( |
| 477 |
key = "repos", |
| 478 |
preamble = "One or more errors occurred while finding updates for the following packages:", |
| 479 |
postamble = "Ensure that these packages are available from your active package repositories." |
| 480 |
) |
| 481 |
|
| 482 |
} |
| 483 |
|
| 484 |
renv_update_errors_emit_remote <- function(key, label) { |
| 485 |
|
| 486 |
renv_update_errors_emit_impl( |
| 487 |
key = key, |
| 488 |
preamble = sprintf("One or more errors occurred while finding updates for the following %s packages:", label), |
| 489 |
postamble = sprintf("Ensure that these packages were installed from an accessible %s remote.", label) |
| 490 |
) |
| 491 |
|
| 492 |
} |
| 493 |
|