-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
integration.R
120 lines (112 loc) · 4.47 KB
/
integration.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
#' Enable/Disable Bridge to System Package Manager
#'
#' Functions to enable or disable the integration of \code{\link{install_sys}}
#' into \code{\link{install.packages}}. When enabled, packages are installed
#' transparently from system repositories if available, including dependencies,
#' and from the configured \R repositories if not.
#'
#' @details To enable \pkg{bspm} system-wide by default, include the following:
#'
#' \code{suppressMessages(bspm::enable())}
#'
#' into the \code{Rprofile.site} file. To enable it just for a particular user,
#' move that line to the user's \code{~/.Rprofile} instead.
#'
#' By default, enabling \pkg{bspm} triggers a check of the backend, and a
#' warning is raised if the system service is required but not available. To
#' avoid this check, \code{options(bspm.backend.check=FALSE)} can be set.
#'
#' Enabling \pkg{bspm} sets default installation \code{type} to \code{"both"},
#' which means 'use binary if available and current, otherwise try source'.
#' The action if there are source packages which are preferred is controlled by
#' \code{getOption("install.packages.compile.from.source")}. Set this option to
#' \code{"never"} to always prefer binaries over source packages.
#'
#' @seealso \code{\link{manager}}
#'
#' @examples
#' \dontrun{
#' # install 'units' and all its dependencies from the system repos
#' bspm::enable()
#' install.packages("units")
#'
#' # install packages again from CRAN
#' bspm::disable()
#' install.packages("errors")
#' }
#'
#' @name integration
#' @export
enable <- function() {
if (getOption("bspm.backend.check", TRUE))
backend_check()
options(pkgType="both")
trace(utils::install.packages, print=FALSE, tracer=quote({
if (missing(pkgs)) stop("no packages were specified")
if (is.null(repos)) {
type <- "source"
} else if (type == "both") {
# get pkgs with non-installed dependencies
dbs <- available.packages(type="source")
inst <- row.names(installed.packages(.Library.site))
pkgs <- tools::package_dependencies(pkgs, dbs, recursive=TRUE)
pkgs <- lapply(pkgs, function(x) setdiff(x, inst))
pkgs <- unique(c(names(pkgs), unlist(pkgs, use.names=FALSE)))
# get available binaries and pkgs with later versions available
dbb <- bspm::available_sys()
row.names(dbb) <- tolower(row.names(dbb))
bins <- pkgs[tolower(pkgs) %in% row.names(dbb)]
srcs <- pkgs[! pkgs %in% bins]
binvers <- dbb[tolower(bins), "Version"]
srcvers <- dbs[bins, "Version"]
later <- as.numeric_version(binvers) < srcvers
# determine whether later versions should be installed
if (any(later)) {
msg <- ngettext(
sum(later),
"There is a binary version available but the source version is later",
"There are binary versions available but the source versions are later")
cat("\n", paste(strwrap(msg, indent = 2, exdent = 2), collapse = "\n"),
":\n", sep = "")
print(data.frame(`binary` = binvers, `source` = srcvers,
row.names = bins, check.names = FALSE)[later, ])
cat("\n")
action <- getOption("install.packages.compile.from.source", "interactive")
if (action == "interactive" && interactive()) {
msg <- gettext("Do you want to install later versions from sources?")
res <- utils::askYesNo(msg)
if (is.na(res)) stop("Cancelled by user")
if (!isTRUE(res)) later <- FALSE
} else if (action == "never") {
cat(" Binaries will be installed\n")
later <- FALSE
}
}
# install binaries and forward the rest
pkgs <- c(bspm::install_sys(bins[!later]), bins[later], srcs)
type <- "source"
} else if (type == "binary") {
# try just binaries and fail otherwise
if (!length(pkgs <- bspm::install_sys(pkgs)))
type <- "source"
} else if (type == "binary-source") {
# install as many binaries as possible and fallback to source
if (length(pkgs <- bspm::install_sys(pkgs))) {
inst <- row.names(installed.packages(.Library.site))
deps <- tools::package_dependencies(pkgs, recursive=TRUE)
deps <- lapply(deps, function(x) setdiff(x, inst))
deps <- unique(unlist(deps, use.names=FALSE))
if (length(deps)) bspm::install_sys(deps)
}
type <- "source"
}
}))
invisible()
}
#' @name integration
#' @export
disable <- function() {
options(pkgType="source")
untrace(utils::install.packages)
invisible()
}