Skip to content

Commit

Permalink
Re-org the utils.R
Browse files Browse the repository at this point in the history
  • Loading branch information
coatless committed Dec 7, 2023
1 parent 7a1ccf6 commit 85af401
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 146 deletions.
4 changes: 4 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,18 @@ Suggests:
rstudioapi,
testthat (>= 3.0.0)
Collate:
'assertions.R'
'blocks.R'
'renviron.R'
'shell.R'
'utils.R'
'installers.R'
'gfortran.R'
'macos-versions.R'
'paths.R'
'recipes.R'
'system-checks.R'
'xcode-cli.R'
'toolchain.R'
'version-check.R'
Config/testthat/edition: 3
36 changes: 36 additions & 0 deletions R/assertions.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Assertions ----

#' Assert a condition
#'
#' @description
#' `assert()` allows a function state to be checked and stopped.
#'
#' @param condition A logical indicating the status of the condition.
#' @param message A string to display.
#'
#' @keywords internal
#' @rdname assert
#' @export
assert = function(condition, message = NULL) {
if(isFALSE(condition)) {
stop(paste0(message, " is not TRUE"))
}
}

#' @rdname assert
#' @export
assert_mac = function(){
assert(is_macos(), "On macOS")
}

#' @rdname assert
#' @export
assert_aarch64 = function(){
assert(is_aarch64(), "On aarch64")
}

#' @rdname assert
#' @export
assert_x86_64 = function(){
assert(is_x86_64(), "On x86_64")
}
57 changes: 57 additions & 0 deletions R/macos-versions.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Detect macOS Operating System ----

shell_mac_version = function() {
sys::as_text(sys::exec_internal("sw_vers", "-productVersion")$stdout)
}

is_macos_r_supported = function() {
mac_version = shell_mac_version()

version_between(mac_version, "10.13.0", "15.0.0")
}

is_macos_sonoma = function() {
mac_version = shell_mac_version()

version_between(mac_version, "14.0.0", "15.0.0")
}

is_macos_ventura = function() {
mac_version = shell_mac_version()

version_between(mac_version, "13.0.0", "14.0.0")
}

is_macos_monterey = function() {
mac_version = shell_mac_version()

version_between(mac_version, "12.0.0", "13.0.0")
}

is_macos_big_sur= function() {
mac_version = shell_mac_version()

version_between(mac_version, "11.0.0", "12.0.0")
}

is_macos_catalina = function() {
mac_version = shell_mac_version()

version_between(mac_version, "10.15.0", "10.16.0")
}

is_macos_mojave = function() {
mac_version = shell_mac_version()

version_between(mac_version, "10.14.0", "10.15.0")
}

is_macos_high_sierra = function() {
mac_version = shell_mac_version()

version_between(mac_version, "10.13.0", "10.14.0")
}

is_macos = function() {
system_os() == "darwin"
}
17 changes: 17 additions & 0 deletions R/system-checks.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# System functions ----
system_os = function() {
tolower(Sys.info()[["sysname"]])
}

system_arch = function() {
R.version$arch
}

# Architecture Checks ----
is_aarch64 = function() {
system_arch() == "aarch64"
}

is_x86_64 = function() {
system_arch() == "x86_64"
}
147 changes: 2 additions & 145 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -1,145 +1,3 @@
# System functions ----
system_os = function() {
tolower(Sys.info()[["sysname"]])
}

system_arch = function() {
R.version$arch
}

# Detect macOS Operating System ----

shell_mac_version = function() {
sys::as_text(sys::exec_internal("sw_vers", "-productVersion")$stdout)
}


is_macos_r_supported = function() {
mac_version = shell_mac_version()

version_between(mac_version, "10.13.0", "14.0.0")
}

is_macos_ventura = function() {
mac_version = shell_mac_version()

version_between(mac_version, "13.0.0", "14.0.0")
}

is_macos_monterey = function() {
mac_version = shell_mac_version()

version_between(mac_version, "12.0.0", "13.0.0")
}

