Debian Codegraph @

R/infrastructure.R

raw
1
2 # tools for writing / removing renv-related infrastructure
3 renv_infrastructure_write <- function(project = NULL,
4 profile = NULL,
5 version = NULL)
6 {
7 # don't do anything in embedded mode
8 if (renv_metadata_embedded())
9 return()
10
11 project <- renv_project_resolve(project)
12
13 renv_infrastructure_write_profile(project, profile = profile)
14 renv_infrastructure_write_rprofile(project)
15 renv_infrastructure_write_rbuildignore(project)
16 renv_infrastructure_write_gitignore(project)
17 renv_infrastructure_write_activate(project, version = version)
18 }
19
20 renv_infrastructure_write_profile <- function(project, profile = NULL) {
21
22 path <- renv_paths_renv("profile", profile = FALSE, project = project)
23 profile <- renv_profile_normalize(profile)
24 if (is.null(profile))
25 return(unlink(path))
26
27 ensure_parent_directory(path)
28 writeLines(profile, con = path)
29
30 }
31
32 renv_infrastructure_write_rprofile <- function(project) {
33
34 if (!config$autoloader.enabled())
35 return()
36
37 # NOTE: intentionally leave project NULL to compute relative path
38 path <- renv_paths_activate(project = NULL)
39 add <- sprintf("source(%s)", renv_json_quote(path))
40
41 renv_infrastructure_write_entry_impl(
42 add = add,
43 remove = character(),
44 file = file.path(project, ".Rprofile"),
45 create = TRUE
46 )
47
48 }
49
50 renv_infrastructure_write_rbuildignore <- function(project) {
51
52 lines <- c("^renv$", "^renv\\.lock$")
53
54 if (file.exists(file.path(project, "requirements.txt")))
55 lines <- c(lines, "^requirements\\.txt$")
56
57 if (file.exists(file.path(project, "environment.yml")))
58 lines <- c(lines, "^environment\\.yml$")
59
60 renv_infrastructure_write_entry_impl(
61 add = lines,
62 remove = character(),
63 file = file.path(project, ".Rbuildignore"),
64 create = renv_project_type(project) == "package"
65 )
66
67 }
68
69 renv_infrastructure_write_gitignore <- function(project) {
70
71 if (!settings$vcs.manage.ignores())
72 return()
73
74 add <- stack(mode = "character")
75 remove <- stack(mode = "character")
76
77 stk <- if (settings$vcs.ignore.library()) add else remove
78 stk$push("library/")
79
80 stk <- if (settings$vcs.ignore.local()) add else remove
81 stk$push("local/")
82
83 stk <- if (settings$vcs.ignore.cellar()) add else remove
84 stk$push("cellar/")
85
86 add$push("lock/", "python/", "sandbox/", "staging/")
87
88 renv_infrastructure_write_entry_impl(
89 add = as.character(add$data()),
90 remove = as.character(remove$data()),
91 file = renv_paths_renv(".gitignore", project = project),
92 create = TRUE
93 )
94
95 }
96
97 renv_infrastructure_write_activate <- function(project = NULL,
98 version = NULL,
99 create = TRUE)
100 {
101 # skip when activate.R generation has been deferred; checkout() uses
102 # this so the newly-installed version of renv can write the template
103 # via a subprocess at the end, after snapshot() has finished
104 if (isTRUE(the$activate_deferred))
105 return(invisible(FALSE))
106
107 project <- renv_project_resolve(project)
108 version <- version %||% renv_activate_version(project)
109 md5 <- attr(version, "md5", exact = TRUE)
110 sha <- attr(version, "sha", exact = TRUE)
111
112 source <- system.file("resources/activate.R", package = "renv")
113 target <- renv_paths_activate(project = project)
114
115 if (!create && !file.exists(target))
116 return(FALSE)
117
118 template <- renv_file_read(source)
119 new <- renv_template_replace(
120 text = template,
121 replacements = list(
122 version = stringify(as.character(version)),
123 md5 = stringify(md5),
124 sha = stringify(sha)
125 ),
126 format = "..%s.."
127 )
128
129 if (file.exists(target)) {
130 old <- renv_file_read(target)
131 if (old == new)
132 return(TRUE)
133 }
134
135 ensure_parent_directory(target)
136 writeLines(new, con = target)
137 }
138
139
140 renv_infrastructure_write_entry_impl <- function(add, remove, file, create) {
141
142 # check to see if file doesn't exist
143 if (!file.exists(file)) {
144
145 # if we're not forcing file creation, just bail
146 if (!create)
147 return(TRUE)
148
149 # otherwise, write the file
150 ensure_parent_directory(file)
151 writeLines(add, con = file)
152 return(TRUE)
153
154 }
155
156 # if the file already has the requested line, nothing to do
157 before <- readLines(file, warn = FALSE)
158 after <- before
159
160 # add requested entries
161 for (item in rev(add)) {
162
163 # check to see if the requested line exists (either commented
164 # or uncommented). if it exists, we'll attempt to uncomment
165 # any commented lines
166 cpattern <- sprintf("^\\s*#?\\s*\\Q%s\\E\\s*(?:#|\\s*$)", item)
167 matches <- grepl(cpattern, after, perl = TRUE)
168 if (any(matches))
169 after[matches] <- gsub("^(\\s*)#\\s*", "\\1", after[matches])
170 else
171 after <- c(item, after)
172
173 }
174
175 # remove requested entries
176 for (item in rev(remove)) {
177 pattern <- sprintf("^\\s*\\Q%s\\E\\s*(?:#|\\s*$)", item)
178 matches <- grepl(pattern, after, perl = TRUE)
179 if (any(matches)) {
180 replacement <- gsub("^(\\s*)", "\\1# ", after[matches], perl = TRUE)
181 after[matches] <- replacement
182 }
183 }
184
185 # write to file if we have changes
186 if (!identical(before, after))
187 writeLines(after, con = file)
188
189 TRUE
190
191 }
192
193
194
195 renv_infrastructure_remove <- function(project = NULL) {
196 project <- renv_project_resolve(project)
197
198 renv_infrastructure_remove_rprofile(project)
199 renv_infrastructure_remove_rbuildignore(project)
200
201 unlink(file.path(project, "renv"), recursive = TRUE)
202 }
203
204
205 renv_infrastructure_remove_rprofile <- function(project) {
206
207 # NOTE: intentionally leave project NULL to compute relative path
208 path <- renv_paths_activate(project = NULL)
209 line <- sprintf("source(%s)", renv_json_quote(path))
210
211 renv_infrastructure_remove_entry_impl(
212 line = line,
213 file = file.path(project, ".Rprofile"),
214 removable = TRUE
215 )
216
217 }
218
219 renv_infrastructure_remove_rbuildignore <- function(project) {
220
221 renv_infrastructure_remove_entry_impl(
222 line = "^renv$",
223 file = file.path(project, ".Rbuildignore"),
224 removable = FALSE
225 )
226
227 }
228
229 renv_infrastructure_remove_entry_impl <- function(line, file, removable) {
230
231 # if the file doesn't exist, nothing to do
232 if (!file.exists(file))
233 return(TRUE)
234
235 # find and comment out the line
236 contents <- readLines(file, warn = FALSE)
237 pattern <- sprintf("^\\s*\\Q%s\\E\\s*(?:#|\\s*$)", line)
238 matches <- grepl(pattern, contents, perl = TRUE)
239
240 # if this file is removable, check to see if we matched all non-blank
241 # lines; if so, remove the file
242 if (removable) {
243 rest <- contents[!matches]
244 if (all(grepl("^\\s*$", rest)))
245 return(unlink(file))
246 }
247
248 # otherwise, just mutate the file
249 replacement <- gsub("^(\\s*)", "\\1# ", contents[matches], perl = TRUE)
250 contents[matches] <- replacement
251 writeLines(contents, con = file)
252
253 TRUE
254
255 }
256
257