Skip to content

Commit

Permalink
Read cargo metadata (#389)
Browse files Browse the repository at this point in the history
* draft and test

* use expect_type()

* list under utility functions

* document

* check string

* more meaningful test

* update news

* Update NEWS.md

Co-authored-by: Ilia Kosenkov <[email protected]>

---------

Co-authored-by: Ilia Kosenkov <[email protected]>
  • Loading branch information
kbvernon and Ilia-Kosenkov authored Nov 9, 2024
1 parent a8d7434 commit ea68f4e
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export(document)
export(eng_extendr)
export(eng_extendrsrc)
export(make_module_macro)
export(read_cargo_metadata)
export(register_extendr)
export(rust_eval)
export(rust_function)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* Fixed an issue in `rust_source()` family of functions that prevented usage of `r#` escape sequences in Rust function names (#374)
* `use_cran_defaults()` now checks the `SystemRequirements` field in the `DESCRIPTION` file for cargo and rustc. It will display installation instructions if either is missing or provide the minimum required version if the installed version is outdated.
* Added `use_msrv()` to aid in specifying the minimum supported rust version (MSRV) for an R package
* Added `read_cargo_metadata()` to retrieve Cargo metadata for packages and
workspaces. (#389)

# rextend 0.3.1

Expand Down
46 changes: 46 additions & 0 deletions R/read_cargo_metadata.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#' Retrieve metadata for packages and workspaces
#'
#' @param path character scalar, the R package directory
#'
#' @details
#' For more details, see
#' \href{https://doc.rust-lang.org/cargo/commands/cargo-metadata.html}{Cargo docs}
#' for `cargo-metadata`. See especially "JSON Format" to get a sense of what you
#' can expect to find in the returned list.
#'
#' @return `list`, including the following elements:
#' - "packages"
#' - "workspace_members"
#' - "workspace_default_members"
#' - "resolve"
#' - "target_directory"
#' - "version"
#' - "workspace_root"
#' - "metadata"
#'
#' @export
#'
#' @examples
#' \dontrun{
#' read_cargo_metadata()
#' }
#'
read_cargo_metadata <- function(path = ".") {
check_string(path, class = "rextendr_error")

root <- rprojroot::find_package_root_file(path = path)

rust_folder <- normalizePath(
file.path(root, "src", "rust"),
winslash = "/",
mustWork = FALSE
)

out <- processx::run(
"cargo",
args = c("metadata", "--format-version=1", "--no-deps"),
wd = rust_folder
)

jsonlite::fromJSON(out[["stdout"]])
}
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ reference:
- to_toml
- make_module_macro
- rust_sitrep
- read_cargo_metadata
39 changes: 39 additions & 0 deletions man/read_cargo_metadata.Rd

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

38 changes: 38 additions & 0 deletions tests/testthat/test-read_cargo_metadata.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
test_that("read_cargo_metadata() returns crate or workspace metadata", {
skip_if_not_installed("usethis")

path <- local_package("testpkg")

# capture setup messages
withr::local_options(usethis.quiet = FALSE)

use_extendr(path, quiet = TRUE)

out <- read_cargo_metadata(path)

expect_type(out, "list")

expect_equal(
out[["packages"]][["name"]],
"testpkg"
)

expect_equal(
out[["packages"]][["version"]],
"0.1.0"
)

expect_equal(
out[["packages"]][["dependencies"]][[1]][["name"]],
"extendr-api"
)

expect_equal(
out[["workspace_root"]],
normalizePath(
file.path(path, "src", "rust"),
winslash = "\\",
mustWork = FALSE
)
)
})

0 comments on commit ea68f4e

Please sign in to comment.