Debian Codegraph @

R/purge.R

raw
1
2 #' Purge packages from the cache
3 #'
4 #' Purge packages from the cache. This can be useful if a package which had
5 #' previously been installed in the cache has become corrupted or unusable,
6 #' and needs to be reinstalled.
7 #'
8 #' `purge()` is an inherently destructive option. It removes packages from the
9 #' cache, and so any project which had symlinked that package into its own
10 #' project library would find that package now unavailable. These projects would
11 #' hence need to reinstall any purged packages. Take heed of this in case you're
12 #' looking to purge the cache of a package which is difficult to install, or
13 #' if the original sources for that package are no longer available!
14 #'
15 #' @inherit renv-params
16 #'
17 #' @param package A single package to be removed from the cache.
18 #' @param version The package version to be removed. When `NULL`, all versions
19 #' of the requested package will be removed.
20 #' @param hash The specific hashes to be removed. When `NULL`, all hashes
21 #' associated with a particular package's version will be removed.
22 #'
23 #' @return The set of packages removed from the renv global cache,
24 #' as a character vector of file paths.
25 #'
26 #' @export
27 #'
28 #' @examples
29 #' \dontrun{
30 #'
31 #' # remove all versions of 'digest' from the cache
32 #' renv::purge("digest")
33 #'
34 #' # remove only a particular version of 'digest' from the cache
35 #' renv::purge("digest", version = "0.6.19")
36 #'
37 #' }
38 purge <- function(package,
39 ...,
40 version = NULL,
41 hash = NULL,
42 prompt = interactive())
43 {
44 renv_scope_error_handler()
45 renv_dots_check(...)
46 renv_scope_verbose_if(prompt)
47 invisible(renv_purge_impl(package, version, hash, prompt))
48 }
49
50 renv_purge_impl <- function(package,
51 version = NULL,
52 hash = NULL,
53 prompt = interactive())
54 {
55 if (length(package) != 1)
56 stop("argument 'package' is not of length one", call. = FALSE)
57
58 bail <- function() {
59 writef("- The requested package is not installed in the cache -- nothing to do.")
60 character()
61 }
62
63 # get root cache path entry for package
64 paths <- renv_paths_cache(package)
65 if (!any(file.exists(paths)))
66 return(bail())
67
68 # construct versioned path
69 paths <- if (is.null(version))
70 list.files(paths, full.names = TRUE)
71 else
72 file.path(paths, version)
73 if (!any(file.exists(paths)))
74 return(bail())
75
76 # construct hashed path
77 paths <- if (is.null(hash))
78 list.files(paths, full.names = TRUE)
79 else
80 file.path(paths, hash)
81 if (all(!file.exists(paths)))
82 return(bail())
83
84 # now add package name
85 paths <- file.path(paths, renv_path_component(paths, 3))
86
87 # check that these entries exist
88 missing <- !file.exists(paths)
89 if (any(missing)) {
90
91 bulletin(
92 "The following entries were not found in the cache:",
93 paths[missing],
94 "They will be ignored."
95 )
96
97 paths <- paths[!missing]
98
99 }
100
101 # nocov start
102 if (prompt || renv_verbose()) {
103
104 bulletin(
105 "The following packages will be purged from the cache:",
106 renv_cache_format_path(paths)
107 )
108
109 cancel_if(prompt && !proceed())
110
111 }
112 # nocov end
113
114 unlink(paths, recursive = TRUE)
115 renv_cache_clean_empty()
116
117 n <- length(paths)
118 writef("- Removed %s from the cache.", nplural("package", n))
119
120 invisible(paths)
121
122 }