Skip to content

Commit a45f667

Browse files
Merge pull request #1041 from r-lib/cache_minor_cleanup
2 parents a760cfb + b3429b6 commit a45f667

File tree

4 files changed

+23
-19
lines changed

4 files changed

+23
-19
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ importFrom(purrr,partial)
3636
importFrom(purrr,pmap)
3737
importFrom(purrr,pwalk)
3838
importFrom(purrr,when)
39+
importFrom(rlang,"%||%")
3940
importFrom(rlang,abort)
4041
importFrom(rlang,is_empty)
4142
importFrom(rlang,is_installed)

R/ui-caching.R

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ cache_clear <- function(cache_name = NULL, ask = TRUE) {
4949
#'
5050
#' @section Using a cache for styler in CI/CD:
5151
#' If you want to set up caching in a CI/CD pipeline, we suggest to set the
52-
#' `{R.cache}` root path to a directory for which you have the cache enabled as
52+
#' `{R.cache}` root path to a directory for which you have the cache enabled.
5353
#' This can often be set in config files of CI/CD tools, e.g. see the
5454
#' [Travis documentation on caching](https://docs.travis-ci.com/user/caching).
5555
#'
@@ -86,6 +86,7 @@ cache_info <- function(cache_name = NULL, format = "both") {
8686
activated = cache_is_activated(cache_name),
8787
stringsAsFactors = FALSE
8888
)
89+
8990
if (any(c("lucid", "both") == format)) {
9091
cat(
9192
"Size:\t\t", tbl$size, " bytes (", tbl$n, " cached expressions)",
@@ -111,23 +112,23 @@ cache_info <- function(cache_name = NULL, format = "both") {
111112
#' @inheritParams cache_clear
112113
#' @param verbose Whether or not to print an informative message about what the
113114
#' function is doing.
115+
#'
116+
#' @importFrom rlang "%||%"
114117
#' @family cache managers
115118
#' @export
116119
cache_activate <- function(cache_name = NULL,
117120
verbose = !getOption("styler.quiet", FALSE)) {
118-
if (!is.null(cache_name)) {
119-
options("styler.cache_name" = cache_name)
120-
} else {
121-
options("styler.cache_name" = styler_version)
122-
}
121+
options("styler.cache_name" = cache_name %||% styler_version)
123122
path <- cache_find_path(cache_name)
123+
124124
if (verbose) {
125125
cat(
126126
"Using cache ", cache_get_name(), " at ",
127127
path, ".\n",
128128
sep = ""
129129
)
130130
}
131+
131132
invisible(path)
132133
}
133134

R/utils-cache.R

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ is_cached <- function(text,
4545
#' @param more_specs A named vector coercible to character that determines the
4646
#' styling but are style guide independent, such as `include_roxygen_examples`
4747
#' or `base_indention`.
48+
#'
4849
#' @details
4950
#' We need to compare:
5051
#'
@@ -64,6 +65,7 @@ is_cached <- function(text,
6465
#' see `as.character(list(purrr::partial(sum, x = 4)))`. For that reason,
6566
#' all arguments passed to a `purrr::partial()` call must be put in the
6667
#' style guide under `more_specs_style_guide`.
68+
#'
6769
#' @section Experiments:
6870
#'
6971
#' There is unexplainable behavior in conjunction with hashing and
@@ -84,6 +86,7 @@ is_cached <- function(text,
8486
#' overwritten / rebased by now) contains a reprex. Otherwise, search for
8587
#' 43219ixmypi in commit messages and restore this commit to reproduce the
8688
#' behavior.
89+
#'
8790
#' @examples
8891
#' add <- function(x, y) {
8992
#' x + y
@@ -123,13 +126,16 @@ cache_find_path <- function(cache_name = NULL) {
123126
#' @keywords internal
124127
cache_is_activated <- function(cache_name = NULL) {
125128
current_cache <- cache_get_name()
129+
126130
if (is.null(cache_name)) {
127-
!is.null(current_cache)
128-
} else if (!is.null(current_cache)) {
129-
cache_name == current_cache
130-
} else {
131-
FALSE
131+
return(!is.null(current_cache))
132132
}
133+
134+
if (!is.null(current_cache)) {
135+
return(cache_name == current_cache)
136+
}
137+
138+
return(FALSE)
133139
}
134140

135141
#' Cache text
@@ -186,13 +192,9 @@ cache_get_name <- function() {
186192
getOption("styler.cache_name")
187193
}
188194

189-
cache_get_or_derive_name <- function(cache_name) {
190-
if (is.null(cache_name)) {
191-
cache_name <- cache_get_name()
192-
if (is.null(cache_name)) {
193-
cache_name <- styler_version
194-
}
195-
}
195+
cache_get_or_derive_name <- function(cache_name = NULL) {
196+
cache_name <- cache_name %||% cache_get_name()
197+
cache_name <- cache_name %||% styler_version
196198
cache_name
197199
}
198200

man/caching.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)