Skip to content

Commit

Permalink
feat!: set SystemRequirements field automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
eitsupi committed Jun 27, 2023
1 parent 440a83d commit 6aabb0a
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 7 deletions.
27 changes: 24 additions & 3 deletions R/setup.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ rextendr_setup <- function(path = ".", cur_version = NULL) {
}

update_rextendr_version(path, cur_version = cur_version)
update_sys_reqs(path)

invisible(is_first)
invisible(TRUE)
}

update_rextendr_version <- function(path, cur_version = NULL) {
Expand All @@ -27,11 +28,31 @@ update_rextendr_version <- function(path, cur_version = NULL) {
"You have {.str {cur}} but you need {.str {prev}}"
))
} else if (!identical(cur, prev)) {
cli::cli_alert_info("Setting {.var Config/rextendr/version} to {.str {cur}}")
desc::desc_set(`Config/rextendr/version` = cur, file = path)
update_description("Config/rextendr/version", cur)
}
}

update_sys_reqs <- function(path) {
cur <- "Cargo (rustc package manager)"
prev <- stringi::stri_trim_both(desc::desc_get("SystemRequirements", path)[[1]])

if (is.na(prev)) {
update_description("SystemRequirements", cur)
} else if (!identical(cur, prev)) {
cli::cli_ul(
c(
"The SystemRequirements field in the {.file DESCRIPTION} file is already set.",
"Please update it manually if needed."
)
)
}
}

update_description <- function(field, value) {
cli::cli_alert_info("Setting {.var {field}} to {.str {value}} in the {.file DESCRIPTION} file.")
desc::desc_set(field, value)
}

rextendr_version <- function(path = ".") {
stringi::stri_trim_both(desc::desc_get("Config/rextendr/version", path)[[1]])
}
1 change: 0 additions & 1 deletion R/use_extendr.R
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ use_extendr <- function(path = ".",
cli::cli_alert_success("Finished configuring {.pkg extendr} for package {.pkg {pkg_name}}.")
cli::cli_ul(
c(
"Please update the system requirement in {.file DESCRIPTION} file.",
"Please run {.fun rextendr::document} for changes to take effect."
)
)
Expand Down
24 changes: 22 additions & 2 deletions tests/testthat/_snaps/use_extendr.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
use_extendr()
Message
i First time using rextendr. Upgrading automatically...
i Setting `Config/rextendr/version` to "0.3.1.9000"
i Setting `Config/rextendr/version` to "0.3.1.9000" in the 'DESCRIPTION' file.
i Setting `SystemRequirements` to "Cargo (rustc package manager)" in the 'DESCRIPTION' file.
v Creating 'src/rust/src'.
v Writing 'src/entrypoint.c'
v Writing 'src/Makevars'
Expand All @@ -17,9 +18,28 @@
v Writing 'src/testpkg-win.def'
v Writing 'R/extendr-wrappers.R'
v Finished configuring extendr for package testpkg.
* Please update the system requirement in 'DESCRIPTION' file.
* Please run `rextendr::document()` for changes to take effect.

---

Code
cat_file("DESCRIPTION")
Output
Package: testpkg
Title: What the Package Does (One Line, Title Case)
Version: 0.0.0.9000
Authors@R:
person("First", "Last", , "first.last@example.com", role = c("aut", "cre"),
comment = c(ORCID = "YOUR-ORCID-ID"))
Description: What the package does (one paragraph).
License: `use_mit_license()`, `use_gpl3_license()` or friends to pick a
license
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
Config/rextendr/version: 0.3.1.9000
SystemRequirements: Cargo (rustc package manager)

---

Code
Expand Down
20 changes: 19 additions & 1 deletion tests/testthat/test-use_extendr.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ test_that("use_extendr() sets up extendr files correctly", {
expect_true(dir.exists(file.path("src", "rust")))
expect_true(dir.exists(file.path("src", "rust", "src")))

expect_snapshot(cat_file("DESCRIPTION"))
expect_snapshot(cat_file("R", "extendr-wrappers.R"))
expect_snapshot(cat_file("src", "Makevars"))
expect_snapshot(cat_file("src", "Makevars.win"))
Expand Down Expand Up @@ -46,7 +47,6 @@ test_that("use_extendr() does not set up packages with pre-existing src", {
expect_false(created)
})


test_that("use_extendr() does not set up packages with pre-existing wrappers", {
skip_if_not_installed("usethis")

Expand Down Expand Up @@ -151,3 +151,21 @@ test_that("R/ folder is created when not present", {
# expect no error
expect_rextendr_error(use_extendr(), regexp = NA)
})

test_that("Message if the SystemRequirements field is already set.", {
skip_if_not_installed("usethis")

path <- local_package("testpkg")
sys_req <- "testreq"

desc::desc_set("SystemRequirements", sys_req)

withr::local_options(usethis.quiet = FALSE)
expect_message(
created <- use_extendr(),
"Please update it manually if needed"
)

expect_true(created)
expect_equal(desc::desc_get("SystemRequirements")[[1]], sys_req)
})

0 comments on commit 6aabb0a

Please sign in to comment.