Skip to content

Commit d042b8f

Browse files
show user a message to clear old caches in interactive use only.
1 parent d5dd188 commit d042b8f

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ BugReports: https://github.com/r-lib/styler/issues
1919
Imports:
2020
backports (>= 1.1.0),
2121
cli (>= 1.1.0),
22+
glue,
2223
magrittr (>= 2.0.0),
2324
purrr (>= 0.2.3),
2425
R.cache (>= 0.15.0),

R/zzz.R

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,51 @@
88
styler.ignore_start = "# styler: off",
99
styler.ignore_stop = "# styler: on",
1010
styler.quiet = FALSE,
11-
styler.test_dir_writable = TRUE
11+
styler.test_dir_writable = TRUE,
12+
styler.interactive_ask_remove_old_caches = TRUE
1213
)
1314
toset <- !(names(op.styler) %in% names(op))
1415
if (any(toset)) options(op.styler[toset])
16+
remind_removing_old_cache()
1517
invisible()
1618
}
19+
20+
#' Ask people to remove the cache
21+
#'
22+
#' The way RStudio Startup works does not allow o read the prompt for some
23+
#' reasons (https://stackoverflow.com/questions/55772436/readline-does-not-prompt-user-input-from-rprofile-site-in-rstudio)
24+
#' So we better don't use the prompt and issue a message only.
25+
#' @keywords internal
26+
remind_removing_old_cache <- function() {
27+
if (interactive() && getOption("styler.interactive_ask_remove_old_caches", TRUE)) {
28+
dirs <- list.dirs(R.cache::getCachePath("styler"), recursive = FALSE)
29+
dirs <- dirs[unname(sapply(dirs, function(x) length(list.files(x)) > 0))]
30+
package_versions <- package_version(basename(dirs), strict = FALSE)
31+
package_versions <- package_versions[!is.na(package_versions)]
32+
old_package_versions <- package_versions[package_versions < styler_version]
33+
if (length(old_package_versions) < 1) {
34+
return()
35+
}
36+
37+
cmd <- glue::glue("styler::cache_clear(\"{basename(dirs)}\", ask = FALSE)") %>%
38+
paste0(collapse = "\n")
39+
cli::cli_alert_info(paste0(
40+
"You are using {{styler}} version {styler_version} but we found ",
41+
"caches for older versions of {{styler}}.\n",
42+
"You can delete them with the following commands:"
43+
))
44+
cat("\n")
45+
cli::cli_code(cmd)
46+
cat("\n")
47+
cli::cli_alert_info(
48+
paste(
49+
"We'll remind you every time you update {{styler}}.",
50+
"To suppress this prompt in the future:"
51+
)
52+
)
53+
cat("\n")
54+
cli::cli_code(
55+
'options("styler.interactive_ask_remove_old_caches" = FALSE)'
56+
)
57+
}
58+
}

inst/WORDLIST

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ questionr
144144
rcmdcheck
145145
RcppExports
146146
rds
147+
readline
147148
readme
148149
README
149150
rebased
@@ -199,6 +200,7 @@ shinyMonacoEditor
199200
shinyobjects
200201
ShinyQuickStarter
201202
sprintf
203+
stackoverflow
202204
StackOverflow
203205
startsWith
204206
STR

man/remind_removing_old_cache.Rd

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)