Debian Codegraph @

R/rehash.R

raw
1
2 #' Re-hash packages in the renv cache
3 #'
4 #' Re-hash packages in the renv cache, ensuring that any previously-cached
5 #' packages are copied to a new cache location appropriate for this version of
6 #' renv. This can be useful if the cache scheme has changed in a new version
7 #' of renv, but you'd like to preserve your previously-cached packages.
8 #'
9 #' Any packages which are re-hashed will retain links to the location of the
10 #' newly-hashed package, ensuring that prior installations of renv can still
11 #' function as expected.
12 #'
13 #' @inheritParams renv-params
14 #'
15 #' @export
16 rehash <- function(prompt = interactive(), ...) {
17 renv_scope_error_handler()
18 renv_dots_check(...)
19 renv_scope_verbose_if(prompt)
20 invisible(renv_rehash_impl(prompt))
21 }
22
23 renv_rehash_impl <- function(prompt) {
24
25 # check for cache migration
26 oldcache <- renv_paths_cache(version = renv_cache_version_previous())[[1L]]
27 newcache <- renv_paths_cache(version = renv_cache_version())[[1L]]
28 if (file.exists(oldcache) && !file.exists(newcache))
29 renv_rehash_cache(oldcache, prompt, renv_file_copy, "copied")
30
31 # re-cache packages as necessary
32 renv_rehash_cache(newcache, prompt, renv_file_move, "moved")
33
34 }
35
36 renv_rehash_cache <- function(cache, prompt, action, label) {
37
38 # re-compute package hashes
39 old <- renv_cache_list(cache = cache)
40
41 printf("- Re-computing package hashes ... ")
42 new <- map_chr(old, renv_progress_callback(renv_cache_path, length(old)))
43 writef("Done!")
44
45 changed <- which(old != new & file.exists(old) & !file.exists(new))
46 if (empty(changed)) {
47 writef("- Your cache is already up-to-date -- nothing to do.")
48 return(TRUE)
49 }
50
51 if (prompt) {
52
53 fmt <- "%s [%s -> %s]"
54 packages <- basename(old)[changed]
55 oldhash <- renv_path_component(old[changed], 2L)
56 newhash <- renv_path_component(new[changed], 2L)
57 bulletin(
58 "The following packages will be re-cached:",
59 sprintf(fmt, format(packages), format(oldhash), format(newhash)),
60 sprintf("Packages will be %s to their new locations in the cache.", label)
61 )
62
63 cancel_if(prompt && !proceed())
64
65 }
66
67 sources <- old[changed]
68 targets <- new[changed]
69 names(sources) <- targets
70 names(targets) <- sources
71
72 printf("- Re-caching packages ... ")
73 enumerate(targets, renv_progress_callback(action, length(targets)))
74 writef("Done!")
75
76 n <- length(targets)
77 fmt <- "Successfully re-cached %s."
78 writef(fmt, nplural("package", n))
79 renv_cache_clean_empty()
80
81 TRUE
82 }