diff --git a/R/build-article.R b/R/build-article.R index d7fc69cdc..84ec3dafd 100644 --- a/R/build-article.R +++ b/R/build-article.R @@ -13,8 +13,9 @@ build_article <- function(name, seed = 1014L, new_process = TRUE, pandoc_args = character(), + override = list(), quiet = TRUE) { - pkg <- as_pkgdown(pkg) + pkg <- section_init(pkg, "articles", override = override) # Look up in pkg vignette data - this allows convenient automatic # specification of depth, output destination, and other parameters that diff --git a/R/build-articles.R b/R/build-articles.R index b3a626b0f..6f095bb63 100644 --- a/R/build-articles.R +++ b/R/build-articles.R @@ -199,7 +199,7 @@ build_articles <- function(pkg = ".", seed = 1014L, override = list(), preview = FALSE) { - pkg <- section_init(pkg, depth = 1L, override = override) + pkg <- section_init(pkg, "articles", override = override) check_bool(quiet) check_bool(lazy) check_number_whole(seed, allow_null = TRUE) @@ -228,10 +228,8 @@ build_articles <- function(pkg = ".", #' @export #' @rdname build_articles #' @order 3 -build_articles_index <- function(pkg = ".") { - pkg <- as_pkgdown(pkg) - - create_subdir(pkg, "articles") +build_articles_index <- function(pkg = ".", override = list()) { + pkg <- section_init(pkg, "articles", override = override) render_page( pkg, "article-index", diff --git a/R/build-home-index.R b/R/build-home-index.R index 36801a8c9..cf5cf79d9 100644 --- a/R/build-home-index.R +++ b/R/build-home-index.R @@ -1,7 +1,7 @@ #' @export #' @rdname build_home -build_home_index <- function(pkg = ".", quiet = TRUE) { - pkg <- section_init(pkg, depth = 0L) +build_home_index <- function(pkg = ".", override = list(), quiet = TRUE) { + pkg <- section_init(pkg, override = override) src_path <- path_index(pkg) dst_path <- path(pkg$dst_path, "index.html") diff --git a/R/build-home.R b/R/build-home.R index 194ca69a8..ec06a6e64 100644 --- a/R/build-home.R +++ b/R/build-home.R @@ -291,11 +291,10 @@ build_home <- function(pkg = ".", preview = FALSE, quiet = TRUE) { - pkg <- section_init(pkg, depth = 0L, override = override) + pkg <- section_init(pkg, override = override) check_bool(quiet) cli::cli_rule("Building home") - create_subdir(pkg, "") build_citation_authors(pkg) diff --git a/R/build-news.R b/R/build-news.R index 9ebc1e6ef..da554025a 100644 --- a/R/build-news.R +++ b/R/build-news.R @@ -75,12 +75,11 @@ build_news <- function(pkg = ".", override = list(), preview = FALSE) { - pkg <- section_init(pkg, depth = 1L, override = override) + pkg <- section_init(pkg, "news", override = override) if (!has_news(pkg$src_path)) return(invisible()) cli::cli_rule("Building news") - create_subdir(pkg, "news") one_page <- config_pluck_bool(pkg, "news.one_page", default = TRUE) if (one_page) { diff --git a/R/build-redirects.R b/R/build-redirects.R index f4e4fc735..ac55a0f04 100644 --- a/R/build-redirects.R +++ b/R/build-redirects.R @@ -25,7 +25,7 @@ #' @export build_redirects <- function(pkg = ".", override = list()) { - pkg <- section_init(pkg, depth = 1L, override = override) + pkg <- section_init(pkg, override = override) has_url <- !is.null(config_pluck_string(pkg, "url")) redirects <- data_redirects(pkg, has_url) diff --git a/R/build-reference.R b/R/build-reference.R index 70550d540..de8992149 100644 --- a/R/build-reference.R +++ b/R/build-reference.R @@ -158,7 +158,7 @@ build_reference <- function(pkg = ".", preview = FALSE, devel = TRUE, topics = NULL) { - pkg <- section_init(pkg, depth = 1L, override = override) + pkg <- section_init(pkg, "reference", override = override) check_bool(lazy) check_bool(examples) check_bool(run_dont_run) @@ -242,9 +242,8 @@ examples_env <- function(pkg, seed = 1014L, devel = TRUE, envir = parent.frame() #' @export #' @rdname build_reference -build_reference_index <- function(pkg = ".") { - pkg <- section_init(pkg, depth = 1L) - create_subdir(pkg, "reference") +build_reference_index <- function(pkg = ".", override = list()) { + pkg <- section_init(pkg, "reference", override = override) # Copy icons, if needed dir_copy_to( diff --git a/R/build-search-docs.R b/R/build-search-docs.R index f1a2fad18..788d00525 100644 --- a/R/build-search-docs.R +++ b/R/build-search-docs.R @@ -79,8 +79,9 @@ build_sitemap <- function(pkg = ".") { #' build_search <- function(pkg = ".", override = list()) { - pkg <- section_init(pkg, depth = 1L, override = override) + pkg <- section_init(pkg, override = override) cli::cli_rule("Building search index") + search_index <- build_search_index(pkg) jsonlite::write_json( search_index, diff --git a/R/build-tutorials.R b/R/build-tutorials.R index 6157f4e30..006f9f72f 100644 --- a/R/build-tutorials.R +++ b/R/build-tutorials.R @@ -29,7 +29,7 @@ #' @family site components #' @export build_tutorials <- function(pkg = ".", override = list(), preview = FALSE) { - pkg <- section_init(pkg, depth = 1L, override = override) + pkg <- section_init(pkg, "tutorials", override = override) tutorials <- pkg$tutorials @@ -38,7 +38,6 @@ build_tutorials <- function(pkg = ".", override = list(), preview = FALSE) { } cli::cli_rule("Building tutorials") - create_subdir(pkg, "tutorials") data <- purrr::transpose(tutorials) diff --git a/R/build.R b/R/build.R index 7434d61ae..1889c4a9b 100644 --- a/R/build.R +++ b/R/build.R @@ -422,7 +422,7 @@ build_site_local <- function(pkg = ".", preview = NA, devel = TRUE) { - pkg <- section_init(pkg, depth = 0, override = override) + pkg <- section_init(pkg, override = override) cli::cli_rule("Building pkgdown site for package {.pkg {pkg$package}}") cli::cli_inform("Reading from: {src_path(path_abs(pkg$src_path))}") diff --git a/R/clean.R b/R/clean.R index 6116f3ae8..6c4ba235d 100644 --- a/R/clean.R +++ b/R/clean.R @@ -15,8 +15,8 @@ clean_site <- function(pkg = ".", quiet = FALSE) { if (!dir_exists(pkg$dst_path)) return(invisible()) - top_level <- dir_ls(pkg$dst_path) - top_level <- top_level[!path_file(top_level) %in% c("CNAME", "dev")] + check_dest_is_pkgdown(pkg) + top_level <- dest_files(pkg) is_dir <- is_dir(top_level) dir_delete(top_level[is_dir]) @@ -24,3 +24,25 @@ clean_site <- function(pkg = ".", quiet = FALSE) { invisible(TRUE) } + +check_dest_is_pkgdown <- function(pkg) { + if (file_exists(path(pkg$dst_path, "pkgdown.yml"))) { + return() + } + + cli::cli_abort(c( + "{.file {pkg$dst_path}} is non-empty and not built by pkgdown", + "!" = "Make sure it contains no important information \\ + and use {.run pkgdown::clean_site()} to delete its contents." + ) + ) +} + +dest_files <- function(pkg) { + if (!dir_exists(pkg$dst_path)) { + character() + } else { + top_level <- dir_ls(pkg$dst_path) + top_level[!path_file(top_level) %in% c("CNAME", "dev")] + } +} diff --git a/R/context.R b/R/context.R index a82ecc0b6..a0fd58e45 100644 --- a/R/context.R +++ b/R/context.R @@ -1,7 +1,23 @@ -section_init <- function(pkg, depth, override = list(), .frame = parent.frame()) { +section_init <- function(pkg, + subdir = NULL, + override = list(), + .frame = parent.frame()) { + rstudio_save_all() pkg <- as_pkgdown(pkg, override = override) + + if (length(dest_files(pkg)) > 0) { + check_dest_is_pkgdown(pkg) + } else { + init_site(pkg) + } + + if (is.null(subdir)) { + depth <- 0 + } else { + depth <- 1 + dir_create(path(pkg$dst_path, subdir)) + } - rstudio_save_all() local_envvar_pkgdown(pkg, .frame) local_options_link(pkg, depth = depth, .frame = .frame) diff --git a/R/init.R b/R/init.R index 90ddc21d3..25516ec32 100644 --- a/R/init.R +++ b/R/init.R @@ -20,17 +20,11 @@ #' #' @inheritParams build_articles #' @export -init_site <- function(pkg = ".") { - pkg <- as_pkgdown(pkg) - - if (is_non_pkgdown_site(pkg$dst_path)) { - cli::cli_abort(c( - "{.file {pkg$dst_path}} is non-empty and not built by pkgdown", - "!" = "Make sure it contains no important information \\ - and use {.run pkgdown::clean_site()} to delete its contents." - ) - ) - } +init_site <- function(pkg = ".", override = list()) { + # This is the only user facing function that doesn't call section_init() + # because section_init() can conditionally call init_site() + rstudio_save_all() + pkg <- as_pkgdown(pkg, override = override) cli::cli_rule("Initialising site") dir_create(pkg$dst_path) @@ -156,14 +150,3 @@ site_meta <- function(pkg) { print_yaml(yaml) } - -is_non_pkgdown_site <- function(dst_path) { - if (!dir_exists(dst_path)) { - return(FALSE) - } - - top_level <- dir_ls(dst_path) - top_level <- top_level[!path_file(top_level) %in% c("CNAME", "dev", "deps")] - - length(top_level) >= 1 && !"pkgdown.yml" %in% path_file(top_level) -} diff --git a/R/pkgdown.R b/R/pkgdown.R index 3ecf33449..ef88c1100 100644 --- a/R/pkgdown.R +++ b/R/pkgdown.R @@ -25,6 +25,11 @@ local_pkgdown_site <- function(path = NULL, check_string(path, allow_null = TRUE) dst_path <- withr::local_tempdir(.local_envir = env) + # Simulate init_site() so we only have to run it if we care about + file_create(path(dst_path, "pkgdown.yml")) + dir_create(path(dst_path, "deps")) + file_create(path(dst_path, "deps", "data-deps.txt")) + meta <- modify_list(meta, list(destination = dst_path)) if (is.null(path)) { @@ -46,12 +51,8 @@ local_pkgdown_site <- function(path = NULL, # Make it a bit easier to create other files dir_create(path(path, "R")) dir_create(path(path, "vignettes")) - - # Create dummy deps so it's not 100% necessary to run init_site() - dir_create(path(dst_path, "deps")) - file_create(path(dst_path, "deps", "data-deps.txt")) } - + as_pkgdown(path, meta) } diff --git a/R/utils-fs.R b/R/utils-fs.R index f424d1e09..5c1b98b02 100644 --- a/R/utils-fs.R +++ b/R/utils-fs.R @@ -52,15 +52,6 @@ file_copy_to <- function(src_paths, file_copy(src_paths[!eq], dst_paths[!eq], overwrite = TRUE) } -# Checks init_site() first. -create_subdir <- function(pkg, subdir) { - if (!dir_exists(pkg$dst_path)) { - init_site(pkg) - } - dir_create(path(pkg$dst_path, subdir)) - -} - out_of_date <- function(source, target, call = caller_env()) { if (!file_exists(target)) { return(TRUE) diff --git a/man/build_articles.Rd b/man/build_articles.Rd index e6f89ecee..f5be82a18 100644 --- a/man/build_articles.Rd +++ b/man/build_articles.Rd @@ -22,10 +22,11 @@ build_article( seed = 1014L, new_process = TRUE, pandoc_args = character(), + override = list(), quiet = TRUE ) -build_articles_index(pkg = ".") +build_articles_index(pkg = ".", override = list()) } \arguments{ \item{pkg}{Path to package.} diff --git a/man/build_home.Rd b/man/build_home.Rd index b854a52e3..9db92d68c 100644 --- a/man/build_home.Rd +++ b/man/build_home.Rd @@ -7,7 +7,7 @@ \usage{ build_home(pkg = ".", override = list(), preview = FALSE, quiet = TRUE) -build_home_index(pkg = ".", quiet = TRUE) +build_home_index(pkg = ".", override = list(), quiet = TRUE) } \arguments{ \item{pkg}{Path to package.} diff --git a/man/build_reference.Rd b/man/build_reference.Rd index 4d1388429..3ae9edd16 100644 --- a/man/build_reference.Rd +++ b/man/build_reference.Rd @@ -17,7 +17,7 @@ build_reference( topics = NULL ) -build_reference_index(pkg = ".") +build_reference_index(pkg = ".", override = list()) } \arguments{ \item{pkg}{Path to package.} diff --git a/man/init_site.Rd b/man/init_site.Rd index 5e0638c91..9d44cc1aa 100644 --- a/man/init_site.Rd +++ b/man/init_site.Rd @@ -4,10 +4,13 @@ \alias{init_site} \title{Initialise site infrastructure} \usage{ -init_site(pkg = ".") +init_site(pkg = ".", override = list()) } \arguments{ \item{pkg}{Path to package.} + +\item{override}{An optional named list used to temporarily override +values in \verb{_pkgdown.yml}} } \description{ \code{init_site()}: