forked from openbiox/Bizard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate-dependencies.R
More file actions
68 lines (62 loc) · 2.1 KB
/
Copy pathupdate-dependencies.R
File metadata and controls
68 lines (62 loc) · 2.1 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# scan_qmd_packages: find used R packages in .qmd files
scan_qmd_packages <- function(root = ".") {
# Skip scanning `library()` and `require()` calls, since we cannot reliably
# determine whether the packages come from CRAN, Bioconductor, or a remote
# source.
# pak::pkg_install()
# BiocManager::install() // install is a common pattern,
# remotes::install_github()
# devtools::install_gitlab()
# "\\b([a-zA-z][a-zA-z0-9]*])::"
patterns <- c(
"(?:install\\.packages|pkg_install|BiocManager::install|install_github|install_gitlab)\\((?:'|\")([a-zA-z].*)(?:'|\")\\)"
)
paths <- list.files(root,
pattern = "\\.qmd$", recursive = TRUE,
ignore.case = TRUE
)
lines <- lapply(paths, function(path) readLines(path))
lines <- unlist(lines)
deps <- lapply(patterns, function(pattern) {
pkgs <- stringr::str_match_all(lines, pattern)
unlist(
lapply(pkgs, function(mat) mat[, 2L, drop = TRUE]),
use.names = FALSE
)
})
unique(unlist(deps, use.names = FALSE))
}
used <- scan_qmd_packages()
current <- desc::desc_get_deps()$package
current <- c(
current,
tools::package_dependencies(current, recursive = TRUE)
)
current <- c(
"rmarkdown", "pak", "remotes", "devtools",
"BiocManager", unlist(current)
)
# get the package name
deps <- used
remote <- grepl("/", deps)
local_deps <- deps[!remote]
local_deps <- setdiff(local_deps, current)
local_deps <- grep("^[a-zA-Z][a-zA-Z0-9]*$", local_deps, value = TRUE)
remote_name <- deps[remote]
remote_deps <- sub("^.*/", "", remote_name)
remote_valid <- !remote_deps %in% c(local_deps, current) &
grepl("^[a-zA-Z][a-zA-Z0-9]*$", remote_deps)
remote_name <- remote_name[remote_valid]
remote_deps <- remote_deps[remote_valid]
new_deps <- c(local_deps, remote_deps)
if (length(new_deps)) {
desc::desc_set_deps(
rbind(
desc::desc_get_deps(),
data.frame(type = "Imports", package = new_deps, version = "*")
)
)
}
if (length(remote_name)) {
desc::desc_set_remotes(c(desc::desc_get_remotes(), remote_name))
}