Skip to content

Commit e059a3f

Browse files
authored
refactor(prep-lib): use sha256sum() if available (#319)
1 parent 0f2105f commit e059a3f

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

tools/prep-lib.R

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
check_sha256 <- function(file, sum, os = c("linux", "macos", "windows")) {
22
message("Checking SHA256 for <", file, ">...")
33

4-
if (match.arg(os) == "linux") {
5-
out <- system2("sha256sum", args = file, stdout = TRUE) |>
6-
gsub(r"(\s.*)", "", x = _)
7-
} else if (match.arg(os) == "macos") {
8-
out <- system2("shasum", args = c("-a", "256", file), stdout = TRUE) |>
9-
gsub(r"(\s.*)", "", x = _)
10-
} else if (match.arg(os) == "windows") {
11-
out <- system2("certutil", args = c("-hashfile", file, "SHA256"), stdout = TRUE)[2]
4+
# tools::sha256sum should be available in R >= 4.5
5+
if (exists("sha256sum", where = asNamespace("tools"), mode = "function")) {
6+
out <- tools::sha256sum(file)
127
} else {
13-
stop("Unsupported OS: ", os)
8+
out <- switch(match.arg(os),
9+
linux = system2("sha256sum", args = file, stdout = TRUE) |>
10+
gsub(r"(\s.*)", "", x = _),
11+
macos = system2("shasum", args = c("-a", "256", file), stdout = TRUE) |>
12+
gsub(r"(\s.*)", "", x = _),
13+
windows = system2("certutil", args = c("-hashfile", file, "SHA256"), stdout = TRUE)[2],
14+
stop("Unsupported OS: ", os)
15+
)
1416
}
1517

1618
if (out != sum) {
@@ -49,15 +51,12 @@ which_arch <- function() {
4951
}
5052

5153
which_vendor_sys_abi <- function(os = c("linux", "macos", "windows")) {
52-
if (match.arg(os) == "linux") {
53-
"unknown-linux-musl"
54-
} else if (match.arg(os) == "macos") {
55-
"apple-darwin"
56-
} else if (match.arg(os) == "windows") {
57-
"pc-windows-gnu"
58-
} else {
54+
switch(match.arg(os),
55+
linux = "unknown-linux-musl",
56+
macos = "apple-darwin",
57+
windows = "pc-windows-gnu",
5958
stop("Unsupported OS: ", os)
60-
}
59+
)
6160
}
6261

6362
current_os <- which_os()

0 commit comments

Comments
 (0)