is_macos_big_sur= function() {
mac_version = shell_mac_version()

version_between(mac_version, "11.0.0", "12.0.0")
}

is_macos_catalina = function() {
mac_version = shell_mac_version()

version_between(mac_version, "10.15.0", "10.16.0")
}

is_macos_mojave = function() {
mac_version = shell_mac_version()

version_between(mac_version, "10.14.0", "10.15.0")
}

is_macos_high_sierra = function() {
mac_version = shell_mac_version()

version_between(mac_version, "10.13.0", "10.14.0")
}

is_macos = function() {
system_os() == "darwin"
}

# Architecture Checks ----

is_aarch64 = function() {
system_arch() == "aarch64"
}

is_x86_64 = function() {
system_arch() == "x86_64"
}


# Version Checks ----

is_r_version = function(target_version, compare_major_minor = TRUE) {

minor_value = if (compare_major_minor) {
# If x.y.z, this retrieves y
strsplit(R.version$minor, ".", fixed = TRUE)[[1]][1]
} else {
# If x.y.z, this retrieves y.z
R.version$minor
}

# Build the version string of x.y or x.y.z
version_string = paste(R.version$major, minor_value, sep = ".")

# Check for equality.
return(version_string == target_version)
}




version_above = function(software_version, than) {
utils::compareVersion(software_version, than) == 1L
}

version_between = function(software_version, lower, greater_strict) {
above = utils::compareVersion(software_version, lower) %in% c(0L, 1L)
below = utils::compareVersion(software_version, greater_strict) %in% c(-1L)
above && below
}

# Assertions ----

#' Assert a condition
#'
#' @description
#' `assert()` allows a function state to be checked and stopped.
#'
#' @param condition A logical indicating the status of the condition.
#' @param message A string to display.
#'
#' @keywords internal
#' @rdname assert
#' @export
assert = function(condition, message = NULL) {
if(isFALSE(condition)) {
stop(paste0(message, " is not TRUE"))
}
}

#' @rdname assert
#' @export
assert_mac = function(){
assert(is_macos(), "On macOS")
}

#' @rdname assert
#' @export
assert_aarch64 = function(){
assert(is_aarch64(), "On aarch64")
}

#' @rdname assert
#' @export
assert_x86_64 = function(){
assert(is_x86_64(), "On x86_64")
}

# Custom CLI Printing -----

#' Print CLI Responses
Expand All @@ -151,8 +9,7 @@ print.cli = function(x, ...) {
cat("Status:\n", paste(x$status, collapse = "\n"), "\n")
}

# Display warning ----

# Display download warning ----
verify_status = function(status, program, url, type = c("uninstall", "install")) {
type = match.arg(type)

Expand All @@ -166,7 +23,7 @@ verify_status = function(status, program, url, type = c("uninstall", "install"))
}
}


# Obtain a password if not present ----
force_password = function(supplied_password) {
entered_password = supplied_password
if(is.null(entered_password)) {
Expand Down
30 changes: 30 additions & 0 deletions R/version-check.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Version Checks ----

version_above = function(software_version, than) {
utils::compareVersion(software_version, than) == 1L
}

version_between = function(software_version, lower, greater_strict) {
above = utils::compareVersion(software_version, lower) %in% c(0L, 1L)
below = utils::compareVersion(software_version, greater_strict) %in% c(-1L)
above && below
}


is_r_version = function(target_version, compare_major_minor = TRUE) {

minor_value = if (compare_major_minor) {
# If x.y.z, this retrieves y
strsplit(R.version$minor, ".", fixed = TRUE)[[1]][1]
} else {
# If x.y.z, this retrieves y.z
R.version$minor
}

# Build the version string of x.y or x.y.z
version_string = paste(R.version$major, minor_value, sep = ".")

# Check for equality.
return(version_string == target_version)
}

2 changes: 1 addition & 1 deletion man/assert.Rd

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

0 comments on commit 85af401

Please sign in to comment.