From 90080605e64d5af981ae61b3f7802c74140ab1e4 Mon Sep 17 00:00:00 2001 From: olivroy Date: Fri, 12 Apr 2024 11:24:41 -0400 Subject: [PATCH 1/4] Avoid creation of faulty site --- R/build-articles.R | 2 +- R/build-news.R | 2 +- R/build-reference.R | 2 +- R/build-tutorials.R | 2 +- R/init.R | 4 +++- R/utils-fs.R | 9 +++++++++ 6 files changed, 16 insertions(+), 5 deletions(-) diff --git a/R/build-articles.R b/R/build-articles.R index 8d858e41f..827e32464 100644 --- a/R/build-articles.R +++ b/R/build-articles.R @@ -351,7 +351,7 @@ rmarkdown_template <- function(pkg, name, data, depth) { build_articles_index <- function(pkg = ".") { pkg <- as_pkgdown(pkg) - dir_create(path(pkg$dst_path, "articles")) + create_subdir(pkg$dst_path, "articles") render_page( pkg, "article-index", diff --git a/R/build-news.R b/R/build-news.R index dec9ab041..e19889ed2 100644 --- a/R/build-news.R +++ b/R/build-news.R @@ -79,7 +79,7 @@ build_news <- function(pkg = ".", return() cli::cli_rule("Building news") - dir_create(path(pkg$dst_path, "news")) + create_subdir(pkg$dst_path, "news") switch(news_style(pkg$meta), single = build_news_single(pkg), diff --git a/R/build-reference.R b/R/build-reference.R index f968e4025..9efce6eb5 100644 --- a/R/build-reference.R +++ b/R/build-reference.R @@ -245,7 +245,7 @@ examples_env <- function(pkg, seed = 1014L, devel = TRUE, envir = parent.frame() #' @rdname build_reference build_reference_index <- function(pkg = ".") { pkg <- section_init(pkg, depth = 1L) - dir_create(path(pkg$dst_path, "reference")) + create_subdir(pkg$dst_path, "reference") # Copy icons, if needed src_icons <- path(pkg$src_path, "icons") diff --git a/R/build-tutorials.R b/R/build-tutorials.R index 2dd240d76..f3b8adf1b 100644 --- a/R/build-tutorials.R +++ b/R/build-tutorials.R @@ -36,7 +36,7 @@ build_tutorials <- function(pkg = ".", override = list(), preview = NA) { } cli::cli_rule("Building tutorials") - dir_create(path(pkg$dst_path, "tutorials")) + create_subdir(pkg$dst_path, "tutorials") data <- purrr::transpose(tutorials) diff --git a/R/init.R b/R/init.R index 8946dfd86..67a522cf5 100644 --- a/R/init.R +++ b/R/init.R @@ -24,8 +24,10 @@ init_site <- function(pkg = ".") { pkg <- as_pkgdown(pkg) if (is_non_pkgdown_site(pkg$dst_path)) { - cli::cli_abort( + cli::cli_abort(c( "{.file {pkg$dst_path}} is non-empty and not built by pkgdown", + i = "Make sure no important information, and use {.run pkgdown::clean_site()} to clear" + ), call = caller_env() ) } diff --git a/R/utils-fs.R b/R/utils-fs.R index 9e4445da0..ff187581b 100644 --- a/R/utils-fs.R +++ b/R/utils-fs.R @@ -50,6 +50,15 @@ file_copy_to <- function(pkg, file_copy(from_paths[!eq], to_paths[!eq], overwrite = overwrite) } +# Checks init_site() first. +create_subdir <- function(dest_base, subdir) { + if (!fs::dir_exists(dest_base)) { + init_site() + } + dir_create(path(dest_base, subdir)) + +} + out_of_date <- function(source, target) { if (!file_exists(target)) return(TRUE) From d8812e47041dc3ea5657b4cf0e4b5c05b621771a Mon Sep 17 00:00:00 2001 From: olivroy Date: Fri, 12 Apr 2024 12:31:02 -0400 Subject: [PATCH 2/4] Add NEWS + pkg argument to `create_subdir()` --- NEWS.md | 1 + R/utils-fs.R | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index cb35d88b0..68ec2564f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,6 +2,7 @@ * Preview links now work once again (#2435). * `build_home()` no longer renders Github issue and pull request templates (@hsloot, #2362) +* It is now easier to preview parts of the website locally interactively. `build_reference_index()` and friends will call `init_site()` internally instead of erroring (@olivroy, #2329). # pkgdown 2.0.8 diff --git a/R/utils-fs.R b/R/utils-fs.R index ff187581b..b0ea6ac2a 100644 --- a/R/utils-fs.R +++ b/R/utils-fs.R @@ -51,9 +51,9 @@ file_copy_to <- function(pkg, } # Checks init_site() first. -create_subdir <- function(dest_base, subdir) { +create_subdir <- function(dest_base, subdir, pkg = pkg) { if (!fs::dir_exists(dest_base)) { - init_site() + init_site(pkg) } dir_create(path(dest_base, subdir)) From 64828088dfe11f481bf912baa88d16232a00e0c7 Mon Sep 17 00:00:00 2001 From: olivroy Date: Fri, 12 Apr 2024 13:12:32 -0400 Subject: [PATCH 3/4] forgot that having default arg = arg is problematic --- R/build-articles.R | 2 +- R/build-news.R | 2 +- R/build-reference.R | 2 +- R/build-tutorials.R | 2 +- R/utils-fs.R | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/R/build-articles.R b/R/build-articles.R index 827e32464..2f355000f 100644 --- a/R/build-articles.R +++ b/R/build-articles.R @@ -351,7 +351,7 @@ rmarkdown_template <- function(pkg, name, data, depth) { build_articles_index <- function(pkg = ".") { pkg <- as_pkgdown(pkg) - create_subdir(pkg$dst_path, "articles") + create_subdir(pkg$dst_path, "articles", pkg) render_page( pkg, "article-index", diff --git a/R/build-news.R b/R/build-news.R index e19889ed2..2126101d5 100644 --- a/R/build-news.R +++ b/R/build-news.R @@ -79,7 +79,7 @@ build_news <- function(pkg = ".", return() cli::cli_rule("Building news") - create_subdir(pkg$dst_path, "news") + create_subdir(pkg$dst_path, "news", pkg) switch(news_style(pkg$meta), single = build_news_single(pkg), diff --git a/R/build-reference.R b/R/build-reference.R index 9efce6eb5..f57c43180 100644 --- a/R/build-reference.R +++ b/R/build-reference.R @@ -245,7 +245,7 @@ examples_env <- function(pkg, seed = 1014L, devel = TRUE, envir = parent.frame() #' @rdname build_reference build_reference_index <- function(pkg = ".") { pkg <- section_init(pkg, depth = 1L) - create_subdir(pkg$dst_path, "reference") + create_subdir(pkg$dst_path, "reference", pkg) # Copy icons, if needed src_icons <- path(pkg$src_path, "icons") diff --git a/R/build-tutorials.R b/R/build-tutorials.R index f3b8adf1b..390c5f9ab 100644 --- a/R/build-tutorials.R +++ b/R/build-tutorials.R @@ -36,7 +36,7 @@ build_tutorials <- function(pkg = ".", override = list(), preview = NA) { } cli::cli_rule("Building tutorials") - create_subdir(pkg$dst_path, "tutorials") + create_subdir(pkg$dst_path, "tutorials", pkg) data <- purrr::transpose(tutorials) diff --git a/R/utils-fs.R b/R/utils-fs.R index b0ea6ac2a..044916a11 100644 --- a/R/utils-fs.R +++ b/R/utils-fs.R @@ -51,7 +51,7 @@ file_copy_to <- function(pkg, } # Checks init_site() first. -create_subdir <- function(dest_base, subdir, pkg = pkg) { +create_subdir <- function(dest_base, subdir, pkg) { if (!fs::dir_exists(dest_base)) { init_site(pkg) } From 16bfa409abf0e6a58655fed4f517050cc54b3383 Mon Sep 17 00:00:00 2001 From: olivroy Date: Fri, 12 Apr 2024 17:35:34 -0400 Subject: [PATCH 4/4] address comments --- R/build-articles.R | 2 +- R/build-news.R | 2 +- R/build-reference.R | 2 +- R/build-tutorials.R | 2 +- R/init.R | 3 ++- R/utils-fs.R | 6 +++--- 6 files changed, 9 insertions(+), 8 deletions(-) diff --git a/R/build-articles.R b/R/build-articles.R index 2f355000f..7693a5a25 100644 --- a/R/build-articles.R +++ b/R/build-articles.R @@ -351,7 +351,7 @@ rmarkdown_template <- function(pkg, name, data, depth) { build_articles_index <- function(pkg = ".") { pkg <- as_pkgdown(pkg) - create_subdir(pkg$dst_path, "articles", pkg) + create_subdir(pkg, "articles") render_page( pkg, "article-index", diff --git a/R/build-news.R b/R/build-news.R index 2126101d5..a65057bc0 100644 --- a/R/build-news.R +++ b/R/build-news.R @@ -79,7 +79,7 @@ build_news <- function(pkg = ".", return() cli::cli_rule("Building news") - create_subdir(pkg$dst_path, "news", pkg) + create_subdir(pkg, "news") switch(news_style(pkg$meta), single = build_news_single(pkg), diff --git a/R/build-reference.R b/R/build-reference.R index f57c43180..77f9cd41e 100644 --- a/R/build-reference.R +++ b/R/build-reference.R @@ -245,7 +245,7 @@ examples_env <- function(pkg, seed = 1014L, devel = TRUE, envir = parent.frame() #' @rdname build_reference build_reference_index <- function(pkg = ".") { pkg <- section_init(pkg, depth = 1L) - create_subdir(pkg$dst_path, "reference", pkg) + create_subdir(pkg, "reference") # Copy icons, if needed src_icons <- path(pkg$src_path, "icons") diff --git a/R/build-tutorials.R b/R/build-tutorials.R index 390c5f9ab..a08b48cae 100644 --- a/R/build-tutorials.R +++ b/R/build-tutorials.R @@ -36,7 +36,7 @@ build_tutorials <- function(pkg = ".", override = list(), preview = NA) { } cli::cli_rule("Building tutorials") - create_subdir(pkg$dst_path, "tutorials", pkg) + create_subdir(pkg, "tutorials") data <- purrr::transpose(tutorials) diff --git a/R/init.R b/R/init.R index 67a522cf5..e2aec0078 100644 --- a/R/init.R +++ b/R/init.R @@ -26,7 +26,8 @@ init_site <- function(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", - i = "Make sure no important information, and use {.run pkgdown::clean_site()} to clear" + "!" = "Make sure it contains no important information \\ + and use {.run pkgdown::clean_site()} to delete its contents." ), call = caller_env() ) diff --git a/R/utils-fs.R b/R/utils-fs.R index 044916a11..20b669fc6 100644 --- a/R/utils-fs.R +++ b/R/utils-fs.R @@ -51,11 +51,11 @@ file_copy_to <- function(pkg, } # Checks init_site() first. -create_subdir <- function(dest_base, subdir, pkg) { - if (!fs::dir_exists(dest_base)) { +create_subdir <- function(pkg, subdir) { + if (!fs::dir_exists(pkg$dst_path)) { init_site(pkg) } - dir_create(path(dest_base, subdir)) + dir_create(path(pkg$dst_path, subdir)) }