Skip to content

Commit

Permalink
initial infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
teunbrand committed Nov 26, 2024
1 parent f468053 commit 0154df1
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
3 changes: 3 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ Depends:
R (>= 4.0)
Imports:
cli,
desc,
grDevices,
grid,
gtable (>= 0.1.1),
isoband,
lifecycle (> 1.0.1),
pkgload,
rlang (>= 1.1.0),
scales (>= 1.3.0),
stats,
Expand Down Expand Up @@ -122,6 +124,7 @@ Collate:
'coord-transform.R'
'data.R'
'docs_layer.R'
'edition.R'
'facet-.R'
'facet-grid-.R'
'facet-null.R'
Expand Down
72 changes: 72 additions & 0 deletions R/edition.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@

ggplot_edition <- new.env(parent = emptyenv())

local_edition <- function(edition, .env = parent.frame()) {
stopifnot(is_zap(edition) || (is.numeric(edition) && length(edition) == 1))
pkg <- get_pkg_name(.env)
local_bindings(!!pkg := edition, .env = ggplot_edition, .frame = .env)

Check warning on line 7 in R/edition.R

View check run for this annotation

Codecov / codecov/patch

R/edition.R#L5-L7

Added lines #L5 - L7 were not covered by tests
}

edition_get <- function(.env = parent.frame(), default = 2024L) {
pkg <- get_pkg_name(.env)

Check warning on line 11 in R/edition.R

View check run for this annotation

Codecov / codecov/patch

R/edition.R#L11

Added line #L11 was not covered by tests

# Try to query edition from cache
edition <- env_get(ggplot_edition, nm = pkg, default = NULL)
if (!is.null(edition)) {
return(edition)

Check warning on line 16 in R/edition.R

View check run for this annotation

Codecov / codecov/patch

R/edition.R#L14-L16

Added lines #L14 - L16 were not covered by tests
}

# Try to query package description
desc <- find_description(path = ".", package = pkg)
if (is.null(desc)) {
return(default)

Check warning on line 22 in R/edition.R

View check run for this annotation

Codecov / codecov/patch

R/edition.R#L20-L22

Added lines #L20 - L22 were not covered by tests
}

# Look up edition from the description
field_name <- "Config/ggplot2/edition"
edition <- as.integer(desc$get_field(field_name, default = default))

Check warning on line 27 in R/edition.R

View check run for this annotation

Codecov / codecov/patch

R/edition.R#L26-L27

Added lines #L26 - L27 were not covered by tests

# Cache result
env_bind(ggplot_edition, !!pkg := edition)
return(edition)

Check warning on line 31 in R/edition.R

View check run for this annotation

Codecov / codecov/patch

R/edition.R#L30-L31

Added lines #L30 - L31 were not covered by tests
}

edition_deprecate <- function(edition, ..., .env = parent.frame()) {
check_number_whole(edition)
if (edition_get(.env) < edition) {
return(invisible(NULL))

Check warning on line 37 in R/edition.R

View check run for this annotation

Codecov / codecov/patch

R/edition.R#L35-L37

Added lines #L35 - L37 were not covered by tests
}

edition <- I(paste0("edition ", edition))
lifecycle::deprecate_stop(edition, ...)

Check warning on line 41 in R/edition.R

View check run for this annotation

Codecov / codecov/patch

R/edition.R#L40-L41

Added lines #L40 - L41 were not covered by tests
}

edition_require <- function(edition, what, .env = parent.frame()) {
check_number_whole(edition)
current <- edition_get(.env)
if (current >= edition) {
return(invisible(NULL))

Check warning on line 48 in R/edition.R

View check run for this annotation

Codecov / codecov/patch

R/edition.R#L45-L48

Added lines #L45 - L48 were not covered by tests
}
msg <- paste0(what, " requires the ", edition, " edition of {.pkg ggplot2}.")
cli::cli_abort(msg)

Check warning on line 51 in R/edition.R

View check run for this annotation

Codecov / codecov/patch

R/edition.R#L50-L51

Added lines #L50 - L51 were not covered by tests
}

find_description <- function(path, package = NULL) {
if (!is.null(package)) {
return(desc::desc(package = package))

Check warning on line 56 in R/edition.R

View check run for this annotation

Codecov / codecov/patch

R/edition.R#L55-L56

Added lines #L55 - L56 were not covered by tests
} else {
try_fetch(
pkgload::pkg_desc(path),
error = function(e) NULL
)

Check warning on line 61 in R/edition.R

View check run for this annotation

Codecov / codecov/patch

R/edition.R#L58-L61

Added lines #L58 - L61 were not covered by tests
}
}

get_pkg_name <- function(env = parent.frame()) {
env <- topenv(env)
name <- environmentName(env)
if (!isNamespace(env) && name != "R_GlobalEnv") {
return(NULL)

Check warning on line 69 in R/edition.R

View check run for this annotation

Codecov / codecov/patch

R/edition.R#L66-L69

Added lines #L66 - L69 were not covered by tests
}
name

Check warning on line 71 in R/edition.R

View check run for this annotation

Codecov / codecov/patch

R/edition.R#L71

Added line #L71 was not covered by tests
}

0 comments on commit 0154df1

Please sign in to comment.