Skip to content

Commit

Permalink
refactor(install): use sha256sum() if available
Browse files Browse the repository at this point in the history
  • Loading branch information
eitsupi committed Sep 22, 2024
1 parent dde9f1b commit a42b566
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 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

0 comments on commit a42b566

Please sign in to comment.