-
Notifications
You must be signed in to change notification settings - Fork 152
/
install-dev.R
81 lines (67 loc) · 2.41 KB
/
install-dev.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
#' Install the development version of a package
#'
#' `install_dev()` retrieves the package DESCRIPTION from the CRAN mirror and
#' looks in the 'URL' and 'BugReports' fields for GitHub, GitLab or Bitbucket
#' URLs. It then calls the appropriate `install_()` function to install the
#' development package.
#'
#' @param package The package name to install.
#' @param cran_url The URL of the CRAN mirror to use, by default based on the
#' 'repos' option. If unset uses 'https://cloud.r-project.org'.
#' @param ... Additional arguments passed to [install_github()],
#' [install_gitlab()], or [install_bitbucket()] functions.
#' @family package installation
#' @export
#' @examples
#' \dontrun{
#' # From GitHub
#' install_dev("dplyr")
#'
#' # From GitLab
#' install_dev("iemiscdata")
#'
#' # From Bitbucket
#' install_dev("argparser")
#' }
install_dev <- function(package, cran_url = getOption("repos")[["CRAN"]], ...) {
if (is.null(cran_url) || identical(cran_url, "@CRAN@")) {
cran_url <- "https://cloud.r-project.org"
}
refs <- dev_split_ref(package)
url <- build_url(cran_url, "web", "packages", refs[["pkg"]], "DESCRIPTION")
f <- tempfile()
on.exit(unlink(f))
download(f, url)
desc <- read_dcf(f)
url_fields <- c(desc$URL, desc$BugReports)
if (length(url_fields) == 0) {
stop("Could not determine development repository", call. = FALSE)
}
pkg_urls <- unlist(strsplit(url_fields, "[[:space:]]*,[[:space:]]*"))
# Remove trailing "/issues" from the BugReports URL
pkg_urls <- sub("/issues/?$", "", pkg_urls)
valid_domains <- c("github[.]com", "gitlab[.]com", "bitbucket[.]org")
parts <-
re_match(pkg_urls,
sprintf("^https?://(?<domain>%s)/(?<username>%s)/(?<repo>%s)(?:/(?<subdir>%s))?",
domain = paste0(valid_domains, collapse = "|"),
username = "[^/]+",
repo = "[^/@#]+",
subdir = "[^/@$ ]+"
)
)[c("domain", "username", "repo", "subdir")]
# Remove cases which don't match and duplicates
parts <- unique(stats::na.omit(parts))
if (nrow(parts) != 1) {
stop("Could not determine development repository", call. = FALSE)
}
full_ref <- paste0(
paste0(c(parts$username, parts$repo, if (nzchar(parts$subdir)) parts$subdir), collapse = "/"),
refs[["ref"]]
)
switch(parts$domain,
github.com = install_github(full_ref, ...),
gitlab.com = install_gitlab(full_ref, ...),
bitbucket.org = install_bitbucket(full_ref, ...)
)
}