From 8803e0a4ce441b54b8aa4b605c87cef1a316765f Mon Sep 17 00:00:00 2001 From: Ilia Kosenkov Date: Sun, 19 Nov 2023 18:47:21 +0200 Subject: [PATCH 1/7] Prototype test --- tests/testthat/test-use_extendr_with_path.R | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 tests/testthat/test-use_extendr_with_path.R diff --git a/tests/testthat/test-use_extendr_with_path.R b/tests/testthat/test-use_extendr_with_path.R new file mode 100644 index 00000000..68b8e863 --- /dev/null +++ b/tests/testthat/test-use_extendr_with_path.R @@ -0,0 +1,8 @@ +test_that("`use_extendr()` works correctly when path is specified explicitly", { + skip_if_not_installed("usethis") + local_temp_dir("temp_dir") + usethis::create_package("testpkg") + + use_extendr(path = "testpkg") + succeed() +}) From 0dabcf2a68c18f8313c8d6a479ef9ba16aac11cb Mon Sep 17 00:00:00 2001 From: Ilia Kosenkov Date: Sun, 19 Nov 2023 18:57:13 +0200 Subject: [PATCH 2/7] Fix paths in setup.R --- R/setup.R | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/R/setup.R b/R/setup.R index 4f9d7a12..31d77fc5 100644 --- a/R/setup.R +++ b/R/setup.R @@ -1,26 +1,27 @@ rextendr_setup <- function(path = ".", cur_version = NULL) { - if (!file.exists(file.path(path, "DESCRIPTION"))) { + desc_path <- rprojroot::find_package_root_file("DESCRIPTION", path = path) + if (!file.exists(desc_path)) { cli::cli_abort( "{.arg path} ({.path {path}}) does not contain a DESCRIPTION", class = "rextendr_error" ) } - is_first <- is.na(rextendr_version(path)) + is_first <- is.na(rextendr_version(desc_path = desc_path)) if (is_first) { cli::cli_alert_info("First time using rextendr. Upgrading automatically...") } - update_rextendr_version(path, cur_version = cur_version) - update_sys_reqs(path) + update_rextendr_version(desc_path = desc_path, cur_version = cur_version) + update_sys_reqs(desc_path = desc_path) invisible(TRUE) } -update_rextendr_version <- function(path, cur_version = NULL) { +update_rextendr_version <- function(desc_path, cur_version = NULL) { cur <- cur_version %||% as.character(utils::packageVersion("rextendr")) - prev <- rextendr_version(path) + prev <- rextendr_version(desc_path = desc_path) if (!is.na(cur) && !is.na(prev) && package_version(cur) < package_version(prev)) { cli::cli_alert_warning(c( @@ -28,16 +29,16 @@ update_rextendr_version <- function(path, cur_version = NULL) { "You have {.str {cur}} but you need {.str {prev}}" )) } else if (!identical(cur, prev)) { - update_description("Config/rextendr/version", cur) + update_description("Config/rextendr/version", cur, desc_path = desc_path) } } -update_sys_reqs <- function(path) { +update_sys_reqs <- function(desc_path) { cur <- "Cargo (rustc package manager)" - prev <- stringi::stri_trim_both(desc::desc_get("SystemRequirements", path)[[1]]) + prev <- stringi::stri_trim_both(desc::desc_get("SystemRequirements", file = desc_path)[[1]]) if (is.na(prev)) { - update_description("SystemRequirements", cur) + update_description("SystemRequirements", cur, desc_path = desc_path) } else if (!identical(cur, prev)) { cli::cli_ul( c( @@ -48,11 +49,11 @@ update_sys_reqs <- function(path) { } } -update_description <- function(field, value) { +update_description <- function(field, value, desc_path) { cli::cli_alert_info("Setting {.var {field}} to {.str {value}} in the {.file DESCRIPTION} file.") - desc::desc_set(field, value) + desc::desc_set(field, value, file = desc_path) } -rextendr_version <- function(path = ".") { - stringi::stri_trim_both(desc::desc_get("Config/rextendr/version", path)[[1]]) +rextendr_version <- function(desc_path = ".") { + stringi::stri_trim_both(desc::desc_get("Config/rextendr/version", desc_path)[[1]]) } From 1b799d6a61f36afaa480a177e0b23c9fed9112a1 Mon Sep 17 00:00:00 2001 From: Ilia Kosenkov Date: Sun, 19 Nov 2023 19:34:27 +0200 Subject: [PATCH 3/7] Temporarily setup local project --- R/use_extendr.R | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/R/use_extendr.R b/R/use_extendr.R index c7a1a0c0..a2688c3d 100644 --- a/R/use_extendr.R +++ b/R/use_extendr.R @@ -39,7 +39,11 @@ use_extendr <- function(path = ".", rextendr_setup(path = path) - pkg_name <- pkg_name(path) + if (isFALSE(path == ".")) { + usethis::local_project(path, quiet = quiet) + } + + pkg_name <- pkg_name() mod_name <- as_valid_rust_name(pkg_name) if (is.null(crate_name)) { @@ -54,8 +58,9 @@ use_extendr <- function(path = ".", throw_if_invalid_rust_name(lib_name) } - src_dir <- rprojroot::find_package_root_file("src", path = path) - r_dir <- rprojroot::find_package_root_file("R", path = path) + src_dir <- rprojroot::find_package_root_file("src") + r_dir <- rprojroot::find_package_root_file("R") + if (!dir.exists(r_dir)) { dir.create(r_dir) From 031c535d61dcaf5445bf162840e6a6a46e08dce6 Mon Sep 17 00:00:00 2001 From: Ilia Kosenkov Date: Sun, 19 Nov 2023 19:45:45 +0200 Subject: [PATCH 4/7] Checking if current path is usethis project path --- R/use_extendr.R | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/R/use_extendr.R b/R/use_extendr.R index a2688c3d..68daa923 100644 --- a/R/use_extendr.R +++ b/R/use_extendr.R @@ -39,7 +39,10 @@ use_extendr <- function(path = ".", rextendr_setup(path = path) - if (isFALSE(path == ".")) { + root_path <- normalizePath(rprojroot::find_package_root_file(path = path), mustWork = FALSE, winslash = "/") + usethis_proj_path <- try_get_proj_path() + + if (!isTRUE(root_path == usethis_proj_path)) { usethis::local_project(path, quiet = quiet) } @@ -165,6 +168,10 @@ use_extendr <- function(path = ".", return(invisible(TRUE)) } +try_get_proj_path <- function() { + tryCatch(usethis::proj_get(), error = function(e) NA) +} + #' Checks if provided name is a valid Rust name (identifier) #' #' @param name \[ character(n) \] Names to test. From 1729306df0e2952ae52f3f63488493554d40bbaf Mon Sep 17 00:00:00 2001 From: Ilia Kosenkov Date: Sun, 19 Nov 2023 20:11:22 +0200 Subject: [PATCH 5/7] Fix paths issue --- R/use_extendr.R | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/R/use_extendr.R b/R/use_extendr.R index 68daa923..e0694db3 100644 --- a/R/use_extendr.R +++ b/R/use_extendr.R @@ -37,15 +37,31 @@ use_extendr <- function(path = ".", rlang::check_installed("usethis") - rextendr_setup(path = path) - - root_path <- normalizePath(rprojroot::find_package_root_file(path = path), mustWork = FALSE, winslash = "/") + # Root path computed from user input + root_path <- try_get_root_path(path) + # Root path computed from `{usethis}` usethis_proj_path <- try_get_proj_path() + # If they do not match, something is off, try to set up temporary project if (!isTRUE(root_path == usethis_proj_path)) { usethis::local_project(path, quiet = quiet) } + # Check project path once again + usethis_proj_path <- try_get_proj_path() + # Check what is current working directory + curr_path <- try_get_normalized_path(getwd) + + # If they do not match, let's temporarily change working directory + if (!isTRUE(curr_path == usethis_proj_path)) { + withr::local_dir(usethis_proj_path) + } + + # At this point, our working directory is at the project root and + # we have an active `{usethis}` project + + rextendr_setup() + pkg_name <- pkg_name() mod_name <- as_valid_rust_name(pkg_name) @@ -168,8 +184,16 @@ use_extendr <- function(path = ".", return(invisible(TRUE)) } +try_get_normalized_path <- function(path_fn) { + tryCatch(normalizePath(path_fn(), winslash = "/", mustWork = FALSE), error = function(e) NA) +} + try_get_proj_path <- function() { - tryCatch(usethis::proj_get(), error = function(e) NA) + try_get_normalized_path(usethis::proj_get) +} + +try_get_root_path <- function(path) { + try_get_normalized_path(function() rprojroot::find_package_root_file(path = path)) } #' Checks if provided name is a valid Rust name (identifier) From 1ad658118c62f8c88287649c4c503025ed4e0033 Mon Sep 17 00:00:00 2001 From: Ilia Kosenkov Date: Sun, 19 Nov 2023 20:12:00 +0200 Subject: [PATCH 6/7] Move test to the shared file --- tests/testthat/test-use_extendr.R | 9 +++++++++ tests/testthat/test-use_extendr_with_path.R | 8 -------- 2 files changed, 9 insertions(+), 8 deletions(-) delete mode 100644 tests/testthat/test-use_extendr_with_path.R diff --git a/tests/testthat/test-use_extendr.R b/tests/testthat/test-use_extendr.R index f00cac2e..9dbbe16f 100644 --- a/tests/testthat/test-use_extendr.R +++ b/tests/testthat/test-use_extendr.R @@ -175,3 +175,12 @@ test_that("Message if the SystemRequirements field is already set.", { expect_true(created) expect_equal(desc::desc_get("SystemRequirements")[[1]], sys_req) }) + +test_that("`use_extendr()` works correctly when path is specified explicitly", { + skip_if_not_installed("usethis") + local_temp_dir("temp_dir") + usethis::create_package("testpkg") + + use_extendr(path = "testpkg") + succeed() +}) diff --git a/tests/testthat/test-use_extendr_with_path.R b/tests/testthat/test-use_extendr_with_path.R deleted file mode 100644 index 68b8e863..00000000 --- a/tests/testthat/test-use_extendr_with_path.R +++ /dev/null @@ -1,8 +0,0 @@ -test_that("`use_extendr()` works correctly when path is specified explicitly", { - skip_if_not_installed("usethis") - local_temp_dir("temp_dir") - usethis::create_package("testpkg") - - use_extendr(path = "testpkg") - succeed() -}) From 7e442e714c2d4ac14e2717f9a3ed3161722b92e7 Mon Sep 17 00:00:00 2001 From: Ilia Kosenkov Date: Sun, 19 Nov 2023 20:17:28 +0200 Subject: [PATCH 7/7] NEWS.md --- NEWS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS.md b/NEWS.md index 735f959f..6601ab21 100644 --- a/NEWS.md +++ b/NEWS.md @@ -5,6 +5,7 @@ `Cargo (rustc package manager)` if the field is empty (#298). * `use_extendr()` gets a new ability to overwrite existing rextendr templates (#292). * `use_extendr()` sets `publish = false` in the `[package]` section of the `Cargo.toml` (#297). +* `use_extendr()` correctly handles calls with `path` not equal to `"."` (current folder), or when there is no active `{usethis}` project (#323). # rextend 0.3.1