Skip to content

Commit 0154df1

Browse files
committed
initial infrastructure
1 parent f468053 commit 0154df1

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

DESCRIPTION

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@ Depends:
3333
R (>= 4.0)
3434
Imports:
3535
cli,
36+
desc,
3637
grDevices,
3738
grid,
3839
gtable (>= 0.1.1),
3940
isoband,
4041
lifecycle (> 1.0.1),
42+
pkgload,
4143
rlang (>= 1.1.0),
4244
scales (>= 1.3.0),
4345
stats,
@@ -122,6 +124,7 @@ Collate:
122124
'coord-transform.R'
123125
'data.R'
124126
'docs_layer.R'
127+
'edition.R'
125128
'facet-.R'
126129
'facet-grid-.R'
127130
'facet-null.R'

R/edition.R

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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

Comments
 (0)