Skip to content

Commit

Permalink
Add print method for scan_deps() result
Browse files Browse the repository at this point in the history
  • Loading branch information
gaborcsardi committed Nov 22, 2024
1 parent d967c1b commit c986c2e
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 1 deletion.
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by roxygen2: do not edit by hand

S3method("[",pkg_resolution_result)
S3method("[",pkg_scan_deps)
S3method("[",pkg_solution_result)
S3method("[",pkg_sysreqs_check_result)
S3method("[",pkgplan_downloads)
Expand All @@ -13,6 +14,7 @@ S3method(format,pkg_name_check_sentiment)
S3method(format,pkg_name_check_urban)
S3method(format,pkg_name_check_wikipedia)
S3method(format,pkg_name_check_wiktionary)
S3method(format,pkg_scan_deps)
S3method(format,pkg_solution_failures)
S3method(format,pkg_solution_result)
S3method(format,pkg_sysreqs_check_result)
Expand All @@ -22,6 +24,7 @@ S3method(print,package_build_error)
S3method(print,package_packaging_error)
S3method(print,package_uncompress_error)
S3method(print,pkg_name_check)
S3method(print,pkg_scan_deps)
S3method(print,pkg_solution_result)
S3method(print,pkg_sysreqs_check_result)
S3method(print,pkginstall_result)
Expand Down
45 changes: 45 additions & 0 deletions R/scan-deps-print.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#' @export

format.pkg_scan_deps <- function(x, ...) {
labels <- c(
prod = "Dependencies",
test = "Test dependencies",
dev = "Development dependencies",
# TODO: generic label for others
NULL
)
lns <- lapply(seq_along(labels), function(i) {
deps <- x[x$type == names(labels)[i], , drop = FALSE]
if (nrow(deps) == 0) return(NULL)
fls <- tapply(deps$path, deps$package, "c", simplify = FALSE)
fls[] <- lapply(fls, unique)
fls <- vcapply(fls, paste, collapse = ", ")
pkg <- format(names(fls))
flsw <- cli::console_width() - nchar(pkg[1]) - 5
c(
"", cli::col_yellow(paste0(labels[i], ":")),
paste0(
cli::col_grey("+ "),
cli::col_blue(pkg),
cli::col_grey(" @ "),
cli::col_silver(cli::ansi_strtrim(fls, flsw))
)
)
})

unlist(lns)
}

#' @export

print.pkg_scan_deps <- function(x, ...) {
writeLines(format(x, ...))
invisible(x)
}

#' @export

`[.pkg_scan_deps` <- function (x, i, j, drop = FALSE) {
class(x) <- setdiff(class(x), "pkg_scan_deps")
NextMethod("[")
}
1 change: 1 addition & 0 deletions R/scan-deps.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ scan_deps <- function(path = ".") {
# write back the relative paths
deps$path <- paths[match(deps$path, full_paths)]
deps$type <- get_dep_type_from_path(deps$path)
class(deps) <- c("pkg_scan_deps", class(deps))
deps
}

Expand Down
16 changes: 15 additions & 1 deletion tests/testthat/_snaps/unix/scan-deps.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# scan_deps

Code
scan_deps(project)
scan_deps(project)[]
Output
# A data frame: 6 x 7
path package type code start_row start_column start_byte
Expand All @@ -13,6 +13,20 @@
5 index.Rmd ST prod ST::fun 10 1 97
6 index.Rmd RS prod library(RS) 9 1 85

---

Code
scan_deps(project)
Output
Dependencies:
+ AB @ R/code.R
+ BC @ R/code.R
+ CD @ R/code.R
+ RS @ index.Rmd
+ ST @ index.Rmd
+ pkgload @ doc.qmd

# scan_path_deps

Code
Expand Down
3 changes: 3 additions & 0 deletions tests/testthat/test-scan-deps.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ test_that("scan_deps", {
on.exit(unlink(tmp), add = TRUE)

project <- test_path("fixtures/scan/project-1")
expect_snapshot(variant = .Platform$OS.type, {
scan_deps(project)[]
})
expect_snapshot(variant = .Platform$OS.type, {
scan_deps(project)
})
Expand Down

0 comments on commit c986c2e

Please sign in to comment.