From c986c2ee6aa604604de040d5211a896b7b314746 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Cs=C3=A1rdi?= Date: Fri, 22 Nov 2024 14:23:15 +0100 Subject: [PATCH] Add print method for scan_deps() result --- NAMESPACE | 3 ++ R/scan-deps-print.R | 45 +++++++++++++++++++++++++ R/scan-deps.R | 1 + tests/testthat/_snaps/unix/scan-deps.md | 16 ++++++++- tests/testthat/test-scan-deps.R | 3 ++ 5 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 R/scan-deps-print.R diff --git a/NAMESPACE b/NAMESPACE index 3ab00bc8..5f50ef03 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) @@ -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) @@ -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) diff --git a/R/scan-deps-print.R b/R/scan-deps-print.R new file mode 100644 index 00000000..5ea68b54 --- /dev/null +++ b/R/scan-deps-print.R @@ -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("[") +} diff --git a/R/scan-deps.R b/R/scan-deps.R index 3c9b3a6e..78277ed6 100644 --- a/R/scan-deps.R +++ b/R/scan-deps.R @@ -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 } diff --git a/tests/testthat/_snaps/unix/scan-deps.md b/tests/testthat/_snaps/unix/scan-deps.md index 16b25438..cf2f3ed5 100644 --- a/tests/testthat/_snaps/unix/scan-deps.md +++ b/tests/testthat/_snaps/unix/scan-deps.md @@ -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 @@ -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 diff --git a/tests/testthat/test-scan-deps.R b/tests/testthat/test-scan-deps.R index 579f3b97..bd337917 100644 --- a/tests/testthat/test-scan-deps.R +++ b/tests/testthat/test-scan-deps.R @@ -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) })