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

extendr project template #321

Merged
merged 21 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
76 changes: 76 additions & 0 deletions R/create_extendr_package.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@

#' Create a project for R package development with Rust
kbvernon marked this conversation as resolved.
Show resolved Hide resolved
#'
#' @description
#' This function creates an R project directory for package development
#' with Rust extensions.
#'
#' @param path a path to new directory
kbvernon marked this conversation as resolved.
Show resolved Hide resolved
#' @param ... arguments passed on to `usethis::create_package()` and
#' `rextendr::use_extendr()`
#'
#' @return Path to the newly created project or package, invisibly.
#' @keywords internal
#'
#' @examples
create_extendr_package <- function(path, ...) {
kbvernon marked this conversation as resolved.
Show resolved Hide resolved

# error if usethis is not installed
rlang::check_installed("usethis")

args <- rlang::list2(...)

# hunch is that rstudio project text input widgets return empty strings
# when no value is given, want to make sure it is NULL so `use_extendr()`
# handles it correctly
args <- lapply(args, function(x) if (x == "") return(NULL) else return(x))
kbvernon marked this conversation as resolved.
Show resolved Hide resolved

# build package directory, but don't open yet!
Ilia-Kosenkov marked this conversation as resolved.
Show resolved Hide resolved
usethis::create_package(
path,
fields = list(),
rstudio = TRUE,
roxygen = args[["roxygen"]],
check_name = args[["check_name"]],
open = FALSE
)

# add rust scaffolding to project dir
use_extendr(
path,
crate_name = args[["crate_name"]],
lib_name = args[["lib_name"]],
quiet = TRUE,
overwrite = TRUE,
edition = args[["edition"]]
)

# generate header for INDEX file
header <- c(
paste0("Package: ", basename(path)),
"",
"BUILD COMPLETE:",
"The project build successfully generated the necessary R package files.",
""
)

text <- c(
"NOTE:",
"To use {rextendr} in any meaningful way, the user must have",
"Rust and Cargo available on their local machine. To check that you do,",
"please run `rextendr::rust_sitrep()`. This will provide a",
"detailed report of the current state of your Rust infrastructure, along",
"with some helpful advice about how to address any issues that may arise."
kbvernon marked this conversation as resolved.
Show resolved Hide resolved
)

content <- paste(
paste(header, collapse = "\n"),
paste(text, collapse = "\n"),
sep = "\n"
)
kbvernon marked this conversation as resolved.
Show resolved Hide resolved

writeLines(content, con = file.path(path, "INDEX"))
kbvernon marked this conversation as resolved.
Show resolved Hide resolved

return(invisible(path))
kbvernon marked this conversation as resolved.
Show resolved Hide resolved

}
34 changes: 34 additions & 0 deletions inst/rstudio/templates/project/extendr.dcf
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Binding: create_extendr_package
Title: R package with extendr
Subtitle: Create an R package with Rust extensions.
Caption: Create an R package with Rust extensions.
OpenFiles: INDEX

Parameter: roxygen
Widget: CheckboxInput
Label: Use roxygen2
Default: On
Position: left

Parameter: check_name
Widget: CheckboxInput
Label: Validate package name
Default: On
Position: left

Parameter: crate_name
Widget: TextInput
Label: Rust crate name
Position: right

Parameter: lib_name
Widget: TextInput
Label: Rust library name
Position: right

Parameter: edition
Widget: SelectInput
Label: Rust edition
Fields: 2021, 2018
Default: 2021
Position: right