Skip to content

Commit

Permalink
Change default verbosity control
Browse files Browse the repository at this point in the history
  • Loading branch information
richfitz committed Oct 2, 2024
1 parent d709a6e commit 4ede487
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 7 deletions.
14 changes: 13 additions & 1 deletion R/compile.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
##' @param quiet Logical, indicating if compilation messages from
##' `pkgbuild` should be displayed. Error messages will be
##' displayed on compilation failure regardless of the value used.
##' If `NULL` is given, then we take the value from `DUST_QUIET`, or
##' `FALSE` otherwise.
##'
##' @param workdir Optional working directory to use. If `NULL`, the
##' behaviour depends on the existence of the environment variable
Expand Down Expand Up @@ -72,7 +74,7 @@
##' a `dust_system_generator` function.
##'
##' @export
dust_compile <- function(filename, quiet = FALSE, workdir = NULL,
dust_compile <- function(filename, quiet = NULL, workdir = NULL,
linking_to = NULL, cpp_std = NULL,
compiler_options = NULL, optimisation_level = NULL,
debug = FALSE, skip_cache = FALSE) {
Expand All @@ -92,6 +94,7 @@ dust_compile <- function(filename, quiet = FALSE, workdir = NULL,
}

workdir <- dust_workdir(workdir, hash)
quiet <- dust_quiet(quiet)
dir_create(c(workdir, file.path(workdir, c("R", "src"))))
writelines_if_changed(res$description, workdir, "DESCRIPTION", quiet)
writelines_if_changed(res$namespace, workdir, "NAMESPACE", quiet)
Expand Down Expand Up @@ -286,3 +289,12 @@ dust_workdir <- function(path, hash, call = parent.frame()) {
}
path
}


dust_quiet <- function(quiet, call = parent.frame()) {
if (is.null(quiet)) {
envvar_is_truthy("DUST_QUIET")
} else {
assert_scalar_logical(quiet, call = call)
}
}
7 changes: 5 additions & 2 deletions R/package.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,21 @@
##' @param path Path to the package
##'
##' @param quiet Passed to `cpp11::cpp_register`, if `TRUE` suppresses
##' informational notices about updates to the cpp11 files
##' informational notices about updates to the cpp11 files. If
##' `NULL`, uses the value of the environment variable `DUST_QUIET`
##' of set or `FALSE` otherwise.
##'
##' @title Create dust system in package
##'
##' @return Nothing, this function is called for its side effects
##'
##' @export
dust_package <- function(path, quiet = FALSE) {
dust_package <- function(path, quiet = NULL) {
call <- environment()
pkg <- package_validate_root(path, call)
path_dust <- file.path(path, "inst/dust")

quiet <- dust_quiet(quiet)
if (!quiet) {
cli::cli_alert_info("Working in package '{pkg}' at '{path}'")
}
Expand Down
5 changes: 5 additions & 0 deletions R/util.R
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,8 @@ tail_errors <- function(x, n = 5) {
fmod <- function(n, m) {
(floor(n / m) * m) - n
}


envvar_is_truthy <- function(name) {
tolower(Sys.getenv(name, "false")) %in% c("t", "true", "1")
}
6 changes: 4 additions & 2 deletions man/dust_compile.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions man/dust_package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions tests/testthat/test-compile.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,18 @@ test_that("can work in a stable temporary directory", {
c(DUST_WORKDIR_ROOT = "my/path"),
expect_equal(dust_workdir(NULL, hash), "my/path/dust_abc1234"))
})


test_that("quiet responds to envvar", {
withr::with_envvar(c(DUST_QUIET = NA_character_), {
expect_false(dust_quiet(NULL))
expect_false(dust_quiet(FALSE))
expect_false(dust_quiet(TRUE))
expect_error(dust_quiet(1), "Expected 'quiet' to be logical")
})

withr::with_envvar(c(DUST_QUIET = "true"), {
expect_true(dust_quiet(NULL))
expect_false(dust_quiet(FALSE))
})
})
2 changes: 2 additions & 0 deletions vignettes/details.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ knitr::opts_chunk$set(
* You want to cache the compilation across sessions

If you set this environment variable, then we'll create models here, each with a name in the pattern `dust_<hash>` where `<hash>` is the hash of the generated code. This means that if your model changes we'll still recompile it.

`DUST_QUIET`: an environment which controls the default level of verbosity when compiling dust models. Set this to a truthy value (e.g., `TRUE`) to change the default. This can always be overridden by providing a value for the `quiet` argument to `dust_compile` and `dust_package`.

0 comments on commit 4ede487

Please sign in to comment.