Skip to content

Commit

Permalink
feat: add messages and remove ui_*()
Browse files Browse the repository at this point in the history
  • Loading branch information
ahasverus committed Oct 27, 2023
1 parent b24c4fd commit b8f624c
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 14 deletions.
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
Imports:
cli,
crayon,
rstudioapi,
stringr,
usethis,
Expand Down
3 changes: 0 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# Generated by roxygen2: do not edit by hand

export(add_deps)
importFrom(usethis,ui_done)
importFrom(usethis,ui_oops)
importFrom(usethis,ui_value)
54 changes: 46 additions & 8 deletions R/add_deps.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#' compendium, website, etc.) and updates the sections **Depends**,
#' **Imports**, and **Suggests** of the `DESCRIPTION` file.
#'
#' A `DESCRIPTION` file can be created with the function
#' [usethis::use_description()].
#' A `DESCRIPTION` file can be created and added to an existing project with
#' the function [usethis::use_description()].
#'
#' All `.R`, `.Rmd`, and `.qmd` files are screened to identify packages
#' called by `library(foo)`, `library("foo")`, `library('foo')`,
Expand Down Expand Up @@ -163,37 +163,75 @@ add_deps <- function() {

## Add packages in DESCRIPTION file ----

msg_info("Searching for", msg_value('Depends'), "dependencies")

if (is.null(deps_depends)) {

pos <- which(colnames(descr_file) == "Depends")
if (length(pos)) descr_file <- descr_file[ , -pos]

msg_oops("No package found", indent = " ")

} else {

deps_depends <- paste0(deps_depends, collapse = ",\n ")
descr_file$"Depends" <- paste0("\n ", deps_depends)
deps_depends_txt <- paste0(deps_depends, collapse = ",\n ")
descr_file$"Depends" <- paste0("\n ", deps_depends_txt)

msg_done("Found", msg_value(length(deps_depends)), "package(s)",
indent = " ")

msg <- paste0("Depends: ", paste0(deps_depends, collapse = ", "))
msg_done("Adding the following line in", msg_value('DESCRIPTION'),
indent = " ")
msg_line(msg_code(msg), indent = " ")
}


msg_info("Searching for", msg_value('Imports'), "dependencies")

if (is.null(deps_imports)) {

pos <- which(colnames(descr_file) == "Imports")
if (length(pos)) descr_file <- descr_file[ , -pos]

msg_oops("No package found", indent = " ")

} else {

deps_imports <- paste0(deps_imports, collapse = ",\n ")
descr_file$"Imports" <- paste0("\n ", deps_imports)
deps_imports_txt <- paste0(deps_imports, collapse = ",\n ")
descr_file$"Imports" <- paste0("\n ", deps_imports_txt)

msg_done("Found", msg_value(length(deps_imports)), "package(s)",
indent = " ")

msg <- paste0("Imports: ", paste0(deps_imports, collapse = ", "))
msg_done("Adding the following line in", msg_value('DESCRIPTION'),
indent = " ")
msg_line(msg_code(msg), indent = " ")
}


msg_info("Searching for", msg_value('Suggests'), "dependencies")

if (is.null(deps_suggests)) {

pos <- which(colnames(descr_file) == "Suggests")
if (length(pos)) descr_file <- descr_file[ , -pos]

msg_oops("No package found", indent = " ")

} else {

deps_suggests <- paste0(deps_suggests, collapse = ",\n ")
descr_file$"Suggests" <- paste0("\n ", deps_suggests)
deps_suggests_txt <- paste0(deps_suggests, collapse = ",\n ")
descr_file$"Suggests" <- paste0("\n ", deps_suggests_txt)

msg_done("Found", msg_value(length(deps_suggests)), "package(s)",
indent = " ")

msg <- paste0("Suggests: ", paste0(deps_suggests, collapse = ", "))
msg_done("Adding the following line in", msg_value('DESCRIPTION'),
indent = " ")
msg_line(msg_code(msg), indent = " ")
}


Expand Down
1 change: 0 additions & 1 deletion R/rdeps-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"_PACKAGE"

# Imports: start ----
#' @importFrom usethis ui_done ui_value ui_oops
# Imports: end ----

NULL
99 changes: 99 additions & 0 deletions R/utils-msg.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#' @noRd

msg_done <- function(..., indent = "") {

x <- paste(...)
x <- msg_bullet(x, indent = indent, crayon::green(cli::symbol$"tick"))
cli::cat_line(x)

invisible(NULL)
}



#' @noRd

msg_oops <- function(..., indent = "") {

x <- paste(...)
x <- msg_bullet(x, indent = indent, crayon::red(cli::symbol$"cross"))
cli::cat_line(x)

invisible(NULL)
}



#' @noRd

msg_info <- function(..., indent = "") {

x <- paste(...)
x <- msg_bullet(x, indent = indent, crayon::blue("(*)"))
cli::cat_line(x)

invisible(NULL)
}



#' @noRd

msg_line <- function(..., indent = "") {

x <- paste(...)
x <- paste0(indent, x)
cli::cat_line(x)

invisible(NULL)
}


#' @noRd

msg_value <- function(...) {

x <- unlist(list(...))

if (is.character(x)) {
x <- encodeString(x, quote = "'")
}

invisible(paste0(crayon::blue(x), collapse = ", "))
}



#' @noRd

msg_code <- function(...) {

x <- unlist(list(...))

if (is.character(x)) {
x <- encodeString(x, quote = "`")
}

invisible(paste0(crayon::silver(x), collapse = ", "))
}



#' @noRd

msg_bullet <- function(x, indent = "", bullet = cli::symbol$"bullet") {

bullet <- paste0(indent, bullet, " ")
msg_indent(x, bullet, " ")
}



#' @noRd

msg_indent <- function (x, first = " ", indent = first) {

x <- gsub("\n\\s", "\n", x)
x <- gsub("\n", paste0("\n", indent), x)
paste0(first, x)
}
4 changes: 2 additions & 2 deletions man/add_deps.Rd

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

0 comments on commit b8f624c

Please sign in to comment.