-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathcache-dirs.R
More file actions
129 lines (109 loc) · 3.22 KB
/
cache-dirs.R
File metadata and controls
129 lines (109 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
get_user_cache_dir <- function() {
ichk <- Sys.getenv("_R_CHECK_PACKAGE_NAME_", "") != ""
rdir <- Sys.getenv("R_USER_CACHE_DIR", "")
cdir <- Sys.getenv("R_PKG_CACHE_DIR", "")
# If in R CMD check, then refuse unless R_USER_CACHE_DIR is set.
# This is to prevent R CMD check creating files in the user directories,
# while a reverse dependency runs its check.
if (ichk && cdir == "" && rdir == "") {
stop(
"R_USER_CACHE_DIR env var not set during package check, ",
"see https://github.com/r-lib/pkgcache#README"
)
}
# R_PKG_CACHE_DIR first. R_user_dir uses R_USER_CACHE_DIR, if set.
if (cdir == "") {
cdir <- R_user_dir("pkgcache", "cache")
} else {
cdir <- file.path(cdir, "R", "pkgcache")
}
mkdirp(cdir)
cdir <- normalizePath(cdir)
res <- list(
root = cdir,
pkg = file.path(cdir, "pkg"),
meta = file.path(cdir, "_metadata"),
lock = file.path(cdir, "_metadata.lock")
)
mkdirp(res$meta)
res
}
#' @rawNamespace if (getRversion() >= "4.0.0") importFrom(tools, R_user_dir)
# nocov start
my_R_user_dir <- function(package, which = "cache") {
stopifnot(is.character(package), length(package) == 1L)
stopifnot(which == "cache")
ev <- Sys.getenv("R_USER_CACHE_DIR", "")
if (ev != "") {
path.expand(file.path(ev, "R", package))
} else if (Sys.info()[["sysname"]] == "Darwin") {
path.expand(file.path(user_cache_dir(), "org.R-project.R", "R", package))
} else if (.Platform$OS.type == "windows") {
path.expand(file.path(user_cache_dir(), "R", "cache", "R", package))
} else {
path.expand(file.path(user_cache_dir(), "R", package))
}
}
user_cache_dir <- function() {
if (nzchar(cache <- Sys.getenv("R_PKG_CACHE_DIR", ""))) {
return(cache)
}
if (nzchar(cache <- Sys.getenv("R_USER_CACHE_DIR", ""))) {
return(file.path(cache, "R"))
}
switch(
get_os(),
win = file_path(win_path_local()),
mac = "~/Library/Caches",
unix = ,
unknown = file_path(Sys.getenv("XDG_CACHE_HOME", "~/.cache"))
)
}
get_os <- function() {
if (.Platform$OS.type == "windows") {
"win"
} else if (Sys.info()["sysname"] == "Darwin") {
"mac"
} else if (.Platform$OS.type == "unix") {
"unix"
} else {
"unknown"
}
}
file_path <- function(...) {
normalizePath(do.call("file.path", as.list(c(...))), mustWork = FALSE)
}
win_path_local <- function() {
if (nzchar(lapp <- Sys.getenv("LOCALAPPDATA", ""))) {
lapp
} else if (nzchar(usrprof <- Sys.getenv("USERPROFILE", ""))) {
file.path(usrprof, "AppData", "Local")
} else {
file.path(tempdir(), "r-pkg-cache")
}
}
# nocov end
if (getRversion() < "4.0.0") {
R_user_dir <- my_R_user_dir
}
cleanup_old_cache_dir <- function(force = FALSE) {
dir <- file.path(user_cache_dir(), "R-pkg")
if (!file.exists(dir)) {
message("Old cache directory does not exists, nothing to do")
return(invisible())
}
if (!interactive() && !force) {
stop("Set `force = TRUE` in non-interactive sessions")
}
if (!force) {
msg <- sprintf(
"Are you sure you want to clean up `%s` (y/N)? ",
dir
)
ans <- readline(msg)
if (!ans %in% c("y", "Y")) stop("Aborted")
}
unlink(dir, recursive = TRUE, force = TRUE)
message("Cleaned up cache.")
invisible()
}