Debian Codegraph @

R/cli.R

raw
1
2 renv_cli_install <- function(target = NULL) {
3
4 # get path to bundled tool
5 exe <- if (renv_platform_windows()) "bin/renv.bat" else "bin/renv"
6 path <- system.file(exe, package = "renv")
7
8 # copy into directory on PATH
9 target <- target %||% path.expand("~/bin/renv")
10 ensure_parent_directory(target)
11 file.copy(path, target)
12
13 writef("- renv binary copied to %s.", renv_path_pretty(target))
14 invisible(target)
15
16 }
17
18 renv_cli_exec <- function(clargs = commandArgs(trailingOnly = TRUE)) {
19 invisible(renv_cli_exec_impl(clargs))
20 }
21
22 renv_cli_exec_impl <- function(clargs) {
23
24 # check for tool called without arguments, or called with '--help'
25 usage <-
26 length(clargs) == 0 ||
27 clargs[1L] %in% c("help", "--help")
28
29 if (usage)
30 return(renv_cli_usage())
31
32 # extract method
33 method <- clargs[1L]
34
35 # check request for help on requested method
36 help <-
37 clargs[2L] %in% c("help", "--help")
38
39 if (help)
40 return(renv_cli_help(method))
41
42 # check for known function in renv
43 exports <- getNamespaceExports("renv")
44 if (!method %in% exports)
45 return(renv_cli_unknown(method, exports))
46
47 # begin building call
48 args <- list(call("::", as.symbol("renv"), as.symbol(method)))
49
50 for (clarg in clargs[-1L]) {
51
52 # convert '--no-<flag>' into a FALSE parameter
53 if (grepl("^--no-", clarg)) {
54 key <- substring(clarg, 6L)
55 args[[key]] <- FALSE
56 }
57
58 # convert '--param=value' flags
59 else if (grepl("^--[^=]+=", clarg)) {
60 index <- regexpr("=", clarg, fixed = TRUE)
61 key <- substring(clarg, 3L, index - 1L)
62 val <- substring(clarg, index + 1L)
63 args[[key]] <- renv_cli_parse(val)
64 }
65
66 # convert '--flag' into a TRUE parameter
67 else if (grepl("^--", clarg)) {
68 key <- substring(clarg, 3L)
69 args[[key]] <- TRUE
70 }
71
72 # convert 'param=value' flags
73 else if (grepl("=", clarg, fixed = TRUE)) {
74 index <- regexpr("=", clarg, fixed = TRUE)
75 key <- substring(clarg, 1L, index - 1L)
76 val <- substring(clarg, index + 1L)
77 args[[key]] <- renv_cli_parse(val)
78 }
79
80 # take other parameters as-is
81 else {
82 args[[length(args) + 1L]] <- renv_cli_parse(clarg)
83 }
84
85 }
86
87 # invoke method with parsed arguments
88 expr <- as.call(args)
89 eval(expr = expr, envir = globalenv())
90
91 }
92
93 renv_cli_usage <- function() {
94
95 usage <- "
96 Usage: renv [method] [args...]
97
98 [method] should be the name of a function exported from renv.
99 [args...] should be arguments accepted by that function.
100
101 Use renv [method] --help for more information about the associated function.
102
103 Examples:
104
105 # basic commands
106 renv init # initialize a project
107 renv snapshot # snapshot project library
108 renv restore # restore project library
109 renv status # check project status
110
111 # install a package
112 renv install dplyr
113
114 # run a script in an renv project
115 renv run path/to/script.R
116 "
117
118 writeLines(usage, con = stderr())
119
120 }
121
122 renv_cli_help <- function(method) {
123 print(help(method, package = "renv"))
124 }
125
126 renv_cli_unknown <- function(method, exports) {
127
128 # report unknown command
129 caution("renv: '%s' is not a known command.", method)
130
131 # check for similar commands
132 distance <- c(adist(method, exports))
133 names(distance) <- exports
134 n <- min(distance)
135 if (n > 2)
136 return(1L)
137
138 candidates <- names(distance)[distance == n]
139 fmt <- "did you mean %s?"
140 caution(fmt, paste(dQuote(candidates), collapse = " or "))
141 return(1L)
142
143 }
144
145 renv_cli_parse <- function(text) {
146
147 # handle logical-like values up-front
148 if (text %in% c("true", "True", "TRUE"))
149 return(TRUE)
150 else if (text %in% c("false", "False", "FALSE"))
151 return(FALSE)
152
153 # parse the expression
154 value <- parse(text = text)[[1L]]
155 if (is.language(value)) text else value
156
157 }