diff --git a/R/build-articles.R b/R/build-articles.R index 32c60612d..b0e6240e4 100644 --- a/R/build-articles.R +++ b/R/build-articles.R @@ -177,7 +177,7 @@ build_articles <- function(pkg = ".", return(invisible()) } - rule("Building articles") + cli::cli_h2("Building articles") build_articles_index(pkg) purrr::walk( @@ -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 {ui_path(name)}") } input <- pkg$vignettes$file_in[vig] @@ -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}" ) } @@ -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}" + ) } # Match topics against any aliases @@ -424,7 +422,6 @@ default_articles_index <- function(pkg = ".") { return(NULL) } - print_yaml(list( list( title = tr_("All vignettes"), diff --git a/R/build-favicons.R b/R/build-favicons.R index f1e9fda82..3245b3e9d 100644 --- a/R/build-favicons.R +++ b/R/build-favicons.R @@ -19,20 +19,23 @@ build_favicons <- function(pkg = ".", overwrite = FALSE) { rlang::check_installed("openssl") pkg <- as_pkgdown(pkg) - rule("Building favicons") + cli::cli_rule("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.") } 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) @@ -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.") } content <- httr::content(resp) result <- content$favicon_generation_result if (!identical(result$result$status, "success")) { - stop( - "API request failed. ", " - Please submit bug report to ", - call. = FALSE + cli::cli_abort( + c( + "API request failed.", + "i" = "{.href [Please submit a bug report](https://github.com/r-lib/pkgdown/issues)}" + ) ) } @@ -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.") }, 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() @@ -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( + "{.fun build_favicon} is deprecated as of pkgdown 1.4.0. ", + "i" = "Please use {.fun build_favicons} instead." + )) build_favicons(pkg, overwrite) } diff --git a/R/build-github.R b/R/build-github.R index 1aff9055a..76111e896 100644 --- a/R/build-github.R +++ b/R/build-github.R @@ -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_rule("Cleaning files from old site") clean_site(pkg) } @@ -36,7 +36,7 @@ build_site_github_pages <- function(pkg = ".", } build_github_pages <- function(pkg = ".") { - rule("Extra files for GitHub pages") + cli::cli_rule("Extra files for GitHub pages") pkg <- as_pkgdown(pkg) # Add .nojekyll since site is static HTML diff --git a/R/build-home-authors.R b/R/build-home-authors.R index dd1763ad9..79fbdf72e 100644 --- a/R/build-home-authors.R +++ b/R/build-home-authors.R @@ -187,7 +187,7 @@ 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)) + cli::cli_alert_warning("Unknown MARC role abbreviation{?s}: {missing}") out[is.na(out)] <- abbr[is.na(out)] } out diff --git a/R/build-home-index.R b/R/build-home-index.R index 3c39aca2a..d98249a30 100644 --- a/R/build-home-index.R +++ b/R/build-home-index.R @@ -64,12 +64,8 @@ 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")) - ) + cli::cli_abort( + "Can't find file {.file {html_path}} specified by {pkgdown_field(pkg, c('home', 'sidebar', 'html'))}" ) } return(read_file(html_path)) @@ -215,10 +211,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}" )) } } diff --git a/R/build-home-md.R b/R/build-home-md.R index 38c9d461e..04e9a60b0 100644 --- a/R/build-home-md.R +++ b/R/build-home-md.R @@ -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))}") body <- markdown_body(filename, strip_header = TRUE) path <- path_ext_set(basename(filename), "html") diff --git a/R/build-home.R b/R/build-home.R index c987490e9..e287bc929 100644 --- a/R/build-home.R +++ b/R/build-home.R @@ -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) diff --git a/R/build-news.R b/R/build-news.R index 27a0fac7a..614b0929c 100644 --- a/R/build-news.R +++ b/R/build-news.R @@ -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), @@ -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] @@ -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

or all

.", - i = "See ?build_news for more details." + "i" = "Top-level headings must be either all

or all

