|
| 1 | +# Base URLs for SimpleITK binary releases. |
| 2 | +# These values are updated by the build workflow when generating the foyer package. |
| 3 | +.sitk_releases_base_url <- "https://github.com/SimpleITK/SimpleITKRInstaller/releases/download" |
| 4 | +.sitk_releases_page_url <- "https://github.com/SimpleITK/SimpleITKRInstaller/releases" |
| 5 | +.sitk_repo_url <- "https://github.com/SimpleITK/SimpleITKRInstaller" |
| 6 | + |
| 7 | +#' Install SimpleITK Binary Package |
| 8 | +#' |
| 9 | +#' Downloads and installs the pre-built SimpleITK binary package for your |
| 10 | +#' platform and R version from GitHub releases. |
| 11 | +#' |
| 12 | +#' @param version Character string specifying the SimpleITK version to install. |
| 13 | +#' Defaults to the version of this foyer package. |
| 14 | +#' @param lib Character string specifying the library path where the package |
| 15 | +#' should be installed. Defaults to the first element of \code{.libPaths()}. |
| 16 | +#' @param repos Character string specifying alternative repository URL. |
| 17 | +#' By default, uses GitHub releases. |
| 18 | +#' @param force Logical. If \code{TRUE}, forces reinstallation even if the |
| 19 | +#' package is already installed. |
| 20 | +#' @param quiet Logical. If \code{TRUE}, suppresses progress messages. |
| 21 | +#' |
| 22 | +#' @return Invisibly returns \code{TRUE} if installation succeeds, |
| 23 | +#' \code{FALSE} otherwise. |
| 24 | +#' |
| 25 | +#' @details |
| 26 | +#' This function detects your operating system, R version, and architecture, |
| 27 | +#' then downloads the appropriate pre-built binary package from the SimpleITK |
| 28 | +#' GitHub releases. The binary packages are built by the SimpleITK project |
| 29 | +#' and hosted on GitHub releases. |
| 30 | +#' |
| 31 | +#' Supported platforms: |
| 32 | +#' \itemize{ |
| 33 | +#' \item Windows x86_64 (R >= 4.0) |
| 34 | +#' \item macOS x86_64 (R >= 4.0) |
| 35 | +#' \item macOS ARM64 (R >= 4.0) |
| 36 | +#' \item Linux x86_64 (R >= 4.0) |
| 37 | +#' } |
| 38 | +#' |
| 39 | +#' @examples |
| 40 | +#' \dontrun{ |
| 41 | +#' # Install SimpleITK for your platform |
| 42 | +#' install_simpleitk() |
| 43 | +#' |
| 44 | +#' # Install a specific version |
| 45 | +#' install_simpleitk(version = "2.5.0") |
| 46 | +#' |
| 47 | +#' # Force reinstallation |
| 48 | +#' install_simpleitk(force = TRUE) |
| 49 | +#' } |
| 50 | +#' |
| 51 | +#' @export |
| 52 | +install_simpleitk <- function(version = NULL, |
| 53 | + lib = .libPaths()[1], |
| 54 | + repos = NULL, |
| 55 | + force = FALSE, |
| 56 | + quiet = FALSE) { |
| 57 | + |
| 58 | + # Check if already installed |
| 59 | + if (!force && requireNamespace("SimpleITK", quietly = TRUE)) { |
| 60 | + if (!quiet) { |
| 61 | + message("SimpleITK is already installed. Use force = TRUE to reinstall.") |
| 62 | + } |
| 63 | + return(invisible(TRUE)) |
| 64 | + } |
| 65 | + |
| 66 | + # Get version from DESCRIPTION if not specified |
| 67 | + if (is.null(version)) { |
| 68 | + version <- utils::packageDescription("SimpleITK.foyer", fields = "Version") |
| 69 | + if (is.na(version)) { |
| 70 | + stop("Cannot determine SimpleITK version. Please specify version parameter.") |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + # Get R version (major.minor only) |
| 75 | + r_version <- paste(R.version$major, |
| 76 | + strsplit(R.version$minor, "\\.")[[1]][1], |
| 77 | + sep = ".") |
| 78 | + |
| 79 | + # Detect platform |
| 80 | + platform_info <- get_platform_info() |
| 81 | + |
| 82 | + if (is.null(platform_info)) { |
| 83 | + stop("Unsupported platform. SimpleITK binaries are only available for ", |
| 84 | + "Windows (x86_64), macOS (x86_64, ARM64), and Linux (x86_64).") |
| 85 | + } |
| 86 | + |
| 87 | + # Construct download URL |
| 88 | + if (is.null(repos)) { |
| 89 | + tag <- paste0("v", version) |
| 90 | + filename <- sprintf("SimpleITK_%s_R%s_%s.%s", |
| 91 | + version, r_version, |
| 92 | + platform_info$platform, |
| 93 | + platform_info$extension) |
| 94 | + download_url <- file.path(.sitk_releases_base_url, tag, filename) |
| 95 | + } else { |
| 96 | + download_url <- file.path(repos, sprintf("SimpleITK_%s.%s", |
| 97 | + version, |
| 98 | + platform_info$extension)) |
| 99 | + } |
| 100 | + |
| 101 | + if (!quiet) { |
| 102 | + message("Downloading SimpleITK ", version, " for ", platform_info$platform, |
| 103 | + " (R ", r_version, ")...") |
| 104 | + message("URL: ", download_url) |
| 105 | + } |
| 106 | + |
| 107 | + # Create temporary directory and file with proper naming |
| 108 | + # R expects package files to be named: PackageName_Version.extension |
| 109 | + temp_dir <- tempfile() |
| 110 | + dir.create(temp_dir, showWarnings = FALSE) |
| 111 | + on.exit(unlink(temp_dir, recursive = TRUE), add = TRUE) |
| 112 | + |
| 113 | + pkg_filename <- sprintf("SimpleITK_%s.%s", version, platform_info$extension) |
| 114 | + temp_file <- file.path(temp_dir, pkg_filename) |
| 115 | + |
| 116 | + # Download the binary |
| 117 | + tryCatch({ |
| 118 | + download.file(download_url, temp_file, mode = "wb", quiet = quiet) |
| 119 | + }, error = function(e) { |
| 120 | + stop("Failed to download SimpleITK binary package.\n", |
| 121 | + "URL: ", download_url, "\n", |
| 122 | + "Error: ", conditionMessage(e), "\n", |
| 123 | + "Please check that:\n", |
| 124 | + " 1. You have an internet connection\n", |
| 125 | + " 2. The specified version (", version, ") has pre-built binaries\n", |
| 126 | + " 3. A binary exists for your platform and R version\n", |
| 127 | + "Available releases: ", .sitk_releases_page_url) |
| 128 | + }) |
| 129 | + |
| 130 | + if (!quiet) { |
| 131 | + message("Installing SimpleITK package...") |
| 132 | + } |
| 133 | + |
| 134 | + # Install the binary package |
| 135 | + tryCatch({ |
| 136 | + install.packages(temp_file, repos = NULL, type = "source", lib = lib, |
| 137 | + quiet = quiet) |
| 138 | + |
| 139 | + if (!quiet) { |
| 140 | + message("SimpleITK successfully installed!") |
| 141 | + message("Load it with: library(SimpleITK)") |
| 142 | + } |
| 143 | + return(invisible(TRUE)) |
| 144 | + |
| 145 | + }, error = function(e) { |
| 146 | + stop("Failed to install SimpleITK binary package.\n", |
| 147 | + "Error: ", conditionMessage(e), "\n", |
| 148 | + "You may need to build from source instead, see ", .sitk_repo_url, ".\n") |
| 149 | + }) |
| 150 | +} |
| 151 | + |
| 152 | + |
| 153 | +#' Get Platform Information |
| 154 | +#' |
| 155 | +#' @return A list with platform and extension, or NULL if unsupported |
| 156 | +#' @keywords internal |
| 157 | +get_platform_info <- function() { |
| 158 | + os <- Sys.info()["sysname"] |
| 159 | + arch <- Sys.info()["machine"] |
| 160 | + |
| 161 | + # Support both x86_64 and ARM64 architectures |
| 162 | + # Note: Windows may report "x86-64" (with hyphen) or "x86_64" (with underscore) |
| 163 | + is_x86_64 <- grepl("x86[_-]64|amd64", arch, ignore.case = TRUE) |
| 164 | + is_arm64 <- grepl("arm64|aarch64", arch, ignore.case = TRUE) |
| 165 | + |
| 166 | + if (!is_x86_64 && !is_arm64) { |
| 167 | + return(NULL) |
| 168 | + } |
| 169 | + |
| 170 | + if (os == "Windows") { |
| 171 | + return(list(platform = "windows-x86_64", extension = "zip")) |
| 172 | + } else if (os == "Darwin") { |
| 173 | + # macOS: distinguish between x86_64 and ARM64 |
| 174 | + if (is_arm64) { |
| 175 | + return(list(platform = "macos-arm64", extension = "tgz")) |
| 176 | + } else { |
| 177 | + return(list(platform = "macos-x86_64", extension = "tgz")) |
| 178 | + } |
| 179 | + } else if (os == "Linux") { |
| 180 | + return(list(platform = "linux-x86_64", extension = "tar.gz")) |
| 181 | + } else { |
| 182 | + return(NULL) |
| 183 | + } |
| 184 | +} |
0 commit comments