| 1 |
|
| 2 |
#' Activate or deactivate a project |
| 3 |
#' |
| 4 |
#' @description |
| 5 |
#' `activate()` enables renv for a project in both the current session and |
| 6 |
#' in all future sessions. You should not generally need to call `activate()` |
| 7 |
#' yourself as it's called automatically by [renv::init()], which is the best |
| 8 |
#' way to start using renv in a new project. |
| 9 |
#' |
| 10 |
#' `activate()` first calls [renv::scaffold()] to set up the project |
| 11 |
#' infrastructure. Most importantly, this creates a project library and adds a |
| 12 |
#' an auto-loader to `.Rprofile` to ensure that the project library is |
| 13 |
#' automatically used for all future instances of the project. It then restarts |
| 14 |
#' the session to use that auto-loader. |
| 15 |
#' |
| 16 |
#' `deactivate()` removes the infrastructure added by `activate()`, and |
| 17 |
#' restarts the session. By default it will remove the auto-loader from the |
| 18 |
#' `.Rprofile`; use `clean = TRUE` to also delete the lockfile and the project |
| 19 |
#' library. |
| 20 |
#' |
| 21 |
#' # Temporary deactivation |
| 22 |
#' |
| 23 |
#' If you need to temporarily disable autoload activation you can set |
| 24 |
#' the `RENV_CONFIG_AUTOLOADER_ENABLED` envvar, e.g. |
| 25 |
#' `Sys.setenv(RENV_CONFIG_AUTOLOADER_ENABLED = "false")`. |
| 26 |
#' |
| 27 |
#' @inherit renv-params |
| 28 |
#' |
| 29 |
#' @export |
| 30 |
#' |
| 31 |
#' @examples |
| 32 |
#' \dontrun{ |
| 33 |
#' |
| 34 |
#' # activate the current project |
| 35 |
#' renv::activate() |
| 36 |
#' |
| 37 |
#' # activate a separate project |
| 38 |
#' renv::activate(project = "~/projects/analysis") |
| 39 |
#' |
| 40 |
#' # deactivate the currently-activated project |
| 41 |
#' renv::deactivate() |
| 42 |
#' |
| 43 |
#' } |
| 44 |
activate <- function(project = NULL, profile = NULL) { |
| 45 |
|
| 46 |
renv_consent_check() |
| 47 |
renv_scope_error_handler() |
| 48 |
|
| 49 |
project <- renv_project_resolve(project) |
| 50 |
renv_project_lock(project = project) |
| 51 |
|
| 52 |
profile <- profile %||% renv_profile_get() |
| 53 |
renv_profile_set(profile) |
| 54 |
|
| 55 |
renv_activate_impl( |
| 56 |
project = project, |
| 57 |
profile = profile, |
| 58 |
version = NULL |
| 59 |
) |
| 60 |
|
| 61 |
invisible(project) |
| 62 |
|
| 63 |
} |
| 64 |
|
| 65 |
renv_activate_impl <- function(project, |
| 66 |
profile, |
| 67 |
version = NULL, |
| 68 |
load = TRUE, |
| 69 |
restart = TRUE) |
| 70 |
{ |
| 71 |
# prepare renv infrastructure |
| 72 |
renv_infrastructure_write( |
| 73 |
project = project, |
| 74 |
profile = profile, |
| 75 |
version = version |
| 76 |
) |
| 77 |
|
| 78 |
# ensure renv is imbued into the new library path if necessary |
| 79 |
if (!renv_tests_running()) |
| 80 |
renv_imbue_self(project) |
| 81 |
|
| 82 |
# restart session if requested |
| 83 |
if (restart && !renv_tests_running()) |
| 84 |
return(renv_restart_request(project, reason = "renv activated")) |
| 85 |
|
| 86 |
if (renv_rstudio_available()) |
| 87 |
renv_rstudio_initialize(project) |
| 88 |
|
| 89 |
# try to load the project |
| 90 |
if (load) { |
| 91 |
setwd(project) |
| 92 |
load(project) |
| 93 |
} |
| 94 |
|
| 95 |
invisible(project) |
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
renv_activate_version <- function(project) { |
| 100 |
|
| 101 |
# try to get version from activate.R |
| 102 |
methods <- list( |
| 103 |
renv_activate_version_lockfile, |
| 104 |
renv_activate_version_activate, |
| 105 |
renv_activate_version_metadata |
| 106 |
) |
| 107 |
|
| 108 |
for (method in methods) { |
| 109 |
version <- catch(method(project)) |
| 110 |
if (is.character(version)) |
| 111 |
return(version) |
| 112 |
} |
| 113 |
|
| 114 |
fmt <- "failed to determine renv version for project %s" |
| 115 |
stopf(fmt, renv_path_pretty(project)) |
| 116 |
|
| 117 |
} |
| 118 |
|
| 119 |
renv_activate_version_activate <- function(project) { |
| 120 |
|
| 121 |
# get path to the activate script |
| 122 |
activate <- renv_paths_activate(project = project) |
| 123 |
if (!file.exists(activate)) |
| 124 |
return(NULL) |
| 125 |
|
| 126 |
# check for version |
| 127 |
contents <- readLines(activate, warn = FALSE) |
| 128 |
line <- grep("version <-", contents, fixed = TRUE, value = TRUE)[[1L]] |
| 129 |
version <- parse(text = line)[[1L]][[3L]] |
| 130 |
|
| 131 |
# check for md5 as well |
| 132 |
line <- grep("attr(version, \"md5\")", contents, fixed = TRUE, value = TRUE) |
| 133 |
if (length(line)) { |
| 134 |
md5 <- parse(text = line)[[1L]][[3L]] |
| 135 |
attr(version, "md5") <- md5 |
| 136 |
} |
| 137 |
|
| 138 |
# check for sha as well |
| 139 |
line <- grep("attr(version, \"sha\")", contents, fixed = TRUE, value = TRUE) |
| 140 |
if (length(line)) { |
| 141 |
sha <- parse(text = line)[[1L]][[3L]] |
| 142 |
attr(version, "sha") <- sha |
| 143 |
} |
| 144 |
|
| 145 |
version |
| 146 |
|
| 147 |
} |
| 148 |
|
| 149 |
renv_activate_version_lockfile <- function(project) { |
| 150 |
|
| 151 |
path <- renv_lockfile_path(project) |
| 152 |
if (!file.exists(path)) |
| 153 |
return(NULL) |
| 154 |
|
| 155 |
# read the renv record |
| 156 |
lockfile <- renv_lockfile_read(path) |
| 157 |
records <- renv_lockfile_records(lockfile) |
| 158 |
renv_metadata_version_create(records[["renv"]]) |
| 159 |
|
| 160 |
} |
| 161 |
|
| 162 |
renv_activate_version_metadata <- function(project) { |
| 163 |
the$metadata$version |
| 164 |
} |
| 165 |
|
| 166 |
renv_activate_prompt <- function(action, library, prompt, project) { |
| 167 |
|
| 168 |
# check whether we should ask user to activate |
| 169 |
ask <- |
| 170 |
config$activate.prompt() && |
| 171 |
prompt && |
| 172 |
interactive() && |
| 173 |
is.null(library) && |
| 174 |
!renv_project_loaded(project) && |
| 175 |
!testing() |
| 176 |
|
| 177 |
# for snapshot, since users might want to snapshot their system library |
| 178 |
# in an renv-lite configuration, only prompt if it looks like they're |
| 179 |
# working within an renv project that hasn't been loaded |
| 180 |
if ("snapshot" %in% action) { |
| 181 |
libpath <- renv_paths_library(project = project) |
| 182 |
ask <- ask && file.exists(libpath) |
| 183 |
} |
| 184 |
|
| 185 |
if (!ask) |
| 186 |
return(FALSE) |
| 187 |
|
| 188 |
renv_activate_prompt_impl(action, project) |
| 189 |
|
| 190 |
|
| 191 |
} |
| 192 |
|
| 193 |
renv_activate_prompt_impl <- function(action, project = NULL) { |
| 194 |
|
| 195 |
fmt <- "It looks like you've called renv::%s() in a project that hasn't been activated yet." |
| 196 |
title <- c(sprintf(fmt, action), "How would you like to proceed?") |
| 197 |
|
| 198 |
choices <- c( |
| 199 |
activate = "Activate the project and use the project library.", |
| 200 |
continue = "Do not activate the project and use the current library paths.", |
| 201 |
cancel = "Cancel and resolve the situation another way." |
| 202 |
) |
| 203 |
|
| 204 |
choice <- menu(choices, title, default = "continue") |
| 205 |
switch(choice, |
| 206 |
activate = { activate(project = project); TRUE }, |
| 207 |
continue = FALSE, |
| 208 |
cancel = cancel() |
| 209 |
) |
| 210 |
|
| 211 |
} |