Debian Codegraph @

R/consent.R

raw
1
2 #' Consent to usage of renv
3 #'
4 #' Provide consent to renv, allowing it to write and update certain files
5 #' on your filesystem.
6 #'
7 #' As part of its normal operation, renv will write and update some files
8 #' in your project directory, as well as an application-specific cache
9 #' directory. These paths are documented within [paths].
10 #'
11 #' In accordance with the
12 #' [CRAN Repository Policy](https://cran.r-project.org/web/packages/policies.html),
13 #' renv must first obtain consent from you, the user, before these actions
14 #' can be taken. Please call `renv::consent()` first to provide this consent.
15 #'
16 #' You can also set the \R option:
17 #'
18 #' ```
19 #' options(renv.consent = TRUE)
20 #' ```
21 #'
22 #' to implicitly provide consent for e.g. non-interactive \R sessions.
23 #'
24 #' @param provided The default provided response. If you need to provide
25 #' consent from a non-interactive \R session, you can invoke
26 #' `renv::consent(provided = TRUE)` explicitly.
27 #'
28 #' @return `TRUE` if consent is provided, or an \R error otherwise.
29 #'
30 #' @export
31 consent <- function(provided = FALSE) {
32
33 # assume consent if embedded
34 if (renv_metadata_embedded())
35 return(TRUE)
36
37 # compute path to root directory
38 root <- renv_paths_root()
39 if (renv_file_type(root) == "directory") {
40 writef("- Consent to use renv has already been provided -- nothing to do.")
41 return(invisible(TRUE))
42 }
43
44 # write welcome message
45 template <- system.file("resources/WELCOME", package = "renv")
46 contents <- readLines(template)
47 replacements <- list(RENV_PATHS_ROOT = renv_path_pretty(root))
48 welcome <- renv_template_replace(contents, replacements)
49 writef(welcome)
50
51 # ask user if they want to proceed
52 response <- catchall(proceed(default = provided))
53 if (!identical(response, TRUE)) {
54 msg <- "consent was not provided; operation aborted"
55 stop(msg, call. = FALSE)
56 }
57
58 # cache the user response
59 options(renv.consent = TRUE)
60 ensure_directory(root)
61 writef("- %s has been created.", renv_path_pretty(root))
62
63 invisible(TRUE)
64
65 }
66
67 renv_consent_check <- function() {
68
69 # check for explicit consent
70 consent <- getOption("renv.consent")
71 if (identical(consent, TRUE))
72 return(TRUE)
73 else if (identical(consent, FALSE))
74 stopf("consent has been explicitly withdrawn")
75
76 # check for existence of root
77 root <- renv_paths_root()
78 if (renv_file_type(root) == "directory")
79 return(TRUE)
80
81 # check for implicit consent
82 consented <-
83 !interactive() ||
84 renv_envvar_exists("CI") ||
85 renv_envvar_exists("GITHUB_ACTION") ||
86 renv_envvar_exists("RENV_PATHS_ROOT") ||
87 file.exists("/.singularity.d") ||
88 renv_virtualization_type() != "native"
89
90 if (consented) {
91 ensure_directory(root)
92 return(TRUE)
93 }
94
95 # looks like the user's first interactive use of renv
96 consent()
97
98 }