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

Read cargo metadata #389

Merged
merged 9 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
44 changes: 44 additions & 0 deletions R/read_cargo_metadata.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#' 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 = ".") {
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.

14 changes: 14 additions & 0 deletions tests/testthat/test-read_cargo_metadata.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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")
Ilia-Kosenkov marked this conversation as resolved.
Show resolved Hide resolved
})
Loading