| 1 |
|
| 2 |
renv_description_read <- function(path = NULL, |
| 3 |
package = NULL, |
| 4 |
subdir = NULL, |
| 5 |
field = NULL, |
| 6 |
...) |
| 7 |
{ |
| 8 |
# if given a package name, construct path to that package |
| 9 |
path <- path %||% renv_package_find_impl(package, compat = TRUE) |
| 10 |
|
| 11 |
# normalize non-absolute paths |
| 12 |
if (!renv_path_absolute(path)) |
| 13 |
path <- renv_path_normalize(path) |
| 14 |
|
| 15 |
# if 'path' refers to a directory, try to resolve the DESCRIPTION file |
| 16 |
if (dir.exists(path)) { |
| 17 |
components <- c(path, if (nzchar(subdir %||% "")) subdir, "DESCRIPTION") |
| 18 |
path <- paste(components, collapse = "/") |
| 19 |
} |
| 20 |
|
| 21 |
# if the DESCRIPTION file doesn't exist, bail |
| 22 |
if (!file.exists(path)) |
| 23 |
stopf("DESCRIPTION file %s does not exist", renv_path_pretty(path)) |
| 24 |
|
| 25 |
# read value with filebacked cache |
| 26 |
description <- filebacked( |
| 27 |
context = "renv_description_read", |
| 28 |
path = path, |
| 29 |
callback = renv_description_read_impl, |
| 30 |
subdir = subdir, |
| 31 |
... |
| 32 |
) |
| 33 |
|
| 34 |
if (!is.null(field)) |
| 35 |
return(description[[field]]) |
| 36 |
|
| 37 |
description |
| 38 |
|
| 39 |
} |
| 40 |
|
| 41 |
renv_description_read_impl <- function(path = NULL, subdir = NULL, ...) { |
| 42 |
|
| 43 |
# if we have an archive, attempt to unpack the DESCRIPTION |
| 44 |
type <- renv_archive_type(path) |
| 45 |
if (type != "unknown") { |
| 46 |
|
| 47 |
# list files within the archive |
| 48 |
files <- renv_archive_list(path) |
| 49 |
|
| 50 |
# find the DESCRIPTION file. note that for some source tarballs (e.g. |
| 51 |
# those from GitHub) the first entry may not be the package name, so |
| 52 |
# just consume everything up to the first slash |
| 53 |
subdir <- subdir %||% "" |
| 54 |
|
| 55 |
# tolerate leading './' components in the archive paths |
| 56 |
# https://github.com/rstudio/renv/issues/1852 |
| 57 |
prefix <- "^(?:\\./)*[^/]+" |
| 58 |
|
| 59 |
# build pattern looking for the DESCRIPTION file |
| 60 |
parts <- c(prefix, if (nzchar(subdir)) subdir, "DESCRIPTION$") |
| 61 |
pattern <- paste(parts, collapse = "/") |
| 62 |
|
| 63 |
descs <- grep(pattern, files, perl = TRUE, value = TRUE) |
| 64 |
if (empty(descs)) { |
| 65 |
fmt <- "archive '%s' does not appear to contain a DESCRIPTION file" |
| 66 |
stopf(fmt, renv_path_aliased(path)) |
| 67 |
} |
| 68 |
|
| 69 |
# choose the shortest DESCRPITION file matching |
| 70 |
file <- descs[[which.min(nchar(descs))]] |
| 71 |
|
| 72 |
# unpack into tempdir location |
| 73 |
exdir <- renv_scope_tempfile("renv-description-") |
| 74 |
renv_archive_decompress(path, files = file, exdir = exdir) |
| 75 |
|
| 76 |
# update path to extracted DESCRIPTION |
| 77 |
path <- file.path(exdir, file) |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
# read DESCRIPTION as dcf |
| 82 |
dcf <- renv_dcf_read(path, ...) |
| 83 |
if (empty(dcf)) |
| 84 |
stopf("DESCRIPTION file at '%s' is empty", path) |
| 85 |
|
| 86 |
dcf |
| 87 |
|
| 88 |
} |
| 89 |
|
| 90 |
# parse the dependency requirements normally presented in |
| 91 |
# Depends, Imports, Suggests, and so on |
| 92 |
renv_description_parse_field <- function(field) { |
| 93 |
|
| 94 |
# handle list inputs (e.g. from v2 lockfile records where |
| 95 |
# dependency fields are stored as JSON arrays) |
| 96 |
if (is.list(field)) |
| 97 |
field <- paste(field, collapse = ", ") |
| 98 |
|
| 99 |
# check for invalid / unexpected inputs |
| 100 |
if (is.null(field) || is.na(field) || !nzchar(field)) |
| 101 |
return(NULL) |
| 102 |
|
| 103 |
pattern <- paste0( |
| 104 |
"^\\s*", # leading whitespace |
| 105 |
"([a-zA-Z0-9._]+)", # package name |
| 106 |
"(?:\\s*\\(([><=]+)\\s*([0-9.-]+)\\))?", # optional version specification |
| 107 |
"\\s*$" # trailing whitespace |
| 108 |
) |
| 109 |
|
| 110 |
# split on commas |
| 111 |
x <- strsplit(field, ",", fixed = TRUE)[[1L]] |
| 112 |
|
| 113 |
# drop any empty fields |
| 114 |
x <- x[nzchar(x)] |
| 115 |
if (empty(x)) |
| 116 |
return(NULL) |
| 117 |
|
| 118 |
data_frame( |
| 119 |
Package = sub(pattern, "\\1", x, perl = TRUE), |
| 120 |
Require = sub(pattern, "\\2", x, perl = TRUE), |
| 121 |
Version = sub(pattern, "\\3", x, perl = TRUE) |
| 122 |
) |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
renv_description_resolve <- function(path) { |
| 127 |
|
| 128 |
case( |
| 129 |
is.list(path) ~ path, |
| 130 |
is.character(path) ~ renv_description_read(path = path) |
| 131 |
) |
| 132 |
|
| 133 |
} |
| 134 |
|
| 135 |
renv_description_built_version <- function(desc = NULL) { |
| 136 |
|
| 137 |
desc <- renv_description_resolve(desc) |
| 138 |
|
| 139 |
built <- desc[["Built"]] |
| 140 |
if (is.null(built)) |
| 141 |
return(NA) |
| 142 |
|
| 143 |
substring(built, 3L, regexpr(";", built, fixed = TRUE) - 1L) |
| 144 |
} |
| 145 |
|
| 146 |
renv_description_remotes <- function(path) { |
| 147 |
|
| 148 |
desc <- catch(renv_description_read(path)) |
| 149 |
if (inherits(desc, "error")) |
| 150 |
return(list()) |
| 151 |
|
| 152 |
profile <- renv_profile_get() |
| 153 |
field <- if (is.null(profile)) |
| 154 |
"Remotes" |
| 155 |
else |
| 156 |
sprintf("Config/renv/profiles/%s/remotes", profile) |
| 157 |
|
| 158 |
remotes <- desc[[field]] |
| 159 |
if (is.null(remotes)) |
| 160 |
return(list()) |
| 161 |
|
| 162 |
# if possible, resolve remotes lazily |
| 163 |
splat <- strsplit(remotes, "[[:space:]]*,[[:space:]]*")[[1]] |
| 164 |
remotes <- lapply(splat, function(spec) { |
| 165 |
|
| 166 |
# if this is a named remote, we can resolve it lazily |
| 167 |
idx <- c(regexpr("=", spec, fixed = TRUE)) |
| 168 |
result <- if (idx == -1L) { |
| 169 |
remote <- renv_remotes_resolve(spec) |
| 170 |
list(Package = remote$Package, Remote = remote) |
| 171 |
} else { |
| 172 |
package <- substring(spec, 1L, idx - 1L) |
| 173 |
list(Package = package, Remote = function() renv_remotes_resolve(spec)) |
| 174 |
} |
| 175 |
|
| 176 |
}) |
| 177 |
|
| 178 |
# put together into named list |
| 179 |
records <- map(remotes, `[[`, 2L) |
| 180 |
names(records) <- map_chr(remotes, `[[`, 1L) |
| 181 |
records |
| 182 |
|
| 183 |
} |
| 184 |
|