-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathpackage-deps.R
35 lines (27 loc) · 1.13 KB
/
package-deps.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
parse_deps <- function(string) {
if (is.null(string)) return()
stopifnot(is.character(string), length(string) == 1)
if (grepl("^\\s*$", string)) return()
# Split by commas with surrounding whitespace removed
pieces <- strsplit(string, "[[:space:]]*,[[:space:]]*")[[1]]
# Get the names
names <- gsub("\\s*\\(.*?\\)", "", pieces)
names <- gsub("^\\s+|\\s+$", "", names)
# Get the versions and comparison operators
versions_str <- pieces
have_version <- grepl("\\(.*\\)", versions_str)
versions_str[!have_version] <- NA
compare <- sub(".*\\(\\s*(\\S+)\\s+.*\\s*\\).*", "\\1", versions_str)
versions <- sub(".*\\(\\s*\\S+\\s+(\\S*)\\s*\\).*", "\\1", versions_str)
# Check that non-NA comparison operators are valid
compare_nna <- compare[!is.na(compare)]
compare_valid <- compare_nna %in% c(">", ">=", "==", "<=", "<")
if(!all(compare_valid)) {
stop("Invalid comparison operator in dependency: ",
paste(compare_nna[!compare_valid], collapse = ", "))
}
deps <- data.frame(name = names, compare = compare,
version = versions, stringsAsFactors = FALSE)
# Remove R dependency
deps[names != "R", ]
}