Skip to content

Commit

Permalink
Combine section_init() and create_subdir() (#2646)
Browse files Browse the repository at this point in the history
* Combine section_init() and create_subdir()

* We can figure out `depth` based on whether or not a `subdir` is supplied
* Ensures all user facing functions have the same interface
* Brings together directory checking code in one place
* Check for files, not directory

Fixes #2644. Closes #2645
  • Loading branch information
hadley authored Jun 7, 2024
1 parent f24f8b7 commit 8defd98
Show file tree
Hide file tree
Showing 19 changed files with 78 additions and 65 deletions.
3 changes: 2 additions & 1 deletion R/build-article.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions R/build-articles.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions R/build-home-index.R
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
3 changes: 1 addition & 2 deletions R/build-home.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
3 changes: 1 addition & 2 deletions R/build-news.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion R/build-redirects.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 3 additions & 4 deletions R/build-reference.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down
3 changes: 2 additions & 1 deletion R/build-search-docs.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions R/build-tutorials.R
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion R/build.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))}")
Expand Down
26 changes: 24 additions & 2 deletions R/clean.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,34 @@ 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])
file_delete(top_level[!is_dir])

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")]
}
}
20 changes: 18 additions & 2 deletions R/context.R
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
27 changes: 5 additions & 22 deletions R/init.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
11 changes: 6 additions & 5 deletions R/pkgdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 <head>
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)) {
Expand All @@ -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)
}

Expand Down
9 changes: 0 additions & 9 deletions R/utils-fs.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion man/build_articles.Rd

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

2 changes: 1 addition & 1 deletion man/build_home.Rd

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

2 changes: 1 addition & 1 deletion man/build_reference.Rd

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

5 changes: 4 additions & 1 deletion man/init_site.Rd

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

0 comments on commit 8defd98

Please sign in to comment.