Skip to content

Commit 322b6b8

Browse files
authored
Improve build_vignettes() (#2425)
* Extract out `local_install()` code from build_rmd() and use in `build_vignettes()` * Implement `local_package_copy()` and use in tests. This means we don't modify directories inside the package making cleaning up much easier, and avoiding a buglet in `usethis_use_directory()` * Use more consistent messaging * Change default to `quiet = FALSE`. Fixes #2360
1 parent 7cc2bdf commit 322b6b8

8 files changed

Lines changed: 76 additions & 100 deletions

File tree

R/build-readme.R

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ build_rmd <- function(files, path = ".", output_options = list(), ..., quiet = T
2828
cli::cli_abort("Can't find file{?s}: {.path {files[!ok]}}.")
2929
}
3030

31-
cli::cli_inform(c(i = "Installing {.pkg {pkg$package}} in temporary library"))
32-
withr::local_temp_libpaths()
33-
install(pkg, upgrade = "never", reload = FALSE, quick = TRUE, quiet = quiet)
31+
local_install(pkg, quiet = TRUE)
3432

3533
# Ensure rendering github_document() doesn't generate HTML file
3634
output_options$html_preview <- FALSE

R/install.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,11 @@ install_dev_deps <- function(pkg = ".",
177177
...
178178
)
179179
}
180+
181+
local_install <- function(pkg = ".", quiet = TRUE, env = parent.frame()) {
182+
pkg <- as.package(pkg)
183+
184+
cli::cli_inform(c(i = "Installing {.pkg {pkg$package}} in temporary library"))
185+
withr::local_temp_libpaths(.local_envir = env)
186+
install(pkg, upgrade = "never", reload = FALSE, quick = TRUE, quiet = quiet)
187+
}

R/vignette-r.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ copy_vignettes <- function(pkg, keep_md) {
2323
))
2424
out_cp <- vignettes$docs
2525

26-
cli::cli_alert_info("Moving {.file {path_file(out_mv)}} to {.path doc/}")
26+
cli::cli_inform(c(i = "Moving {.file {path_file(out_mv)}} to {.path doc/}"))
2727
file_copy(out_mv, doc_dir, overwrite = TRUE)
2828
file_delete(out_mv)
2929

30-
cli::cli_alert_info("Copying {.file {path_file(out_cp)}} to {.path doc/}")
30+
cli::cli_inform(c(i = "Copying {.file {path_file(out_cp)}} to {.path doc/}"))
3131
file_copy(out_cp, doc_dir, overwrite = TRUE)
3232

3333
# Copy extra files, if needed
@@ -36,7 +36,7 @@ copy_vignettes <- function(pkg, keep_md) {
3636
return(invisible())
3737
}
3838

39-
cli::cli_alert_info("Copying extra files {.file {path_file(extra_files)}} to {.path doc/}")
39+
cli::cli_inform(c(i = "Copying extra files {.file {path_file(extra_files)}} to {.path doc/}"))
4040
file_copy(extra_files, doc_dir)
4141

4242
invisible()

R/vignettes.R

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,59 +31,52 @@ build_vignettes <- function(pkg = ".",
3131
dependencies = "VignetteBuilder",
3232
clean = TRUE,
3333
upgrade = "never",
34-
quiet = TRUE,
34+
quiet = FALSE,
3535
install = TRUE,
3636
keep_md = TRUE) {
3737
pkg <- as.package(pkg)
38-
39-
deps <- remotes::dev_package_deps(pkg$path, dependencies)
40-
update(deps, upgrade = upgrade)
38+
save_all()
4139

4240
vigns <- tools::pkgVignettes(dir = pkg$path)
4341
if (length(vigns$docs) == 0) return()
4442

45-
cli::cli_alert_info("Building {.pkg {pkg$package}} vignettes")
43+
deps <- remotes::dev_package_deps(pkg$path, dependencies)
44+
update(deps, upgrade = upgrade)
4645

4746
if (isTRUE(install)) {
48-
build <- function(pkg_path, clean, quiet, upgrade) {
49-
withr::with_temp_libpaths(action = "prefix", {
50-
devtools::install(pkg_path, upgrade = upgrade, reload = FALSE, quiet = quiet)
51-
tools::buildVignettes(dir = pkg_path, clean = clean, tangle = TRUE, quiet = quiet)
52-
})
53-
}
54-
} else {
55-
build <- function(pkg_path, clean, quiet, upgrade) {
56-
tools::buildVignettes(dir = pkg_path, clean = clean, tangle = TRUE, quiet = quiet)
57-
}
47+
local_install(pkg, quiet = TRUE)
5848
}
5949

50+
cli::cli_inform(c(i = "Building vignettes for {.pkg {pkg$package}}"))
6051
callr::r(
61-
build,
62-
args = list(pkg_path = pkg$path, clean = clean, upgrade = upgrade, quiet = quiet),
52+
function(...) tools::buildVignettes(...),
53+
args = list(
54+
dir = pkg$path,
55+
clean = clean,
56+
tangle = TRUE,
57+
quiet = quiet
58+
),
6359
show = !quiet,
64-
spinner = FALSE,
65-
stderr = "2>&1"
60+
spinner = FALSE
6661
)
6762

6863
# We need to re-run pkgVignettes now that they are built to get the output
6964
# files as well
65+
cli::cli_inform(c(i = "Copying vignettes"))
7066
vigns <- tools::pkgVignettes(dir = pkg$path, source = TRUE, output = TRUE)
71-
7267
copy_vignettes(pkg, keep_md)
73-
7468
create_vignette_index(pkg, vigns)
7569

7670
invisible(TRUE)
7771
}
7872

