diff --git a/NEWS.md b/NEWS.md index d7f3a70a0..11582e9b5 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # usethis (development version) +* `use_pkgdown()` and `use_logo()` now nudge towards building favicons for your website if you + use pkgdown (@olivroy, #1995). + * The `ui_*()` functions have been marked as [superseded](https://lifecycle.r-lib.org/articles/stages.html#superseded). External users of these functions are encouraged to use the diff --git a/R/logo.R b/R/logo.R index 05fc59b34..fb8f1748c 100644 --- a/R/logo.R +++ b/R/logo.R @@ -69,4 +69,11 @@ use_logo <- function(img, geometry = "240x278", retina = TRUE) { language = "" ) } + if (uses_pkgdown()) { + # Rebuild favicons if new logo. + ui_bullets(c( + "_" = "Call {.run pkgdown::build_favicons(pkg = '.', overwrite = TRUE)} + to rebuild favicons." + )) + } } diff --git a/R/pkgdown.R b/R/pkgdown.R index 78b3457d0..45a2be8d6 100644 --- a/R/pkgdown.R +++ b/R/pkgdown.R @@ -36,6 +36,20 @@ use_pkgdown <- function(config_file = "_pkgdown.yml", destdir = "docs") { config <- pkgdown_config(destdir) config_path <- proj_path(config_file) + + if (has_logo() && !dir_exists("pkgdown/favicon")) { + use_build_ignore("pkgdown") + ui_bullets(c( + "_" = "Call {.run pkgdown::build_favicons(pkg = '.', overwrite = FALSE)} + to create favicons for your website." + )) + } else if (potentially_has_logo() && !dir_exists("pkgdown/favicon")) { + ui_bullets(c( + "_" = "If your package has a logo, see \\ + {.help [use_logo](usethis::use_logo)} to set it up." + )) + } + write_over(config_path, yaml::as.yaml(config)) edit_file(config_path) @@ -154,6 +168,21 @@ pkgdown_config_path <- function() { ) } +has_logo <- function() { + !is.null( + path_first_existing(c("man/figures/logo.png", "man/figures/logo.svg")) + ) +} + +potentially_has_logo <- function() { + images <- dir_ls( + type = "file", + recurse = TRUE, + regexp = "\\.svg|\\.png" + ) + length(images) > 0 && !has_logo() +} + uses_pkgdown <- function() { !is.null(pkgdown_config_path()) } diff --git a/tests/testthat/_snaps/logo.md b/tests/testthat/_snaps/logo.md index 39b3791e5..6aa866070 100644 --- a/tests/testthat/_snaps/logo.md +++ b/tests/testthat/_snaps/logo.md @@ -8,3 +8,17 @@ [ ] Add logo to 'README.md' with the following html: # {TESTPKG} +# use_logo() nudges towards adding favicons + + Code + use_logo("logo.png") + Message + v Creating 'man/figures/'. + v Resized 'logo.png' to 240x278. + [ ] Add logo to your README with the following html: + ! pkgdown config does not specify the site's 'url', which is optional but + recommended. + # {TESTPKG} + [ ] Call `pkgdown::build_favicons(pkg = '.', overwrite = TRUE)` to rebuild + favicons. + diff --git a/tests/testthat/_snaps/pkgdown.md b/tests/testthat/_snaps/pkgdown.md index 75ac9ba7d..a8fd19d95 100644 --- a/tests/testthat/_snaps/pkgdown.md +++ b/tests/testthat/_snaps/pkgdown.md @@ -28,3 +28,27 @@ Output NULL +# use_pkgdown() nudges towards use_logo() if the package seems to have a logo + + Code + use_pkgdown() + Message + v Adding "^_pkgdown\\.yml$", "^docs$", and "^pkgdown$" to '.Rbuildignore'. + v Adding "docs" to '.gitignore'. + [ ] If your package has a logo, see use_logo (`?usethis::use_logo()`) to set it + up. + v Writing '_pkgdown.yml'. + [ ] Edit '_pkgdown.yml'. + +# use_pkgdown() nudges towards build_favicons(). + + Code + use_pkgdown() + Message + v Adding "^_pkgdown\\.yml$", "^docs$", and "^pkgdown$" to '.Rbuildignore'. + v Adding "docs" to '.gitignore'. + [ ] Call `pkgdown::build_favicons(pkg = '.', overwrite = FALSE)` to create + favicons for your website. + v Writing '_pkgdown.yml'. + [ ] Edit '_pkgdown.yml'. + diff --git a/tests/testthat/test-logo.R b/tests/testthat/test-logo.R index a84475ac3..1d3dbc0f9 100644 --- a/tests/testthat/test-logo.R +++ b/tests/testthat/test-logo.R @@ -17,3 +17,14 @@ test_that("use_logo() shows a clickable path with README", { withr::local_options(usethis.quiet = FALSE) expect_snapshot(use_logo("logo.png"), transform = scrub_testpkg) }) + +test_that("use_logo() nudges towards adding favicons", { + skip_if_not_installed("magick") + skip_if_not_installed("pkgdown") + skip_on_os("solaris") + create_local_package() + use_pkgdown() + img <- magick::image_write(magick::image_read("logo:"), "logo.png") + withr::local_options(usethis.quiet = FALSE) + expect_snapshot(use_logo("logo.png"), transform = scrub_testpkg) +}) diff --git a/tests/testthat/test-pkgdown.R b/tests/testthat/test-pkgdown.R index 9ea3f5932..088a3582f 100644 --- a/tests/testthat/test-pkgdown.R +++ b/tests/testthat/test-pkgdown.R @@ -77,6 +77,36 @@ test_that("pkgdown_url() returns correct data, warns if pedantic", { expect_equal(pkgdown_url(), "https://malcolmbarrett.github.io/tidysmd/") }) +test_that("use_pkgdown() nudges towards use_logo() if the package seems to have a logo", { + skip_if_not_installed("magick") + skip_on_os("solaris") + + create_local_package() + local_interactive(FALSE) + local_check_installed() + local_mocked_bindings(pkgdown_version = function() "1.9000") + + img <- magick::image_write(magick::image_read("logo:"), "hex-sticker.svg") + withr::local_options("usethis.quiet" = FALSE) + expect_snapshot({ + use_pkgdown()}, transform = scrub_testpkg) +}) + +test_that("use_pkgdown() nudges towards build_favicons().", { + skip_if_not_installed("magick") + skip_on_os("solaris") + + create_local_package() + local_interactive(FALSE) + local_check_installed() + local_mocked_bindings(pkgdown_version = function() "1.9000") + create_directory("man/figures") + img <- magick::image_write(magick::image_read("logo:"), path = "man/figures/logo.svg") + withr::local_options("usethis.quiet" = FALSE) + expect_snapshot({ + use_pkgdown()}, transform = scrub_testpkg) +}) + test_that("tidyverse_url() leaves trailing slash alone, almost always", { url <- "https://malcolmbarrett.github.io/tidysmd/" out <- tidyverse_url(url, tr = list(repo_name = "REPO", repo_owner = "OWNER"))