Skip to content

Commit

Permalink
Drop glue dependency
Browse files Browse the repository at this point in the history
Reformat many errors as cli errors.
  • Loading branch information
gaborcsardi committed Nov 20, 2023
1 parent 51e416f commit 1bdb534
Show file tree
Hide file tree
Showing 26 changed files with 297 additions and 252 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@ License: MIT + file LICENSE
URL: https://r-lib.github.io/pkgdepends/,
https://github.com/r-lib/pkgdepends#readme
BugReports: https://github.com/r-lib/pkgdepends/issues
Depends:
Depends:
R (>= 3.5)
Imports:
callr (>= 3.3.1),
cli (>= 3.6.0),
curl,
desc (>= 1.2.0),
filelock (>= 1.0.2),
glue,
jsonlite,
lpSolve,
pkgbuild (>= 1.0.2),
Expand All @@ -44,6 +43,7 @@ Suggests:
debugme,
fansi,
fs,
glue,
htmlwidgets,
mockery,
pak,
Expand Down
5 changes: 0 additions & 5 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,6 @@ importFrom(desc,desc)
importFrom(desc,desc_get_deps)
importFrom(filelock,lock)
importFrom(filelock,unlock)
importFrom(glue,backtick)
importFrom(glue,glue)
importFrom(glue,glue_collapse)
importFrom(glue,glue_data)
importFrom(glue,single_quote)
importFrom(jsonlite,fromJSON)
importFrom(jsonlite,toJSON)
importFrom(jsonlite,unbox)
Expand Down
4 changes: 2 additions & 2 deletions R/download-progress-bar.R
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ pkgplan__update_progress_bar <- function(bar, idx, event, data) {
#' @param bar The progress bar object.
#'
#' @noRd
#' @importFrom glue glue_collapse

#' @importFrom prettyunits pretty_bytes pretty_dt

pkgplan__show_progress_bar <- function(bar) {
Expand Down Expand Up @@ -348,7 +348,7 @@ calculate_progress_parts <- function(bar) {
pkgs <- bar$what$package[bar$what$idx %in% bar$events$data]
parts$msg <- paste0(
"Getting ",
glue_collapse(pkgs, sep = ", ", last = " and ")
cli::ansi_collapse(pkgs, sep = ", ", last = " and ")
)
bar$lastmsg <- parts$msg
}
Expand Down
6 changes: 2 additions & 4 deletions R/download.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ pkgplan_stop_for_solution_download_error <- function(self, private) {
which(bad),
function(i) {
urls <- format_items(dl$sources[[i]])
glue("Failed to download {dl$package[i]} \\
from {urls}.")
sprintf("Failed to download %s from %s.", dl$package[i], urls)
}
)
msg <- paste(msgs, collapse = "\n")
Expand All @@ -112,8 +111,7 @@ pkgplan_stop_for_resolution_download_error <- function(self, private) {
which(bad),
function(i) {
urls <- format_items(dl$sources[[i]])
glue("Failed to download {dl$package[i]} \\
from {urls}.")
sprintf("Failed to download %s from %s.", dl$package[i], urls)
}
)
msg <- paste(msgs, collapse = "\n")
Expand Down
40 changes: 26 additions & 14 deletions R/install-binary.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,41 @@ install_extracted_binary <- function(filename, lib_cache, pkg_cache, lib,
dir.create(dirname(move_to), showWarnings = FALSE, recursive = TRUE)
ret <- file.rename(installed_path, move_to)
if (!ret) {
throw(new_fs_error(
"Failed to move installed package at {installed_path}",
package = pkg_name))
throw(pkg_error(
"Failed to move installed package {.pkg {pkg_name}} at
{.path {installed_path}}.",
.data = list(package = pkg_name),
.class = "install_filesystem_error"
))
}
ret <- unlink(move_to, recursive = TRUE, force = TRUE)
if (ret != 0) {
throw(new_fs_warning(
"Failed to remove installed package at {move_to}",
package = pkg_name))
throw(pkg_warning(
"Failed to remove installed package at {.path {move_to}}.",
.data = list(package = pkg_name),
.class = "install_filesystem_warning"
))
}
} else {
# On Unix we are fine with just deleting the old package
ret <- unlink(installed_path, recursive = TRUE, force = TRUE)
if (ret != 0) {
throw(new_fs_warning(
"Failed to remove installed package at {installed_path}",
package = pkg_name))
throw(pkg_warning(
"Failed to remove installed package at {.path {installed_path}}.",
.data = list(package = pkg_name),
.class = "install_filesystem_warning"
))
}
}
}
ret <- file.rename(pkg$path, installed_path)
if (!ret) {
throw(new_fs_error(
"Unable to move package from {pkg$path} to {installed_path}",
package = pkg_name))
throw(pkg_error(
"Unable to move package from {.path {pkg$path}} to
{.path {installed_path}}",
.data = list(package = pkg_name),
.class = "install_filesystem_error"
))
}

installed_path
Expand Down Expand Up @@ -116,8 +126,10 @@ make_install_process <- function(filename, lib = .libPaths()[[1L]],

type <- detect_package_archive_type(filename)
if (type == "unknown") {
throw(new_input_error(
"Cannot extract {filename}, unknown archive type?"))
throw(pkg_error(
"Cannot extract {.path {filename}}, unknown archive type.",
.class = "install_input_error"
))
}

lib_cache <- library_cache(lib)
Expand Down
37 changes: 22 additions & 15 deletions R/install-plan.R
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ make_build_process <- function(path, pkg, tmp_dir, lib, vignettes,
pkgbuild_process$new(
path, tmp_dir, binary = binary, vignettes = vignettes,
needs_compilation = needscompilation, compile_attributes = FALSE,
args = c("--no-lock", cmd_args, if (binary) glue("--library={tmplib}"))
args = c("--no-lock", cmd_args, if (binary) sprintf("--library=%s", tmplib))
)
)
}
Expand Down Expand Up @@ -593,14 +593,16 @@ stop_task_package_uncompress <- function(state, worker) {
state$plan$package_stdout[[pkgidx]] <- worker$stdout
state$plan$worker_id[[pkgidx]] <- NA_character_

throw(new_pkg_uncompress_error(
"Failed to uncompress {pkg} from {state$plan$file[[pkgidx]]}.",
data = list(
throw(pkg_error(
"Failed to uncompress {.pkg {pkg}} from
{.path {state$plan$file[[pkgidx]]}}.",
.data = list(
package = pkg,
version = version,
time = time,
stdout = worker$stdout
)
),
.class = "package_uncompress_error"
))
}

Expand Down Expand Up @@ -642,15 +644,16 @@ stop_task_package_build <- function(state, worker) {
state$plan$worker_id[[pkgidx]] <- NA_character_

if (!success) {
throw(new_pkg_packaging_error(
c("Failed to create source package {pkg} from source tree ",
"{state$plan$file[[pkgidx]]}"),
data = list(
throw(pkg_error(
"Failed to create source package {.pkg {pkg}} from source tree at
{.path {state$plan$file[[pkgidx]]}}",
.data = list(
package = pkg,
version = version,
stdout = worker$stdout,
time = time
)
),
.class = "package_packaging_error"
))
}

Expand Down Expand Up @@ -704,14 +707,15 @@ stop_task_build <- function(state, worker) {
state$plan$worker_id[[pkgidx]] <- NA_character_

if (!success) {
throw(new_pkg_build_error(
"Failed to build source package {pkg}",
data = list(
throw(pkg_error(
"Failed to build source package {.pkg {pkg}}.",
.data = list(
package = pkg,
version = version,
stdout = worker$stdout,
time = time
)
),
.class = "package_build_error"
))
}

Expand Down Expand Up @@ -806,7 +810,10 @@ stop_task_install <- function(state, worker) {
state$plan$worker_id[[pkgidx]] <- NA_character_

if (!success) {
throw(new_pkg_install_error("Failed to install binary package {pkg}."))
throw(pkg_error(
"Failed to install binary package {.pkg {pkg}}.",
.class = "package_install_error"
))
}

## Need to remove from the dependency list
Expand Down
11 changes: 7 additions & 4 deletions R/install-tar.R
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,10 @@ make_uncompress_process <- function(archive, exdir = ".", ...) {
type <- detect_package_archive_type(archive)

if (type == "unknown") {
throw(new_input_error(
"Cannot extract {archive}, unknown archive type?"))
throw(pkg_error(
"Cannot extract {.path {archive}}, unknown archive type.",
.class = "install_input_error"
))
}

if (type == "zip") {
Expand All @@ -297,8 +299,9 @@ make_uncompress_process <- function(archive, exdir = ".", ...) {
run_uncompress_process <- function(archive, exdir = ".", ...) {
type <- detect_package_archive_type(archive)
if (type == "unknown") {
throw(new_input_error(
"Cannot extract {.path {archive}}, unknown archive type?"
throw(pkg_error(
"Cannot extract {.path {archive}}, unknown archive type.",
.class = "install_input_error"
))
}

Expand Down
83 changes: 3 additions & 80 deletions R/install-utils.R
Original file line number Diff line number Diff line change
@@ -1,62 +1,21 @@

#' @importFrom glue single_quote glue_collapse
collapse_quote_transformer <- function(code, envir) {
collapse_re <- "[*]$"
quote_re <- "^[|]"
should_collapse <- grepl(collapse_re, code)
should_quote <- !grepl(quote_re, code)
code <- sub(collapse_re, "",
sub(quote_re, "", code))
res <- eval(parse(text = code, keep.source = FALSE), envir = envir)
if (should_quote) {
res <- single_quote(res)
}
if (should_collapse) {
res <- glue_collapse(res, sep = ", ", last = " and ")
}
res
}

#' @importFrom glue glue

new_cnd_msg <- function(msg, .envir) {
msg <- paste(msg, collapse = "")
glue(msg, .envir = .envir, .transformer = collapse_quote_transformer)
}

new_pkg_packaging_error <- function(msg, data) {
cnd <- new_error(new_cnd_msg(msg, .envir = parent.frame()))
cnd$package <- data$package
cnd$data <- data
class(cnd) <- c("package_packaging_error", class(cnd))
cnd
}

#' @export

format.package_packaging_error <- function(x, ...) {
format_error_with_stdout(x, ...)
}

#' @export

print.package_packaging_error <- function(x, ...) {
cat(format(x, ...), sep = "\n")
}

new_pkg_build_error <- function(msg, data) {
cnd <- new_error(new_cnd_msg(msg, .envir = parent.frame()))
cnd$package <- data$package
cnd$data <- data
class(cnd) <- c("package_build_error", class(cnd))
cnd
}

#' @export

format.package_uncompress_error <- function(x, ...) {
out <- conditionMessage(x)
if (!is.null(x$data$stdout)) {
stdout <- last_stdout_lines(x$data$stdout, "", prefix = "O> ")[-(1:2)]
stdout <- last_stdout_lines(x$stdout, "", prefix = "O> ")[-(1:2)]
out <- c(out, "", "Standard output:", stdout)
}
out
Expand All @@ -68,14 +27,6 @@ print.package_uncompress_error <- function(x, ...) {
cat(format(x, ...), sep = "\n")
}

new_pkg_uncompress_error <- function(msg, data) {
cnd <- new_error(new_cnd_msg(msg, .envir = parent.frame()))
cnd$package <- data$package
cnd$data <- data
class(cnd) <- c("package_uncompress_error", class(cnd))
cnd
}

#' @export

format.package_build_error <- function(x, ...) {
Expand All @@ -88,34 +39,6 @@ print.package_build_error <- function(x, ...) {
cat(format(x, ...), sep = "\n")
}

new_pkg_install_error <- function(msg, package = NULL) {
cnd <- new_error(new_cnd_msg(msg, .envir = parent.frame()))
cnd$package <- package
class(cnd) <- c("package_install_error", class(cnd))
cnd
}

new_fs_error <- function(msg, package = NULL) {
cnd <- new_error(new_cnd_msg(msg, .envir = parent.frame()))
cnd$package <- package
class(cnd) <- c("install_filesystem_error", class(cnd))
cnd
}

new_input_error <- function(msg, package = NULL) {
cnd <- new_error(new_cnd_msg(msg, .envir = parent.frame()))
cnd$package <- package
class(cnd) <- c("install_input_error", class(cnd))
cnd
}

new_fs_warning <- function(msg, package = NULL) {
cnd <- err$new_cond(new_cnd_msg(msg, .envir = parent.frame()))
cnd$package <- package
class(cnd) <- c("install_filesystem_warning", "warning", class(cnd))
cnd
}

is_loaded <- function(package) {
package %in% loadedNamespaces()
}
Expand All @@ -136,7 +59,7 @@ lock_cache <- function(cache, pkg_name, lock = TRUE) {
use_lock <- !identical(lock, FALSE)
my_lock <- NULL
if (use_lock) {
lockfile <- file.path(cache, glue("{pkg_name}.lock"))
lockfile <- file.path(cache, sprintf("%s.lock", pkg_name))
# TODO: timeout and fail?
my_lock <- lock(lockfile)
}
Expand Down
Loading

0 comments on commit 1bdb534

Please sign in to comment.