Skip to content

Commit

Permalink
Use the$ convention for internal env
Browse files Browse the repository at this point in the history
  • Loading branch information
cderv committed Sep 10, 2021
1 parent 0316f6f commit 48f7971
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 36 deletions.
3 changes: 3 additions & 0 deletions R/aaa.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# internal environment acting as storage and cache
# Using convention in https://github.com/tidyverse/design/issues/126
the <- new.env(parent = emptyenv())
2 changes: 1 addition & 1 deletion R/pandoc-available.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#' @export
pandoc_available <- function(min = NULL, max = NULL, strict = FALSE) {
# TODO: get the version from calling pandoc instead.
active_version <- pandoc_active_get()
active_version <- the$active_version
# No active pandoc
if (active_version == "") return(FALSE)

Expand Down
30 changes: 9 additions & 21 deletions R/pandoc-install.R
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ pandoc_bundle_name <- function(version, os = pandoc_os(), arch = pandoc_arch(os)
}

pandoc_releases <- function() {
rlang::env_cache(pandocenv, "pandoc_releases", fetch_gh_releases())
rlang::env_cache(the, "pandoc_releases", fetch_gh_releases())
}

fetch_gh_releases <- function() {
Expand Down Expand Up @@ -343,10 +343,10 @@ pandoc_uninstall <- function(version) {
latest <- pandoc_installed_latest()
if (is.null(latest)) {
# no more version installed
pandoc_active_unset()
the$active_version <- NULL
} else {
# Change to the latest version
pandoc_active_set(latest)
the$active_version <- latest
}

}
Expand All @@ -369,18 +369,6 @@ pandoc_available_versions <- function() {
keep(versions, ~ numeric_version(.x) >= .min_supported_version)
}

pandoc_active_set <- function(version) {
rlang::env_poke(pandocenv, "active_version", version, inherit = FALSE)
}

pandoc_active_unset <- function(version) {
rlang::env_unbind(pandocenv, "active_version", inherit = FALSE)
}

pandoc_active_get <- function() {
rlang::env_get(pandocenv, "active_version", default = "", inherit = FALSE)
}

#' Activate a specific Pandoc version to be used
#'
#' This function will set the specified version as the default version for the
Expand All @@ -395,16 +383,16 @@ pandoc_active_get <- function() {
#' @return invisibly, the previous active version.
#' @export
pandoc_set_version <- function(version, rmarkdown = TRUE) {
old_active <- pandoc_active_get()
old_active <- the$active_version
if (version == "latest") version <- pandoc_installed_latest()
if (is.null(version)) {
pandoc_active_set("")
the$active_version <- ""
} else {
if (!pandoc_is_installed(version)) {
rlang::abort(sprintf("Version %s is not yet installed", version))
}
pandoc_active_set(version)
rlang::inform(c(v = sprintf("Version %s is now the active one.", pandoc_active_get())))
the$active_version <- version
rlang::inform(c(v = sprintf("Version %s is now the active one.", the$active_version)))
if (rmarkdown && rlang::is_installed("rmarkdown")) {
rmarkdown::find_pandoc(cache = FALSE, dir = pandoc_locate())
rlang::inform(c(i = "This is also true for using with rmarkdown functions."))
Expand All @@ -420,7 +408,7 @@ pandoc_set_version <- function(version, rmarkdown = TRUE) {
#' @export
pandoc_is_active <- function(version) {
if (version == "latest") version <- pandoc_installed_latest()
version == pandoc_active_get()
version == the$active_version
}

#' Locate a specific installed Pandoc version
Expand All @@ -438,7 +426,7 @@ pandoc_locate <- function(version = "default") {
if (!is.character(version) && length(version) != 1L) {
rlang::abort("version must be a length one character")
}
if (version == "default") version <- pandoc_active_get()
if (version == "default") version <- the$active_version
if (version == "latest") version <- pandoc_installed_latest()
if (is.null(version) || !nzchar(version)) {
rlang::warn("No Pandoc version available.")
Expand Down
3 changes: 0 additions & 3 deletions R/pandoc-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,3 @@
## usethis namespace: start
## usethis namespace: end
NULL

# internal environment acting as storage and cache
pandocenv <- rlang::new_environment()
4 changes: 2 additions & 2 deletions R/pandoc-run.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ pandoc_version <- function(bin = pandoc_bin()) {
#' @export
with_pandoc_version <- function(version, code, rmarkdown = FALSE) {
old <- pandoc_set_version(version, rmarkdown = rmarkdown)
on.exit(pandoc_active_set(old))
on.exit(the$active_version <- old)
force(code)
}

local_pandoc_version <- function(version, rmarkdown = FALSE,
.local_envir = parent.frame()) {
rlang::check_installed("withr")
old <- suppressMessages(pandoc_set_version(version, rmarkdown = rmarkdown))
withr::defer(pandoc_active_set(old), envir = .local_envir)
withr::defer(the$active_version <- old, envir = .local_envir)
invisible(old)
}
2 changes: 1 addition & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.onLoad <- function(libname, pkgname) {
latest <- pandoc_installed_latest()
if (!is.null(latest)) pandoc_active_set(latest)
if (!is.null(latest)) the$active_version <- latest
}

4 changes: 2 additions & 2 deletions tests/testthat/test-pandoc-available.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ test_that("pandoc_available() works", {
expect_false(pandoc_available(max = "2.11.4", strict = TRUE))
expect_false(pandoc_available(min = "2.11.4", strict = TRUE))
# as if no active version
pandoc_active_set("")
the$active_version <- ""
expect_false(pandoc_available())
suppressMessages(pandoc_active_set(old))
the$active_version <- old
})

12 changes: 6 additions & 6 deletions tests/testthat/test-pandoc-install.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ skip_if_offline()
test_that("Release information are cached", {
skip_on_cran()
skip_if_offline()
# clean cache
rlang::env_bind(pandocenv, pandoc_releases = rlang::zap())
# clean cached values
the$pandoc_releases <- NULL
# with message Fetching
expect_snapshot(x <- pandoc_releases())
# without message Fetching
Expand Down Expand Up @@ -102,7 +102,7 @@ test_that("No versions are installed", {
walk(pandoc_installed_versions(), pandoc_uninstall)
expect_null(pandoc_installed_versions())
expect_null(pandoc_installed_latest())
expect_identical(pandoc_active_get(), "")
expect_identical(the$active_version, "")
})

test_that("Pandoc nightly can be installed", {
Expand Down Expand Up @@ -186,11 +186,11 @@ test_that("Pandoc release can be uninstall", {
skip_if_offline()
suppressMessages(pandoc_install("2.11.4"))
suppressMessages(pandoc_install("2.7.3"))
pandoc_active_set("2.11.4")
the$active_version <- "2.11.4"
expect_true(pandoc_uninstall("2.11.4"))
expect_equal(pandoc_active_get(), pandoc_installed_latest())
expect_equal(the$active_version, pandoc_installed_latest())
expect_true(pandoc_uninstall("2.7.3"))
expect_equal(pandoc_active_get(), "")
expect_equal(the$active_version, "")
})

test_that("Pandoc latest release can be installed", {
Expand Down

0 comments on commit 48f7971

Please sign in to comment.