r-lib / pak

A fresh approach to package installation
https://pak.r-lib.org
643 stars 56 forks source link

method argument from pkg_install for utils::download.file method . Checked and set if necessary -> ensure_method_type(method) #547

Closed SermetPekin closed 8 months ago

SermetPekin commented 8 months ago

to be able to pass method parameter from pak::pkg_install to utils::download.file added the ensure_method_type function to check and set a valid option from valid params of
c("internal", "libcurl", "wget", "curl" , "wininet" , "auto")

ensure_method_type<-function(method = NULL    ){
  # utils::download.file
  # ...
  # method
  #     Method to be used for downloading files.
  #     Current download methods are "internal", "libcurl", "wget", "curl" and "wininet" (Windows only),
  #     and there is a value "auto": see ‘Details’ and ‘Note’.
  #     The method can also be set through the option "download.file.method": see options().
  types <- c("internal", "libcurl", "wget", "curl" , "wininet" , "auto")

  if(is.null(method))
    method<- getOption("download.file.method", default = "auto")

  # to avoid side effect we will return without changing/setting options
  # if no `download.file.method` option set
  if( method %in% types )  return( invisible(method) )

  # if only there is an invalid value set in the options at least set smt valid
  # @`side effect` it will set it with the default value
  options("download.file.method" = "auto"   )
  return(  invisible("auto") )

}

some tests for the new method to check side effect


`test_ensure_method_type( ){
  option_name <- "download.file.method"

  m0 <- getOption(option_name  )

  m1 <- ensure_method_type(NULL )
  stopifnot(m1 == "auto")

  m2 <- ensure_method_type("xx")
  stopifnot(m2 == "auto")

  m3 <- ensure_method_type("internal")
  stopifnot(m3 == "internal")

  m_last <- getOption(option_name )
  stopifnot(m0 == m_last )
}`

...


`pkg_install <- function(pkg, lib = .libPaths()[[1L]], upgrade = FALSE,
                        ask = interactive(), dependencies = NA , method = NULL  ) {

  start <- Sys.time()

  ensure_method_type(method)

  status <- remote(
    function(...) get("pkg_install_make_plan", asNamespace("pak"))(...),
    list(pkg = pkg, lib = lib, upgrade = upgrade, ask = ask,
         start = start, dependencies = dependencies,
         loaded = loaded_packages(lib)))

  unloaded <- handle_status(status, lib, ask)$unloaded

  inst <- remote(
    function(...) get("pkg_install_do_plan", asNamespace("pak"))(...),
    list(proposal = NULL))

  if (length(unloaded) > 0) offer_restart(unloaded)

  invisible(inst)
}`
SermetPekin commented 8 months ago

noticed some unintended changes on .gitignore file.