Debian Codegraph @

R/lockfile-write.R

raw
1
2 the$lockfile_state <- new.env(parent = emptyenv())
3
4 renv_lockfile_state_get <- function(key) {
5 if (exists(key, envir = the$lockfile_state))
6 get(key, envir = the$lockfile_state, inherits = FALSE)
7 }
8
9 renv_lockfile_state_set <- function(key, value) {
10 assign(key, value, envir = the$lockfile_state, inherits = FALSE)
11 }
12
13 renv_lockfile_state_clear <- function() {
14 rm(list = ls(the$lockfile_state), envir = the$lockfile_state)
15 }
16
17 renv_lockfile_sanitize <- function(lockfile) {
18
19 # sanitize repository URLs
20 repos <- lockfile$R$Repositories
21 if (length(repos)) {
22 lockfile$R$Repositories <- lapply(repos, function(url) {
23 renv_url_sanitize(url)
24 })
25 }
26
27 # sanitize per-package Repository fields
28 packages <- lockfile$Packages
29 if (length(packages)) {
30 lockfile$Packages <- lapply(packages, function(record) {
31 repo <- record$Repository
32 if (is.character(repo) && grepl("://", repo, fixed = TRUE))
33 record$Repository <- renv_url_sanitize(repo)
34 record
35 })
36 }
37
38 lockfile
39
40 }
41
42 renv_lockfile_write_preflight <- function(old, new) {
43
44 # sanitize repository URLs (remove embedded credentials)
45 sanitize <- settings$lockfile.sanitize(project = renv_project_resolve())
46 if (sanitize)
47 new <- renv_lockfile_sanitize(new)
48
49 if (is.null(old))
50 return(new)
51
52 diff <- renv_lockfile_diff(old, new)
53 if (empty(diff))
54 return(new)
55
56 packages <- diff$Packages
57 if (empty(diff$Packages))
58 return(new)
59
60 enumerate(packages, function(package, changes) {
61
62 # avoid spurious changes
63 lhs <- "CRAN"
64 for (rhs in c("RSPM", "PPM", "P3M")) {
65
66 spurious <-
67 identical(changes, list(Repository = list(before = lhs, after = rhs))) ||
68 identical(changes, list(Repository = list(before = rhs, after = lhs)))
69
70 if (spurious)
71 new$Packages[[package]]$Repository <<- old$Packages[[package]]$Repository
72
73 }
74
75 })
76
77 new
78
79 }
80
81 renv_lockfile_write <- function(lockfile, file = stdout()) {
82
83 # if we're updating an existing lockfile, try to avoid
84 # "unnecessary" diffs that might otherwise be annoying
85 old <- NULL
86 if (is.character(file) && file.exists(file)) {
87 old <- catch(renv_lockfile_read(file))
88 if (inherits(old, "error"))
89 old <- NULL
90 }
91
92 lockfile <- renv_lockfile_write_preflight(old, lockfile)
93
94 lockfile <- renv_lockfile_sort(lockfile)
95 result <- renv_lockfile_write_json(lockfile, file)
96
97 if (is.character(file))
98 writef("- Lockfile written to %s.", renv_path_pretty(file))
99
100 result
101
102 }
103
104 renv_lockfile_write_json_prepare_repos <- function(repos) {
105
106 prepared <- enumerate(repos, function(name, url) {
107 url <- sub("/+$", "", url)
108 list(Name = name, URL = url)
109 })
110
111 unname(prepared)
112 }
113
114 renv_lockfile_write_json_prepare <- function(key, val) {
115
116 if (key == "Repositories")
117 renv_lockfile_write_json_prepare_repos(val)
118 else if (is.list(val) && !is.null(names(val)))
119 enumerate(val, renv_lockfile_write_json_prepare)
120 else
121 val
122
123 }
124
125 renv_lockfile_write_json <- function(lockfile, file = stdout()) {
126
127 prepared <- enumerate(lockfile, renv_lockfile_write_json_prepare)
128
129 box <- c("Requirements")
130 config <- list(box = box)
131
132 json <- renv_json_convert(prepared, config)
133 if (is.null(file))
134 return(json)
135
136 writeLines(json, con = file)
137
138 }
139
140 renv_lockfile_write_list <- function(entry, section) {
141 enumerate(entry, renv_lockfile_write_atoms, section = section)
142 enumerate(entry, renv_lockfile_write_lists, section = section)
143 }
144
145 renv_lockfile_write_atoms <- function(key, value, section) {
146
147 sublists <- map_lgl(value, function(x) identical(class(x), "list"))
148 if (all(sublists))
149 return()
150
151 subsection <- c(section, key)
152 label <- sprintf("[%s]", paste(subsection, collapse = "/"))
153 renv_lockfile_write_emit(label)
154
155 enumerate(value[!sublists], renv_lockfile_write_atom)
156 renv_lockfile_write_emit()
157
158 }
159
160 renv_lockfile_write_atom <- function(key, value) {
161
162 lhs <- key
163 rhs <- if (is_named(value))
164 paste(sprintf("\n\t%s=%s", names(value), value), collapse = "")
165 else
166 paste(value, collapse = ", ")
167
168 delim <- renv_lockfile_state_get("delim")
169 text <- paste(lhs, rhs, sep = delim)
170 renv_lockfile_write_emit(text)
171
172 }
173
174 renv_lockfile_write_lists <- function(key, value, section) {
175 sublists <- map_lgl(value, function(x) identical(class(x), "list"))
176 renv_lockfile_write_list(value[sublists], section = c(section, key))
177 }
178
179 renv_lockfile_write_emit <- function(text = "") {
180 emitter <- renv_lockfile_state_get("emitter")
181 emitter(text)
182 }