From a7bb4e4e752cfcdfd04f0735fdb6aec09369f3d4 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Fri, 10 May 2024 10:27:06 -0500 Subject: [PATCH] Inform user when bslib files are created (#2525) --- R/theme.R | 25 +++++++++++++++++++++++++ tests/testthat/_snaps/init.md | 16 ++++++++++++++++ tests/testthat/test-init.R | 6 ++++++ 3 files changed, 47 insertions(+) diff --git a/R/theme.R b/R/theme.R index f1dad9df6..c05cc6df6 100644 --- a/R/theme.R +++ b/R/theme.R @@ -2,10 +2,26 @@ build_bslib <- function(pkg = ".") { pkg <- as_pkgdown(pkg) bs_theme <- bs_theme(pkg) + cur_deps <- find_deps(pkg) + cur_digest <- purrr::map_chr(cur_deps, file_digest) + deps <- bslib::bs_theme_dependencies(bs_theme) deps <- lapply(deps, htmltools::copyDependencyToDir, file.path(pkg$dst_path, "deps")) deps <- lapply(deps, htmltools::makeDependencyRelative, pkg$dst_path) + new_deps <- find_deps(pkg) + new_digest <- purrr::map_chr(cur_deps, file_digest) + + all_deps <- union(new_deps, cur_deps) + diff <- (cur_digest[all_deps] == new_digest[all_deps]) + changed <- all_deps[!diff | is.na(diff)] + + if (length(changed) > 0) { + purrr::walk(changed, function(dst) { + cli::cli_inform("Updating {dst_path(path_rel(dst, pkg$dst_path))}") + }) + } + head <- htmltools::renderDependencies(deps, srcType = "file") write_lines(head, data_deps_path(pkg)) } @@ -31,6 +47,15 @@ data_deps_path <- function(pkg) { file.path(pkg$dst_path, "deps", "data-deps.txt") } +find_deps <- function(pkg) { + deps_path <- path(pkg$dst_path, "deps") + if (!dir_exists(deps_path)) { + character() + } else { + dir_ls(deps_path, type = "file", recurse = TRUE) + } +} + bs_theme <- function(pkg = ".") { pkg <- as_pkgdown(pkg) diff --git a/tests/testthat/_snaps/init.md b/tests/testthat/_snaps/init.md index ffc67baba..62dc0e6f1 100644 --- a/tests/testthat/_snaps/init.md +++ b/tests/testthat/_snaps/init.md @@ -13,6 +13,22 @@ Copying /BS3/assets/pkgdown.js to pkgdown.js Copying pkgdown/extra.css to extra.css +--- + + Code + init_site(pkg) + Message + -- Initialising site ----------------------------------------------------------- + Copying /BS5/assets/link.svg to link.svg + Copying /BS5/assets/pkgdown.js to pkgdown.js + Copying pkgdown/extra.css to extra.css + Updating deps/bootstrap-5.3.1/bootstrap.bundle.min.js + Updating deps/bootstrap-5.3.1/bootstrap.bundle.min.js.map + Updating deps/bootstrap-5.3.1/bootstrap.min.css + Updating deps/jquery-3.6.0/jquery-3.6.0.js + Updating deps/jquery-3.6.0/jquery-3.6.0.min.js + Updating deps/jquery-3.6.0/jquery-3.6.0.min.map + # site meta doesn't break unexpectedly Code diff --git a/tests/testthat/test-init.R b/tests/testthat/test-init.R index fa2904608..5316b2258 100644 --- a/tests/testthat/test-init.R +++ b/tests/testthat/test-init.R @@ -1,6 +1,12 @@ test_that("informative print method", { pkg <- local_pkgdown_site(test_path("assets/init-extra-1")) expect_snapshot(init_site(pkg)) + + pkg <- local_pkgdown_site(test_path("assets/init-extra-1"), meta = " + template: + bootstrap: 5 + ") + expect_snapshot(init_site(pkg)) }) test_that("extra.css and extra.js copied and linked", {