| 1 |
|
| 2 |
# The path to the currently-loaded project, if any. |
| 3 |
# NULL when no project is currently loaded. |
| 4 |
the$project_path <- NULL |
| 5 |
|
| 6 |
# Flag indicating whether we're checking if the project is synchronized. |
| 7 |
the$project_synchronized_check_running <- FALSE |
| 8 |
|
| 9 |
#' Retrieve the active project |
| 10 |
#' |
| 11 |
#' Retrieve the path to the active project (if any). |
| 12 |
#' |
| 13 |
#' @param default The value to return when no project is |
| 14 |
#' currently active. Defaults to `NULL`. |
| 15 |
#' |
| 16 |
#' @export |
| 17 |
#' |
| 18 |
#' @return The active project directory, as a length-one character vector. |
| 19 |
#' |
| 20 |
#' @examples |
| 21 |
#' \dontrun{ |
| 22 |
#' |
| 23 |
#' # get the currently-active renv project |
| 24 |
#' renv::project() |
| 25 |
#' |
| 26 |
#' } |
| 27 |
project <- function(default = NULL) { |
| 28 |
renv_project_get(default = default) |
| 29 |
} |
| 30 |
|
| 31 |
renv_project_get <- function(default = NULL) { |
| 32 |
the$project_path %||% default |
| 33 |
} |
| 34 |
|
| 35 |
# NOTE: 'RENV_PROJECT' kept for backwards compatibility with RStudio |
| 36 |
renv_project_set <- function(project) { |
| 37 |
the$project_path <- project |
| 38 |
# https://github.com/rstudio/renv/issues/2036 |
| 39 |
options(renv.project.path = project) |
| 40 |
Sys.setenv(RENV_PROJECT = project) |
| 41 |
} |
| 42 |
|
| 43 |
# NOTE: 'RENV_PROJECT' kept for backwards compatibility with RStudio |
| 44 |
renv_project_clear <- function() { |
| 45 |
the$project_path <- NULL |
| 46 |
# https://github.com/rstudio/renv/issues/2036 |
| 47 |
options(renv.project.path = NULL) |
| 48 |
Sys.unsetenv("RENV_PROJECT") |
| 49 |
} |
| 50 |
|
| 51 |
renv_project_resolve <- function(project = NULL, default = getwd()) { |
| 52 |
project <- project %||% renv_project_get(default = default) |
| 53 |
if (is.null(project)) project else renv_path_normalize(project) |
| 54 |
} |
| 55 |
|
| 56 |
renv_project_initialized <- function(project) { |
| 57 |
|
| 58 |
lockfile <- renv_lockfile_path(project) |
| 59 |
if (file.exists(lockfile)) |
| 60 |
return(TRUE) |
| 61 |
|
| 62 |
library <- renv_paths_library(project = project) |
| 63 |
if (file.exists(library)) |
| 64 |
return(TRUE) |
| 65 |
|
| 66 |
FALSE |
| 67 |
|
| 68 |
} |
| 69 |
|
| 70 |
renv_project_type <- function(path) { |
| 71 |
|
| 72 |
if (!nzchar(path)) |
| 73 |
return("unknown") |
| 74 |
|
| 75 |
path <- renv_path_normalize(path) |
| 76 |
filebacked( |
| 77 |
context = "renv_project_type", |
| 78 |
path = file.path(path, "DESCRIPTION"), |
| 79 |
callback = renv_project_type_impl |
| 80 |
) |
| 81 |
|
| 82 |
} |
| 83 |
|
| 84 |
renv_project_type_impl <- function(path) { |
| 85 |
|
| 86 |
if (!file.exists(path)) |
| 87 |
return("unknown") |
| 88 |
|
| 89 |
desc <- tryCatch( |
| 90 |
renv_dcf_read(path), |
| 91 |
error = identity |
| 92 |
) |
| 93 |
|
| 94 |
if (inherits(desc, "error")) |
| 95 |
return("unknown") |
| 96 |
|
| 97 |
type <- desc$Type |
| 98 |
if (!is.null(type)) |
| 99 |
return(tolower(type)) |
| 100 |
|
| 101 |
package <- desc$Package |
| 102 |
if (!is.null(package)) |
| 103 |
return("package") |
| 104 |
|
| 105 |
"unknown" |
| 106 |
|
| 107 |
} |
| 108 |
|
| 109 |
renv_project_remotes <- function(project, filter = NULL, resolve = FALSE) { |
| 110 |
|
| 111 |
descpath <- file.path(project, "DESCRIPTION") |
| 112 |
if (!file.exists(descpath)) |
| 113 |
return(NULL) |
| 114 |
|
| 115 |
# find packages mentioned in the DESCRIPTION file |
| 116 |
deps <- renv_dependencies_discover_description( |
| 117 |
path = descpath, |
| 118 |
project = project |
| 119 |
) |
| 120 |
|
| 121 |
if (empty(deps)) |
| 122 |
return(list()) |
| 123 |
|
| 124 |
# split according to package |
| 125 |
specs <- split(deps, deps$Package) |
| 126 |
|
| 127 |
# drop ignored specs |
| 128 |
ignored <- renv_project_ignored_packages(project = project) |
| 129 |
specs <- specs[setdiff(names(specs), c("R", ignored))] |
| 130 |
|
| 131 |
# if any Roxygen fields are included, |
| 132 |
# infer a dependency on roxygen2 and devtools |
| 133 |
desc <- renv_description_read(descpath) |
| 134 |
if (any(grepl("^Roxygen", names(desc)))) { |
| 135 |
for (package in c("devtools", "roxygen2")) { |
| 136 |
if (!package %in% ignored) { |
| 137 |
specs[[package]] <- |
| 138 |
specs[[package]] %||% |
| 139 |
renv_dependencies_list(descpath, package, dev = TRUE) |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
# parse remotes if available |
| 145 |
remotes <- renv_description_remotes(descpath) |
| 146 |
|
| 147 |
# apply filter |
| 148 |
if (!is.null(filter)) |
| 149 |
specs <- filter(specs, remotes) |
| 150 |
|
| 151 |
# now, try to resolve the packages |
| 152 |
records <- enumerate(specs, function(package, spec) { |
| 153 |
|
| 154 |
# return a function here, so we can lazily resolve these as needed |
| 155 |
# https://github.com/rstudio/renv/issues/1755 |
| 156 |
function() { |
| 157 |
|
| 158 |
# use remote if supplied |
| 159 |
remote <- remotes[[package]] |
| 160 |
if (!is.null(remote)) { |
| 161 |
if (is.function(remote)) |
| 162 |
remote <- remote() |
| 163 |
return(remote) |
| 164 |
} |
| 165 |
|
| 166 |
# check for explicit version requirement |
| 167 |
explicit <- spec[spec$Require == "==", ] |
| 168 |
if (nrow(explicit)) { |
| 169 |
version <- explicit$Version[[1L]] |
| 170 |
if (nzchar(version)) { |
| 171 |
entry <- paste(package, version, sep = "@") |
| 172 |
return(renv_remotes_resolve(entry)) |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
# check if we're being invoked during restore or install |
| 177 |
# if so, we may want to re-use an already-existing package |
| 178 |
# https://github.com/rstudio/renv/issues/2071 |
| 179 |
packages <- renv_restore_state(key = "packages") |
| 180 |
renv_remotes_resolve(package, infer = !package %in% packages) |
| 181 |
|
| 182 |
} |
| 183 |
|
| 184 |
}) |
| 185 |
|
| 186 |
if (resolve) map(records, resolve) else records |
| 187 |
|
| 188 |
} |
| 189 |
|
| 190 |
renv_project_ignored_packages <- function(project) { |
| 191 |
|
| 192 |
# if we don't have a project, nothing to do |
| 193 |
if (is.null(project)) |
| 194 |
return(character()) |
| 195 |
|
| 196 |
# read base set of ignored packages |
| 197 |
ignored <- c( |
| 198 |
settings$ignored.packages(project = project), |
| 199 |
renv_project_ignored_packages_self(project) |
| 200 |
) |
| 201 |
|
| 202 |
# return collected set of ignored packages |
| 203 |
ignored |
| 204 |
|
| 205 |
} |
| 206 |
|
| 207 |
renv_project_ignored_packages_self <- function(project) { |
| 208 |
|
| 209 |
# only ignore self in package projects |
| 210 |
if (renv_project_type(project) != "package") |
| 211 |
return(NULL) |
| 212 |
|
| 213 |
# read current package |
| 214 |
desc <- renv_description_read(project) |
| 215 |
package <- desc[["Package"]] |
| 216 |
|
| 217 |
# respect user preference if set |
| 218 |
ignore <- getOption("renv.snapshot.ignore.self", default = NULL) |
| 219 |
if (identical(ignore, TRUE)) |
| 220 |
return(package) |
| 221 |
else if (identical(ignore, FALSE)) |
| 222 |
return(NULL) |
| 223 |
|
| 224 |
# don't ignore self in golem projets |
| 225 |
golem <- file.path(project, "inst/golem-config.yml") |
| 226 |
if (file.exists(golem)) |
| 227 |
return(NULL) |
| 228 |
|
| 229 |
# hack for renv: don't depend on self |
| 230 |
if (identical(package, "renv")) |
| 231 |
return(NULL) |
| 232 |
|
| 233 |
# return the package name |
| 234 |
package |
| 235 |
|
| 236 |
} |
| 237 |
|
| 238 |
renv_project_id <- function(project) { |
| 239 |
|
| 240 |
idpath <- renv_id_path(project = project) |
| 241 |
if (!file.exists(idpath)) { |
| 242 |
id <- renv_id_generate() |
| 243 |
writeLines(id, con = idpath) |
| 244 |
} |
| 245 |
|
| 246 |
readLines(idpath, n = 1L, warn = FALSE) |
| 247 |
|
| 248 |
} |
| 249 |
|
| 250 |
# TODO: this gets really dicey once the user starts configuring where |
| 251 |
# renv places its project-local state ... |
| 252 |
renv_project_find <- function(path = NULL) { |
| 253 |
|
| 254 |
path <- path %||% getwd() |
| 255 |
|
| 256 |
anchors <- c("renv.lock", "renv/activate.R") |
| 257 |
resolved <- renv_file_find(path, function(parent) { |
| 258 |
for (anchor in anchors) |
| 259 |
if (file.exists(file.path(parent, anchor))) |
| 260 |
return(parent) |
| 261 |
}) |
| 262 |
|
| 263 |
if (is.null(resolved)) { |
| 264 |
fmt <- "couldn't resolve renv project associated with path %s" |
| 265 |
stopf(fmt, renv_path_pretty(path)) |
| 266 |
} |
| 267 |
|
| 268 |
resolved |
| 269 |
|
| 270 |
} |
| 271 |
|
| 272 |
renv_project_lock <- function(project = NULL) { |
| 273 |
|
| 274 |
if (!config$locking.enabled()) |
| 275 |
return() |
| 276 |
|
| 277 |
path <- the$project_path |
| 278 |
if (!identical(project, path)) |
| 279 |
return() |
| 280 |
|
| 281 |
project <- renv_project_resolve(project) |
| 282 |
path <- file.path(project, "renv/lock") |
| 283 |
ensure_parent_directory(path) |
| 284 |
renv_scope_lock(path, scope = parent.frame()) |
| 285 |
|
| 286 |
} |
| 287 |
|
| 288 |
renv_project_loaded <- function(project) { |
| 289 |
!is.null(project) && identical(project, the$project_path) |
| 290 |
} |