Debian Codegraph @

R/lockfile-read.R

raw
1
2 renv_lockfile_read_finish_impl <- function(key, val) {
3
4 # convert repository records to named vectors
5 # (be careful to handle NAs, NULLs)
6 if (identical(key, "Repositories") && is.null(names(val))) {
7
8 getter <- function(name) function(record) record[[name]] %||% "" %NA% ""
9 keys <- map_chr(val, getter("Name"))
10 vals <- map_chr(val, getter("URL"))
11
12 result <- case(
13 empty(keys) ~ list(),
14 any(nzchar(keys)) ~ named(vals, keys),
15 TRUE ~ vals
16 )
17
18 return(as.list(result))
19
20 }
21
22 # convert the "Requirements" field to a character vector
23 if (identical(key, "Requirements"))
24 return(unlist(val))
25
26 # recurse for lists
27 if (is.list(val))
28 return(enumerate(val, renv_lockfile_read_finish_impl))
29
30 # return other values as-is
31 val
32
33 }
34
35 renv_lockfile_read_finish <- function(data) {
36
37 # create lockfile
38 lockfile <- enumerate(data, renv_lockfile_read_finish_impl)
39 class(lockfile) <- "renv_lockfile"
40
41 # compute hashes for records if possible
42 renv_lockfile_records(lockfile) <-
43 renv_lockfile_records(lockfile) %>%
44 map(function(record) {
45
46 record$Hash <- record$Hash %||% {
47 fields <- renv_hash_fields_remotes(record)
48 if (all(names(record) %in% fields))
49 renv_hash_record(record)
50 }
51
52 record
53
54 })
55
56 # return lockfile
57 lockfile
58 }
59
60 renv_lockfile_read_preflight <- function(contents) {
61
62 # check for merge conflict markers
63 starts <- grep("^[<]+", contents)
64 ends <- grep("^[>]+", contents)
65
66 hasconflicts <-
67 length(starts) &&
68 length(ends) &&
69 length(starts) == length(ends)
70
71 if (hasconflicts) {
72
73 parts <- .mapply(function(start, end) {
74 c(contents[start:end], "")
75 }, list(starts, ends), NULL)
76
77 all <- unlist(parts, recursive = TRUE, use.names = FALSE)
78
79 bulletin(
80 "The lockfile contains one or more merge conflict markers:",
81 head(all, n = -1L),
82 "You will need to resolve these merge conflicts before the file can be read."
83 )
84
85 stop("lockfile contains merge conflict markers; cannot proceed", call. = FALSE)
86
87 }
88
89 }
90
91 renv_lockfile_read <- function(file = NULL, text = NULL) {
92
93 # read the lockfile
94 contents <- if (is.null(file))
95 unlist(strsplit(text, "\n", fixed = TRUE))
96 else
97 readLines(file, warn = FALSE, encoding = "UTF-8")
98
99 # check and report some potential errors (e.g. merge conflicts)
100 renv_lockfile_read_preflight(contents)
101 withCallingHandlers(
102 json <- renv_json_read(text = contents),
103 error = function(err) {
104 stop("Failed to parse 'renv.lock':\n", conditionMessage(err))
105 }
106 )
107
108 renv_lockfile_read_finish(json)
109
110 }