From d998fb2bb99c1e97a1537d23a005ce2ad02bbbf3 Mon Sep 17 00:00:00 2001 From: olivroy Date: Thu, 23 May 2024 08:58:58 -0400 Subject: [PATCH 1/4] Nudge towards building favicons in `use_logo()` and `use_pkgdown()` --- NEWS.md | 3 +++ R/logo.R | 7 +++++++ R/pkgdown.R | 29 +++++++++++++++++++++++++++++ tests/testthat/_snaps/logo.md | 14 ++++++++++++++ tests/testthat/_snaps/pkgdown.md | 24 ++++++++++++++++++++++++ tests/testthat/test-logo.R | 11 +++++++++++ tests/testthat/test-pkgdown.R | 29 +++++++++++++++++++++++++++++ 7 files changed, 117 insertions(+) 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..9c14873ea 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 'README.md' 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..94e11adff 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_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..0f85c6ee8 100644 --- a/tests/testthat/test-pkgdown.R +++ b/tests/testthat/test-pkgdown.R @@ -77,6 +77,35 @@ 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_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")) From 24723f420bebb1d9a6b3dd7e0e49e01141ba5325 Mon Sep 17 00:00:00 2001 From: olivroy Date: Thu, 23 May 2024 09:09:21 -0400 Subject: [PATCH 2/4] use conditionally on pkgdown --- tests/testthat/test-logo.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-logo.R b/tests/testthat/test-logo.R index 94e11adff..1d3dbc0f9 100644 --- a/tests/testthat/test-logo.R +++ b/tests/testthat/test-logo.R @@ -20,8 +20,8 @@ test_that("use_logo() shows a clickable path with README", { 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") From 3fa773e7433f3f0bee6a5fc62f900356e61da2fe Mon Sep 17 00:00:00 2001 From: olivroy Date: Thu, 23 May 2024 09:19:42 -0400 Subject: [PATCH 3/4] commit snap --- tests/testthat/_snaps/logo.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/_snaps/logo.md b/tests/testthat/_snaps/logo.md index 9c14873ea..6aa866070 100644 --- a/tests/testthat/_snaps/logo.md +++ b/tests/testthat/_snaps/logo.md @@ -15,7 +15,7 @@ Message v Creating 'man/figures/'. v Resized 'logo.png' to 240x278. - [ ] Add logo to 'README.md' with the following html: + [ ] Add logo to your README with the following html: ! pkgdown config does not specify the site's 'url', which is optional but recommended. # {TESTPKG} From e25d62cd04ea3460662be862463434d1fb1af748 Mon Sep 17 00:00:00 2001 From: olivroy <52606734+olivroy@users.noreply.github.com> Date: Thu, 23 May 2024 09:49:03 -0400 Subject: [PATCH 4/4] Update tests/testthat/test-pkgdown.R --- tests/testthat/test-pkgdown.R | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/testthat/test-pkgdown.R b/tests/testthat/test-pkgdown.R index 0f85c6ee8..088a3582f 100644 --- a/tests/testthat/test-pkgdown.R +++ b/tests/testthat/test-pkgdown.R @@ -93,6 +93,7 @@ test_that("use_pkgdown() nudges towards use_logo() if the package seems to have }) test_that("use_pkgdown() nudges towards build_favicons().", { + skip_if_not_installed("magick") skip_on_os("solaris") create_local_package()