Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add a field to DESCRIPTION to notify which version of rextendr the package was created with #225

Merged
merged 12 commits into from
Jan 6, 2023
Merged
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ importFrom(purrr,map2)
importFrom(purrr,map2_chr)
importFrom(purrr,map_if)
importFrom(purrr,map_lgl)
importFrom(rlang,"%||%")
importFrom(rlang,.data)
importFrom(rlang,.env)
importFrom(rlang,as_function)
Expand Down
10 changes: 7 additions & 3 deletions R/register_extendr.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@
#' @seealso [rextendr::document()]
#' @export
register_extendr <- function(path = ".", quiet = FALSE, force = FALSE, compile = NA) {
usethis_quiet <- getOption("usethis.quiet")
on.exit(options(usethis.quiet = usethis_quiet))
options(usethis.quiet = quiet)

rextendr_setup(path = path)

pkg_name <- pkg_name(path)

if (!isTRUE(quiet)) {
ui_i("Generating extendr wrapper functions for package: {.pkg {pkg_name}}.")
}
ui_i("Generating extendr wrapper functions for package: {.pkg {pkg_name}}.")

entrypoint_c_file <- rprojroot::find_package_root_file("src", "entrypoint.c", path = path)
if (!file.exists(entrypoint_c_file)) {
Expand Down
2 changes: 1 addition & 1 deletion R/rextendr.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
#' @importFrom purrr map2 map2_chr map_lgl flatten_chr map_if every map discard
#' @importFrom glue glue glue_collapse
#' @importFrom rlang dots_list names2 as_function is_missing is_atomic is_null
#' @importFrom rlang is_na .data .env caller_env as_name as_label enquo
#' @importFrom rlang is_na .data .env caller_env as_name as_label enquo %||%
#' @importFrom stringi stri_replace_all_regex
NULL
36 changes: 36 additions & 0 deletions R/setup.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
rextendr_setup <- function(path = ".", cur_version = NULL) {
if (!file.exists(file.path(path, "DESCRIPTION"))) {
ui_throw(
"{.arg package.dir} ({.path {path}}) does not contain a DESCRIPTION"
)
}

is_first <- is.na(rextendr_version(path))

if (is_first) {
ui_i("First time using rextendr. Upgrading automatically...")
}

update_rextendr_version(path, cur_version = cur_version)

invisible(is_first)
}

update_rextendr_version <- function(path, cur_version = NULL) {
cur <- cur_version %||% as.character(utils::packageVersion("rextendr"))
prev <- rextendr_version(path)

if (!is.na(cur) && !is.na(prev) && package_version(cur) < package_version(prev)) {
ui_w(c(
"Installed rextendr is older than the version used with this package\n",
"You have {.str {cur}} but you need {.str {prev}}"
))
} else if (!identical(cur, prev)) {
ui_i("Setting {.var Config/rextendr/version} to {.str {cur}}")
desc::desc_set(`Config/rextendr/version` = cur, file = path)
}
}

rextendr_version <- function(path = ".") {
stringi::stri_trim_both(desc::desc_get("Config/rextendr/version", path)[[1]])
}
18 changes: 9 additions & 9 deletions R/use_extendr.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ use_extendr <- function(path = ".",
lib_name = NULL,
quiet = getOption("usethis.quiet", FALSE),
edition = c("2021", "2018")) {
usethis_quiet <- getOption("usethis.quiet")
on.exit(options(usethis.quiet = usethis_quiet))
options(usethis.quiet = quiet)

rextendr_setup(path = path)

pkg_name <- pkg_name(path)
mod_name <- as_valid_rust_name(pkg_name)

Expand All @@ -44,22 +50,16 @@ use_extendr <- function(path = ".",
r_dir <- rprojroot::find_package_root_file("R", path = path)
wrappers_file <- rprojroot::find_package_root_file("R", "extendr-wrappers.R", path = path)
if (!dir.exists(r_dir)) {
if (!isTRUE(quiet)) {
ui_v("Writing {.file R/}")
}
ui_v("Writing {.file R/}")
dir.create(r_dir)
}

if (dir.exists(src_dir)) {
if (!isTRUE(quiet)) {
ui_x("Directory {.file src} already present in package source. No action taken.")
}
ui_x("Directory {.file src} already present in package source. No action taken.")
return(invisible(FALSE))
}
if (file.exists(wrappers_file)) {
if (!isTRUE(quiet)) {
ui_x("File {.file R/extendr-wrappers.R} already present in package source. No action taken.")
}
ui_x("File {.file R/extendr-wrappers.R} already present in package source. No action taken.")
return(invisible(FALSE))
}

Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/_snaps/use_extendr.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
Code
use_extendr()
Message
i First time using rextendr. Upgrading automatically...
i Setting `Config/rextendr/version` to "0.2.0.9000"
v Creating 'src/rust/src'.
v Writing 'src/entrypoint.c'
v Writing 'src/Makevars'
Expand Down Expand Up @@ -171,3 +173,8 @@
fn hello_world;
}

# use_extendr() quiet if quiet=TRUE

Code
use_extendr(quiet = TRUE)

19 changes: 19 additions & 0 deletions tests/testthat/test-document.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,22 @@ test_that("Running `document` after adding multiple files", {

expect_error(rextendr::document(), NA)
})

test_that("Warn if using older rextendr", {
path <- local_package("futurepkg")
use_extendr()
desc::desc_set(`Config/rextendr/version` = "999.999")

expect_message(document(quiet = FALSE), "Installed rextendr is older than the version used with this package")
})

test_that("Update the Config/rextendr/version field in DESCRIPTION file", {
path <- local_package("oldpkg")
use_extendr()
desc::desc_set(`Config/rextendr/version` = "0.1")

expect_message(document(quiet = FALSE), "Setting `Config/rextendr/version` to")

version_in_desc <- stringi::stri_trim_both(desc::desc_get("Config/rextendr/version", path)[[1]])
expect_equal(version_in_desc, as.character(packageVersion("rextendr")))
})
10 changes: 10 additions & 0 deletions tests/testthat/test-use_extendr.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ test_that("use_extendr() sets up extendr files correctly", {
withr::local_options(usethis.quiet = FALSE)
expect_snapshot(use_extendr())

# DESCRITION file
version_in_desc <- stringi::stri_trim_both(desc::desc_get("Config/rextendr/version", path)[[1]])
expect_equal(version_in_desc, as.character(packageVersion("rextendr")))

# directory structure
expect_true(dir.exists("src"))
expect_true(dir.exists(file.path("src", "rust")))
Expand All @@ -19,6 +23,12 @@ test_that("use_extendr() sets up extendr files correctly", {
expect_snapshot(cat_file("src", "rust", "src", "lib.rs"))
})

test_that("use_extendr() quiet if quiet=TRUE", {
path <- local_package("quiet")
expect_snapshot(use_extendr(quiet = TRUE))
}
)

test_that("use_extendr() does not set up packages with pre-existing src", {
path <- local_package("testpkg.src")
dir.create("src")
Expand Down