diff --git a/NEWS.md b/NEWS.md index ee0769379..2b85a8ed2 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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). diff --git a/R/logo.R b/R/logo.R index cfc3eac00..c1d09b12b 100644 --- a/R/logo.R +++ b/R/logo.R @@ -56,8 +56,3 @@ use_logo <- function(img, geometry = "240x278", retina = TRUE) { ui_code_block("# {pkg} ") } } - -has_logo <- function() { - file_exists(proj_path("man", "figures", "logo.png")) || - file_exists(proj_path("man", "figures", "logo.svg")) -} diff --git a/R/pkgdown.R b/R/pkgdown.R index 31fff22f2..6b9fdca72 100644 --- a/R/pkgdown.R +++ b/R/pkgdown.R @@ -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: @@ -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 @@ -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 @@ -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"]] @@ -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')}") @@ -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")(...) -} diff --git a/man/use_pkgdown.Rd b/man/use_pkgdown.Rd index cce253192..d11a5903e 100644 --- a/man/use_pkgdown.Rd +++ b/man/use_pkgdown.Rd @@ -21,9 +21,8 @@ use_pkgdown_travis() \href{https://pkgdown.r-lib.org}{pkgdown} makes it easy to turn your package into a beautiful website. usethis provides two functions to help you use pkgdown: \itemize{ -\item \code{use_pkgdown()}: creates a pkgdown config file, adds relevant files or -directories to \code{.Rbuildignore} and \code{.gitignore}, and builds favicons if -your package has a logo. +\item \code{use_pkgdown()}: creates a pkgdown config file and adds relevant files or +directories to \code{.Rbuildignore} and \code{.gitignore}. \item \code{use_pkgdown_github_pages()}: implements the GitHub setup needed to automatically publish your pkgdown site to GitHub pages: \itemize{ diff --git a/tests/testthat/test-pkgdown.R b/tests/testthat/test-pkgdown.R index 3b94bcea5..11c4bd62d 100644 --- a/tests/testthat/test-pkgdown.R +++ b/tests/testthat/test-pkgdown.R @@ -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()) @@ -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(), @@ -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() )