diff --git a/NAMESPACE b/NAMESPACE index 6b0139d46..4d1a3ba01 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -36,6 +36,7 @@ importFrom(purrr,partial) importFrom(purrr,pmap) importFrom(purrr,pwalk) importFrom(purrr,when) +importFrom(rlang,"%||%") importFrom(rlang,abort) importFrom(rlang,is_empty) importFrom(rlang,is_installed) diff --git a/R/ui-caching.R b/R/ui-caching.R index 19a5877c6..0fab7c216 100644 --- a/R/ui-caching.R +++ b/R/ui-caching.R @@ -49,7 +49,7 @@ cache_clear <- function(cache_name = NULL, ask = TRUE) { #' #' @section Using a cache for styler in CI/CD: #' If you want to set up caching in a CI/CD pipeline, we suggest to set the -#' `{R.cache}` root path to a directory for which you have the cache enabled as +#' `{R.cache}` root path to a directory for which you have the cache enabled. #' This can often be set in config files of CI/CD tools, e.g. see the #' [Travis documentation on caching](https://docs.travis-ci.com/user/caching). #' @@ -86,6 +86,7 @@ cache_info <- function(cache_name = NULL, format = "both") { activated = cache_is_activated(cache_name), stringsAsFactors = FALSE ) + if (any(c("lucid", "both") == format)) { cat( "Size:\t\t", tbl$size, " bytes (", tbl$n, " cached expressions)", @@ -111,16 +112,15 @@ cache_info <- function(cache_name = NULL, format = "both") { #' @inheritParams cache_clear #' @param verbose Whether or not to print an informative message about what the #' function is doing. +#' +#' @importFrom rlang "%||%" #' @family cache managers #' @export cache_activate <- function(cache_name = NULL, verbose = !getOption("styler.quiet", FALSE)) { - if (!is.null(cache_name)) { - options("styler.cache_name" = cache_name) - } else { - options("styler.cache_name" = styler_version) - } + options("styler.cache_name" = cache_name %||% styler_version) path <- cache_find_path(cache_name) + if (verbose) { cat( "Using cache ", cache_get_name(), " at ", @@ -128,6 +128,7 @@ cache_activate <- function(cache_name = NULL, sep = "" ) } + invisible(path) } diff --git a/R/utils-cache.R b/R/utils-cache.R index 8b1cd9887..dd063f8a6 100644 --- a/R/utils-cache.R +++ b/R/utils-cache.R @@ -45,6 +45,7 @@ is_cached <- function(text, #' @param more_specs A named vector coercible to character that determines the #' styling but are style guide independent, such as `include_roxygen_examples` #' or `base_indention`. +#' #' @details #' We need to compare: #' @@ -64,6 +65,7 @@ is_cached <- function(text, #' see `as.character(list(purrr::partial(sum, x = 4)))`. For that reason, #' all arguments passed to a `purrr::partial()` call must be put in the #' style guide under `more_specs_style_guide`. +#' #' @section Experiments: #' #' There is unexplainable behavior in conjunction with hashing and @@ -84,6 +86,7 @@ is_cached <- function(text, #' overwritten / rebased by now) contains a reprex. Otherwise, search for #' 43219ixmypi in commit messages and restore this commit to reproduce the #' behavior. +#' #' @examples #' add <- function(x, y) { #' x + y @@ -123,13 +126,16 @@ cache_find_path <- function(cache_name = NULL) { #' @keywords internal cache_is_activated <- function(cache_name = NULL) { current_cache <- cache_get_name() + if (is.null(cache_name)) { - !is.null(current_cache) - } else if (!is.null(current_cache)) { - cache_name == current_cache - } else { - FALSE + return(!is.null(current_cache)) } + + if (!is.null(current_cache)) { + return(cache_name == current_cache) + } + + return(FALSE) } #' Cache text @@ -186,13 +192,9 @@ cache_get_name <- function() { getOption("styler.cache_name") } -cache_get_or_derive_name <- function(cache_name) { - if (is.null(cache_name)) { - cache_name <- cache_get_name() - if (is.null(cache_name)) { - cache_name <- styler_version - } - } +cache_get_or_derive_name <- function(cache_name = NULL) { + cache_name <- cache_name %||% cache_get_name() + cache_name <- cache_name %||% styler_version cache_name } diff --git a/man/caching.Rd b/man/caching.Rd index 62802db31..990cfa33a 100644 --- a/man/caching.Rd +++ b/man/caching.Rd @@ -39,7 +39,7 @@ empty files which have the hash of output code as name). \section{Using a cache for styler in CI/CD}{ If you want to set up caching in a CI/CD pipeline, we suggest to set the -\code{{R.cache}} root path to a directory for which you have the cache enabled as +\code{{R.cache}} root path to a directory for which you have the cache enabled. This can often be set in config files of CI/CD tools, e.g. see the \href{https://docs.travis-ci.com/user/caching}{Travis documentation on caching}. }