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

Nudge towards building favicons in use_logo() and use_pkgdown() #1996

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 7 additions & 0 deletions R/logo.R
Original file line number Diff line number Diff line change
Expand Up @@ -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."
))
}
}
29 changes: 29 additions & 0 deletions R/pkgdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This idea came to me, because of PRs I opened in various repos of package devs who didn't seem to be aware of this. see #1995

images <- dir_ls(
type = "file",
recurse = TRUE,
regexp = "\\.svg|\\.png"
)
length(images) > 0 && !has_logo()
}

uses_pkgdown <- function() {
!is.null(pkgdown_config_path())
}
Expand Down
14 changes: 14 additions & 0 deletions tests/testthat/_snaps/logo.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,17 @@
[ ] Add logo to 'README.md' with the following html:
# {TESTPKG} <img src="man/figures/logo.png" align="right" height="90" alt="" />

# 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} <img src="man/figures/logo.png" align="right" height="90" alt="" />
[ ] Call `pkgdown::build_favicons(pkg = '.', overwrite = TRUE)` to rebuild
favicons.

24 changes: 24 additions & 0 deletions tests/testthat/_snaps/pkgdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'.

11 changes: 11 additions & 0 deletions tests/testthat/test-logo.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
29 changes: 29 additions & 0 deletions tests/testthat/test-pkgdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
Loading