Skip to content
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ usethis has a more sophisticated understanding of the default branch and gains s
* `use_release_issue()` also takes bullets from `release_questions()`,
for compatibility with `devtools::release()`.

* `use_pkgdown()` automatically uses bootstrap4 if the pkgdown version supports
it (anticipated for pkgdown 2.0.0).

* `git_vaccinate()`, `edit_git_ignore()`, and `git_sitrep()` are more careful to
consult, reveal, and set the `core.excludesFile` in user's Git configuration
(#1461).
Expand Down
5 changes: 0 additions & 5 deletions R/logo.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,3 @@ use_logo <- function(img, geometry = "240x278", retina = TRUE) {
ui_code_block("# {pkg} <a href={ui_value(pd_link)}><img src={ui_path(logo_path)} align=\"right\" height=\"{height}\" /></a>")
}
}

has_logo <- function() {
file_exists(proj_path("man", "figures", "logo.png")) ||
file_exists(proj_path("man", "figures", "logo.svg"))
}
91 changes: 40 additions & 51 deletions R/pkgdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
#' [pkgdown](https://pkgdown.r-lib.org) makes it easy to turn your package into
#' a beautiful website. usethis provides two functions to help you use pkgdown:
#'
#' * `use_pkgdown()`: creates a pkgdown config file, adds relevant files or
#' directories to `.Rbuildignore` and `.gitignore`, and builds favicons if
#' your package has a logo.
#' * `use_pkgdown()`: creates a pkgdown config file and adds relevant files or
#' directories to `.Rbuildignore` and `.gitignore`.
#'
#' * `use_pkgdown_github_pages()`: implements the GitHub setup needed to
#' automatically publish your pkgdown site to GitHub pages:
Expand All @@ -31,28 +30,37 @@ use_pkgdown <- function(config_file = "_pkgdown.yml", destdir = "docs") {
check_is_package("use_pkgdown()")
check_installed("pkgdown")

use_build_ignore(c(config_file, destdir))
use_build_ignore("pkgdown")
use_build_ignore(c(config_file, destdir, "pkgdown"))
use_git_ignore(destdir)

ui_todo("
Record your site's {ui_field('url')} in the pkgdown config file \\
(optional, but recommended)")
config <- pkgdown_config(destdir)
config_path <- proj_path(config_file)
write_over(config_path, yaml::as.yaml(config))
edit_file(config_path)

invisible(TRUE)
}

pkgdown_config <- function(destdir) {
config <- list(
url = NULL
)

if (has_logo()) {
pkgdown_build_favicons(proj_get(), overwrite = TRUE)
if (pkgdown_version() >= "1.9000") {
config$template <- list(bootstrap = 4L)
}

config <- proj_path(config_file)
if (!identical(destdir, "docs")) {
write_over(config, paste("destination:", destdir))
config$destination <- destdir
}
edit_file(config)

invisible(TRUE)
config
}

# tidyverse pkgdown setup ------------------------------------------------------
# wrapping because I need to be able to mock this in tests
pkgdown_version <- function() {
utils::packageVersion("pkgdown")
}

#' @rdname use_pkgdown
#' @export
Expand All @@ -75,6 +83,8 @@ use_pkgdown_github_pages <- function() {
}
}

# tidyverse pkgdown setup ------------------------------------------------------

#' @details
#' * `use_tidy_pkgdown_github_pages()` is basically
#' [use_pkgdown_github_pages()], so does full pkgdown set up. Note that there
Expand All @@ -94,32 +104,22 @@ use_tidy_pkgdown_github_pages <- function() {
use_pkgdown_url <- function(url, tr = NULL) {
tr <- tr %||% target_repo(github_get = TRUE)

config <- pkgdown_config_path()
config_lines <- read_utf8(config)
url_line <- paste0("url: ", url)
if (!any(grepl(url_line, config_lines))) {
ui_done("
Recording {ui_value(url)} as site's {ui_field('url')} in \\
{ui_path(config)}")
config_lines <- config_lines[!grepl("^url:", config_lines)]
write_utf8(config, c(
url_line,
if (length(config_lines) && nzchar(config_lines[[1]])) "",
config_lines
))
config_path <- pkgdown_config_path()
ui_done("
Recording {ui_value(url)} as site's {ui_field('url')} in \\
{ui_path(config_path)}")
config <- pkgdown_config_meta()
if (has_name(config, "url")) {
config$url <- url
} else {
config <- c(url = url, config)
}
write_utf8(config_path, yaml::as.yaml(config))

urls <- desc::desc_get_urls()
if (!url %in% urls) {
ui_done("Adding {ui_value(url)} to {ui_field('URL')} field in DESCRIPTION")
ui_silence(
use_description_field(
"URL",
glue_collapse(c(url, urls), ", "),
overwrite = TRUE
)
)
}
ui_done("Adding {ui_value(url)} to {ui_field('URL')} field in DESCRIPTION")
desc <- desc::desc(file = proj_get())
desc$add_urls(url)
desc$write()

gh <- gh_tr(tr)
homepage <- gh("GET /repos/{owner}/{repo}")[["homepage"]]
Expand Down Expand Up @@ -215,16 +215,11 @@ use_pkgdown_travis <- function() {

tr <- target_repo(github_get = TRUE)

use_build_ignore("docs/")
use_build_ignore(c("docs/", "pkgdown"))
use_git_ignore("docs/")
# TODO: suggest git rm -r --cache docs/
# Can't currently detect if git known files in that directory

if (has_logo()) {
pkgdown_build_favicons(proj_get(), overwrite = TRUE)
use_build_ignore("pkgdown")
}

ui_todo("
Set up deploy keys by running {ui_code('travis::use_travis_deploy()')}")
ui_todo("Insert the following code in {ui_path('.travis.yml')}")
Expand All @@ -242,9 +237,3 @@ use_pkgdown_travis <- function() {

invisible()
}

# usethis itself should not depend on pkgdown
# all usage of this wrapper is guarded by `check_installed("pkgdown")`
pkgdown_build_favicons <- function(...) {
get("build_favicons", asNamespace("pkgdown"), mode = "function")(...)
}
5 changes: 2 additions & 3 deletions man/use_pkgdown.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion tests/testthat/test-pkgdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ test_that("use_pkgdown() creates and ignores the promised file/dir", {
local_interactive(FALSE)
with_mock(
check_installed = function(pkg) TRUE,
pkgdown_version = function() "1.9000",
use_pkgdown()
)
expect_true(uses_pkgdown())
Expand All @@ -29,9 +30,11 @@ test_that("pkgdown_config_meta() returns a list", {
local_interactive(FALSE)
with_mock(
check_installed = function(pkg) TRUE,
pkgdown_version = function() "1.9000",
use_pkgdown()
)
expect_equal(pkgdown_config_meta(), list())
expect_type(pkgdown_config_meta(), "list")

writeLines(c("home:", " strip_header: true"), pkgdown_config_path())
expect_equal(
pkgdown_config_meta(),
Expand All @@ -44,6 +47,7 @@ test_that("pkgdown_url() returns correct data, warns if pedantic", {
local_interactive(FALSE)
with_mock(
check_installed = function(pkg) TRUE,
pkgdown_version = function() "1.9000",
use_pkgdown()
)

Expand Down