R/url-packages.R

Defines functions url_PACKAGES_cache wood_url_packages

Documented in wood_url_packages

#' List available packages in any repository
#'
#' @description This function queries any online repository for a list of
#' available packages. They are returned as a vector of strings, each element
#' being a package name. The data is retrieved from `/src/contrib/PACKAGES`
#' or `/src/contrib/PACKAGES.gz` file.
#'
#' @template repository
#'
#' @return A character vector of available packages.
#'
#' @examplesIf !woodendesc:::is_cran_check()
#' wood_url_packages("https://colinfay.me")
#' # Trailing slashes are removed
#' wood_url_packages("https://colinfay.me/")
#'
#' @family url
#' @family packages
#' @export
wood_url_packages <- function(repository) {
  assert_param_url_repo(repository)

  url_PACKAGES_cache(repository) |>
    vapply(function(x) { x[["Package"]] }, character(1)) |>
    unique()
}

url_PACKAGES_cache <- function(repository) {
  # TODO: should we allow setting a different URL path?
  req <- httr2::request(repository) |>
    httr2::req_url_path_append("src", "contrib")

  # Note that we can't hash() `repository` because it may contain trailing slash which doesn't affect actual URL
  with_cache({
    download_repo_data(req)
  }, "PACKAGES", rlang::hash(req))
}
ErdaradunGaztea/woodendesc documentation built on Feb. 1, 2025, 2:26 a.m.