extendr / rextendr

An R package that helps scaffolding extendr-enabled packages or compiling Rust code dynamically
https://extendr.github.io/rextendr/
Other
197 stars 27 forks source link

FR: rextendr::use_crate()? #360

Closed kbvernon closed 3 months ago

kbvernon commented 5 months ago

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 commented 5 months ago

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 commented 5 months ago

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 commented 5 months ago

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

Ilia-Kosenkov commented 3 months ago

@kbvernon , did your PR resolve this issue or are you planning on adding more features?

kbvernon commented 3 months ago

Ah, yeah, this issue is resolved with https://github.com/extendr/rextendr/pull/361. Sorry, I forgot to close it!