-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathinstall-remote.R
More file actions
241 lines (208 loc) · 7.25 KB
/
Copy pathinstall-remote.R
File metadata and controls
241 lines (208 loc) · 7.25 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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#' Install a remote package.
#'
#' This:
#' \enumerate{
#' \item downloads source bundle
#' \item decompresses & checks that it's a package
#' \item adds metadata to DESCRIPTION
#' \item calls install
#' }
#' @noRd
install_remote <- function(remote,
dependencies,
upgrade,
force,
quiet,
build,
build_opts,
repos,
type,
...) {
stopifnot(is.remote(remote))
package_name <- remote_package_name(remote)
local_sha <- local_sha(package_name)
remote_sha <- remote_sha(remote, local_sha, ...)
if (!isTRUE(force) &&
!different_sha(remote_sha = remote_sha, local_sha = local_sha)) {
if (!quiet) {
message(
"Skipping install of '", package_name, "' from a ", sub("_remote", "", class(remote)[1L]), " remote,",
" the SHA1 (", substr(remote_sha, 1L, 8L), ") has not changed since last install.\n",
" Use `force = TRUE` to force installation")
}
return(invisible(package_name))
}
if (inherits(remote, "cran_remote")) {
install_packages(
package_name, repos = remote$repos, type = remote$pkg_type,
dependencies = dependencies,
quiet = quiet,
...)
return(invisible(package_name))
}
bundle <- remote_download(remote, quiet = quiet)
on.exit(unlink(bundle), add = TRUE)
source <- source_pkg(bundle, subdir = remote$subdir)
on.exit(unlink(source, recursive = TRUE), add = TRUE)
update_submodules(source, quiet)
add_metadata(source, remote_metadata(remote, bundle, source, remote_sha))
# Because we've modified DESCRIPTION, its original MD5 value is wrong
clear_description_md5(source)
install(source,
dependencies = dependencies,
upgrade = upgrade,
force = force,
quiet = quiet,
build = build,
build_opts = build_opts,
repos = repos,
type = type,
...)
}
install_remotes <- function(remotes, ...) {
invisible(vapply(remotes, install_remote, ..., FUN.VALUE = character(1)))
}
# Add metadata
add_metadata <- function(pkg_path, meta) {
# During installation, the DESCRIPTION file is read and an package.rds file
# created with most of the information from the DESCRIPTION file. Functions
# that read package metadata may use either the DESCRIPTION file or the
# package.rds file, therefore we attempt to modify both of them
source_desc <- file.path(pkg_path, "DESCRIPTION")
binary_desc <- file.path(pkg_path, "Meta", "package.rds")
if (file.exists(source_desc)) {
desc <- read_dcf(source_desc)
desc <- utils::modifyList(desc, meta)
write_dcf(source_desc, desc)
}
if (file.exists(binary_desc)) {
pkg_desc <- base::readRDS(binary_desc)
desc <- as.list(pkg_desc$DESCRIPTION)
desc <- utils::modifyList(desc, meta)
pkg_desc$DESCRIPTION <- stats::setNames(as.character(desc), names(desc))
base::saveRDS(pkg_desc, binary_desc)
}
}
# Modify the MD5 file - remove the line for DESCRIPTION
clear_description_md5 <- function(pkg_path) {
path <- file.path(pkg_path, "MD5")
if (file.exists(path)) {
text <- readLines(path)
text <- text[!grepl(".*\\*DESCRIPTION$", text)]
writeLines(text, path)
}
}
remote <- function(type, ...) {
structure(list(...), class = c(paste0(type, "_remote"), "remote"))
}
is.remote <- function(x) inherits(x, "remote")
remote_download <- function(x, quiet = FALSE) UseMethod("remote_download")
remote_metadata <- function(x, bundle = NULL, source = NULL, sha = NULL) UseMethod("remote_metadata")
remote_package_name <- function(remote, ...) UseMethod("remote_package_name")
remote_sha <- function(remote, ...) UseMethod("remote_sha")
remote_package_name.default <- function(remote, ...) remote$repo
remote_sha.default <- function(remote, ...) NA_character_
different_sha <- function(remote_sha, local_sha) {
same <- remote_sha == local_sha
same <- isTRUE(same) && !is.na(same)
!same
}
local_sha <- function(name) {
package2remote(name)$sha %||% NA_character_
}
# Convert an installed package to its equivalent remote. This constructs the
# remote from metadata stored in the package's DESCRIPTION file; the metadata
# is added to the package when it is installed by remotes. If the package is
# installed some other way, such as by `install.packages()` there will be no
# meta-data, so there we construct a generic CRAN remote.
package2remote <- function(name, lib = .libPaths(), repos = getOption("repos"), type = getOption("pkgType"), ...) {
x <- tryCatch(utils::packageDescription(name, lib.loc = lib), error = function(e) NA, warning = function(e) NA)
# will be NA if not installed
if (identical(x, NA)) {
return(remote("cran",
name = name,
repos = repos,
pkg_type = type,
sha = NA_character_))
}
if (is.null(x$RemoteType) || x$RemoteType == "cran") {
# Packages installed with install.packages() or locally without remotes
return(remote("cran",
name = x$Package,
repos = repos,
pkg_type = type,
sha = x$Version))
}
switch(x$RemoteType,
github = remote("github",
host = x$RemoteHost,
package = x$RemotePackage,
repo = x$RemoteRepo,
subdir = x$RemoteSubdir,
username = x$RemoteUsername,
ref = x$RemoteRef,
sha = x$RemoteSha,
auth_token = github_pat()),
gitlab = remote("gitlab",
host = x$RemoteHost,
repo = x$RemoteRepo,
subdir = x$RemoteSubdir,
username = x$RemoteUsername,
ref = x$RemoteRef,
sha = x$RemoteSha,
auth_token = gitlab_pat()),
xgit = remote("xgit",
url = trim_ws(x$RemoteUrl),
ref = x$RemoteRef %||% x$RemoteBranch,
sha = x$RemoteSha,
subdir = x$RemoteSubdir,
args = x$RemoteArgs),
git2r = remote("git2r",
url = trim_ws(x$RemoteUrl),
ref = x$RemoteRef %||% x$RemoteBranch,
sha = x$RemoteSha,
subdir = x$RemoteSubdir),
bitbucket = remote("bitbucket",
host = x$RemoteHost,
repo = x$RemoteRepo,
username = x$RemoteUsername,
ref = x$RemoteRef,
sha = x$RemoteSha,
subdir = x$RemoteSubdir,
auth_user = bitbucket_user(),
password = bitbucket_password()),
svn = remote("svn",
url = trim_ws(x$RemoteUrl),
svn_subdir = x$RemoteSubdir,
revision = x$RemoteSha,
args = x$RemoteArgs),
local = remote("local",
path = trim_ws(x$RemoteUrl),
subdir = x$RemoteSubdir,
sha = {
# Packages installed locally might have RemoteSha == NA_character_
x$RemoteSha %||% x$Version
}),
url = remote("url",
url = trim_ws(x$RemoteUrl),
subdir = x$RemoteSubdir,
config = x$RemoteConfig,
pkg_type = x$RemotePkgType),
bioc_git2r = remote("bioc_git2r",
mirror = x$RemoteMirror,
repo = x$RemoteRepo,
release = x$RemoteRelease,
sha = x$RemoteSha,
branch = x$RemoteBranch),
bioc_xgit = remote("bioc_xgit",
mirror = x$RemoteMirror,
repo = x$RemoteRepo,
release = x$RemoteRelease,
sha = x$RemoteSha,
branch = x$RemoteBranch)
)
}
#' @export
format.remotes <- function(x, ...) {
vapply(x, format, character(1))
}