Skip to content

Commit 682f32f

Browse files
richfitzjimhester
authored andcommitted
Optionally register quietly
1 parent 9cba571 commit 682f32f

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

R/register.R

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
#' In order to use `cpp_register()` the `cli`, `decor`, `desc`, `glue`,
88
#' `tibble` and `vctrs` packages must also be installed.
99
#' @param path The path to the package root directory
10+
#' @param quiet If `TRUE` suppresses output from this function
1011
#' @return The paths to the generated R and C++ source files (in that order).
1112
#' @export
12-
cpp_register <- function(path = ".") {
13+
cpp_register <- function(path = ".", quiet = FALSE) {
1314
stop_unless_installed(c("brio", "cli", "decor", "desc", "glue", "tibble", "vctrs"))
1415

1516
r_path <- file.path(path, "R", "cpp11.R")
@@ -24,13 +25,13 @@ cpp_register <- function(path = ".") {
2425
return(invisible(character()))
2526
}
2627

27-
funs <- get_registered_functions(all_decorations, "cpp11::register")
28+
funs <- get_registered_functions(all_decorations, "cpp11::register", quiet)
2829

2930
package <- desc::desc_get("Package", file = file.path(path, "DESCRIPTION"))
3031

3132
cpp_functions_definitions <- generate_cpp_functions(funs, package)
3233

33-
init <- generate_init_functions(get_registered_functions(all_decorations, "cpp11::init"))
34+
init <- generate_init_functions(get_registered_functions(all_decorations, "cpp11::init", quiet))
3435

3536
r_functions <- generate_r_functions(funs, package)
3637

@@ -42,7 +43,9 @@ cpp_register <- function(path = ".") {
4243
{r_functions}
4344
'
4445
))
45-
cli::cli_alert_success("generated file {.file {basename(r_path)}}")
46+
if (!quiet) {
47+
cli::cli_alert_success("generated file {.file {basename(r_path)}}")
48+
}
4649

4750
call_entries <- get_call_entries(path)
4851

@@ -88,14 +91,16 @@ cpp_register <- function(path = ".") {
8891
call_entries = glue::glue_collapse(call_entries, "\n")
8992
))
9093

91-
cli::cli_alert_success("generated file {.file {basename(cpp_path)}}")
94+
if (!quiet) {
95+
cli::cli_alert_success("generated file {.file {basename(cpp_path)}}")
96+
}
9297

9398
invisible(c(r_path, cpp_path))
9499
}
95100

96101
utils::globalVariables(c("name", "return_type", "line", "decoration", "context", ".", "functions", "res"))
97102

98-
get_registered_functions <- function(decorations, tag) {
103+
get_registered_functions <- function(decorations, tag, quiet = FALSE) {
99104
if (NROW(decorations) == 0) {
100105
return(tibble::tibble(file = character(), line = integer(), decoration = character(), params = list(), context = list(), name = character(), return_type = character(), args = list()))
101106
}
@@ -107,7 +112,9 @@ get_registered_functions <- function(decorations, tag) {
107112
out <- out[!(names(out) %in% "functions")]
108113
out$decoration <- sub("::[[:alpha:]]+", "", out$decoration)
109114

110-
cli::cli_alert_info(glue::glue("{n} functions decorated with [[{tag}]]", n = nrow(out)))
115+
if (!quiet) {
116+
cli::cli_alert_info(glue::glue("{n} functions decorated with [[{tag}]]", n = nrow(out)))
117+
}
111118

112119
out
113120
}

man/cpp_register.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-register.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,23 @@ extern \"C\" void R_init_testPkg(DllInfo* dll){
547547
}
548548
")
549549
})
550+
551+
it("can be run without messages", {
552+
pkg <- local_package()
553+
p <- pkg_path(pkg)
554+
dir.create(file.path(p, "src"))
555+
file.copy(test_path("single.cpp"), file.path(p, "src", "single.cpp"))
556+
expect_silent(cpp_register(p, quiet = TRUE))
557+
})
558+
559+
560+
it("can be run with messages, by default", {
561+
pkg <- local_package()
562+
p <- pkg_path(pkg)
563+
dir.create(file.path(p, "src"))
564+
file.copy(test_path("single.cpp"), file.path(p, "src", "single.cpp"))
565+
expect_message(cpp_register(p), "1 functions decorated with [[cpp11::register]]", fixed = TRUE)
566+
})
550567
})
551568

552569
describe("generate_init_functions", {

0 commit comments

Comments
 (0)