Skip to content

Commit

Permalink
Add quarto create project support
Browse files Browse the repository at this point in the history
closes #87
  • Loading branch information
cderv committed Jan 26, 2024
1 parent d5dd70f commit ff9b71d
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 2 deletions.
61 changes: 61 additions & 0 deletions R/create.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#' Create a quarto project
#'
#' This function calls `quarto create project <type> <name>`. It will create a
#' new directory with the project name and add some skeletons files for the
#' project type chosen.
#'
#' # Quarto version required
#'
#' This function require Quarto 1.4 or higher. Use [`quarto_version()`]to check
#' the version of Quarto detected.
#'
#' @param type The type of project to create. As of 1.4, it can be one of
#' `r paste0("\\code{", paste(quarto_project_type, collapse = "}, \\code{"),"}")`.
#' @param name The name of the project and the directory that will be created.
#' @param dir The directory where to create the new Quarto project.
#'
#' @seealso Quarto documentation on [Quarto projects](https://quarto.org/docs/projects/quarto-projects.html)
#'
#' @inheritParams quarto_render
quarto_create_project <- function(name, type = "default", dir = ".", no_prompt = FALSE, quiet = FALSE, quarto_args = NULL) {

check_quarto_version("1.4", "quarto create project", "https://quarto.org/docs/projects/quarto-projects.html")

if (rlang::is_missing(name)) {
cli::cli_abort("You need to provide {.arg name} for the new project.")
}

if (rlang::is_interactive() && !no_prompt) {
cli::cli_inform(c(
"This will create a new Quarto {.emph {type}} project as a folder named {.strong {name}} in {.path {xfun::normalize_path(dir)}}."
))
prompt_value <- tolower(readline(sprintf("Do you want to proceed (Y/n)? ")))
if (!prompt_value %in% "y") {
cli::cli_abort("Operation aborted.")
}
}

quarto_bin <- find_quarto()

args <- c("project", type, name, "--no-prompt", "--no-open", if (quiet) cli_arg_quiet(), quarto_args = NULL)

owd <- setwd(dir)
on.exit(setwd(owd), add = TRUE, after = FALSE)
quarto_create(args, quarto_bin = quarto_bin, echo = TRUE)

}

quarto_create <- function(args = character(), ...) {
quarto_run_what("create", args = args, ...)
}


# This list is for 1.4
quarto_project_type <- c(
"default",
"website",
"blog",
"book",
"manuscript",
"confluence"
)
9 changes: 8 additions & 1 deletion R/quarto.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,11 @@ is_using_quarto <- function(dir = ".", verbose = FALSE) {
return(FALSE)
}


check_quarto_version <- function(ver, what, url) {
if (quarto_version() < ver) {
cli::cli_abort(c(
"{.code {what}} has been added in Quarto {ver}. See {.url {url}}.",
i = "You are using {.strong {quarto_version()}} from {.file {quarto_path()}}.")
)
}
}
2 changes: 1 addition & 1 deletion R/use.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ quarto_use_template <- function(template, no_prompt = FALSE, quiet = FALSE, quar

quarto_bin <- find_quarto()

# This will ask for approval or stop installation
# This will ask for approval or stop installation
check_extension_approval(no_prompt, "Quarto templates", "https://quarto.org/docs/extensions/formats.html#distributing-formats")

# quarto use template does not support `--quiet` so we mimic it by suppressing `echo` in processx
Expand Down
42 changes: 42 additions & 0 deletions man/quarto_create_project.Rd

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

9 changes: 9 additions & 0 deletions tests/testthat/_snaps/before-1-4/create.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# create project only available for 1.4

Code
quarto_create_project(name = "test")
Condition
Error in `check_quarto_version()`:
! `quarto create project` has been added in Quarto 1.4. See <https://quarto.org/docs/projects/quarto-projects.html>.
i You are using 1.3.433 from '<quarto full path>'.

8 changes: 8 additions & 0 deletions tests/testthat/_snaps/create.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Create a quarto project

Code
quarto_create_project()
Condition
Error in `quarto_create_project()`:
! You need to provide `name` for the new project.

31 changes: 31 additions & 0 deletions tests/testthat/test-create.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
test_that("Create a quarto project", {
skip_if_no_quarto("1.4")
expect_snapshot(
error = TRUE,
quarto_create_project()
)
tempdir <- withr::local_tempdir()
withr::local_dir(tempdir)
expect_no_error(quarto_create_project(name = "test-project", quiet = TRUE))
expect_true(dir.exists("test-project"))
})

test_that("Create a quarto project in another directory", {
skip_if_no_quarto("1.4")
tempdir <- withr::local_tempdir()
curr_wd <- getwd()
expect_no_error(quarto_create_project(name = "test-project", dir = tempdir, quiet = TRUE))
expect_true(dir.exists(file.path(tempdir, "test-project")))
expect_identical(curr_wd, getwd())
})

test_that("create project only available for 1.4", {
skip_if_quarto("1.4")
local_reproducible_output(width = 1000)
expect_snapshot(
error = TRUE,
quarto_create_project(name = "test"),
transform = transform_quarto_cli_in_output(full_path = TRUE),
variant = "before-1-4"
)
})

0 comments on commit ff9b71d

Please sign in to comment.