|
13 | 13 | )
|
14 | 14 | toset <- !(names(op.styler) %in% names(op))
|
15 | 15 | if (any(toset)) options(op.styler[toset])
|
16 |
| - remind_removing_old_cache() |
| 16 | + remove_cache_old_versions() |
| 17 | + remove_old_cache_files() |
17 | 18 | invisible()
|
18 | 19 | }
|
19 | 20 |
|
| 21 | +remove_old_cache_files <- function() { |
| 22 | + all_cached <- list.files( |
| 23 | + R.cache::getCachePath(c("styler", styler_version)), |
| 24 | + full.names = TRUE |
| 25 | + ) |
| 26 | + date_boundary <- Sys.time() - 60 * 60 * 24 * 6 |
| 27 | + file.remove( |
| 28 | + all_cached[file.info(all_cached)$mtime < date_boundary] |
| 29 | + ) |
| 30 | +} |
| 31 | + |
20 | 32 | #' Ask people to remove the cache
|
21 | 33 | #'
|
22 | 34 | #' The way RStudio Startup works does not allow o read the prompt for some
|
23 | 35 | #' reasons (https://stackoverflow.com/questions/55772436/readline-does-not-prompt-user-input-from-rprofile-site-in-rstudio)
|
24 | 36 | #' So we better don't use the prompt and issue a message only.
|
25 | 37 | #' @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 |
| - if (length(dirs) < 1) { |
30 |
| - return() |
31 |
| - } |
32 |
| - dirs <- dirs[unname(sapply(dirs, function(x) length(list.files(x)) > 0))] |
33 |
| - package_versions <- package_version(basename(dirs), strict = FALSE) |
34 |
| - package_versions <- package_versions[!is.na(package_versions)] |
35 |
| - old_package_versions <- package_versions[package_versions < styler_version] |
36 |
| - if (length(old_package_versions) < 1) { |
37 |
| - return() |
38 |
| - } |
39 |
| - |
40 |
| - cmd <- glue::glue("styler::cache_clear(\"{basename(dirs)}\", ask = FALSE)") %>% |
41 |
| - paste0(collapse = "\n") |
42 |
| - cli::cli_alert_info(paste0( |
43 |
| - "You are using {{styler}} version {styler_version} but we found ", |
44 |
| - "caches for older versions of {{styler}}.\n", |
45 |
| - "You can delete them with the following commands:" |
46 |
| - )) |
47 |
| - cat("\n") |
48 |
| - cli::cli_code(cmd) |
49 |
| - cat("\n") |
50 |
| - cli::cli_alert_info( |
51 |
| - paste( |
52 |
| - "We'll remind you every time you update {{styler}}.", |
53 |
| - "To suppress this prompt in the future:" |
54 |
| - ) |
55 |
| - ) |
56 |
| - cat("\n") |
57 |
| - cli::cli_code( |
58 |
| - 'options("styler.interactive_ask_remove_old_caches" = FALSE)' |
59 |
| - ) |
| 38 | +remove_cache_old_versions <- function() { |
| 39 | + dirs <- list.dirs(R.cache::getCachePath("styler"), recursive = FALSE) |
| 40 | + if (length(dirs) < 1) { |
| 41 | + return() |
| 42 | + } |
| 43 | + old_package_dirs <- dirs[basename(dirs) != as.character(styler_version)] |
| 44 | + if (length(old_package_dirs) < 1) { |
| 45 | + return() |
60 | 46 | }
|
| 47 | + purrr::walk(old_package_dirs, function(dir) { |
| 48 | + cache_clear(basename(dir), ask = FALSE) |
| 49 | + file.remove(dir) |
| 50 | + }) |
61 | 51 | }
|
0 commit comments