Debian Codegraph @

R/shims.R

raw
1
2 the$shims <- new.env(parent = emptyenv())
3
4 # determine whether we can safely handle a call to install.packages()
5 renv_shim_install_packages_compatible <- function(matched) {
6
7 # check if the user has only specified arguments which we know how to handle
8 ok <- c("", "dependencies", "pkgs", "lib", "repos", "type")
9 unhandled <- setdiff(names(matched), ok)
10 if (length(unhandled) != 0L)
11 return(FALSE)
12
13 # if 'repos' is explicitly NULL, assume this is a request for local install
14 if ("repos" %in% names(matched) && is.null(matched[["repos"]]))
15 return(FALSE)
16
17 # ok, we can handle it
18 TRUE
19
20 }
21
22 renv_shim_install_packages <- function(pkgs, ...) {
23
24 # place Rtools on PATH
25 renv_scope_rtools()
26
27 # check for compatible calls
28 matched <- match.call(utils::install.packages)
29 if (!renv_shim_install_packages_compatible(matched)) {
30 call <- sys.call()
31 call[[1L]] <- quote(utils::install.packages)
32 if (testing())
33 call[["quiet"]] <- TRUE
34 return(eval(call, envir = parent.frame()))
35 }
36
37 # otherwise, invoke our own installer
38 call <- sys.call()
39 call[[1L]] <- quote(renv::install)
40
41 # fix up names
42 aliases <- list(lib = "library")
43 idx <- omit_if(match(names(aliases), names(call)), is.na)
44 names(call)[idx] <- aliases[idx]
45
46 # evaluate call
47 eval(call, envir = parent.frame())
48
49 }
50
51 renv_shim_update_packages <- function(lib.loc = NULL, ...) {
52
53 # handle only 0-argument case
54 if (nargs() != 0) {
55 call <- sys.call()
56 call[[1L]] <- quote(utils::update.packages)
57 return(eval(call, envir = parent.frame()))
58 }
59
60 update(library = lib.loc)
61
62 }
63
64 renv_shim_remove_packages <- function(pkgs, lib) {
65
66 # handle single-argument case
67 if (nargs() != 1) {
68 call <- sys.call()
69 call[[1L]] <- quote(utils::remove.packages)
70 if (testing())
71 return(suppressMessages(eval(call, envir = parent.frame())))
72 return(eval(call, envir = parent.frame()))
73 }
74
75 remove(pkgs)
76
77 }
78
79 renv_shim_create <- function(shim, sham) {
80 formals(shim) <- formals(sham)
81 shim
82 }
83
84 renv_shims_enabled <- function(project) {
85 config$shims.enabled()
86 }
87
88 renv_shims_activate <- function() {
89
90 renv_shims_deactivate()
91
92 install_shim <- renv_shim_create(renv_shim_install_packages, utils::install.packages)
93 assign("install.packages", install_shim, envir = the$shims)
94
95 update_shim <- renv_shim_create(renv_shim_update_packages, utils::update.packages)
96 assign("update.packages", update_shim, envir = the$shims)
97
98 remove_shim <- renv_shim_create(renv_shim_remove_packages, utils::remove.packages)
99 assign("remove.packages", remove_shim, envir = the$shims)
100
101 args <- list(the$shims, name = "renv:shims", warn.conflicts = FALSE)
102 do.call(base::attach, args)
103
104 }
105
106 renv_shims_deactivate <- function() {
107 while ("renv:shims" %in% search())
108 detach("renv:shims")
109 }