|
| 1 | + |
| 2 | +ggplot_edition <- new.env(parent = emptyenv()) |
| 3 | + |
| 4 | +local_edition <- function(edition, .env = parent.frame()) { |
| 5 | + stopifnot(is_zap(edition) || (is.numeric(edition) && length(edition) == 1)) |
| 6 | + pkg <- get_pkg_name(.env) |
| 7 | + local_bindings(!!pkg := edition, .env = ggplot_edition, .frame = .env) |
| 8 | +} |
| 9 | + |
| 10 | +edition_get <- function(.env = parent.frame(), default = 2024L) { |
| 11 | + pkg <- get_pkg_name(.env) |
| 12 | + |
| 13 | + # Try to query edition from cache |
| 14 | + edition <- env_get(ggplot_edition, nm = pkg, default = NULL) |
| 15 | + if (!is.null(edition)) { |
| 16 | + return(edition) |
| 17 | + } |
| 18 | + |
| 19 | + # Try to query package description |
| 20 | + desc <- find_description(path = ".", package = pkg) |
| 21 | + if (is.null(desc)) { |
| 22 | + return(default) |
| 23 | + } |
| 24 | + |
| 25 | + # Look up edition from the description |
| 26 | + field_name <- "Config/ggplot2/edition" |
| 27 | + edition <- as.integer(desc$get_field(field_name, default = default)) |
| 28 | + |
| 29 | + # Cache result |
| 30 | + env_bind(ggplot_edition, !!pkg := edition) |
| 31 | + return(edition) |
| 32 | +} |
| 33 | + |
| 34 | +edition_deprecate <- function(edition, ..., .env = parent.frame()) { |
| 35 | + check_number_whole(edition) |
| 36 | + if (edition_get(.env) < edition) { |
| 37 | + return(invisible(NULL)) |
| 38 | + } |
| 39 | + |
| 40 | + edition <- I(paste0("edition ", edition)) |
| 41 | + lifecycle::deprecate_stop(edition, ...) |
| 42 | +} |
| 43 | + |
| 44 | +edition_require <- function(edition, what, .env = parent.frame()) { |
| 45 | + check_number_whole(edition) |
| 46 | + current <- edition_get(.env) |
| 47 | + if (current >= edition) { |
| 48 | + return(invisible(NULL)) |
| 49 | + } |
| 50 | + msg <- paste0(what, " requires the ", edition, " edition of {.pkg ggplot2}.") |
| 51 | + cli::cli_abort(msg) |
| 52 | +} |
| 53 | + |
| 54 | +find_description <- function(path, package = NULL) { |
| 55 | + if (!is.null(package)) { |
| 56 | + return(desc::desc(package = package)) |
| 57 | + } else { |
| 58 | + try_fetch( |
| 59 | + pkgload::pkg_desc(path), |
| 60 | + error = function(e) NULL |
| 61 | + ) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +get_pkg_name <- function(env = parent.frame()) { |
| 66 | + env <- topenv(env) |
| 67 | + name <- environmentName(env) |
| 68 | + if (!isNamespace(env) && name != "R_GlobalEnv") { |
| 69 | + return(NULL) |
| 70 | + } |
| 71 | + name |
| 72 | +} |
0 commit comments