diff --git a/R/utils-vignettes.R b/R/utils-vignettes.R index 80810f7..1d6db77 100644 --- a/R/utils-vignettes.R +++ b/R/utils-vignettes.R @@ -20,6 +20,19 @@ vig_engine <- function(..., quarto_format) { vweave_quarto <- function(format) { meta <- get_meta(format) function(file, driver, syntax, encoding, quiet = FALSE, ...) { + # protect if Quarto is not installed + if (is.null(quarto_path())) { + msg <- c( + "Quarto binary is required to build Quarto vignettes but is not available.", + i = "Please make sure it is installed and found by {.code find_quarto()}." + ) + if (is_R_CMD_check()) { + cli::cli_inform(msg) + } else { + cli::cli_abort(msg, call = NULL) + } + return(vweave_empty(file)) + } quarto_render(file, ..., output_format = format, metadata = meta) } } @@ -29,6 +42,18 @@ get_meta <- function(format) { if (format == "html") { return(get_meta_for_html()) } + if (format == "pdf") { + return(get_meta_for_pdf()) + } +} + +get_meta_for_pdf <- function() { + meta <- list() + meta$format$pdf <- list( + # don't try to install CTAN package on CRAN environment + `latex-auto-install` = !is_cran_check() + ) + meta } get_meta_for_html <- function() { @@ -45,3 +70,25 @@ get_meta_for_html <- function() { ) meta } + +is_R_CMD_check <- function() { + !is.na(Sys.getenv("_R_CHECK_PACKAGE_NAME_", NA)) || + tolower(Sys.getenv("_R_CHECK_LICENSE_")) == "true" +} + +# from knitr internal +is_cran_check <- function() { + is_cran() && is_R_CMD_check() +} + +is_cran = function() { + !rlang::is_interactive() && !isTRUE(as.logical(Sys.getenv("NOT_CRAN", "false"))) +} + +# trick from knitr to avoid problem on R CMD check (e.g. when no Quarto available) +# It will silently skip the vignette +vweave_empty <- function(file, ..., .reason = 'Quarto') { + out <- sprintf("%s.html", tools::file_path_sans_ext(basename(file))) + writeLines(sprintf("The vignette could not be built because %s is not available.", .reason), out) + out +} diff --git a/tests/testthat/test-utils-vignettes.R b/tests/testthat/test-utils-vignettes.R new file mode 100644 index 0000000..c1af9c0 --- /dev/null +++ b/tests/testthat/test-utils-vignettes.R @@ -0,0 +1,16 @@ +test_that("don't auto install CTAN package on CRAN", { + skip_on_cran() + skip_if_not_installed("withr") + withr::with_envvar(list( + # simulate non CRAN + "NOT_CRAN" = "false", + # simulate R CMD check + "_R_CHECK_LICENSE_" = "true"), { + expect_false(get_meta_for_pdf()$format$pdf$`latex-auto-install`) + }) + withr::with_envvar(list( + "_R_CHECK_PACKAGE_NAME_" = NA + ), { + expect_true(get_meta_for_pdf()$format$pdf$`latex-auto-install`) + }) +})