Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pkg_upgrade() #289

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Suggests:
cli (>= 2.3.1),
covr,
curl,
desc,
distro,
filelock (>= 1.0.2),
glue (>= 1.3.0),
Expand All @@ -48,6 +49,7 @@ Config/needs/dependencies:
callr,
cli,
curl,
desc,
distro,
filelock,
glue,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export(pkg_remove)
export(pkg_search)
export(pkg_status)
export(pkg_system_requirements)
export(pkg_upgrade)
export(repo_add)
export(repo_get)
export(repo_ping)
Expand Down
2 changes: 1 addition & 1 deletion R/dev-mode.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ lookup_deps <- function(pkg, lib_path = .libPaths()) {

## TODO: check for version requirements
find_lib <- function(pkg) {
w <- head(which(vlapply(lib_pkgs, `%in%`, x = pkg)), 1)
w <- utils::head(which(vlapply(lib_pkgs, `%in%`, x = pkg)), 1)
if (!length(w)) {
stop("Required package `", pkg, "` is not available")
}
Expand Down
89 changes: 86 additions & 3 deletions R/package.R
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ pkg_install_do_plan <- function(proposal, lib) {
#' }

pkg_status <- function(pkg, lib = .libPaths()) {
stopifnot(length(pkg == 1) && is.character(pkg))
stopifnot(length(pkg) == 1 && is.character(pkg))

load_extra("tibble")
remote(
Expand Down Expand Up @@ -332,7 +332,7 @@ pkg_remove_internal <- function(pkg, lib) {
#' pkg_deps("r-lib/fs")

pkg_deps <- function(pkg, upgrade = TRUE, dependencies = NA) {
stopifnot(length(pkg == 1) && is.character(pkg))
stopifnot(length(pkg) == 1 && is.character(pkg))
load_extra("tibble")
remote(
function(...) {
Expand Down Expand Up @@ -378,7 +378,7 @@ pkg_deps_internal2 <- function(pkg, upgrade, dependencies) {
#' pkg_deps_tree("r-lib/usethis")

pkg_deps_tree <- function(pkg, upgrade = TRUE, dependencies = NA) {
stopifnot(length(pkg == 1) && is.character(pkg))
stopifnot(length(pkg) == 1 && is.character(pkg))
ret <- remote(
function(...) {
get("pkg_deps_tree_internal", asNamespace("pak"))(...)
Expand Down Expand Up @@ -461,3 +461,86 @@ pkg_download_internal <- function(pkg, dest_dir = ".", dependencies = FALSE,
dl$stop_for_download_error()
dl$get_downloads()
}

#' Upgrade an installed package, and its dependencies
#'
#' pak will try to upgrade the package from the same source as the
#' one used for the original installation E.g. if you installed the package
#' from a branch of a GitHub repository, then it will try to upgrade from
#' the same branch.
#'
#' @param pkg Package name to upgrade. Must be a package name, general
#' remote specifications are not allowed here.
#' @param lib Library to find the installed package in. The new version
#' of the package(s) will be installed into the same library.
#' @param upgrade Whether to upgrade the dependencies of `pkg` as well.
#' @param ... Additional arguments are passed to [pkg_install()].
#'
#' @family package functions
#' @export

pkg_upgrade <- function(pkg, lib = .libPaths()[[1L]], upgrade = TRUE, ...) {
stopifnot(length(pkg) == 1, is.character(pkg), !is.na(pkg))
ref <- get_installed_ref(pkg, lib)

if (is.null(ref)) {
stop("'", pkg, "' is not currently installed, cannot upgrade it.")

} else if (identical(ref, NA_character_)) {
cli <- load_private_cli()
cli$cli_alert_warning(
c("Cannot find source of {.pkg {pkg}}, trying to upgrade it from ",
"the configured repositories."),
wrap = TRUE
)
ref <- pkg
} else if (!is.null(names(ref))) {
cli <- load_private_cli()
cli$cli_alert_info(
"Updating {.emph {names(ref)}} package {.pkg {ref}}.",
wrap = TRUE
)
} else if (pkg != ref){
cli <- load_private_cli()
cli$cli_alert_info(
"Updating package {.pkg {pkg}} from {.pkg {ref}}.",
wrap = TRUE
)
}

pkg_install(ref, lib = lib, upgrade = upgrade, ...)
}

get_installed_ref <- function(pkg, lib) {
remote(
function(...) get("get_installed_ref_internal", asNamespace("pak"))(...),
list(pkg = pkg, lib = lib)
)
}

get_installed_ref_internal <- function(pkg, lib) {
name <- paste0("^", pkgdepends::pkg_rx()$pkg_name, "$")
if (!grepl(name, pkg)) stop("'", pkg, "' is not a valid package name")

pkg_dir <- tryCatch(
find.package(pkg, lib.loc = lib),
error = function(err) NULL
)

if (is.null(pkg_dir)) return(NULL)

dsc <- desc::desc(pkg_dir)
ref <- dsc$get("RemotePkgRef")[[1]]
repo <- dsc$get("Repository")[[1]]
bioc <- dsc$get("biocViews")[[1]]

if (!is.na(ref)) {
ref
} else if (!is.na(repo) && repo == "CRAN") {
c(CRAN = pkg)
} else if (!is.na(bioc)) {
c(Bioconductor = pkg)
} else {
NA_character_
}
}
3 changes: 2 additions & 1 deletion man/lib_status.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/pak.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/pak_package_sources.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/pkg_deps.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/pkg_deps_tree.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/pkg_download.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/pkg_install.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/pkg_remove.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/pkg_status.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions man/pkg_upgrade.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

116 changes: 116 additions & 0 deletions tests/testthat/_snaps/pkg-upgrade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# pkg_upgrade [plain]

Code
pkg_upgrade("foo")
Message <cliMessage>
! Cannot find source of foo, trying to upgrade it from the configured
repositories.

---

Code
pkg_upgrade("foo")
Message <cliMessage>
i Updating CRAN package foo.

---

Code
pkg_upgrade("foo")
Message <cliMessage>
i Updating Bioconductor package foo.

---

Code
pkg_upgrade("foo")
Message <cliMessage>
i Updating package foo from repo/foo@master.

# pkg_upgrade [ansi]

Code
pkg_upgrade("foo")
Message <cliMessage>
! Cannot find source of foo, trying to upgrade it from the configured
repositories.

---

Code
pkg_upgrade("foo")
Message <cliMessage>
i Updating CRAN package foo.

---

Code
pkg_upgrade("foo")
Message <cliMessage>
i Updating Bioconductor package foo.

---

Code
pkg_upgrade("foo")
Message <cliMessage>
i Updating package foo from repo/foo@master.

# pkg_upgrade [unicode]

Code
pkg_upgrade("foo")
Message <cliMessage>
! Cannot find source of foo, trying to upgrade it from the configured
repositories.

---

Code
pkg_upgrade("foo")
Message <cliMessage>
ℹ Updating CRAN package foo.

---

Code
pkg_upgrade("foo")
Message <cliMessage>
ℹ Updating Bioconductor package foo.

---

Code
pkg_upgrade("foo")
Message <cliMessage>
ℹ Updating package foo from repo/foo@master.

# pkg_upgrade [fancy]

Code
pkg_upgrade("foo")
Message <cliMessage>
! Cannot find source of foo, trying to upgrade it from the configured
repositories.

---

Code
pkg_upgrade("foo")
Message <cliMessage>
ℹ Updating CRAN package foo.

---

Code
pkg_upgrade("foo")
Message <cliMessage>
ℹ Updating Bioconductor package foo.

---

Code
pkg_upgrade("foo")
Message <cliMessage>
ℹ Updating package foo from repo/foo@master.

Loading