Skip to content

Commit

Permalink
extendr project template
Browse files Browse the repository at this point in the history
  • Loading branch information
kbvernon committed Nov 17, 2023
1 parent 69eba4d commit 0d6ff51
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 0 deletions.
100 changes: 100 additions & 0 deletions R/create_extendr_package.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@

#' Create a project for R package development with Rust
#'
#' @description
#' This function creates an R project directory for package development with Rust extensions.
#' The function can be called on an existing project; you will be asked before
#' any existing files are changed.
#'
#' @inheritParams use_extendr
#' @inheritParams usethis::create_package
#' @param usethis logical, should usethis be used to build package directory?
#' @param ... ignored
#'
#' @return Path to the newly created project or package, invisibly.
#' @keywords internal
#'
#' @examples
create_extendr_package <- function(path,
usethis = TRUE,
roxygen = TRUE,
check_name = TRUE,
crate_name = NULL,
lib_name = NULL,
edition = c("2021", "2018"),
...){

Check warning on line 25 in R/create_extendr_package.R

View workflow job for this annotation

GitHub Actions / lint

file=R/create_extendr_package.R,line=25,col=40,[brace_linter] There should be a space before an opening curly brace.

Check warning on line 25 in R/create_extendr_package.R

View workflow job for this annotation

GitHub Actions / lint

file=R/create_extendr_package.R,line=25,col=40,[paren_body_linter] There should be a space between a right parenthesis and a body expression.

# check if rust infrastructure is available
# rust_sitrep()

# build package directory, but don't open yet!
if (!usethis) {

Check warning on line 31 in R/create_extendr_package.R

View check run for this annotation

Codecov / codecov/patch

R/create_extendr_package.R#L31

Added line #L31 was not covered by tests

dir.create(path, recursive = TRUE, showWarnings = FALSE)

Check warning on line 33 in R/create_extendr_package.R

View check run for this annotation

Codecov / codecov/patch

R/create_extendr_package.R#L33

Added line #L33 was not covered by tests

# generate header for INDEX file
header <- c(
paste0("Package: ", basename(path)),

Check warning on line 37 in R/create_extendr_package.R

View check run for this annotation

Codecov / codecov/patch

R/create_extendr_package.R#L36-L37

Added lines #L36 - L37 were not covered by tests
"",
"WARNING:",
"The project build failed to generate the necessary R package files.",
"Please consider installing {usethis} with `install.packages('usethis')` and running",
"usethis::create_package(getwd()).",

Check warning on line 42 in R/create_extendr_package.R

View check run for this annotation

Codecov / codecov/patch

R/create_extendr_package.R#L39-L42

Added lines #L39 - L42 were not covered by tests
""
)

} else {

usethis::create_package(
path,
fields = list(),
rstudio = TRUE,
roxygen,
check_name,
open = FALSE

Check warning on line 54 in R/create_extendr_package.R

View check run for this annotation

Codecov / codecov/patch

R/create_extendr_package.R#L48-L54

Added lines #L48 - L54 were not covered by tests
)

# generate header for INDEX file
header <- c(
paste0("Package: ", basename(path)),

Check warning on line 59 in R/create_extendr_package.R

View check run for this annotation

Codecov / codecov/patch

R/create_extendr_package.R#L58-L59

Added lines #L58 - L59 were not covered by tests
"",
"BUILD COMPLETE:",
"The project build successfully generated the necessary R package files.",
paste0("Roxygen: ", roxygen),

Check warning on line 63 in R/create_extendr_package.R

View check run for this annotation

Codecov / codecov/patch

R/create_extendr_package.R#L61-L63

Added lines #L61 - L63 were not covered by tests
""
)

}

# add rust scaffolding to project dir
# hunch is that rstudio project text input widgets return empty strings
# when no value is given
use_extendr(
path,
crate_name = if (crate_name == "") NULL else crate_name,
lib_name = if (lib_name == "") NULL else lib_name,
quiet = TRUE,
overwrite = TRUE,
edition

Check warning on line 78 in R/create_extendr_package.R

View check run for this annotation

Codecov / codecov/patch

R/create_extendr_package.R#L72-L78

Added lines #L72 - L78 were not covered by tests
)

text <- c(
"NOTE:",
"To use {rextendr} in any meaningful way, it is required that the user have",
"Rust and Cargo available on their local machine. To check your own machine",
"please run `rextendr::rust_sitrep()` in the console. 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."

Check warning on line 87 in R/create_extendr_package.R

View check run for this annotation

Codecov / codecov/patch

R/create_extendr_package.R#L81-L87

Added lines #L81 - L87 were not covered by tests
)

content <- paste(
paste(header, collapse = "\n"),
paste(text, collapse = "\n"),
sep = "\n"

Check warning on line 93 in R/create_extendr_package.R

View check run for this annotation

Codecov / codecov/patch

R/create_extendr_package.R#L90-L93

Added lines #L90 - L93 were not covered by tests
)

writeLines(content, con = file.path(path, "INDEX"))

Check warning on line 96 in R/create_extendr_package.R

View check run for this annotation

Codecov / codecov/patch

R/create_extendr_package.R#L96

Added line #L96 was not covered by tests

return(invisible(path))

Check warning on line 98 in R/create_extendr_package.R

View check run for this annotation

Codecov / codecov/patch

R/create_extendr_package.R#L98

Added line #L98 was not covered by tests

}
40 changes: 40 additions & 0 deletions inst/rstudio/templates/project/extendr.dcf
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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: usethis
Widget: CheckboxInput
Label: Use usethis to build project directory
Default: On
Position: left

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

Parameter: check_name
Widget: CheckboxInput
Label: Check if package name is valid
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

0 comments on commit 0d6ff51

Please sign in to comment.