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

refactor(prep-lib): use sha256sum() if available #319

Merged
merged 5 commits into from
Sep 24, 2024
Merged
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
33 changes: 16 additions & 17 deletions tools/prep-lib.R
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
check_sha256 <- function(file, sum, os = c("linux", "macos", "windows")) {
message("Checking SHA256 for <", file, ">...")

if (match.arg(os) == "linux") {
out <- system2("sha256sum", args = file, stdout = TRUE) |>
gsub(r"(\s.*)", "", x = _)
} else if (match.arg(os) == "macos") {
out <- system2("shasum", args = c("-a", "256", file), stdout = TRUE) |>
gsub(r"(\s.*)", "", x = _)
} else if (match.arg(os) == "windows") {
out <- system2("certutil", args = c("-hashfile", file, "SHA256"), stdout = TRUE)[2]
# tools::sha256sum should be available in R >= 4.5
if (exists("sha256sum", where = asNamespace("tools"), mode = "function")) {
out <- tools::sha256sum(file)
} else {
stop("Unsupported OS: ", os)
out <- switch(match.arg(os),
linux = system2("sha256sum", args = file, stdout = TRUE) |>
gsub(r"(\s.*)", "", x = _),
macos = system2("shasum", args = c("-a", "256", file), stdout = TRUE) |>
gsub(r"(\s.*)", "", x = _),
windows = system2("certutil", args = c("-hashfile", file, "SHA256"), stdout = TRUE)[2],
stop("Unsupported OS: ", os)
)
}

if (out != sum) {
Expand Down Expand Up @@ -49,15 +51,12 @@ which_arch <- function() {
}

which_vendor_sys_abi <- function(os = c("linux", "macos", "windows")) {
if (match.arg(os) == "linux") {
"unknown-linux-musl"
} else if (match.arg(os) == "macos") {
"apple-darwin"
} else if (match.arg(os) == "windows") {
"pc-windows-gnu"
} else {
switch(match.arg(os),
linux = "unknown-linux-musl",
macos = "apple-darwin",
windows = "pc-windows-gnu",
stop("Unsupported OS: ", os)
}
)
}

current_os <- which_os()
Expand Down
Loading