| 1 |
|
| 2 |
renv_lockfile_init <- function(project) { |
| 3 |
|
| 4 |
lockfile <- list() |
| 5 |
|
| 6 |
lockfile$R <- renv_lockfile_init_r(project) |
| 7 |
lockfile$Python <- renv_lockfile_init_python(project) |
| 8 |
lockfile$Packages <- list() |
| 9 |
|
| 10 |
class(lockfile) <- "renv_lockfile" |
| 11 |
lockfile |
| 12 |
|
| 13 |
} |
| 14 |
|
| 15 |
renv_lockfile_init_r_version <- function(project) { |
| 16 |
|
| 17 |
# NOTE: older versions of renv may have written out an empty array |
| 18 |
# for the R version in some cases, so we explicitly check that we |
| 19 |
# receive a length-one string here. |
| 20 |
version <- settings$r.version(project = project) |
| 21 |
if (!pstring(version)) |
| 22 |
version <- getRversion() |
| 23 |
|
| 24 |
format(version) |
| 25 |
|
| 26 |
} |
| 27 |
|
| 28 |
renv_lockfile_init_r_repos <- function(project, repos = getOption("repos")) { |
| 29 |
|
| 30 |
# unset the RStudio attribute if it was set |
| 31 |
attr(repos, "RStudio") <- NULL |
| 32 |
|
| 33 |
# make sure it's a character vector in this scope |
| 34 |
repos <- convert(repos, "character") |
| 35 |
|
| 36 |
# make sure a CRAN repository is set |
| 37 |
cran <- getOption("renv.repos.cran", "https://cloud.r-project.org") |
| 38 |
repos[repos == "@CRAN@"] <- cran |
| 39 |
|
| 40 |
# remove PPM bits from URL |
| 41 |
if (renv_ppm_enabled()) { |
| 42 |
pattern <- "/__[^_]+__/[^/]+/" |
| 43 |
repos <- sub(pattern, "/", repos) |
| 44 |
} |
| 45 |
|
| 46 |
# all done; return as list |
| 47 |
convert(repos, "list") |
| 48 |
|
| 49 |
} |
| 50 |
|
| 51 |
renv_lockfile_init_r <- function(project) { |
| 52 |
version <- renv_lockfile_init_r_version(project) |
| 53 |
repos <- renv_lockfile_init_r_repos(project) |
| 54 |
list(Version = version, Repositories = repos) |
| 55 |
} |
| 56 |
|
| 57 |
renv_lockfile_init_python <- function(project) { |
| 58 |
|
| 59 |
python <- Sys.getenv("RENV_PYTHON", unset = NA) |
| 60 |
if (is.na(python)) |
| 61 |
return(NULL) |
| 62 |
|
| 63 |
if (!file.exists(python)) |
| 64 |
return(NULL) |
| 65 |
|
| 66 |
info <- renv_python_info(python) |
| 67 |
if (is.null(info)) |
| 68 |
return(NULL) |
| 69 |
|
| 70 |
version <- renv_python_version(python) |
| 71 |
type <- info$type |
| 72 |
root <- info$root |
| 73 |
name <- renv_python_envname(project, root, type) |
| 74 |
|
| 75 |
fields <- list() |
| 76 |
|
| 77 |
fields$Version <- version |
| 78 |
fields$Type <- type |
| 79 |
fields$Name <- name |
| 80 |
|
| 81 |
fields |
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
renv_lockfile_fini <- function(lockfile, project) { |
| 86 |
lockfile$Bioconductor <- renv_lockfile_fini_bioconductor(lockfile, project) |
| 87 |
lockfile |
| 88 |
} |
| 89 |
|
| 90 |
renv_lockfile_fini_bioconductor <- function(lockfile, project) { |
| 91 |
|
| 92 |
# check for explicit version in settings |
| 93 |
version <- settings$bioconductor.version(project = project) |
| 94 |
if (length(version)) |
| 95 |
return(list(Version = version)) |
| 96 |
|
| 97 |
# otherwise, check for a package which required Bioconductor |
| 98 |
records <- renv_lockfile_records(lockfile) |
| 99 |
if (empty(records)) |
| 100 |
return(NULL) |
| 101 |
|
| 102 |
for (package in c("BiocManager", "BiocInstaller")) |
| 103 |
if (!is.null(records[[package]])) |
| 104 |
return(list(Version = renv_bioconductor_version(project = project))) |
| 105 |
|
| 106 |
sources <- extract_chr(records, "Source") |
| 107 |
if ("Bioconductor" %in% sources) |
| 108 |
return(list(Version = renv_bioconductor_version(project = project))) |
| 109 |
|
| 110 |
# nothing found; return NULL |
| 111 |
NULL |
| 112 |
|
| 113 |
} |
| 114 |
|
| 115 |
renv_lockfile_path <- function(project) { |
| 116 |
renv_paths_lockfile(project = project) |
| 117 |
} |
| 118 |
|
| 119 |
renv_lockfile_save <- function(lockfile, project) { |
| 120 |
file <- renv_lockfile_path(project) |
| 121 |
renv_lockfile_write(lockfile, file = file) |
| 122 |
} |
| 123 |
|
| 124 |
renv_lockfile_load <- function(project, strict = FALSE) { |
| 125 |
|
| 126 |
path <- renv_lockfile_path(project) |
| 127 |
if (file.exists(path)) |
| 128 |
return(renv_lockfile_read(path)) |
| 129 |
|
| 130 |
if (strict) { |
| 131 |
abort(c( |
| 132 |
"This project does not contain a lockfile.", |
| 133 |
i = "Have you called `snapshot()` yet?" |
| 134 |
)) |
| 135 |
} |
| 136 |
|
| 137 |
manifest <- file.path(project, "manifest.json") |
| 138 |
if (file.exists(manifest)) { |
| 139 |
caution("No lockfile found; creating from `manifest.json`.") |
| 140 |
renv_lockfile_from_manifest(manifest, path) |
| 141 |
return(renv_lockfile_read(path)) |
| 142 |
} |
| 143 |
|
| 144 |
renv_lockfile_init(project = project) |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
renv_lockfile_sort <- function(lockfile) { |
| 149 |
|
| 150 |
# extract R records (nothing to do if empty) |
| 151 |
records <- renv_lockfile_records(lockfile) |
| 152 |
if (empty(records)) |
| 153 |
return(lockfile) |
| 154 |
|
| 155 |
# sort the records |
| 156 |
sorted <- records[csort(names(records))] |
| 157 |
renv_lockfile_records(lockfile) <- sorted |
| 158 |
|
| 159 |
# sort top-level fields |
| 160 |
fields <- unique(c("R", "Bioconductor", "Python", "Packages", names(lockfile))) |
| 161 |
lockfile <- lockfile[intersect(fields, names(lockfile))] |
| 162 |
|
| 163 |
# return post-sort |
| 164 |
lockfile |
| 165 |
|
| 166 |
} |
| 167 |
|
| 168 |
renv_lockfile_create <- function(project, |
| 169 |
type = NULL, |
| 170 |
libpaths = NULL, |
| 171 |
packages = NULL, |
| 172 |
exclude = NULL, |
| 173 |
prompt = NULL, |
| 174 |
force = NULL, |
| 175 |
dev = FALSE) |
| 176 |
{ |
| 177 |
libpaths <- libpaths %||% renv_libpaths_all() |
| 178 |
type <- type %||% settings$snapshot.type(project = project) |
| 179 |
|
| 180 |
# use a restart, so we can allow the user to install packages before snapshot |
| 181 |
lockfile <- withRestarts( |
| 182 |
renv_lockfile_create_impl(project, type, libpaths, packages, exclude, prompt, force, dev = dev), |
| 183 |
renv_recompute_records = function() { |
| 184 |
renv_dynamic_reset() |
| 185 |
renv_lockfile_create_impl(project, type, libpaths, packages, exclude, prompt, force, dev = dev) |
| 186 |
} |
| 187 |
) |
| 188 |
} |
| 189 |
|
| 190 |
renv_lockfile_create_impl <- function(project, type, libpaths, packages, exclude, prompt, force, dev = FALSE) { |
| 191 |
|
| 192 |
lockfile <- renv_lockfile_init(project) |
| 193 |
|
| 194 |
# compute the project's top-level package dependencies |
| 195 |
packages <- packages %||% renv_snapshot_dependencies( |
| 196 |
project = project, |
| 197 |
type = type, |
| 198 |
dev = dev |
| 199 |
) |
| 200 |
|
| 201 |
# expand the recursive dependencies of these packages |
| 202 |
records <- renv_snapshot_packages( |
| 203 |
packages = setdiff(packages, exclude), |
| 204 |
libpaths = libpaths, |
| 205 |
project = project |
| 206 |
) |
| 207 |
|
| 208 |
# check for missing packages |
| 209 |
ignored <- c(renv_project_ignored_packages(project), renv_packages_base(), exclude, "renv") |
| 210 |
missing <- setdiff(packages, c(names(records), ignored)) |
| 211 |
|
| 212 |
# cancel automatic snapshots if we have missing packages |
| 213 |
if (length(missing) && the$auto_snapshot_running) { |
| 214 |
cancel <- findRestart("cancel") |
| 215 |
if (isRestart(cancel)) |
| 216 |
invokeRestart(cancel) |
| 217 |
} |
| 218 |
|
| 219 |
# give user a chance to handle missing packages, if any |
| 220 |
# |
| 221 |
# we only run this in top-level calls to snapshot() since renv will internally |
| 222 |
# use snapshot() to create lockfiles, and missing packages are understood / |
| 223 |
# tolerated there. this code mostly exists so interactive usages of snapshot() |
| 224 |
# can recover and install missing packages |
| 225 |
if (identical(topfun(), snapshot)) |
| 226 |
renv_snapshot_report_missing(missing, type) |
| 227 |
|
| 228 |
records <- renv_snapshot_fixup(records) |
| 229 |
renv_lockfile_records(lockfile) <- records |
| 230 |
|
| 231 |
lockfile <- renv_lockfile_fini(lockfile, project) |
| 232 |
|
| 233 |
keys <- unique(c("R", "Bioconductor", names(lockfile))) |
| 234 |
lockfile <- lockfile[intersect(keys, names(lockfile))] |
| 235 |
|
| 236 |
class(lockfile) <- "renv_lockfile" |
| 237 |
lockfile |
| 238 |
|
| 239 |
} |
| 240 |
|
| 241 |
renv_lockfile_modify <- function(lockfile, records) { |
| 242 |
|
| 243 |
enumerate(records, function(package, record) { |
| 244 |
renv_lockfile_records(lockfile)[[package]] <<- record |
| 245 |
}) |
| 246 |
|
| 247 |
lockfile |
| 248 |
|
| 249 |
} |
| 250 |
|
| 251 |
renv_lockfile_compact <- function(lockfile) { |
| 252 |
|
| 253 |
records <- renv_lockfile_records(lockfile) |
| 254 |
remotes <- map_chr(records, renv_record_format_remote) |
| 255 |
|
| 256 |
remotes <- remotes[sort(names(remotes))] |
| 257 |
|
| 258 |
formatted <- sprintf(" %s = \"%s\"", format(names(remotes)), remotes) |
| 259 |
joined <- paste(formatted, collapse = ",\n") |
| 260 |
|
| 261 |
all <- c("renv::use(", joined, ")") |
| 262 |
paste(all, collapse = "\n") |
| 263 |
|
| 264 |
} |
| 265 |
|
| 266 |
renv_lockfile_records <- function(lockfile) { |
| 267 |
as.list(lockfile$Packages %||% lockfile) |
| 268 |
} |
| 269 |
|
| 270 |
`renv_lockfile_records<-` <- function(x, value) { |
| 271 |
x$Packages <- filter(value, zlength) |
| 272 |
invisible(x) |
| 273 |
} |
| 274 |
|
| 275 |
# for compatibility with older versions of RStudio |
| 276 |
renv_records <- renv_lockfile_records |