Skip to content

Commit

Permalink
feat(template): adds configure scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
eitsupi committed Aug 15, 2023
1 parent 82a7be8 commit 690feef
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
22 changes: 22 additions & 0 deletions R/use_extendr.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,22 @@ use_extendr <- function(path = ".",
cli::cli_alert_success("Creating {.file {pretty_rel_path(rust_src_dir, path)}}.")
}

use_rextendr_template(
"configure",
save_as = file.path("configure"),
quiet = quiet,
as_executable = TRUE,
overwrite = overwrite
)

use_rextendr_template(
"configure.win",
save_as = file.path("configure.win"),
quiet = quiet,
as_executable = TRUE,
overwrite = overwrite
)

use_rextendr_template(
"entrypoint.c",
save_as = file.path("src", "entrypoint.c"),
Expand Down Expand Up @@ -211,6 +227,7 @@ throw_if_invalid_rust_name <- function(name, call = caller_env()) {
#'
#' @inheritParams usethis::use_template
#' @inheritParams use_extendr
#' @param as_executable Logical scalar indicating whether the file should be executable.
#' @param overwrite Logical scalar or `NULL` indicating whether the file in the `path` should be overwritten.
#' If `FALSE` and the file already exists, the function will do nothing.
#' If `NULL` and the `usethis` package is installed, the function will ask the user whether the file should
Expand All @@ -221,6 +238,7 @@ use_rextendr_template <- function(template,
save_as = template,
data = list(),
quiet = FALSE,
as_executable = FALSE,
overwrite = NULL) {
local_quiet_cli(quiet)

Expand Down Expand Up @@ -265,6 +283,10 @@ use_rextendr_template <- function(template,
overwrite = overwrite
)

if (isTRUE(as_executable)) {
Sys.chmod(save_as, mode = "755")
}

invisible(TRUE)
}

Expand Down
19 changes: 19 additions & 0 deletions inst/templates/configure
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env sh

export PATH="$PATH:$HOME/.cargo/bin"

if [ ! "$(command -v cargo)" ]; then
echo "----------------------- [RUST NOT FOUND]---------------------------"
echo "The 'cargo' command was not found on the PATH. Please install rustc"
echo "from: https://www.rust-lang.org/tools/install"
echo ""
echo "Alternatively, you may install cargo from your OS package manager:"
echo " - Debian/Ubuntu: apt-get install cargo"
echo " - Fedora/CentOS: dnf install cargo"
echo " - macOS: brew install rustc"
echo "-------------------------------------------------------------------"
echo ""
exit 1
fi

exit 0
14 changes: 14 additions & 0 deletions inst/templates/configure.win
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh

export PATH="$PATH:$HOME/.cargo/bin"

if [ ! "$(command -v cargo)" ]; then
echo "----------------------- [RUST NOT FOUND]---------------------------"
echo "The 'cargo' command was not found on the PATH. Please install rustc"
echo "from: https://www.rust-lang.org/tools/install"
echo "-------------------------------------------------------------------"
echo ""
exit 1
fi

exit 0
6 changes: 6 additions & 0 deletions tests/testthat/_snaps/use_extendr.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
i Setting `Config/rextendr/version` to "0.3.1.9000" in the 'DESCRIPTION' file.
i Setting `SystemRequirements` to "Cargo (rustc package manager)" in the 'DESCRIPTION' file.
v Creating 'src/rust/src'.
v Writing 'configure'
v Writing 'configure.win'
v Writing 'src/entrypoint.c'
v Writing 'src/Makevars'
v Writing 'src/Makevars.win'
Expand Down Expand Up @@ -203,6 +205,8 @@
Code
use_extendr()
Message
> File 'configure' already exists. Skip writing the file.
> File 'configure.win' already exists. Skip writing the file.
> File 'src/entrypoint.c' already exists. Skip writing the file.
> File 'src/Makevars' already exists. Skip writing the file.
> File 'src/Makevars.win' already exists. Skip writing the file.
Expand All @@ -220,6 +224,8 @@
Code
use_extendr(crate_name = "foo", lib_name = "bar", overwrite = TRUE)
Message
v Writing 'configure'
v Writing 'configure.win'
v Writing 'src/entrypoint.c'
v Writing 'src/Makevars'
v Writing 'src/Makevars.win'
Expand Down

0 comments on commit 690feef

Please sign in to comment.