-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeps-cli.R
163 lines (140 loc) · 4.8 KB
/
deps-cli.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
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env Rscript
# Copy local file:
# cp inst/examples/03-cli/deps-cli.R /usr/local/bin/deps-cli
# chmod +x /usr/local/bin/deps-cli
# Or download:
# curl -fsSL https://hub.analythium.io/deps/deps-cli.R -o /usr/local/bin/deps-cli
# chmod +x /usr/local/bin/deps-cli
# Now you can use it as explained in deps-cli help, e.g.:
# deps-cli create && deps-cli sysreqs && deps-cli install
NOW <- Sys.time()
suppressMessages({
if (!requireNamespace("deps")) {
message("Installing deps's dependencies ...")
install.packages(c("rconfig", "deps", "remotes", "pak", "renv"))
}
})
CONFIG <- rconfig::rconfig()
CMD <- rconfig::command(CONFIG)
DIR <- rconfig::value(CONFIG[["dir"]], getwd())
UPGRADE <- rconfig::value(CONFIG[["upgrade"]], FALSE)
SILENT <- rconfig::value(CONFIG[["silent"]], FALSE)
FROMFILE <- rconfig::value(CONFIG[["from-file"]], FALSE)
HEADER <- '
🚀 Quickly install R package dependencies on the command line
👉 MIT (c) Analythium Solutions Inc. 2022-2025
_ _ _
__| | ___ _ __ ___ ___| (_)
/ _` |/ _ \\ \'_ \\/ __|_____ / __| | |
| (_| | __/ |_) \\__ \\_____| (__| | |
\\__,_|\\___| .__/|___/ \\___|_|_|
|_|
🔗 See https://github.com/analythium/deps
'
FOOTER <- '
🚀 Dependencies successfully installed in %s
'
OPTIONS <- '
Usage: deps-cli <command> [options]
Commands:
deps-cli help Print usage and exit
deps-cli version Print version and exit
deps-cli create Scan DIR and write dependencies.json
deps-cli sysreqs Install system requirements
deps-cli install Install R package dependencies
deps-cli all create & sysreqs & install in one go
Options:
--dir DIR Directory to scan, defaults to .
--upgrade Upgrade package dependencies
--silent Silent, no info printed
--from-file Install only from lock/dependency file
Examples:
deps-cli help
deps-cli version
deps-cli create
deps-cli create --silent
deps-cli sysreqs
deps-cli install --from-file
deps-cli all --dir /root/app --upgrade
'
help <- function(...) {
cat(OPTIONS)
invisible(NULL)
}
version <- function(...) {
ver <- read.dcf(
file = system.file("DESCRIPTION", package = "deps"),
fields=c("Version", "Date"))
cat("\ndeps-cli is based on deps", ver, "\n")
invisible(NULL)
}
create <- function(DIR, ...) {
try(deps::create(DIR, overwrite = FALSE, ask = FALSE))
invisible(NULL)
}
sysreqs <- function(DIR, ...) {
if (!file.exists(file.path(DIR, "dependencies.json"))) {
try(deps::create(DIR, output = tempdir(), ask = FALSE))
depsfile <- file.path(tempdir(), "dependencies.json")
} else {
depsfile <- file.path(DIR, "dependencies.json")
}
reqs <- jsonlite::fromJSON(depsfile)[["sysreqs"]]
if (!is.null(reqs) && length(reqs) > 0L) {
system2("apt-get", "update")
system2("apt-get", sprintf(
"install -y --no-install-recommends %s",
paste0(as.character(reqs), collapse = " ")))
system2("rm", "-rf /var/lib/apt/lists/*")
}
invisible(NULL)
}
install <- function(DIR, UPGRADE, FROMFILE, ...) {
if (file.exists(file.path(DIR, "dependencies.json"))) {
try(deps::install(DIR, upgrade = UPGRADE, ask = FALSE))
} else if (file.exists(file.path(DIR, "renv.lock"))) {
options(renv.consent = TRUE)
try(renv::restore(DIR, lockfile = 'renv.lock', prompt = FALSE))
} else if (file.exists(file.path(DIR, "pkg.lock"))) {
try(pak::lockfile_install(file.path(DIR, "pkg.lock"), update = UPGRADE))
} else if (file.exists(file.path(DIR, "DESCRIPTION"))) {
try(remotes::install_deps(DIR, upgrade = UPGRADE))
} else {
if (!FROMFILE)
try(deps::install(DIR, upgrade = UPGRADE, ask = FALSE))
}
invisible(NULL)
}
all <- function(DIR, UPGRADE, FROMFILE, ...) {
if (!file.exists(file.path(DIR, "dependencies.json")))
on.exit(unlink(file.path(DIR, "dependencies.json")))
create(DIR, ask = FALSE)
sysreqs(DIR)
install(DIR, UPGRADE, FROMFILE, ask = FALSE, ...)
invisible(NULL)
}
FUN <- list(
"help" = help,
"version" = version,
"create" = create,
"sysreqs" = sysreqs,
"install" = install,
"all" = all)
if (length(CMD) != 1L || !(CMD %in% names(FUN)))
stop("\nCommand not found, see deps-cli help for available commands.\n",
call. = FALSE)
if (!SILENT) {
cat(HEADER)
if (CMD == "help") {
version()
}
}
FUN[[CMD]](DIR, UPGRADE, FROMFILE)
if (!SILENT && CMD %in% c("sysreqs", "install", "all")) {
if (CMD != "version") {
version()
}
ELAPSED <- Sys.time() - NOW
cat(sprintf(FOOTER, format(ELAPSED, digits = 2L)))
}
quit("no", 0, FALSE)