-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #538 from lorenzwalthert/caching
- Cache styling (#538).
- Loading branch information
Showing
43 changed files
with
892 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
#' Clear the cache | ||
#' | ||
#' Clears the cache that stores which files are already styled. You won't be | ||
#' able to undo this. Note that the file corresponding to the cache (a folder | ||
#' on your file stystem) won't be deleted, but it will be empty after calling | ||
#' `cache_clear`. | ||
#' @param cache_name The name of the styler cache to use. If | ||
#' `NULL`, the option "styler.cache_name" is considered which defaults to | ||
#' the version of styler used. | ||
#' @details | ||
#' Each version of styler has it's own cache by default, because styling is | ||
#' potentially different with different versions of styler. | ||
#' @param ask Whether or not to interactively ask the user again. | ||
#' @family cache managers | ||
#' @export | ||
cache_clear <- function(cache_name = NULL, ask = TRUE) { | ||
assert_R.cache_installation(installation_only = TRUE) | ||
path_cache <- cache_find_path(cache_name) | ||
R.cache::clearCache(path_cache, prompt = ask) | ||
cache_deactivate(verbose = FALSE) | ||
} | ||
|
||
#' Show information about the styler cache | ||
#' | ||
#' Gives information about the cache. Note that the size consumed by the cache | ||
#' will always be displayed as zero because all the cache does is creating an | ||
#' empty file of size 0 bytes for every cached expression. The innode is | ||
#' excluded from this displayed size but negligible. | ||
#' @param cache_name The name of the cache for which to show details. If | ||
#' `NULL`, the active cache is used. If none is active the cache corresponding | ||
#' to the installed styler version is used. | ||
#' @param format Either "lucid" for a summary emitted with [base::cat()], | ||
#' "tabular" for a tabular summary from [base::file.info()] or "both" for | ||
#' both. | ||
#' @family cache managers | ||
#' @export | ||
cache_info <- function(cache_name = NULL, format = "both") { | ||
assert_R.cache_installation(installation_only = TRUE) | ||
rlang::arg_match(format, c("tabular", "lucid", "both")) | ||
path_cache <- cache_find_path(cache_name) | ||
files <- list.files(path_cache, full.names = TRUE) | ||
file_info <- file.info(files) %>% | ||
as_tibble() | ||
tbl <- tibble( | ||
n = nrow(file_info), | ||
size = sum(file_info$size), | ||
last_modified = suppressWarnings(max(file_info$mtime)), | ||
created = file.info(path_cache)$ctime, | ||
location = path_cache, | ||
activated = cache_is_activated(cache_name) | ||
) | ||
if (format %in% c("lucid", "both")) { | ||
cat( | ||
"Size:\t\t", tbl$size, " bytes (", tbl$n, " cached expressions)", | ||
"\nLast modified:\t", as.character(tbl$last_modified), | ||
"\nCreated:\t", as.character(tbl$created), | ||
"\nLocation:\t", path_cache, | ||
"\nActivated:\t", tbl$activated, | ||
"\n", | ||
sep = "" | ||
) | ||
} | ||
if (format == "tabular") { | ||
tbl | ||
} else if (format == "both") { | ||
invisible(tbl) | ||
} | ||
} | ||
|
||
#' Activate or deactivate the styler cache | ||
#' | ||
#' Helper functions to control the behavior of caching. Simple wrappers around | ||
#' [base::options()]. | ||
#' @inheritParams cache_clear | ||
#' @param verbose Whether or not to print an informative message about what the | ||
#' function is doing. | ||
#' @family cache managers | ||
#' @export | ||
cache_activate <- function(cache_name = NULL, verbose = TRUE) { | ||
assert_R.cache_installation(installation_only = TRUE) | ||
if (!is.null(cache_name)) { | ||
options("styler.cache_name" = cache_name) | ||
} else { | ||
options("styler.cache_name" = styler_version) | ||
} | ||
path <- cache_find_path(cache_name) | ||
if (verbose) { | ||
cat( | ||
"Using cache ", cache_get_name(), " at ", | ||
path, ".\n", | ||
sep = "" | ||
) | ||
} | ||
invisible(path) | ||
} | ||
|
||
#' @rdname cache_activate | ||
#' @export | ||
cache_deactivate <- function(verbose = TRUE) { | ||
options("styler.cache_name" = NULL) | ||
|
||
if (verbose) { | ||
cat("Deactivated cache.\n") | ||
} | ||
} |
File renamed without changes.
Oops, something went wrong.