From 2af023393655acf07cdbaec16edf675dfd1f7277 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Cs=C3=A1rdi?= Date: Tue, 21 Nov 2023 09:56:06 +0100 Subject: [PATCH] Drop rappdirs dependency --- DESCRIPTION | 1 - NAMESPACE | 1 - R/cache-api.R | 2 +- R/cache-dirs.R | 47 +++++++++++++++++++++++++++++++-- README.md | 2 +- man/pkg_cache_api.Rd | 2 +- man/pkgcache-package.Rd | 4 +-- tests/testthat/test-cache-dir.R | 7 ++--- tools/README-body.Rmd | 2 +- 9 files changed, 55 insertions(+), 13 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index bc38622d..9c26f731 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -22,7 +22,6 @@ Imports: jsonlite, processx (>= 3.3.0.9001), R6, - rappdirs, tools, utils Suggests: diff --git a/NAMESPACE b/NAMESPACE index 5e41674c..4eea8f12 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/cache-api.R b/R/cache-api.R index 15c6ea53..da738e30 100644 --- a/R/cache-api.R +++ b/R/cache-api.R @@ -6,7 +6,7 @@ #' #' @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()]. +#' See [tools::R_user_dir()]. #' #' @seealso The [package_cache] R6 class for a more flexible API. #' @rdname pkg_cache_api diff --git a/R/cache-dirs.R b/R/cache-dirs.R index c8467538..2214ebd5 100644 --- a/R/cache-dirs.R +++ b/R/cache-dirs.R @@ -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 @@ -56,6 +55,50 @@ my_R_user_dir <- function(package, which = "cache") { } } +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") { @@ -63,7 +106,7 @@ if (getRversion() < "4.0.0") { } 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()) diff --git a/README.md b/README.md index 37606c48..559b6784 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/man/pkg_cache_api.Rd b/man/pkg_cache_api.Rd index 59bbfce9..0b19c571 100644 --- a/man/pkg_cache_api.Rd +++ b/man/pkg_cache_api.Rd @@ -24,7 +24,7 @@ pkg_cache_add_file(cachepath = NULL, file, relpath = dirname(file), ...) \arguments{ \item{cachepath}{Path of the cache. By default the cache directory is in \code{R-pkg}, within the user's cache directory. -See \code{\link[rappdirs:user_cache_dir]{rappdirs::user_cache_dir()}}.} +See \code{\link[tools:userdir]{tools::R_user_dir()}}.} \item{...}{Extra named arguments to select the package file.} diff --git a/man/pkgcache-package.Rd b/man/pkgcache-package.Rd index 5e5ccc50..d3820e9f 100644 --- a/man/pkgcache-package.Rd +++ b/man/pkgcache-package.Rd @@ -229,8 +229,8 @@ curl options. The \code{pkgcache_low_speed_time} and \code{pkgcache_low_speed_limit} options have priority over these environment variables, if they are set. \item \code{R_PKG_CACHE_DIR} is used for the cache directory, if set. (Otherwise -\code{rappdirs::user_cache_dir()} is used, see also \code{meta_cache_summary()} -and \code{pkg_cache_summary()}). +\code{tools::R_user_dir("pkgcache", "cache")} is used, see also +\code{meta_cache_summary()} and \code{pkg_cache_summary()}). } } diff --git a/tests/testthat/test-cache-dir.R b/tests/testthat/test-cache-dir.R index f7dd798f..11e4099f 100644 --- a/tests/testthat/test-cache-dir.R +++ b/tests/testthat/test-cache-dir.R @@ -70,7 +70,8 @@ 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") @@ -78,8 +79,8 @@ test_that("cleanup_old_cache_dir", { 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)) }) diff --git a/tools/README-body.Rmd b/tools/README-body.Rmd index 9e8f46be..281ffd08 100644 --- a/tools/README-body.Rmd +++ b/tools/README-body.Rmd @@ -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