R/download.R

Defines functions download_package_plan

Documented in download_package_plan

#' Download packages of a package installation plan
#'
#' Download packages of a package installation plan, into the R
#' temporary directory.
#'
#' It downloads all files in parallel.
#'
#' It requires the libcurl capability currently, see [capabilities()].
#'
#' @param plan Package installation plan, as created by the
#'   pkgdepends package.
#' @param quiet If `TRUE`, suppress status messages.
#' @return The updated package plan, with additional columns:
#'   * `download_dir`: the directory of the downloaded package,
#'   * `status`: download status,
#'   * `file`: path to the downloaded file.
#'
#' @export
#' @importFrom utils download.file
#' @seealso [install_package_plan()] for installing the packages in the
#'   plan.

download_package_plan <- function(plan, quiet = FALSE) {
  if (!capabilities("libcurl")) {
    stop("Needs capability 'libcurl', and this R installation does not ",
         "support that.")
  }

  ## TODO: support alternative URLs
  ## TODO: caching?
  ## TODO: handle errors

  if (!quiet) message("\n---> Downloading packages\n")

  urls <- map_chr(plan$sources, "[[", 1)
  mkdirp(ddir <- file.path(tempdir(), "pkg_install"))
  dests <- file.path(ddir, plan$target)
  mkdirp(dirname(dests))

  ret <- download.file(urls, dests, mode = "wb", quiet = quiet,
                       method = "libcurl")

  if (ret != 0) stop("Some package downloads failed")

  plan$download_dir <- normalizePath(ddir, winslash = "/")
  plan$status <- "OK"
  plan$file <- dests

  plan
}
gaborcsardi/installlite documentation built on May 22, 2019, 5:33 p.m.