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

use_msrv() #384

Merged
merged 18 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export(to_toml)
export(use_cran_defaults)
export(use_crate)
export(use_extendr)
export(use_msrv)
export(vendor_pkgs)
export(write_license_note)
importFrom(dplyr,"%>%")
Expand Down
85 changes: 85 additions & 0 deletions R/use_msrv.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#' Set the minimum supported rust version (MSRV)
#'
#' `use_msrv()` sets the minimum supported rust version for your R package.
#'
#' @param version character scalar, the minimum supported Rust version.
#' @param path character scalar, path to folder containing DESCRIPTION file.
#' @param overwrite default `FALSE`. Overwrites the `SystemRequirements` field if already set when `TRUE`.
#' @details
#'
#' The minimum supported rust version (MSRV) is determined by the
#' `SystemRequirements` field in a package's `DESCRIPTION` file. For example, to
#' set the MSRV to `1.67.0`, the `SystemRequirements` must have
#' `rustc >= 1.67.0`.
#'
#' By default, there is no MSRV set. However, some crates have features that
#' depend on a minimum version of Rust. As of this writing the version of Rust
#' on CRAN's Fedora machine's is 1.69. If you require a version of Rust that is
#' greater than that, you must set it in your DESCRIPTION file.
#'
#' It is also important to note that if CRAN's machines do not meet the
#' specified MSRV, they will not be able to build a binary of your package. As a
#' consequence, if users try to install the package they will be required to
#' have Rust installed as well.
#'
#' To determine the MSRV of your R package, we recommend installing the
#' `cargo-msrv` cli. You can do so by running `cargo install cargo-msrv`. To
#' determine your MSRV, set your working directory to `src/rust` then run
#' `cargo msrv`. Note that this may take a while.
#'
#' For more details, please see
#' [cargo-msrv](https://github.com/foresterre/cargo-msrv).
#'
#' @return `version`
#' @export
#'
#' @examples
#' \dontrun{
#' use_msrv("1.67.1")
#' }
#'
use_msrv <- function(version, path = ".", overwrite = FALSE) {
check_string(version, class = "rextendr_error")
check_string(path, class = "rextendr_error")
check_bool(overwrite, class = "rextendr_error")

msrv_call <- rlang::caller_call()
version <- tryCatch(numeric_version(version), error = function(e) {
cli::cli_abort(
"Invalid version provided",
class = "rextendr_error",
call = msrv_call
)
})

desc_path <- rprojroot::find_package_root_file("DESCRIPTION", path = path)

if (!file.exists(desc_path)) {
cli::cli_abort(
"{.arg path} ({.path {path}}) does not contain a DESCRIPTION",
class = "rextendr_error"
)
}

cur <- paste("Cargo (Rust's package manager), rustc", paste(">=", version))

prev <- desc::desc_get("SystemRequirements", file = desc_path)[[1]]
prev <- stringi::stri_trim_both(prev)
prev_is_default <- identical(prev, "Cargo (Rust's package manager), rustc")

# if it isn't set update the description or if overwrite is true
if (is.na(prev) || overwrite || prev_is_default) {
update_description("SystemRequirements", cur, desc_path = desc_path)
} else if (!identical(cur, prev) && !overwrite) {
cli::cli_ul(
c(
"The SystemRequirements field in the {.file DESCRIPTION} file is
already set.",
"Please update it manually if needed.",
"{.code SystemRequirements: {cur}}"
)
)
}

invisible(version)
}
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ reference:
- write_license_note
- clean
- cran
- use_msrv

- title: Various utility functions
contents:
Expand Down
51 changes: 51 additions & 0 deletions man/use_msrv.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions tests/testthat/test-use_msrv.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
test_that("use_msrv() modifies the MSRV in the DESCRIPTION", {
skip_if_not_installed("usethis")

path <- local_package("testpkg")

# capture setup messages
withr::local_options(usethis.quiet = FALSE)

use_extendr(path, quiet = TRUE)
use_msrv("1.70", path)

d <- desc::desc("DESCRIPTION")

expect_identical(
"Cargo (Rust's package manager), rustc >= 1.70",
d$get_field("SystemRequirements")
)
})
Loading