| 1 |
|
| 2 |
renv_repos_normalize <- function(repos = getOption("repos")) { |
| 3 |
|
| 4 |
# ensure repos are a character vector |
| 5 |
repos <- convert(repos, "character") |
| 6 |
|
| 7 |
# force a CRAN mirror when needed |
| 8 |
cran <- getOption("renv.repos.cran", "https://cloud.r-project.org") |
| 9 |
repos[repos == "@CRAN@"] <- cran |
| 10 |
|
| 11 |
# ensure all values are named |
| 12 |
names(repos) <- renv_repos_names(repos) |
| 13 |
|
| 14 |
# return normalized repository |
| 15 |
repos |
| 16 |
|
| 17 |
} |
| 18 |
|
| 19 |
renv_repos_names <- function(repos) { |
| 20 |
|
| 21 |
result <- enum_chr(repos, function(name, repo) { |
| 22 |
|
| 23 |
# if we already have a name, use it |
| 24 |
if (nzchar(name %||% "")) |
| 25 |
return(name) |
| 26 |
|
| 27 |
# if this repository matches a known CRAN mirror, call it CRAN |
| 28 |
mirrors <- renv_cran_mirrors() |
| 29 |
if (any(renv_repos_matches(repo, mirrors))) |
| 30 |
return("CRAN") |
| 31 |
|
| 32 |
# otherwise, just use the repository URL as the name |
| 33 |
repo |
| 34 |
|
| 35 |
}) |
| 36 |
|
| 37 |
unname(result) |
| 38 |
|
| 39 |
} |
| 40 |
|
| 41 |
renv_repos_validate <- function(repos = getOption("repos")) { |
| 42 |
|
| 43 |
# allow empty repository explicitly |
| 44 |
if (empty(repos)) |
| 45 |
return(character()) |
| 46 |
|
| 47 |
# otherwise, ensure it's a named list or character vector |
| 48 |
ok <- is.list(repos) || is.character(repos) |
| 49 |
if (!ok) |
| 50 |
stopf("repos has unexpected type '%s'", typeof(repos)) |
| 51 |
|
| 52 |
# read repository names |
| 53 |
nm <- names(repos) %||% rep.int("", length(repos)) |
| 54 |
if (any(nm %in% "")) { |
| 55 |
|
| 56 |
# if this is a length-one repository, assume it's CRAN |
| 57 |
if (length(repos) == 1L) { |
| 58 |
repos <- c(CRAN = repos) |
| 59 |
return(renv_repos_normalize(repos)) |
| 60 |
} |
| 61 |
|
| 62 |
# otherwise, error |
| 63 |
stopf("all repository entries must be named") |
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
# normalize the repos option |
| 68 |
renv_repos_normalize(repos) |
| 69 |
|
| 70 |
} |
| 71 |
|
| 72 |
renv_repos_baseurl <- function(url) { |
| 73 |
pattern <- "/(?:bin|src)/" |
| 74 |
index <- regexpr(pattern, url, perl = TRUE) |
| 75 |
ifelse(index == -1L, url, substring(url, 1L, index - 1L)) |
| 76 |
} |
| 77 |
|
| 78 |
renv_repos_info <- function(url) { |
| 79 |
|
| 80 |
memoize( |
| 81 |
key = url, |
| 82 |
value = renv_repos_info_impl(url) |
| 83 |
) |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
renv_repos_info_impl <- function(url) { |
| 88 |
|
| 89 |
# make sure the repository URL includes a trailing slash |
| 90 |
url <- gsub("/*$", "/", url) |
| 91 |
|
| 92 |
# if this is a file repository, return early |
| 93 |
if (grepl("^file:", url)) |
| 94 |
return(list(nexus = FALSE)) |
| 95 |
|
| 96 |
# try to download it |
| 97 |
destfile <- renv_scope_tempfile("renv-repos-") |
| 98 |
status <- catch(download(url, destfile = destfile, quiet = TRUE)) |
| 99 |
if (inherits(status, "error")) |
| 100 |
return(status) |
| 101 |
|
| 102 |
# read the contents of the page |
| 103 |
contents <- renv_file_read(destfile) |
| 104 |
|
| 105 |
# determine if this is a Nexus repository |
| 106 |
nexus <- |
| 107 |
grepl("Nexus Repository Manager", contents, fixed = TRUE) || |
| 108 |
grepl("<div class=\"nexus-header\">", contents, fixed = TRUE) |
| 109 |
|
| 110 |
list( |
| 111 |
nexus = nexus |
| 112 |
) |
| 113 |
|
| 114 |
} |
| 115 |
|
| 116 |
renv_repos_matches <- function(url, repos) { |
| 117 |
url <- sub("/$", "", as.character(url)) |
| 118 |
repos <- sub("/$", "", as.character(repos)) |
| 119 |
url %in% repos |
| 120 |
} |