Skip to content

Commit

Permalink
Merge pull request #105 from r-lib/fix/no-rappdirs
Browse files Browse the repository at this point in the history
  • Loading branch information
gaborcsardi committed Nov 21, 2023
2 parents 305d7cb + 496bdb8 commit ce75a72
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 16 deletions.
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ Imports:
jsonlite,
processx (>= 3.3.0.9001),
R6,
rappdirs,
tools,
utils
Suggests:
Expand Down
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ importFrom(filelock,lock)
importFrom(filelock,unlock)
importFrom(processx,conn_get_fileno)
importFrom(processx,process)
importFrom(rappdirs,user_cache_dir)
importFrom(tools,file_ext)
importFrom(utils,URLencode)
importFrom(utils,getSrcDirectory)
Expand Down
4 changes: 2 additions & 2 deletions R/cache-api.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#' e.g. the number of files and their total size. It returns a named list.
#'
#' @param cachepath Path of the cache. By default the cache directory is in
#' `R-pkg`, within the user's cache directory.
#' See [rappdirs::user_cache_dir()].
#' `pkgcache`, within the user's cache directory.
#' See `tools::R_user_dir()`.
#'
#' @seealso The [package_cache] R6 class for a more flexible API.
#' @rdname pkg_cache_api
Expand Down
49 changes: 46 additions & 3 deletions R/cache-dirs.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ get_user_cache_dir <- function() {
res
}

#' @importFrom rappdirs user_cache_dir
#' @rawNamespace if (getRversion() >= "4.0.0") importFrom(tools, R_user_dir)

# nocov start
Expand All @@ -50,20 +49,64 @@ my_R_user_dir <- function(package, which = "cache") {
} 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(dirname(user_cache_dir()), "R", "cache", "R", package))
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 <- user_cache_dir("R-pkg")
dir <- file.path(user_cache_dir(), "R-pkg")
if (!file.exists(dir)) {
message("Old cache directory does not exists, nothing to do")
return(invisible())
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ configure Bioconductor support.
`pkgcache_low_speed_limit` options have priority over these
environment variables, if they are set.
- `R_PKG_CACHE_DIR` is used for the cache directory, if set. (Otherwise
`rappdirs::user_cache_dir()` is used, see also `meta_cache_summary()`
`tools::R_user_dir("pkgcache", "cache")` is used, see also `meta_cache_summary()`
and `pkg_cache_summary()`).

## Using pkgcache in CRAN packages
Expand Down
4 changes: 2 additions & 2 deletions man/pkg_cache_api.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/pkgcache-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions tests/testthat/test-cache-dir.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,17 @@ test_that("cleanup_old_cache_dir", {
mockery::stub(cleanup_old_cache_dir, "user_cache_dir", function(...) tmp)
expect_message(cleanup_old_cache_dir(), "nothing to do")

mkdirp(tmp)
cachedir <- file.path(tmp, "R-pkg")
mkdirp(cachedir)
mockery::stub(cleanup_old_cache_dir, "interactive", FALSE)
expect_error(cleanup_old_cache_dir(), "non-interactive session")

mockery::stub(cleanup_old_cache_dir, "interactive", TRUE)
mockery::stub(cleanup_old_cache_dir, "readline", "n")
expect_error(cleanup_old_cache_dir(), "Aborted")

expect_true(file.exists(tmp))
expect_true(file.exists(cachedir))
mockery::stub(cleanup_old_cache_dir, "readline", "y")
expect_message(cleanup_old_cache_dir(), "Cleaned up cache")
expect_false(file.exists(tmp))
expect_false(file.exists(cachedir))
})
2 changes: 2 additions & 0 deletions tests/testthat/test-repo-status.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ test_that("repo_status", {
})

test_that("bioc repo status", {
if (getRversion() < "4.2.0") skip("Need R 4.2.x")

setup_fake_apps()
withr::local_options(width = 1000)

Expand Down
2 changes: 1 addition & 1 deletion tools/README-body.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ Bioconductor support.
curl options. The `pkgcache_low_speed_time` and `pkgcache_low_speed_limit`
options have priority over these environment variables, if they are set.
- `R_PKG_CACHE_DIR` is used for the cache directory, if set. (Otherwise
`rappdirs::user_cache_dir()` is used, see also `meta_cache_summary()` and
`tools::R_user_dir("pkgcache", "cache")` is used, see also `meta_cache_summary()` and
`pkg_cache_summary()`).

## Using pkgcache in CRAN packages
Expand Down

0 comments on commit ce75a72

Please sign in to comment.