.", + "i" = "See {.help pkgdown::build_news} for more details." )) } if (ulevels == "h1") { diff --git a/R/build-redirects.R b/R/build-redirects.R index 408ff09f0..f8e15f582 100644 --- a/R/build-redirects.R +++ b/R/build-redirects.R @@ -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(sprintf("%s required to generate redirects", pkgdown_field(pkg, "url"))) } purrr::iwalk( @@ -25,7 +25,7 @@ build_redirects <- function(pkg = ".", build_redirect <- function(entry, index, pkg) { if (!is.character(entry) || length(entry) != 2) { - abort( + cli::cli_abort( sprintf( "Entry %s in %s must be a character vector of length 2.", index, diff --git a/R/build-reference.R b/R/build-reference.R index add462c13..d6aa69d8e 100644 --- a/R/build-reference.R +++ b/R/build-reference.R @@ -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.") devel <- document } - rule("Building function reference") + cli::cli_h2("Building function reference") build_reference_index(pkg) copy_figures(pkg) @@ -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( diff --git a/R/build-search-docs.R b/R/build-search-docs.R index cb61ed127..6dab99c40 100644 --- a/R/build-search-docs.R +++ b/R/build-search-docs.R @@ -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) @@ -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") search_index <- build_search_index(pkg) jsonlite::write_json( search_index, diff --git a/R/build-tutorials.R b/R/build-tutorials.R index 8138c23cd..caeacdfaf 100644 --- a/R/build-tutorials.R +++ b/R/build-tutorials.R @@ -35,7 +35,7 @@ build_tutorials <- function(pkg = ".", override = list(), preview = NA) { return(invisible()) } - rule("Building tutorials") + cli::cli_h2("Building tutorials") dir_create(path(pkg$dst_path, "tutorials")) data <- purrr::transpose(tutorials) diff --git a/R/build.R b/R/build.R index a44a0a99a..188c3afc7 100644 --- a/R/build.R +++ b/R/build.R @@ -328,13 +328,13 @@ build_site <- function(pkg = ".", pkg <- as_pkgdown(pkg, 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.") devel <- document } if (install) { withr::local_temp_libpaths() - rule("Installing package into temporary library") + cli::cli_h2("Installing package {.pkg {pkg$package}} into temporary library") # Keep source, so that e.g. pillar can show the source code # of its functions in its articles withr::with_options( @@ -404,6 +404,7 @@ build_site_external <- function(pkg = ".", timeout = getOption('pkgdown.timeout', Inf) ) + cli::cli_alert_success("finished building pkgdown site for {.pkg {pkg$package}}.") preview_site(pkg, preview = preview) invisible() } @@ -420,9 +421,9 @@ build_site_local <- function(pkg = ".", pkg <- section_init(pkg, depth = 0, override = override) - rule("Building pkgdown site", line = "=") - cat_line("Reading from: ", src_path(path_abs(pkg$src_path))) - cat_line("Writing to: ", dst_path(path_abs(pkg$dst_path))) + cli::cli_h1("Building pkgdown site for {.pkg {pkg$package}}") + cli::cli_alert("Reading from: {src_path(path_abs(pkg$src_path))}") + cli::cli_alert("Writing to: {dst_path(path_abs(pkg$dst_path))}") init_site(pkg) @@ -447,6 +448,6 @@ build_site_local <- function(pkg = ".", build_search(pkg, override = override) } - rule("DONE", line = "=") + cli::cli_alert_success("finished building pkgdown site for {.pkg {pkg$package}}.") preview_site(pkg, preview = preview) } diff --git a/R/check.R b/R/check.R index 0cfb38a05..92334b69b 100644 --- a/R/check.R +++ b/R/check.R @@ -12,6 +12,6 @@ check_pkgdown <- function(pkg = ".") { data_articles_index(pkg) data_reference_index(pkg) - inform("No problems found") + cli::cli_alert_success("No problems found in {.file _pkgdown.yml}") } diff --git a/R/clean.R b/R/clean.R index d70d02ad9..19f15dd7c 100644 --- a/R/clean.R +++ b/R/clean.R @@ -16,5 +16,6 @@ clean_site <- function(pkg = ".") { dir_delete(top_level[is_dir]) file_delete(top_level[!is_dir]) + cli::cli_alert_success("Site cleaned for {.pkg {pkg$package}} (emptied {.path {pkg$dst_path}})") invisible(TRUE) } diff --git a/R/deploy-site.R b/R/deploy-site.R index fab5d892c..266c7cad0 100644 --- a/R/deploy-site.R +++ b/R/deploy-site.R @@ -43,31 +43,31 @@ deploy_site_github <- function( repo_slug = Sys.getenv("TRAVIS_REPO_SLUG", "")) { rlang::check_installed("openssl") if (!nzchar(tarball)) { - stop("No built tarball detected, please provide the location of one with `tarball`", call. = FALSE) + cli::cli_abort("No built tarball detected, please provide the location of one with {.var tarball}") } if (!nzchar(ssh_id)) { - stop("No deploy key found, please setup with `travis::use_travis_deploy()`", call. = FALSE) + cli::cli_abort("No deploy key found, please setup with {.fn travis::use_travis_deploy}") } if (!nzchar(repo_slug)) { - stop("No repo detected, please supply one with `repo_slug`", call. = FALSE) + cli::cli_abort("No repo detected, please supply one with {.var repo_slug}") } - rule("Deploying site", line = 2) + cli::cli_rule("Deploying site to GitHub") if (install) { - rule("Installing package", line = 1) + cli::cli_inform("Installing package") callr::rcmd("INSTALL", tarball, show = verbose, fail_on_status = TRUE) } ssh_id_file <- "~/.ssh/id_rsa" - rule("Setting up SSH id", line = 1) - cat_line("Copying private key to: ", ssh_id_file) + cli::cli_alert_info("Setting up SSH id") + cli::cli_alert_info("Copying private key to {.file ssh_id_file}") write_lines(rawToChar(openssl::base64_decode(ssh_id)), ssh_id_file) - cat_line("Setting private key permissions to 0600") + cli::cli_alert_info("Setting private key permissions to 0600") fs::file_chmod(ssh_id_file, "0600") - cat_line("Setting remote to use the ssh url") + cli::cli_alert_info("Setting remote to use the ssh url") git("remote", "set-url", "origin", sprintf("git@%s:%s.git", host, repo_slug)) @@ -79,7 +79,7 @@ deploy_site_github <- function( ... ) - rule("Deploy completed", line = 2) + cli::cli_alert_success("Deploy completed") } #' Build and deploy a site locally @@ -158,13 +158,14 @@ deploy_to_branch <- function(pkg = ".", git_has_remote_branch <- function(remote, branch) { has_remote_branch <- git("ls-remote", "--quiet", "--exit-code", remote, branch, echo = FALSE, echo_cmd = FALSE, error_on_status = FALSE)$status == 0 } + git_current_branch <- function() { branch <- git("rev-parse", "--abbrev-ref", "HEAD", echo = FALSE, echo_cmd = FALSE)$stdout sub("\n$", "", branch) } github_worktree_add <- function(dir, remote, branch) { - rule("Adding worktree", line = 1) + cli::cli_alert_info("Adding worktree") git("worktree", "add", "--track", "-B", branch, @@ -174,7 +175,7 @@ github_worktree_add <- function(dir, remote, branch) { } github_worktree_remove <- function(dir) { - rule("Removing worktree", line = 1) + cli::cli_alert_info("Removing worktree") git("worktree", "remove", dir) } @@ -182,13 +183,13 @@ github_push <- function(dir, commit_message, remote, branch) { # force execution before changing working directory force(commit_message) - rule("Commiting updated site", line = 1) + cli::cli_alert_info("Commiting updated site") withr::with_dir(dir, { git("add", "-A", ".") git("commit", "--allow-empty", "-m", commit_message) - rule("Deploying to GitHub Pages", line = 1) + cli::cli_alert("Deploying to GitHub Pages") git("remote", "-v") git("push", "--force", remote, paste0("HEAD:", branch)) }) @@ -201,7 +202,7 @@ git <- function(..., echo_cmd = TRUE, echo = TRUE, error_on_status = TRUE) { construct_commit_message <- function(pkg, commit = ci_commit_sha()) { pkg <- as_pkgdown(pkg) - sprintf("Built site for %s: %s@%s", pkg$package, pkg$version, substr(commit, 1, 7)) + cli::cli_alert_success(sprintf("Built site for %s: %s@%s", pkg$package, pkg$version, substr(commit, 1, 7))) } ci_commit_sha <- function() { diff --git a/R/figure.R b/R/figure.R index 280ae73a4..f55ae4e32 100644 --- a/R/figure.R +++ b/R/figure.R @@ -136,12 +136,12 @@ match_fun <- function(x) { f <- eval(e, globalenv()) if (!is.function(f)) { - stop("`x` must evaluate to a function", call. = FALSE) + cli::cli_abort("{.var x} must evaluate to a function") } f } else { - stop("`x` must be a function or string", call. = FALSE) + cli::cli_abort("{.var x} must be a function or string") } } @@ -157,6 +157,6 @@ fun_name <- function(x) { } else if (is_call(expr, "::")) { as.character(expr[[3]]) } else { - stop("Unknown input", call. = FALSE) + cli::cli_abort("Unknown input") } } diff --git a/R/init.R b/R/init.R index bff37086a..eb411975d 100644 --- a/R/init.R +++ b/R/init.R @@ -24,10 +24,10 @@ init_site <- function(pkg = ".") { pkg <- as_pkgdown(pkg) if (is_non_pkgdown_site(pkg$dst_path)) { - stop(dst_path(pkg$dst_path), " is non-empty and not built by pkgdown", call. = FALSE) + cli::cli_abort("{.file {pkg$dst_path}} is non-empty and not built by pkgdown") } - rule("Initialising site") + cli::cli_h2("Initialising site") dir_create(pkg$dst_path) copy_assets(pkg) diff --git a/R/markdown.R b/R/markdown.R index c0ce0b7dd..2c8189a9e 100644 --- a/R/markdown.R +++ b/R/markdown.R @@ -16,12 +16,8 @@ markdown_text_inline <- function(text, where = "", ...) { children <- xml2::xml_children(xml2::xml_find_first(html, ".//body")) if (length(children) > 1) { - abort( - sprintf( - "Can't use a block element in %s, need an inline element: \n%s", - where, - text - ) + cli::cli_abort( + "Can't use a block element in {.var {where}}, need an inline element: {.var {text}}", ) } diff --git a/R/package.R b/R/package.R index 82c9c83f6..0f06e3401 100644 --- a/R/package.R +++ b/R/package.R @@ -13,7 +13,7 @@ as_pkgdown <- function(pkg = ".", override = list()) { } if (!dir_exists(pkg)) { - stop("`pkg` is not an existing directory", call. = FALSE) + cli::cli_abort("{.file {pkg}} is not an existing directory") } desc <- read_desc(pkg) @@ -89,7 +89,7 @@ is_pkgdown <- function(x) inherits(x, "pkgdown") read_desc <- function(path = ".") { path <- path(path, "DESCRIPTION") if (!file_exists(path)) { - stop("Can't find DESCRIPTION", call. = FALSE) + cli::cli_abort("Can't find {.file DESCRIPTION}") } desc::description$new(path) } @@ -100,16 +100,13 @@ check_bootstrap_version <- function(version, pkg = list()) { } else if (version %in% c(3, 5)) { version } else if (version == 4) { - warn("`bootstrap: 4` no longer supported; using `bootstrap: 5` instead") + cli::cli_warn("{.var bootstrap: 4} no longer supported, using {.var bootstrap: 5} instead") 5 } else { - abort(c( + field <- pkgdown_field(pkg, c("template", "bootstrap")) + cli::cli_abort(c( "Boostrap version must be 3 or 5.", - x = sprintf( - "You specified a value of %s in %s.", - version, - pkgdown_field(pkg, c("template", "bootstrap")) - ) + "x" = "You specified a value of {.var {version}} in {.var {field}}" )) } } @@ -290,12 +287,11 @@ check_unique_article_paths <- function(file_in, file_out) { same_out_bullets <- purrr::map_chr(file_out_dup, function(f_out) { src_files <- src_path(file_in[which(file_out == f_out)]) src_files <- paste(src_files, collapse = " and ") - sprintf("%s both create %s", src_files, dst_path(f_out)) }) names(same_out_bullets) <- rep_len("x", length(same_out_bullets)) - rlang::abort(c( - "Rendered articles must have unique names. Rename or relocate one of the following source files:", + cli::cli_abort(c( + "Rendered articles must have unique names. Rename or relocate:", same_out_bullets )) } diff --git a/R/preview.R b/R/preview.R index 7129d5ce4..f4e7fc9d6 100644 --- a/R/preview.R +++ b/R/preview.R @@ -11,7 +11,7 @@ preview_site <- function(pkg = ".", path = ".", preview = NA) { } if (preview) { - rule("Previewing site") + cli::cli_alert("Previewing site") utils::browseURL(path(pkg$dst_path, path, "index.html")) } diff --git a/R/rd-example.R b/R/rd-example.R index e1fd10a7a..d3f1e8566 100644 --- a/R/rd-example.R +++ b/R/rd-example.R @@ -29,7 +29,7 @@ run_examples <- function(x, code <- flatten_ex(x, run_dont_run = run_dont_run) if (!can_parse(code)) { - warning("Failed to parse example for topic '", topic, "'", call. = FALSE) + cli::cli_warn("Failed to parse example for topic {.var {topic}}") return("") } @@ -48,13 +48,13 @@ process_conditional_examples <- function(rd) { grepl("# examplesIf$", x[[1]]) })) if (length(which_exif) == 0) return(rd) - if (length(which_exif) %% 2 != 0) stop("@examplesIf error, not closed?") + if (length(which_exif) %% 2 != 0) cli::cli_abort("@examplesIf error, not closed?") remove <- integer() modes <- c("begin", "end") for (idx in which_exif) { if (rd[[idx]] != "}) # examplesIf") { # Start of @examplesIf - if (modes[1] == "end") stop("@examplesIf error, not closed?") + if (modes[1] == "end") cli::cli_abort("@examplesIf error, not closed?") cond_expr <- parse(text = paste0(rd[[idx]], "\n})"))[[1]][[2]] cond <- eval(cond_expr) if (isTRUE(cond)) { @@ -64,10 +64,8 @@ process_conditional_examples <- function(rd) { is_false <- cond_expr_str == "FALSE" if (!is_false) { new_cond <- paste0("if (FALSE) { # ", cond_expr_str) - warning( - "@examplesIf condition `", - cond_expr_str, - "` is FALSE" + cli::cli_warn( + "@examplesIf condition {.var cond_expr_str} FALSE" ) } else { new_cond <- "if (FALSE) {" @@ -76,7 +74,7 @@ process_conditional_examples <- function(rd) { } } else { # End of @examplesIf - if (modes[1] == "begin") stop("@examplesIf error, closed twice?") + if (modes[1] == "begin") cli::cli_abort("@examplesIf error, closed twice?") if (isTRUE(cond)) { remove <- c(remove, idx, idx + 1L) } else { @@ -110,11 +108,12 @@ as_example.TEXT <- as_example.RCODE #' @export as_example.COMMENT <- function(x, run_dont_run = FALSE) { if (grepl("^%[^ ]*%", x)) { - warning( - "In the examples, ", unclass(x), "\n", - "is an Rd comment: did you mean ", gsub("%", "\\\\%", x), " ?", - call. = FALSE - ) + meant <- gsub("%", "\\\\%", x) + xun <- unclass(x) + cli::cli_warn(c( + "In the examples, {.var {xun}} is an Rd comment", + "x" = "did you mean {.var {meant}}?" + )) } "" } @@ -155,8 +154,8 @@ block_tag_to_comment <- function(tag, x, run_dont_run = FALSE) { #' @export as_example.tag <- function(x, run_dont_run = FALSE) { - warning("Unknown tag: ", paste(class(x), collapse = "/"), call. = FALSE) - "" + untag <- paste(class(x), collapse = "/") + cli::cli_warn("Unknown tag: {.var {untag}}") } #' @export diff --git a/R/rd-html.R b/R/rd-html.R index 6e00c3b93..03a404c67 100644 --- a/R/rd-html.R +++ b/R/rd-html.R @@ -229,7 +229,7 @@ as_html.tag_Sexpr <- function(x, ...) { text = as.character(res), rd = flatten_text(rd_text(as.character(res))), hide = "", - stop("\\Sexpr{result=", results, "} not yet supported", call. = FALSE) + cli::cli_abort("\\Sexpr{result=", results, "} not yet supported", call. = FALSE) ) } @@ -510,8 +510,7 @@ as_html.tag <- function(x, ...) { if (identical(class(x), "tag")) { flatten_text(x, ...) } else { - message("Unknown tag: ", paste(class(x), collapse = "/")) - "" + cli::cli_inform("Unknown tag: ", paste(class(x), collapse = "/")) } } @@ -566,10 +565,12 @@ parse_opts <- function(string) { } stop_bad_tag <- function(tag, msg = NULL) { - abort(c( - paste0("Failed to parse \\", tag, "{}."), - i = msg - )) + bad_tag <- paste0("\\\\", tag, "{}") + msg_abort <- 'Failed to parse tag {.var {bad_tag}}.' + if (!is.null(msg)) { + msg_abort <- c(msg_abort, "x" = msg) + } + cli::cli_abort(msg_abort) } is_newline <- function(x, trim = FALSE) { diff --git a/R/render.R b/R/render.R index fefb96988..c01e44bdc 100644 --- a/R/render.R +++ b/R/render.R @@ -148,47 +148,41 @@ data_open_graph <- function(pkg = ".") { check_open_graph <- function(og) { if (!is.list(og)) { - abort(paste("`opengraph` must be a list, not", friendly_type_of(og))) + fog <- friendly_type_of(og) + cli::cli_abort("{.var opengraph} must be a list, not {.var fog}") } supported_fields <- c("image", "twitter") unsupported_fields <- setdiff(names(og), supported_fields) if (length(unsupported_fields)) { - warn(paste0( - "Unsupported `opengraph` field(s): ", - paste(unsupported_fields, collapse = ", ") - )) + cli::cli_warn( + "Unsupported `opengraph` field{?s}: {unsupported_fields}." + ) } if ("twitter" %in% names(og)) { if (is.character(og$twitter) && length(og$twitter) == 1 && grepl("^@", og$twitter)) { - abort(paste( - "The `opengraph: twitter` option must be a list. Did you mean this?", - "opengraph:", - " twitter:", - paste(" creator:", og$twitter), - sep = "\n" - )) + cli::cli_abort( + "The `opengraph: twitter` option must be a list." + ) } if (!is.list(og$twitter)) { - abort("The `opengraph: twitter` option must be a list.") + cli::cli_abort("The `opengraph: twitter` option must be a list.") } if (is.null(og$twitter$creator) && is.null(og$twitter$site)) { - abort( - "The `opengraph: twitter` option must include either 'creator' or 'site'." + cli::cli_abort( + "{.var opengraph: twitter} must include either 'creator' or 'site'." ) } } if ("image" %in% names(og)) { if (is.character(og$image) && length(og$image) == 1) { - abort(paste( - "The `opengraph: image` option must be a list. Did you mean this?", - "opengraph", - " image:", - paste(" src:", og$image), - sep = "\n" - )) + cli::cli_abort( + "The {.var opengraph: image} option must be a list." + ) } if (!is.list(og$image)) { - abort("The `opengraph: image` option must be a list.") + cli::cli_abort( + "The {.var opengraph: image} option must be a list." + ) } } og[intersect(supported_fields, names(og))] @@ -208,7 +202,7 @@ write_if_different <- function(pkg, contents, path, quiet = FALSE, check = TRUE) if (check && !made_by_pkgdown(full_path)) { if (!quiet) { - message("Skipping '", path, "': not generated by pkgdown") + cli::cli_inform("Skipping {.file {{path}}: not generated by pkgdown") } return(FALSE) } @@ -221,9 +215,9 @@ write_if_different <- function(pkg, contents, path, quiet = FALSE, check = TRUE) } if (!quiet) { - href <- paste0("ide:run:pkgdown::preview_page('", path_rel(full_path, pkg$dst_path), "')") - cat_line("Writing ", cli::style_hyperlink(dst_path(path), href)) + cli::cli_alert_info("Writing {dst_path(path)}") } + write_lines(contents, path = full_path) TRUE } diff --git a/R/rmarkdown.R b/R/rmarkdown.R index f13241862..d2ee63fa0 100644 --- a/R/rmarkdown.R +++ b/R/rmarkdown.R @@ -7,10 +7,10 @@ render_rmarkdown <- function(pkg, input, output, ..., copy_images = TRUE, quiet output_path <- path_abs(output, pkg$dst_path) if (!file_exists(input_path)) { - stop("Can't find ", src_path(input), call. = FALSE) + cli::cli_abort("Can't find {.file {src_path(input)}") } - cat_line("Reading ", src_path(input)) + cli::cli_alert_info("Reading {src_path(input)}") digest <- file_digest(output_path) args <- list( @@ -38,10 +38,10 @@ render_rmarkdown <- function(pkg, input, output, ..., copy_images = TRUE, quiet ) ), error = function(cnd) { - rule("RMarkdown error") + cli::cli_rule("RMarkdown error") cat(gsub("\r", "", cnd$stderr, fixed = TRUE)) - rule() - abort("Failed to render RMarkdown", parent = cnd) + cli::cli_rule() + cli::cli_abort("Failed to render RMarkdown") } ) @@ -55,7 +55,7 @@ render_rmarkdown <- function(pkg, input, output, ..., copy_images = TRUE, quiet } if (digest != file_digest(output_path)) { href <- paste0("ide:run:pkgdown::preview_page('", path_rel(output_path, pkg$dst_path), "')") - cat_line("Writing ", cli::style_hyperlink(dst_path(output), href)) + cli::cli_alert_info("Writing {cli::style_hyperlink(dst_path(output), href)}") } # Copy over images needed by the document diff --git a/R/sitrep.R b/R/sitrep.R index ca123c8cd..973d53a16 100644 --- a/R/sitrep.R +++ b/R/sitrep.R @@ -16,14 +16,14 @@ pkgdown_sitrep <- function(pkg = ".") { pkg_dir <- pkg pkg <- as_pkgdown(pkg_dir) if (is.null(pkg$meta[["url"]])) { - cat(sprintf("* %s not configured.", pkgdown_field(pkg, "url"))) + cli::cli_alert_warning(sprintf("%s is not configured.", pkgdown_field(pkg, "url"))) } else { urls <- desc::desc(pkg_dir)$get_urls() urls <- sub("/$", "", urls) if (!pkg$meta[["url"]] %in% urls) { - cat("* URL missing from the DESCRIPTION URL field.") + cli::cli_alert_warning("URL is missing in the DESCRIPTION URL field.") } else { - cat("All good :-)") + cli::cli_alert_success("pkgdown situation report is all clear") } } diff --git a/R/templates.R b/R/templates.R index 02376ff73..0ef590160 100644 --- a/R/templates.R +++ b/R/templates.R @@ -6,7 +6,7 @@ find_template <- function(type, name, ext = ".html", pkg = ".") { existing <- paths[file_exists(paths)] if (length(existing) == 0) { - abort(paste0("Can't find template for ", type, "-", name, ".")) + cli::cli_abort(paste0("Can't find template for ", type, "-", name, ".")) } existing[[1]] } @@ -43,7 +43,7 @@ templates_dir <- function(pkg = list()) { if (!is.null(template$path)) { # Directory specified in yaml doesn't exist, so eagerly error if (!dir_exists(template$path)) { - abort(paste0("Can not find templates path ", src_path(template$path))) + cli::cli_abort("Can't find templates path: {src_path(template$path)}") } path_abs(template$path, start = pkg$src_path) } else if (!is.null(template$package)) { diff --git a/R/test.R b/R/test.R index 800de5117..50a0f19ff 100644 --- a/R/test.R +++ b/R/test.R @@ -89,7 +89,7 @@ NULL #' @family tests #' @examples #' \dontrun{ -#' stop("This is an error!", call. = FALSE) +#' cli::cli_abort("This is an error!") #' } #' #' # Inline \donttest is silently ommitted @@ -149,8 +149,8 @@ NULL #' a #' #' cat("This some text!\n") -#' message("This is a message!") -#' warning("This is a warning!") +#' cli::cli_inform("This is a message!") +#' cli::cli_warn("This is a warning!") #' #' # This is a multi-line block #' { diff --git a/R/theme.R b/R/theme.R index 197180281..a4232cfc9 100644 --- a/R/theme.R +++ b/R/theme.R @@ -12,7 +12,7 @@ build_bslib <- function(pkg = ".") { data_deps <- function(pkg, depth) { if (!file.exists(data_deps_path(pkg))) { - abort("Run pkgdown::init_site() first.") + cli::cli_abort("Run {.fn pkgdown::init_site} first.") } deps_path <- paste0(up_path(depth), "deps") @@ -56,9 +56,9 @@ bs_theme_rules <- function(pkg) { theme <- purrr::pluck(pkg, "meta", "template", "theme", .default = "arrow-light") theme_path <- path_pkgdown("highlight-styles", paste0(theme, ".scss")) if (!file_exists(theme_path)) { - abort(c( - paste0("Unknown theme '", theme, "'"), - i = paste0("Valid themes are: ", paste0(highlight_styles(), collapse = ", ")) + cli::cli_abort(c( + "Unknown theme: {.var {theme}}", + i = "Valid themes are: {highlight_styles()}" )) } paths <- c(paths, theme_path) @@ -101,7 +101,7 @@ check_bootswatch_theme <- function(bootswatch_theme, bs_version, pkg) { } else if (bootswatch_theme %in% bslib::bootswatch_themes(bs_version)) { bootswatch_theme } else { - abort( + cli::cli_abort( sprintf( "Can't find Bootswatch theme '%s' (%s) for Bootstrap version '%s' (%s).", bootswatch_theme, diff --git a/R/topics-external.R b/R/topics-external.R index b51091992..312f0afa9 100644 --- a/R/topics-external.R +++ b/R/topics-external.R @@ -29,7 +29,8 @@ get_rd_from_help <- function(package, alias) { help <- utils::help((alias), (package)) if (length(help) == 0) { - abort(sprintf("Could not find documentation for %s::%s", package, alias)) + fun <- paste0(package, "::", alias) + cli::cli_abort("Could not find documentation for {.fn fun}.") return() } diff --git a/R/topics.R b/R/topics.R index e7ffb1a51..cbbe8cd81 100644 --- a/R/topics.R +++ b/R/topics.R @@ -10,7 +10,7 @@ select_topics <- function(match_strings, topics, check = FALSE) { # If none of the specified topics have a match, return no topics if (purrr::every(indexes, is_empty)) { if (check) { - abort("No topics matched in '_pkgdown.yml'. No topics selected.") + cli::cli_abort("No topics matched in '_pkgdown.yml'. No topics selected.") } return(integer()) } @@ -51,7 +51,7 @@ all_sign <- function(x, text) { } } - stop("Must be all negative or all positive: ", text, call. = FALSE) + cli::cli_abort("Must be all negative or all positive: {.var {text}}") } match_env <- function(topics) { @@ -161,7 +161,7 @@ match_eval <- function(string, env) { tryCatch( eval(expr, env), error = function(e) { - topic_must("be a known selector function", string, parent = e) + topic_must("be a known selector function", string) } ) } else { @@ -169,14 +169,12 @@ match_eval <- function(string, env) { } } -topic_must <- function(message, topic, ..., call = NULL) { - abort( +topic_must <- function(message, topic) { + cli::cli_abort( c( - paste0("In '_pkgdown.yml', topic must ", message), - x = paste0("Not ", encodeString(topic, quote = "'")) - ), - ..., - call = call + paste0("In {.file _pkgdown.yml}, topic must ", message), + "x" = paste0("Not {.var {topic}}") + ) ) } diff --git a/R/usage.R b/R/usage.R index bec8fd162..8e9b2488c 100644 --- a/R/usage.R +++ b/R/usage.R @@ -28,7 +28,10 @@ parse_usage <- function(x) { parse_exprs(r) }, error = function(e) { - warning("Failed to parse usage:\n", r, call. = FALSE, immediate. = TRUE) + cli::cli_warn(c( + "Failed to parse usage:", + "x" = "{.var {r}}" + )) list() } ) @@ -83,7 +86,8 @@ usage_type <- function(x) { out } else { - stop("Unknown type: ", typeof(x), " (in ", as.character(x), ")", call. = FALSE) + untype <- paste0(typeof(x), " (in ", as.character(x), ")") + cli::cli_abort("Unknown type: {.var {untype}}") } } @@ -127,7 +131,7 @@ fun_info <- function(fun) { name = call_name(fun) ) } else { - stop("Unknown call: ", as.character(x[[1]])) + cli::cli_abort(paste0("Unknown call: ", as.character(x[[1]]))) } } else { list( @@ -158,7 +162,7 @@ usage_code.NULL <- function(x) character() #' @export usage_code.tag <- function(x) { if (!identical(class(x), "tag")) { - stop("Undefined tag in usage ", class(x)[[1]], call. = FALSE) + cli::cli_abort(paste0("Undefined tag in usage ", class(x)[[1]])) } paste0(purrr::flatten_chr(purrr::map(x, usage_code)), collapse = "") } diff --git a/R/utils-fs.R b/R/utils-fs.R index 05f48890c..f938f1ad8 100644 --- a/R/utils-fs.R +++ b/R/utils-fs.R @@ -41,10 +41,10 @@ file_copy_to <- function(pkg, eq <- purrr::map2_lgl(from_paths, to_paths, file_equal) if (any(!eq)) { - cat_line( - "Copying ", src_path(path_rel(from_paths[!eq], pkg$src_path)), - " to ", dst_path(path_rel(to_paths[!eq], pkg$dst_path)) - ) + cli::cli_alert_info(c( + "Copying {src_path(path_rel(from_paths[!eq], pkg$src_path))}", + " to {dst_path(path_rel(to_paths[!eq], pkg$dst_path))}" + )) } file_copy(from_paths[!eq], to_paths[!eq], overwrite = overwrite) @@ -55,7 +55,7 @@ out_of_date <- function(source, target) { return(TRUE) if (!file_exists(source)) { - stop("'", source, "' does not exist", call. = FALSE) + cli::cli_abort("{.fn {source}} does not exist") } file.info(source)$mtime > file.info(target)$mtime diff --git a/R/utils-yaml.R b/R/utils-yaml.R index 1c21bc19f..1a2dcc16c 100644 --- a/R/utils-yaml.R +++ b/R/utils-yaml.R @@ -6,10 +6,9 @@ check_yaml_has <- function(missing, where, pkg) { missing_components <- lapply(missing, function(x) c(where, x)) missing_fields <- pkgdown_fields(pkg, missing_components) - abort(paste0( - "Can't find component", if (length(missing) > 1) "s", " ", - missing_fields, "." - )) + cli::cli_abort( + "Can't find components {missing_fields}." + ) } yaml_character <- function(pkg, where) { @@ -20,7 +19,8 @@ yaml_character <- function(pkg, where) { } else if (is.character(x)) { x } else { - abort(paste0(pkgdown_field(pkg, where), " must be a character vector")) + fld <- pkgdown_field(pkg, where) + cli::cli_abort("{.var {fld}} must be a character vector") } } diff --git a/R/utils.R b/R/utils.R index 6ace99a97..290748126 100644 --- a/R/utils.R +++ b/R/utils.R @@ -59,36 +59,13 @@ devtools_meta <- function(x) { # CLI --------------------------------------------------------------------- -dst_path <- function(...) { - cli::col_blue(encodeString(path(...), quote = "'")) -} - -src_path <- function(...) { - cli::col_green(encodeString(path(...), quote = "'")) -} - -cat_line <- function(...) { - cat(paste0(..., "\n"), sep = "") -} - -rule <- function(x = NULL, line = "-") { - width <- getOption("width") - - if (!is.null(x)) { - prefix <- paste0(line, line, " ") - suffix <- " " - } else { - prefix <- "" - suffix <- "" - x <- "" - } - - line_length <- width - nchar(x) - nchar(prefix) - nchar(suffix) - # protect against negative values which can result in narrow terminals - line_length <- max(0, line_length) - cat_line(prefix, cli::style_bold(x), suffix, strrep(line, line_length)) -} +dst_path <- cli::combine_ansi_styles( + cli::style_bold, cli::col_cyan +) +src_path <- cli::combine_ansi_styles( + cli::style_bold, cli::col_green +) skip_if_no_pandoc <- function(version = "1.12.3") { testthat::skip_if_not(rmarkdown::pandoc_available(version)) diff --git a/man/test-dont.Rd b/man/test-dont.Rd index dbcfb7959..6f23fc3cc 100644 --- a/man/test-dont.Rd +++ b/man/test-dont.Rd @@ -8,7 +8,7 @@ Test case: don't } \examples{ \dontrun{ - stop("This is an error!", call. = FALSE) + cli::cli_abort("This is an error!") } # Inline \donttest is silently ommitted diff --git a/man/test-output-styles.Rd b/man/test-output-styles.Rd index e7655192f..118a229ae 100644 --- a/man/test-output-styles.Rd +++ b/man/test-output-styles.Rd @@ -13,8 +13,8 @@ a <- 1:100 a cat("This some text!\n") -message("This is a message!") -warning("This is a warning!") +cli::cli_inform("This is a message!") +cli::cli_warn("This is a warning!") # This is a multi-line block { diff --git a/tests/testthat/_snaps/build-articles.md b/tests/testthat/_snaps/build-articles.md index 807792066..c3626334b 100644 --- a/tests/testthat/_snaps/build-articles.md +++ b/tests/testthat/_snaps/build-articles.md @@ -2,11 +2,13 @@ Code build_articles(pkg) - Output - -- Building articles ----------------------------------------------------------- - Writing 'articles/index.html' - Reading 'vignettes/html-vignette.Rmd' - Writing 'articles/html-vignette.html' + Message + + -- Building articles -- + + i Writing articles/index.html + i Reading vignettes/html-vignette.Rmd + i Writing articles/html-vignette.html Condition Warning: Missing images in 'vignettes/html-vignette.Rmd': 'foo.png' diff --git a/tests/testthat/_snaps/build-favicons.new.md b/tests/testthat/_snaps/build-favicons.new.md new file mode 100644 index 000000000..c1426593b --- /dev/null +++ b/tests/testthat/_snaps/build-favicons.new.md @@ -0,0 +1,9 @@ +# existing logo generates message + + Code + build_favicons(pkg) + Message + -- Building favicons ----------------------------------------------------------- + Favicons already exist in 'pkgdown' + i Set `overwrite = TRUE` to re-create. + diff --git a/tests/testthat/_snaps/build-home-authors.new.md b/tests/testthat/_snaps/build-home-authors.new.md new file mode 100644 index 000000000..5de8ba248 --- /dev/null +++ b/tests/testthat/_snaps/build-home-authors.new.md @@ -0,0 +1,27 @@ +# data_home_sidebar_authors() works with text + + Code + cat(data_home_sidebar_authors(pkg)) + Output +
+

Developers

+
    +
  • yay
  • +
  • Hadley Wickham
    + Author, maintainer
  • +
  • RStudio
    + Copyright holder, funder
  • +
  • cool
  • +
  • More about authors...
  • +
+
+ +# role has multiple fallbacks + + Code + role_lookup("unknown") + Message + ! Unknown MARC role abbreviation: 'unknown' + Output + [1] "unknown" + diff --git a/tests/testthat/_snaps/build-home-index.new.md b/tests/testthat/_snaps/build-home-index.new.md new file mode 100644 index 000000000..875903a65 --- /dev/null +++ b/tests/testthat/_snaps/build-home-index.new.md @@ -0,0 +1,92 @@ +# data_home_sidebar() works by default + + Code + cat(data_home_sidebar(pkg)) + Output +
+

License

+ +
+ + +
+

Citation

+ +
+ +
+

Developers

+
    +
  • Hadley Wickham
    + Author, maintainer
  • +
  • RStudio
    + Copyright holder, funder
  • +
+
+ +
+

Dev Status

+
    +
  • placeholder
  • +
+
+ +--- + +
+

Developers

+ +
+ +# data_home_sidebar() errors well when no HTML file + + Can't find file 'html_path' specified by home.sidebar.html in _pkgdown.yml + +# data_home_sidebar() can get a custom markdown formatted component + +
+

Fancy section

+
    +
  • How cool is pkgdown?!

  • +
+
+ +# data_home_sidebar() can add a README + +
+

Table of contents

+
    +
  • +
+
+ +# data_home_sidebar() outputs informative error messages + + Can't find component home.sidebar.components.fancy in _pkgdown.yml. + +--- + + Can't find component home.sidebar.components.fancy, home.sidebar.components.cool in _pkgdown.yml. + +--- + + i In index: 1. + i With name: fancy. + Caused by error in `check_yaml_has()`: + ! Can't find component home.sidebar.components.fancy.title in _pkgdown.yml. + +--- + + i In index: 1. + i With name: fancy. + Caused by error in `check_yaml_has()`: + ! Can't find component home.sidebar.components.fancy.title, home.sidebar.components.fancy.text in _pkgdown.yml. + diff --git a/tests/testthat/_snaps/build-home.new.md b/tests/testthat/_snaps/build-home.new.md new file mode 100644 index 000000000..028a3674e --- /dev/null +++ b/tests/testthat/_snaps/build-home.new.md @@ -0,0 +1,15 @@ +# warns about missing images + + Code + build_home(pkg) + Message + -- Building home --------------------------------------------------------------- + Output + Writing {path} + Condition + Warning: + Missing images in `src_path`: ''foo.png'' + i pkgdown can only use images in 'man/figures' and 'vignettes' + Output + Writing {path} + diff --git a/tests/testthat/_snaps/check.new.md b/tests/testthat/_snaps/check.new.md new file mode 100644 index 000000000..410b2ac2b --- /dev/null +++ b/tests/testthat/_snaps/check.new.md @@ -0,0 +1,25 @@ +# fails if reference index incomplete + + Code + check_pkgdown(pkg) + Condition + Error in `check_missing_topics()`: + ! All topics must be included in reference index + x Missing topics: ? + i Either add to _pkgdown.yml or use @keywords internal + +# fails if article index incomplete + + Code + check_pkgdown(pkg) + Condition + Error in `data_articles_index()`: + ! Vignettes missing from index: articles/nested and width + +# informs if everything is ok + + Code + check_pkgdown(pkg) + Message + No problems found +