1
DEFAULT_RSPM_REPO_ID <-  "1" # cran
2
DEFAULT_RSPM <-  "https://packagemanager.rstudio.com"
3

4
#' Query the system requirements for a dev package (and its dependencies)
5
#'
6
#' Returns a character vector of commands to run that will install system
7
#' requirements for the queried operating system.
8
#'
9
#' @inheritParams local_install
10
#' @param os,os_release The operating system and operating system release version, see
11
#'   <https://github.com/rstudio/r-system-requirements#operating-systems> for the
12
#'   list of supported operating systems. If `NULL`, the default, these will be
13
#'   looked up using [distro::distro()].
14
#' @param execute,sudo If `execute` is `TRUE`, pak will execute the system
15
#'   commands (if any). If `sudo` is `TRUE`, pak will prepend the commands with
16
#'   [sudo](https://en.wikipedia.org/wiki/Sudo).
17
#' @param echo If `echo` is `TRUE` and `execute` is `TRUE`, echo the command output.
18
#' @return A character vector of commands needed to install the system requirements for the package (invisibly).
19
#' @export
20
local_system_requirements <- function(os = NULL, os_release = NULL, root = ".", execute = FALSE, sudo = execute, echo = FALSE) {
21 1
  res <- remote(
22 1
    function(...) asNamespace("pak")$local_system_requirements_internal(...),
23 1
    list(os = os, os_release = os_release, root = root, execute = execute, sudo = sudo, echo = echo))
24 1
  invisible(res)
25
}
26

27
local_system_requirements_internal <- function(os, os_release, root, execute, sudo, echo) {
28 0
  if (is.null(os) || is.null(os_release)) {
29 0
    d <- distro::distro()
30 0
    os <- os %||% d$id
31 0
    os_release <- os_release %||% d$short_version
32
  }
33

34 0
  os_versions <- supported_os_versions()
35

36 0
  os <- match.arg(os, names(os_versions))
37

38 0
  os_release <- match.arg(os_release, os_versions[[os]])
39

40 0
  rspm <- Sys.getenv("RSPM_ROOT", DEFAULT_RSPM)
41 0
  rspm_repo_id <- Sys.getenv("RSPM_REPO_ID", DEFAULT_RSPM_REPO_ID)
42 0
  rspm_repo_url <- sprintf("%s/__api__/repos/%s", rspm, rspm_repo_id)
43

44 0
  desc_file <- normalizePath(file.path(root, "DESCRIPTION"), mustWork = FALSE)
45 0
  if (!file.exists(desc_file)) {
46 0
    stop("`", root, "` must contain a package.", call. = FALSE)
47
  }
48

49 0
  req_url <- sprintf(
50 0
    "%s/sysreqs?distribution=%s&release=%s&suggests=true",
51 0
    rspm_repo_url,
52 0
    os,
53 0
    os_release
54
  )
55

56 0
  h <- curl::new_handle()
57

58 0
  desc_size <- file.size(desc_file)
59 0
  desc_data <- readBin(desc_file, "raw", desc_size)
60

61 0
  curl::handle_setheaders(h,
62 0
    customrequest = "POST",
63 0
    "content-type" = "text/plain"
64
  )
65

66 0
  curl::handle_setopt(h,
67 0
    postfieldsize = desc_size,
68 0
    postfields = desc_data
69
  )
70

71 0
  res <- curl::curl_fetch_memory(req_url, h)
72

73 0
  data <- jsonlite::fromJSON(rawToChar(res$content), simplifyVector = FALSE)
74

75 0
  pre_install <- unique(unlist(c(data[["pre_install"]], lapply(data[["dependencies"]], `[[`, "pre_install"))))
76

77 0
  install_scripts <- unique(unlist(c(data[["install_scripts"]], lapply(data[["dependencies"]], `[[`, "install_scripts"))))
78

79 0
  commands <- as.character(c(pre_install, install_scripts))
80 0
  if (echo) {
81 0
    callback <- function(x, ...) cli::cli_verbatim(sub("[\r\n]+$", "", x))
82
  } else {
83 0
    callback <- function(x, ...) invisible()
84
  }
85

86 0
  if (execute) {
87 0
    for (cmd in commands) {
88 0
      if (sudo) {
89 0
        cmd <- paste("sudo", cmd)
90
      }
91 0
      cli::cli_alert_info("Executing {.code {cmd}}")
92

93 0
      processx::run("sh", c("-c", cmd), stdout_callback = callback, stderr_to_stdout = TRUE)
94
    }
95
  }
96

97 0
  commands
98
}
99

100
# Adapted from https://github.com/rstudio/r-system-requirements/blob/master/systems.json
101
# OSs commented out are not currently supported by the API
102
supported_os_versions <- function() {
103 0
  list(
104
    #"debian" = c("8", "9"),
105 0
    "ubuntu" = c("14.04", "16.04", "18.04", "20.04"),
106 0
    "centos" = c("6", "7", "8"),
107 0
    "redhat" = c("6", "7", "8"),
108 0
    "opensuse" = c("42.3", "15.0"),
109 0
    "sle" = c("12.3", "15.0")
110
    #"windows" = c("")
111
  )
112
}

Read our documentation on viewing source code .

Loading