7973
create_vignette_index <- function(pkg, vigns) {
74+
cli::cli_inform(c(i = "Building vignette index"))
75+
8076
usethis_use_directory(pkg, "Meta", ignore = TRUE)
8177
usethis_use_git_ignore(pkg, "/Meta/")
8278

83-
cli::cli_alert_info("Building vignette index")
84-
8579
vignette_index <- ("tools" %:::% ".build_vignette_index")(vigns)
86-
8780
vignette_index_path <- path(pkg$path, "Meta", "vignette.rds")
8881

8982
saveRDS(vignette_index, vignette_index_path, version = 2L)
@@ -101,7 +94,7 @@ clean_vignettes <- function(pkg = ".") {
10194
vigns <- tools::pkgVignettes(dir = pkg$path)
10295
if (path_file(vigns$dir) != "vignettes") return()
10396

104-
cli::cli_alert_info("Cleaning built vignettes and index from {.pkg {pkg$package}}")
97+
cli::cli_inform(c(i = "Cleaning built vignettes and index from {.pkg {pkg$package}}"))
10598

10699
doc_path <- path(pkg$path, "doc")
107100

@@ -117,7 +110,7 @@ clean_vignettes <- function(pkg = ".") {
117110

118111
to_remove <- c(vig_rm, extra_rm, vig_index_rm)
119112
if (length(to_remove) > 0) {
120-
cli::cli_alert_warning("Removing {.file {path_file(to_remove)}}")
113+
cli::cli_inform(c(x = "Removing {.file {path_file(to_remove)}}"))
121114
file_delete(to_remove)
122115
}
123116

@@ -129,7 +122,7 @@ clean_vignettes <- function(pkg = ".") {
129122
dir_delete_if_empty <- function(x) {
130123
if (dir_exists(x) && rlang::is_empty(dir_ls(x))) {
131124
dir_delete(x)
132-
cli::cli_alert_warning("Removing {.file {path_file(x)}}")
125+
cli::cli_inform(c(x = "Removing {.file {path_file(x)}}"))
133126
}
134127
}
135128

man/build_vignettes.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/helper.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,10 @@ local_package_create <- function(envir = parent.frame()) {
88

99
dir
1010
}
11+
12+
local_package_copy <- function(path, env = parent.frame()) {
13+
temp_path <- withr::local_tempdir(.local_envir = env)
14+
15+
dir_copy(path, temp_path, overwrite = TRUE)
16+
temp_path
17+
}

tests/testthat/test-install.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,20 @@ test_that("install_dev_deps passes ellipsis args to remotes::install_deps", {
111111
})
112112

113113
})
114+
115+
test_that("vignettes built on install", {
116+
skip_on_cran()
117+
118+
if (!pkgbuild::has_latex()) {
119+
skip("pdflatex not available")
120+
}
121+
122+
pkg <- local_package_copy(test_path("testVignettesBuilt"))
123+
124+
withr::local_temp_libpaths()
125+
install(pkg, reload = FALSE, quiet = TRUE, build_vignettes = TRUE)
126+
127+
vigs <- vignette(package = "testVignettesBuilt")$results
128+
expect_equal(nrow(vigs), 1)
129+
expect_equal(vigs[3], "new")
130+
})

tests/testthat/test-vignettes.R

Lines changed: 19 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,105 +2,58 @@ test_that("Sweave vignettes copied into doc", {
22
if (!pkgbuild::has_latex()) {
33
skip("pdflatex not available")
44
}
5-
pkg <- test_path("testVignettes")
6-
doc <- path(pkg, "doc")
7-
8-
suppressMessages(clean_vignettes(pkg))
9-
expect_false(dir_exists(doc))
105

11-
suppressMessages(build_vignettes(pkg))
12-
expect_setequal(path_file(dir_ls(doc)), c("new.pdf", "new.R", "new.Rnw"))
6+
pkg <- local_package_copy(test_path("testVignettes"))
137

14-
suppressMessages(clean_vignettes(pkg))
15-
expect_false(dir_exists(doc))
8+
suppressMessages(build_vignettes(pkg, quiet = TRUE))
9+
expect_setequal(
10+
path_file(dir_ls(path(pkg, "doc"))),
11+
c("new.pdf", "new.R", "new.Rnw")
12+
)
1613
})
1714

1815
test_that("Built files are updated", {
1916
# This test is time dependant and sometimes fails on CRAN because the systems are under heavy load.
2017
skip_on_cran()
21-
if (!pkgbuild::has_latex()) {
22-
skip("pdflatex not available")
23-
}
24-
pkg <- test_path("testVignettes")
25-
26-
suppressMessages(clean_vignettes(pkg))
27-
suppressMessages(build_vignettes(pkg))
28-
on.exit(suppressMessages(clean_vignettes(pkg)))
18+
pkg <- local_package_copy(test_path("testMarkdownVignettes"))
2919

20+
suppressMessages(build_vignettes(pkg, quiet = TRUE))
3021
output <- dir_ls(path(pkg, "doc"), regexp = "new")
3122
first <- file_info(output)$modification_time
3223

3324
Sys.sleep(.01)
34-
suppressMessages(build_vignettes(pkg))
25+
suppressMessages(build_vignettes(pkg, quiet = TRUE))
3526
second <- file_info(output)$modification_time
3627

3728
expect_true(all(second > first))
3829
})
3930

4031
test_that("Rmarkdown vignettes copied into doc", {
41-
pkg <- test_path("testMarkdownVignettes")
32+
pkg <- local_package_copy(test_path("testMarkdownVignettes"))
4233
doc <- path(pkg, "doc")
4334

44-
suppressMessages(clean_vignettes(pkg))
45-
expect_false(dir_exists(doc))
46-
47-
suppressMessages(build_vignettes(pkg))
35+
suppressMessages(build_vignettes(pkg, quiet = TRUE))
4836
expect_setequal(path_file(dir_ls(doc)), c("test.html", "test.R", "test.Rmd"))
49-
50-
suppressMessages(clean_vignettes(pkg))
51-
expect_false(dir_exists(doc))
5237
})
5338

54-
test_that("Extra files copied and removed", {
55-
if (!pkgbuild::has_latex()) {
56-
skip("pdflatex not available")
57-
}
58-
59-
pkg <- test_path("testVignetteExtras")
60-
doc <- path(pkg, "doc")
39+
test_that("extra files copied and removed", {
40+
pkg <- local_package_copy(test_path("testMarkdownVignettes"))
41+
writeLines("a <- 1", path(pkg, "vignettes", "a.R"))
6142

6243
extras_path <- path(pkg, "vignettes", ".install_extras")
6344
writeLines("a.R", extras_path)
64-
on.exit(file_delete(extras_path))
6545

66-
suppressMessages(clean_vignettes(pkg))
67-
expect_false(file_exists(path(doc, "a.R")))
68-
69-
suppressMessages(build_vignettes(pkg))
70-
expect_true(file_exists(path(doc, "a.R")))
46+
suppressMessages(build_vignettes(pkg, quiet = TRUE))
47+
expect_true(file_exists(path(pkg, "doc", "a.R")))
7148

7249
suppressMessages(clean_vignettes(pkg))
73-
expect_false(file_exists(path(doc, "a.R")))
74-
})
75-
76-
test_that("vignettes built on install", {
77-
skip_on_cran()
78-
79-
if (!pkgbuild::has_latex()) {
80-
skip("pdflatex not available")
81-
}
82-
83-
pkg <- test_path("testVignettesBuilt")
84-
85-
withr::local_temp_libpaths()
86-
install(pkg, reload = FALSE, quiet = TRUE, build_vignettes = TRUE)
87-
88-
vigs <- vignette(package = "testVignettesBuilt")$results
89-
expect_equal(nrow(vigs), 1)
90-
expect_equal(vigs[3], "new")
50+
expect_false(file_exists(path(pkg, "doc", "a.R")))
9151
})
9252

9353
test_that(".gitignore updated when building vignettes", {
94-
if (!pkgbuild::has_latex()) {
95-
skip("pdflatex not available")
96-
}
97-
98-
pkg <- test_path("testVignettes")
54+
pkg <- local_package_copy(test_path("testMarkdownVignettes"))
9955
gitignore <- path(pkg, ".gitignore")
10056

101-
suppressMessages(clean_vignettes(pkg))
102-
suppressMessages(build_vignettes(pkg))
103-
on.exit(suppressMessages(clean_vignettes(pkg)))
104-
57+
suppressMessages(build_vignettes(pkg, quiet = TRUE))
10558
expect_true(all(c("/Meta/", "/doc/") %in% readLines(gitignore)))
10659
})

0 commit comments

Comments
 (0)