| 1 |
|
| 2 |
#' User-level settings |
| 3 |
#' |
| 4 |
#' Configure different behaviors of renv. |
| 5 |
#' |
| 6 |
#' For a given configuration option: |
| 7 |
#' |
| 8 |
#' 1. If an \R option of the form `renv.config.<name>` is available, |
| 9 |
#' then that option's value will be used; |
| 10 |
#' |
| 11 |
#' 2. If an environment variable of the form `RENV_CONFIG_<NAME>` is available, |
| 12 |
#' then that option's value will be used; |
| 13 |
#' |
| 14 |
#' 3. Otherwise, the default for that particular configuration value is used. |
| 15 |
#' |
| 16 |
#' Any periods (`.`)s in the option name are transformed into underscores (`_`) |
| 17 |
#' in the environment variable name, and vice versa. For example, the |
| 18 |
#' configuration option `auto.snapshot` could be configured as: |
| 19 |
#' |
| 20 |
#' - `options(renv.config.auto.snapshot = <...>)` |
| 21 |
#' - `Sys.setenv(RENV_CONFIG_AUTO_SNAPSHOT = <...>)` |
| 22 |
#' |
| 23 |
#' Note that if both the \R option and the environment variable are defined, the |
| 24 |
#' \R option will be used instead. Environment variables can be more useful when |
| 25 |
#' you want a particular configuration to be automatically inherited by child |
| 26 |
#' processes; if that behavior is not desired, then the R option may be |
| 27 |
#' preferred. |
| 28 |
#' |
| 29 |
#' If you want to set and persist these options across multiple projects, it is |
| 30 |
#' recommended that you set them in a a startup `.Renviron` file; e.g. in your |
| 31 |
#' own `~/.Renviron`, or in the R installation's `etc/Rprofile.site` file. See |
| 32 |
#' [Startup] for more details. |
| 33 |
#' |
| 34 |
#' Configuration options can also be set within the project `.Rprofile`, but |
| 35 |
#' be aware the options should be set before `source("renv/activate.R")` is |
| 36 |
#' called. |
| 37 |
#' |
| 38 |
#' @eval renv_roxygen_config_section() |
| 39 |
#' |
| 40 |
#' @section Copy methods: |
| 41 |
#' |
| 42 |
#' If you find that renv is unable to copy some directories in your |
| 43 |
#' environment, you may want to try setting the `copy.method` option. By |
| 44 |
#' default, renv will try to choose a system tool that is likely to succeed in |
| 45 |
#' copying files on your system -- `robocopy` on Windows, and `cp` on Unix. |
| 46 |
#' renv will also instruct these tools to preserve timestamps and attributes |
| 47 |
#' when copying files. However, you can select a different method as |
| 48 |
#' appropriate. |
| 49 |
#' |
| 50 |
#' The following methods are supported: |
| 51 |
#' |
| 52 |
#' \tabular{ll}{ |
| 53 |
#' `auto` \tab Use `robocopy` on Windows, and `cp` on Unix-alikes. \cr |
| 54 |
#' `R` \tab Use \R's built-in `file.copy()` function. \cr |
| 55 |
#' `cp` \tab Use `cp` to copy files. \cr |
| 56 |
#' `robocopy` \tab Use `robocopy` to copy files. (Only available on Windows.) \cr |
| 57 |
#' `rsync` \tab Use `rsync` to copy files. \cr |
| 58 |
#' } |
| 59 |
#' |
| 60 |
#' You can also provide a custom copy method if required; e.g. |
| 61 |
#' |
| 62 |
#' ``` |
| 63 |
#' options(renv.config.copy.method = function(src, dst) { |
| 64 |
#' # copy a file from 'src' to 'dst' |
| 65 |
#' }) |
| 66 |
#' ``` |
| 67 |
#' |
| 68 |
#' Note that renv will always first attempt to copy a directory first to a |
| 69 |
#' temporary path within the target folder, and then rename that temporary path |
| 70 |
#' to the final target destination. This helps avoid issues where a failed |
| 71 |
#' attempt to copy a directory could leave a half-copied directory behind |
| 72 |
#' in the final location. |
| 73 |
#' |
| 74 |
#' @section Project-local settings: |
| 75 |
#' |
| 76 |
#' For settings that should persist alongside a particular project, the |
| 77 |
#' various settings available in [settings] can be used. |
| 78 |
#' |
| 79 |
#' @examples |
| 80 |
#' |
| 81 |
#' # disable automatic snapshots |
| 82 |
#' options(renv.config.auto.snapshot = FALSE) |
| 83 |
#' |
| 84 |
#' # disable with environment variable |
| 85 |
#' Sys.setenv(RENV_CONFIG_AUTO_SNAPSHOT = FALSE) |
| 86 |
#' |
| 87 |
#' @rdname config |
| 88 |
#' @name config |
| 89 |
NULL |
| 90 |
|
| 91 |
renv_config_get <- function(name, |
| 92 |
scope = "config", |
| 93 |
type = "*", |
| 94 |
default = NULL, |
| 95 |
args = NULL) |
| 96 |
{ |
| 97 |
# check for R option of associated name |
| 98 |
optname <- tolower(name) |
| 99 |
optkey <- paste("renv", scope, optname, sep = ".") |
| 100 |
optval <- getOption(optkey) |
| 101 |
if (!is.null(optval)) |
| 102 |
return(renv_config_validate(name, optval, type, default, args)) |
| 103 |
|
| 104 |
# check for environment variable |
| 105 |
envname <- chartr(".", "_", toupper(name)) |
| 106 |
envkey <- paste("RENV", toupper(scope), envname, sep = "_") |
| 107 |
envval <- Sys.getenv(envkey, unset = NA) |
| 108 |
if (!is.na(envval) && nzchar(envval)) { |
| 109 |
decoded <- renv_config_decode_envvar(envkey, envval) |
| 110 |
return(renv_config_validate(name, decoded, type, default, args)) |
| 111 |
} |
| 112 |
|
| 113 |
# return default if nothing found |
| 114 |
default |
| 115 |
|
| 116 |
} |
| 117 |
|
| 118 |
renv_config_decode_envvar <- function(envname, envval) { |
| 119 |
|
| 120 |
map <- env( |
| 121 |
"NULL" = NULL, |
| 122 |
"NA" = NA, |
| 123 |
"NaN" = NaN, |
| 124 |
"true" = TRUE, |
| 125 |
"True" = TRUE, |
| 126 |
"TRUE" = TRUE, |
| 127 |
"false" = FALSE, |
| 128 |
"False" = FALSE, |
| 129 |
"FALSE" = FALSE |
| 130 |
) |
| 131 |
|
| 132 |
if (exists(envval, envir = map, inherits = FALSE)) |
| 133 |
return(get(envval, envir = map, inherits = FALSE)) |
| 134 |
|
| 135 |
libvars <- c("RENV_CONFIG_EXTERNAL_LIBRARIES", "RENV_CONFIG_HYDRATE_LIBPATHS") |
| 136 |
if (envname %in% libvars) { |
| 137 |
decoded <- renv_config_decode_libpaths(envval) |
| 138 |
return(decoded) |
| 139 |
} |
| 140 |
|
| 141 |
# handle repos override with KEY=VAL;KEY2=VAL2 format |
| 142 |
if (identical(envname, "RENV_CONFIG_REPOS_OVERRIDE")) { |
| 143 |
decoded <- renv_config_decode_keyval(envval) |
| 144 |
return(decoded) |
| 145 |
} |
| 146 |
|
| 147 |
strsplit(envval, "\\s*,\\s*", perl = TRUE)[[1L]] |
| 148 |
|
| 149 |
} |
| 150 |
|
| 151 |
renv_config_decode_libpaths <- function(envval) { |
| 152 |
|
| 153 |
# get the location of potential split delimiters |
| 154 |
pattern <- "\\s*[:;,]\\s*" |
| 155 |
indices <- gregexpr(pattern, envval, perl = TRUE)[[1L]] |
| 156 |
if (identical(c(indices), -1L)) |
| 157 |
return(envval) |
| 158 |
|
| 159 |
# drop delimiters that are a distance of 2 from previous |
| 160 |
# https://github.com/rstudio/renv/issues/2069 |
| 161 |
diffs <- diff(c(0L, indices)) |
| 162 |
indices <- indices[diffs != 2L] |
| 163 |
|
| 164 |
# split at the discovered indices |
| 165 |
starts <- c(1L, indices + 1L) |
| 166 |
ends <- c(indices - 1L, nchar(envval)) |
| 167 |
substring(envval, starts, ends) |
| 168 |
|
| 169 |
} |
| 170 |
|
| 171 |
renv_config_decode_keyval <- function(envval) { |
| 172 |
|
| 173 |
# split on ';' if present |
| 174 |
parts <- strsplit(envval, ";", fixed = TRUE)[[1L]] |
| 175 |
|
| 176 |
# split into named repositories if present |
| 177 |
idx <- regexpr("=", parts, fixed = TRUE) |
| 178 |
keys <- substring(parts, 1L, idx - 1L) |
| 179 |
vals <- substring(parts, idx + 1L) |
| 180 |
names(vals) <- keys |
| 181 |
|
| 182 |
# if we have a single unnamed repository, call it CRAN |
| 183 |
if (length(vals) == 1L && identical(keys, "")) |
| 184 |
names(vals) <- "CRAN" |
| 185 |
|
| 186 |
vals |
| 187 |
|
| 188 |
} |
| 189 |
|
| 190 |
renv_config_validate <- function(name, value, type, default, args) { |
| 191 |
|
| 192 |
# no validation required for type = '*' |
| 193 |
if (identical(type, "*")) |
| 194 |
return(value) |
| 195 |
|
| 196 |
# if 'value' is a function, invoke it with args |
| 197 |
if (is.function(value)) { |
| 198 |
value <- catch(do.call(value, args)) |
| 199 |
if (inherits(value, "error")) { |
| 200 |
warning(value, call. = FALSE) |
| 201 |
return(default) |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
# parse the type string |
| 206 |
pattern <- paste0( |
| 207 |
"^", # start of specifier |
| 208 |
"([^[(]+)", # type name |
| 209 |
"[[(]", # opening bracket |
| 210 |
"([^])]+)", # length specifier |
| 211 |
"[])]", # closing bracket |
| 212 |
"$" # end of specifier |
| 213 |
) |
| 214 |
|
| 215 |
m <- regexec(pattern, type) |
| 216 |
matches <- regmatches(type, m) |
| 217 |
fields <- matches[[1L]] |
| 218 |
|
| 219 |
# extract declared mode, size |
| 220 |
mode <- fields[[2L]] |
| 221 |
size <- fields[[3L]] |
| 222 |
|
| 223 |
# validate the requested size for this option |
| 224 |
if (!renv_config_validate_size(value, size)) { |
| 225 |
fmt <- "value for option '%s' does not satisfy constraint '%s'" |
| 226 |
warningf(fmt, name, type) |
| 227 |
} |
| 228 |
|
| 229 |
# convert NULL values to requested type |
| 230 |
if (is.null(value)) { |
| 231 |
value <- convert(value, mode) |
| 232 |
return(value) |
| 233 |
} |
| 234 |
|
| 235 |
# otherwise, validate that this is a valid option |
| 236 |
if (identical(storage.mode(value), mode)) |
| 237 |
return(value) |
| 238 |
|
| 239 |
# try converting |
| 240 |
converted <- catchall(convert(value, mode)) |
| 241 |
if (any(is.na(converted)) || inherits(converted, "condition")) { |
| 242 |
fmt <- "'%s' does not satisfy constraint '%s' for config '%s'; using default '%s' instead" |
| 243 |
warningf(fmt, stringify(value), type, name, stringify(default)) |
| 244 |
return(default) |
| 245 |
} |
| 246 |
|
| 247 |
# ok, validated + converted option |
| 248 |
converted |
| 249 |
|
| 250 |
} |
| 251 |
|
| 252 |
renv_config_validate_size <- function(value, size) { |
| 253 |
|
| 254 |
case( |
| 255 |
size == "*" ~ TRUE, |
| 256 |
size == "+" ~ length(value) > 0, |
| 257 |
size == "?" ~ length(value) %in% c(0, 1), |
| 258 |
TRUE ~ as.numeric(size) == length(value) |
| 259 |
) |
| 260 |
|
| 261 |
} |
| 262 |
|
| 263 |
renv_config_install_staged <- function(default = TRUE) { |
| 264 |
|
| 265 |
values <- c( |
| 266 |
config$install.staged(default = NULL), |
| 267 |
config$install.transactional(default = NULL), |
| 268 |
default |
| 269 |
) |
| 270 |
|
| 271 |
values[[1L]] |
| 272 |
|
| 273 |
} |