Skip to content

Commit 2555225

Browse files
authored
Improve quarto_use_template (#261)
* Improve quarto_use_template - Support using in another empty directory - Fails on non-empty directory as interactive prompting is required done by Quarto CLI directly (only for 1.5.15+) * Add NEWS * Bump version
1 parent b1038d5 commit 2555225

File tree

8 files changed

+82
-10
lines changed

8 files changed

+82
-10
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: quarto
22
Title: R Interface to 'Quarto' Markdown Publishing System
3-
Version: 1.4.4.9020
3+
Version: 1.4.4.9021
44
Authors@R: c(
55
person("JJ", "Allaire", , "[email protected]", role = "aut",
66
comment = c(ORCID = "0000-0003-0174-9868")),

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
- `quarto_create_project()` gains a `title` argument to set the project title independently from the directory name. This allows creating projects with custom titles, including when using `name = "."` to create a project in the current directory (thanks, @davidkane9, #148). This matches with `--title` addition for `quarto create project` in Quarto CLI v1.5.15.
44

5+
- `quarto_use_template()` now supports using templates in another empty directory via the `dir` argument. However, the function will fail with a clear error message when used in non-empty directories, as interactive prompting is required and handled by Quarto CLI directly (requires Quarto 1.5.15+). Follow for [quarto-dev/quarto-cli#11127](https://github.com/quarto-dev/quarto-cli/issues/11127) for change with `--no-prompt` behavior in future Quarto versions.
6+
57
- `quarto_render(output_file = )` now sets the `output-file` Quarto metadata instead of the `--output` CLI flag. This allows the output file information to correctly be processed by Quarto, as if passed in a YAML header. e.g. it allows to support multiple output formats in the same render call. `quarto_render(quarto_args = c('--output', 'dummy.html'))` can still be used to set the `--output` CLI flag to enforce using the CLI flag and not the metadata processed by Quarto (#251, #43).
68

79
- Added `check_newer_version()` function to check if a newer version of Quarto is available. The function compares the current Quarto version against the latest stable and prerelease versions. It is aimed for verbosity by default (`verbose = TRUE`), but `verbose = FALSE` can also be set for just checking update availability with TRUE or FALSE return values. Version information is cached per session for up to 24 hours to minimize network requests.

R/use.R

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,42 @@
88
#' @param template The template to install, either an archive or a GitHub
99
#' repository as described in the documentation
1010
#' <https://quarto.org/docs/extensions/formats.html>.
11-
#'
11+
#' @param dir The directory in which to install the template. This must be an empty directory.
12+
#' To use directly in a non-empty directory, use `quarto use template` interactively in the terminal for safe installation
13+
#' without overwrite.
1214
#' @param quiet Suppress warnings and messages.
1315
#'
1416
#'
1517
#' @examples
1618
#' \dontrun{
17-
#' # Install a template and set up a draft document from a GitHub repository
19+
#' # Use a template and set up a draft document from a GitHub repository
1820
#' quarto_use_template("quarto-journals/jss")
1921
#'
20-
#' # Install a template and set up a draft document from a ZIP archive
22+
#' # Use a template in current directory by installing it in an empty directory
23+
#' quarto_use_template("quarto-journals/jss", dir = "new-empty-dir")
24+
#'
25+
#' # Use a template and set up a draft document from a ZIP archive
2126
#' quarto_use_template("https://github.com/quarto-journals/jss/archive/refs/heads/main.zip")
2227
#' }
2328
#'
2429
#' @export
2530
quarto_use_template <- function(
2631
template,
32+
dir = ".",
2733
no_prompt = FALSE,
2834
quiet = FALSE,
2935
quarto_args = NULL
3036
) {
3137
rlang::check_required(template)
3238

39+
if (!is_empty_dir(dir) && quarto_available("1.5.15")) {
40+
cli::cli_abort(c(
41+
"{.arg dir} must be an empty directory.",
42+
"x" = "The directory {.path {dir}} is not empty.",
43+
" " = "Please provide an empty directory or use {.code quarto use template {template} } interactively in terminal."
44+
))
45+
}
46+
3347
quarto_bin <- find_quarto()
3448

3549
# This will ask for approval or stop installation
@@ -47,8 +61,10 @@ quarto_use_template <- function(
4761
if (quarto_version() > "1.5.4" && is_quiet(quiet)) {
4862
args <- cli_arg_quiet(args)
4963
}
50-
51-
quarto_use(args, quarto_bin = quarto_bin, echo = !quiet)
64+
xfun::in_dir(
65+
dir,
66+
quarto_use(args, quarto_bin = quarto_bin, echo = !quiet)
67+
)
5268
}
5369

5470
invisible()

R/utils.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,11 @@ has_internet <- function(host = "https://www.google.com") {
6565
}
6666
)
6767
}
68+
69+
is_empty_dir <- function(dir) {
70+
if (!dir.exists(dir)) {
71+
return(FALSE)
72+
}
73+
files <- list.files(dir, all.files = TRUE, no.. = TRUE)
74+
length(files) == 0
75+
}

man/quarto_use_template.Rd

Lines changed: 10 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/use.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Installing an extension in non empty dir errors
2+
3+
Code
4+
quarto_use_template("quarto-journals/jss", no_prompt = TRUE, quiet = TRUE)
5+
Condition
6+
Error in `quarto_use_template()`:
7+
! `dir` must be an empty directory.
8+
x The directory '.' is not empty.
9+
Please provide an empty directory or use `quarto use template quarto-journals/jss ` interactively in terminal.
10+

tests/testthat/helper.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
skip_if_no_quarto <- function(ver = NULL) {
33
skip_if(is.null(quarto_path()), message = "Quarto is not available")
44
skip_if(
5-
quarto_version() < ver,
5+
quarto_available(min = ver, error = FALSE),
66
message = sprintf(
77
"Version of quarto is lower than %s: %s.",
88
ver,

tests/testthat/test-use.R

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
test_that("Installing an extension", {
1+
test_that("Installing an extension in empty dir", {
22
skip_if_no_quarto()
33
skip_if_offline("github.com")
44
dir <- withr::local_tempdir()
@@ -8,3 +8,31 @@ test_that("Installing an extension", {
88
expect_true(dir.exists("_extensions/quarto-journals/jss"))
99
expect_length(list.files(pattern = "[.]qmd$"), 1)
1010
})
11+
12+
test_that("Installing an extension in a new empty dir", {
13+
skip_if_no_quarto()
14+
skip_if_offline("github.com")
15+
dir <- withr::local_tempdir()
16+
withr::local_dir(dir)
17+
quarto_use_template(
18+
"quarto-journals/jss",
19+
dir = "empty-dir",
20+
no_prompt = TRUE,
21+
quiet = TRUE
22+
)
23+
expect_true(dir.exists("empty-dir/_extensions/quarto-journals/jss"))
24+
expect_length(list.files(path = "empty-dir", pattern = "[.]qmd$"), 1)
25+
expect_equal(list.files(no.. = TRUE, all.files = TRUE), "empty-dir")
26+
})
27+
28+
test_that("Installing an extension in non empty dir errors", {
29+
skip_if_no_quarto("1.5.15")
30+
skip_if_offline("github.com")
31+
dir <- withr::local_tempdir()
32+
withr::local_dir(dir)
33+
file.create("README.md")
34+
expect_snapshot(
35+
error = TRUE,
36+
quarto_use_template("quarto-journals/jss", no_prompt = TRUE, quiet = TRUE),
37+
)
38+
})

0 commit comments

Comments
 (0)