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

FR: rextendr::use_crate()? #360

Open
kbvernon opened this issue Jun 18, 2024 · 3 comments
Open

FR: rextendr::use_crate()? #360

kbvernon opened this issue Jun 18, 2024 · 3 comments

Comments

@kbvernon
Copy link
Contributor

Similar to usethis::use_package() but for adding dependencies to Cargo.toml.

Happy to contribute a PR for this if you all think it is worthwhile.

@JosiahParry
Copy link
Contributor

Yes definitely! You should definitely do this. We do not need to have all the arguments to the CLI either. Just the most common ones. If you need things like optional or nightly etc then you probably wont need an R package to help!

Here's some start code we can use:

compact <- function(.x) Filter(length, .x)

use_crate <- function(crate, features = NULL, git = NULL, path = ".") {
  # TODO check arguments and that they are the right type and length
  # consider adding standalone type checks 

  # get source directory
  src_dir <- rprojroot::find_package_root_file(path, "src/rust")

  # craft additional args
  cargo_add_opts <- compact(list(
    "--features" = features,
    "--git" = git
  ))

  # craft the additional arguments
  adtl_args <- unname(unlist(Map(
    function(.x, .nm) {
      paste(.nm, paste0(.x, collapse = " "))
    },
    cargo_add_opts,
    names(cargo_add_opts)
  )))
  
  # run the commmand
  processx::run(
    "cargo",
    c("add", crate, adtl_args),
    echo_cmd = TRUE,
    wd = src_dir
  )

}

@kbvernon
Copy link
Contributor Author

Thanks, @JosiahParry! I'll try to get a PR together this evening.

But also, what about adding an options parameter that would take a named list to catch all the other flags?

@JosiahParry
Copy link
Contributor

my preference would be to use ... and use rlang::dots_list() to capture the others!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants