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

Use cli functions #2378

Merged
merged 20 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 12 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: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Depends:
Imports:
bslib (>= 0.3.1),
callr (>= 3.7.3),
cli,
cli (>= 3.6.1),
desc (>= 1.4.0),
digest,
downlit (>= 0.4.0),
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# pkgdown (development version)

* Use [cli](https://github.com/r-lib/cli) to provide interactive feedback.
* Preserve Markdown code blocks with class rmd from roxygen2 docs (@salim-b, #2298).
* Avoid unwanted linebreaks from parsing `DESCRIPTION` (@salim-b, #2247).
* Remove redundant entries in the documentation index when multiple explicit `@usage` tags are provided (@klmr, #2302)
Expand Down
17 changes: 7 additions & 10 deletions R/build-articles.R
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ build_articles <- function(pkg = ".",
return(invisible())
}

rule("Building articles")
cli::cli_h2("Building articles")
jayhesselberth marked this conversation as resolved.
Show resolved Hide resolved

build_articles_index(pkg)
purrr::walk(
Expand Down Expand Up @@ -207,7 +207,7 @@ build_article <- function(name,
# allow code sharing with building of the index.
vig <- match(name, pkg$vignettes$name)
if (is.na(vig)) {
stop("Can't find article called ", src_path(name), call. = FALSE)
cli::cli_abort("Can't find article {.file {name}}")
}

input <- pkg$vignettes$file_in[vig]
Expand Down Expand Up @@ -368,12 +368,8 @@ data_articles_index <- function(pkg = ".") {
missing <- setdiff(pkg$vignettes$name, c(listed, pkg$package))

if (length(missing) > 0) {
abort(
paste0(
"Vignettes missing from index: ",
paste(missing, collapse = ", ")
),
call. = FALSE
cli::cli_abort(
"Vignette{?s} missing from index: {missing}"
)
jayhesselberth marked this conversation as resolved.
Show resolved Hide resolved
}

Expand All @@ -385,7 +381,9 @@ data_articles_index <- function(pkg = ".") {

data_articles_index_section <- function(section, pkg) {
if (!set_contains(names(section), c("title", "contents"))) {
abort("Section must have components `title`, `contents`")
cli::cli_abort(
"Section must have components {.field title, contents}"
jayhesselberth marked this conversation as resolved.
Show resolved Hide resolved
)
}

# Match topics against any aliases
Expand Down Expand Up @@ -424,7 +422,6 @@ default_articles_index <- function(pkg = ".") {
return(NULL)
}


print_yaml(list(
list(
title = tr_("All vignettes"),
Expand Down
34 changes: 19 additions & 15 deletions R/build-favicons.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,23 @@ build_favicons <- function(pkg = ".", overwrite = FALSE) {
rlang::check_installed("openssl")
pkg <- as_pkgdown(pkg)

rule("Building favicons")
cli::cli_h2("Building favicons")

logo_path <- find_logo(pkg$src_path)

if (is.null(logo_path)) {
stop("Can't find package logo PNG or SVG to build favicons.", call. = FALSE)
cli::cli_abort("Can't find package logo PNG or SVG to build favicons.")
jayhesselberth marked this conversation as resolved.
Show resolved Hide resolved
}

if (has_favicons(pkg) && !overwrite) {
message("Favicons already exist in `pkgdown/`. Set `overwrite = TRUE` to re-create.")
cli::cli_inform(c(
"Favicons already exist in {.path pkgdown}",
"i" = "Set {.var overwrite = TRUE} to re-create."
))
return(invisible())
}

message("Building favicons with realfavicongenerator.net...")
cli::cli_inform("Building favicons with {.url https://realfavicongenerator.net} ...")

logo <- readBin(logo_path, what = "raw", n = fs::file_info(logo_path)$size)

Expand Down Expand Up @@ -66,17 +69,18 @@ build_favicons <- function(pkg = ".", overwrite = FALSE) {
quiet = TRUE
)
if (httr::http_error(resp)) {
stop("API request failed.", call. = FALSE)
cli::cli_abort("API request failed.")
jayhesselberth marked this conversation as resolved.
Show resolved Hide resolved
}

content <- httr::content(resp)
result <- content$favicon_generation_result

if (!identical(result$result$status, "success")) {
stop(
"API request failed. ", "
Please submit bug report to <https://github.com/r-lib/pkgdown/issues>",
call. = FALSE
cli::cli_abort(
c(
"API request failed.",
"i" = "{.href [Please submit a bug report](https://github.com/r-lib/pkgdown/issues)}"
)
)
}

Expand All @@ -93,10 +97,10 @@ build_favicons <- function(pkg = ".", overwrite = FALSE) {
utils::unzip(tmp, exdir = path(pkg$src_path, "pkgdown", "favicon"))
},
warning = function(e) {
abort("Your logo file couldn't be processed and may be corrupt.", parent = e)
cli::cli_abort("Your logo file couldn't be processed and may be corrupt.")
jayhesselberth marked this conversation as resolved.
Show resolved Hide resolved
},
error = function(e) {
abort("Your logo file couldn't be processed and may be corrupt.", parent = e)
cli::cli_abort("Your logo file couldn't be processed and may be corrupt.")
})

invisible()
Expand All @@ -107,10 +111,10 @@ build_favicons <- function(pkg = ".", overwrite = FALSE) {
#' @inheritParams build_favicons
#' @export
build_favicon <- function(pkg, overwrite) {
message(
"`build_favicon()` is deprecated as of pkgdown 1.4.0. ",
"Please use `build_favicons()` instead."
)
cli::cli_warn(c(
jayhesselberth marked this conversation as resolved.
Show resolved Hide resolved
"{.fun build_favicon} is deprecated as of pkgdown 1.4.0. ",
"i" = "Please use {.fun build_favicons} instead."
))
build_favicons(pkg, overwrite)
}

Expand Down
4 changes: 2 additions & 2 deletions R/build-github.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ build_site_github_pages <- function(pkg = ".",
pkg <- as_pkgdown(pkg, override = list(destination = dest_dir))

if (clean) {
rule("Cleaning files from old site", line = 1)
cli::cli_h2("Cleaning files from old site")
clean_site(pkg)
}

Expand All @@ -36,7 +36,7 @@ build_site_github_pages <- function(pkg = ".",
}

build_github_pages <- function(pkg = ".") {
rule("Extra files for GitHub pages")
cli::cli_h2("Extra files for GitHub pages")
pkg <- as_pkgdown(pkg)

# Add .nojekyll since site is static HTML
Expand Down
10 changes: 5 additions & 5 deletions R/build-home-authors.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ data_home_sidebar_authors <- function(pkg = ".") {
bullets <- c(
markdown_text_inline(
pkg$meta$authors$sidebar$before,
pkgdown_field(pkg, c("authors", "sidebar", "before"))
jayhesselberth marked this conversation as resolved.
Show resolved Hide resolved
pkgdown_field(c("authors", "sidebar", "before"))
),
authors,
markdown_text_inline(
pkg$meta$authors$sidebar$after,
pkgdown_field(pkg, c("authors", "sidebar", "after"))
pkgdown_field(c("authors", "sidebar", "after"))
)
)

Expand Down Expand Up @@ -96,7 +96,7 @@ author_name <- function(x, authors, pkg) {
if (!is.null(author$html)) {
name <- markdown_text_inline(
author$html,
pkgdown_field(pkg, c("authors", name, "html"))
pkgdown_field(c("authors", name, "html"))
)
}

Expand Down Expand Up @@ -186,8 +186,8 @@ role_lookup <- function(abbr) {

out <- unname(roles[abbr])
if (any(is.na(out))) {
missing <- paste0("'", abbr[is.na(out)], "'", collapse = ", ")
warn(paste0("Unknown MARC role abbreviation ", missing))
missing <- abbr[is.na(out)]
cli::cli_warn("Unknown MARC role abbreviation{?s}: {.field {missing}}")
out[is.na(out)] <- abbr[is.na(out)]
}
out
Expand Down
21 changes: 10 additions & 11 deletions R/build-home-index.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,12 @@ data_home_sidebar <- function(pkg = ".") {

if (length(html_path)) {
if (!file.exists(html_path)) {
abort(
sprintf(
"Can't find file '%s' specified by %s.",
pkg$meta$home$sidebar$html,
pkgdown_field(pkg, c("home", "sidebar", "html"))
)
)
rel_html_path <- fs::path_rel(html_path, pkg$src_path)
rel_config_path <- pkgdown_config_relpath(pkg)
cli::cli_abort(c(
"Can't locate {.file {rel_html_path}}",
jayhesselberth marked this conversation as resolved.
Show resolved Hide resolved
"x" = "{.field {pkgdown_field(c('home', 'sidebar', 'html'))}} in {.file {rel_config_path}} is misconfigured"
jayhesselberth marked this conversation as resolved.
Show resolved Hide resolved
))
}
return(read_file(html_path))
}
Expand Down Expand Up @@ -215,10 +214,10 @@ check_missing_images <- function(pkg, src_path, dst_path) {
exists <- fs::file_exists(path(pkg$dst_path, rel_path))

if (any(!exists)) {
paths <- encodeString(rel_src[!exists], quote = "'")
warn(c(
paste0("Missing images in '", src_path, "': ", paste0(paths, collapse = ", ")),
i = "pkgdown can only use images in 'man/figures' and 'vignettes'"
paths <- rel_src[!exists]
cli::cli_warn(c(
"Missing images in {.file {src_path}}: {.file {paths}}",
"i" = "pkgdown can only use images in {.file man/figures} and {.file vignettes}"
))
}
}
2 changes: 1 addition & 1 deletion R/build-home-md.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ build_home_md <- function(pkg) {
}

render_md <- function(pkg, filename) {
cat_line("Reading ", src_path(path_rel(filename, pkg$src_path)))
cli::cli_alert_info("Reading {src_path(path_rel(filename, pkg$src_path))}")
jayhesselberth marked this conversation as resolved.
Show resolved Hide resolved

body <- markdown_body(filename, strip_header = TRUE)
path <- path_ext_set(basename(filename), "html")
Expand Down
2 changes: 1 addition & 1 deletion R/build-home.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ build_home <- function(pkg = ".",
quiet = TRUE) {

pkg <- section_init(pkg, depth = 0L, override = override)
rule("Building home")
cli::cli_h2("Building home")
dir_create(pkg$dst_path)

build_citation_authors(pkg)
Expand Down
10 changes: 5 additions & 5 deletions R/build-news.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ build_news <- function(pkg = ".",
if (!has_news(pkg$src_path))
return()

rule("Building news")
cli::cli_h2("Building news")
dir_create(path(pkg$dst_path, "news"))

switch(news_style(pkg$meta),
Expand Down Expand Up @@ -151,7 +151,7 @@ data_news <- function(pkg = list()) {
sections <- xml2::xml_find_all(xml, "./body/div")
footnotes <- has_class(sections, "footnotes")
if (any(footnotes)) {
warn("Footnotes in NEWS.md are not currently supported")
cli::cli_warn("Footnotes in NEWS.md are not currently supported")
}
sections <- sections[!footnotes]

Expand All @@ -160,10 +160,10 @@ data_news <- function(pkg = list()) {
xml2::xml_name()
ulevels <- unique(levels)
if (!identical(ulevels, "h1") && !identical(ulevels, "h2")) {
abort(c(
cli::cli_abort(c(
"Invalid NEWS.md: inconsistent use of section headings.",
i = "Top-level headings must be either all <h1> or all <h2>.",
i = "See ?build_news for more details."
"i" = "Top-level headings must be either all <h1> or all <h2>.",
jayhesselberth marked this conversation as resolved.
Show resolved Hide resolved
"i" = "See {.help pkgdown::build_news} for more details."
))
}
if (ulevels == "h1") {
Expand Down
12 changes: 4 additions & 8 deletions R/build-redirects.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ build_redirects <- function(pkg = ".",
return(invisible())
}

rule("Building redirects")
cli::cli_h2("Building redirects")
if (is.null(pkg$meta$url)) {
abort(sprintf("%s required to generate redirects", pkgdown_field(pkg, "url")))
cli::cli_abort("{.field {pkgdown_field('url')}} in {.file {pkgdown_config_relpath(pkg)}} required to generate redirects")
}

purrr::iwalk(
Expand All @@ -25,12 +25,8 @@ build_redirects <- function(pkg = ".",

build_redirect <- function(entry, index, pkg) {
if (!is.character(entry) || length(entry) != 2) {
abort(
sprintf(
"Entry %s in %s must be a character vector of length 2.",
index,
pkgdown_field(pkg, "redirects")
)
cli::cli_abort(
"Entry {.emph {index}} in {.field {pkgdown_field('redirects')}} must be a character vector of length 2.",
)
}

Expand Down
30 changes: 8 additions & 22 deletions R/build-reference-index.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,10 @@ check_all_characters <- function(contents, index, pkg) {
any_null <- any(null)

if (any_null) {
abort(
c(
sprintf(
"Item %s in section %s in %s is empty.",
toString(which(null)),
index,
pkgdown_field(pkg, "reference")
),
i = "Either delete the empty line or add a function name."
)
)
cli::cli_abort(c(
"Item {.field {which(null)}} in section {index} in {.field {pkgdown_field('reference')}} is empty.",
i = "Either delete the empty line or add a function name in {.file {pkgdown_config_relpath(pkg)}}."
))
}

not_char <- !purrr::map_lgl(contents, is.character)
Expand All @@ -89,17 +82,10 @@ check_all_characters <- function(contents, index, pkg) {
return(invisible())
}

abort(
c(
sprintf(
"Item %s in section %s in %s must be a character.",
toString(which(not_char)),
index,
pkgdown_field(pkg, "reference")
),
i = "You might need to add '' around e.g. - 'N' or - 'off'."
)
)
cli::cli_abort(c(
"Item {.field {which(not_char)}} in section {index} in {.field {pkgdown_field('reference')}} must be a character.",
i = "You might need to add '' around e.g. - 'N' or - 'off' in {.file {pkgdown_config_relpath(pkg)}}."
))
jayhesselberth marked this conversation as resolved.
Show resolved Hide resolved

}

Expand Down
6 changes: 3 additions & 3 deletions R/build-reference.R
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,11 @@ build_reference <- function(pkg = ".",
pkg <- section_init(pkg, depth = 1L, override = override)

if (!missing(document)) {
warning("`document` is deprecated. Please use `devel` instead.", call. = FALSE)
cli::cli_warn("{.var document} is deprecated. Please use {.var devel} instead.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should really use lifecycle::deprecate_warn(), but that might be easier in a separate PR.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like this?

  if (document != "DEPRECATED") {
    lifecycle::deprecate_warn(
      "1.4.0",
      "build_site(document)",
      details = "Please use `build_site(devel)` instead."
    )
    devel <- document
  }

devel <- document
}

rule("Building function reference")
cli::cli_h2("Building function reference")
build_reference_index(pkg)

copy_figures(pkg)
Expand Down Expand Up @@ -274,7 +274,7 @@ build_reference_topic <- function(topic,
if (lazy && !out_of_date(in_path, out_path))
return(invisible())

cat_line("Reading ", src_path("man", topic$file_in))
cli::cli_alert_info("Reading {src_path(path('man', topic$file_in))}")

data <- withCallingHandlers(
data_reference_topic(
Expand Down
4 changes: 2 additions & 2 deletions R/build-search-docs.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ build_sitemap <- function(pkg = ".") {
}

xml_path <- path(pkg$dst_path, "sitemap.xml")
cat_line("Writing ", dst_path(path_rel(xml_path, pkg$dst_path)))
cli::cli_alert_info("Writing {dst_path(path_rel(xml_path, pkg$dst_path))}")

xml2::write_xml(doc, file = xml_path)

Expand Down Expand Up @@ -86,7 +86,7 @@ url_node <- function(url) {
build_search <- function(pkg = ".",
override = list()) {
pkg <- section_init(pkg, depth = 1L, override = override)
rule("Building search index")
cli::cli_alert("Building search index")
jayhesselberth marked this conversation as resolved.
Show resolved Hide resolved
search_index <- build_search_index(pkg)
jsonlite::write_json(
search_index,
Expand Down
Loading
Loading