Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic orderly_new implementation #114

Merged
merged 5 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: orderly2
Title: Orderly Next Generation
Version: 1.99.6
Version: 1.99.8
Authors@R: c(person("Rich", "FitzJohn", role = c("aut", "cre"),
email = "[email protected]"),
person("Robert", "Ashton", role = "aut"),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export(orderly_location_rename)
export(orderly_metadata)
export(orderly_metadata_extract)
export(orderly_metadata_read)
export(orderly_new)
export(orderly_parameters)
export(orderly_plugin_add_metadata)
export(orderly_plugin_context)
Expand Down
54 changes: 54 additions & 0 deletions R/orderly.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,57 @@ orderly_list_src <- function(root = NULL, locate = TRUE) {
pos <- fs::dir_ls(file.path(root_path, "src"), type = "directory")
basename(pos)[file_exists(file.path(pos, "orderly.R"))]
}


##' Create a new empty report.
##'
##' @title Create a new report
##'
##' @param name The name of the report
##'
##' @param template The template to use. The only acceptable values
##' for now are `NULL` (uses the built-in default) and `FALSE` which
##' suppresses any default content. We may support customisable
##' templates in future - let us know if this would be useful.
##'
##' @param force Create an `orderly.R` file within an existing
##' directory `src/<name>`; this may be useful if you have already
##' created the directory and some files first but want help
##' creating the orderly file.
##'
##' @inheritParams orderly_run
##'
##' @return Nothing, called for its side effects only
##' @export
orderly_new <- function(name, template = NULL, force = FALSE,
root = NULL, locate = TRUE) {
root <- root_open(root, locate, require_orderly = TRUE, call = environment())
dest <- file.path(root$path, "src", name)

if (file.exists(file.path(dest, "orderly.R"))) {
cli::cli_abort("'src/{name}/orderly.R' already exists")
}
if (file.exists(dest) && !fs::is_dir(dest)) {
cli::cli_abort(
c("'src/{name}' already exists, but is not a directory",
i = "This file really should not be here, you might need to tidy up"))
}
if (length(dir(dest, all.files = TRUE, no.. = TRUE)) > 0 && !force) {
cli::cli_abort(
c("'src/{name}/' already exists and contains files",
i = paste("If you want to add an orderly.R to this directory,",
"rerun {.code orderly_new()} with {.code force = TRUE}")))
}

if (is.null(template)) {
contents <- readLines(orderly2_file("template/default.R"))
} else if (isFALSE(template)) {
contents <- character()
} else {
cli::cli_abort("'template' must be 'NULL' or 'FALSE' for now")
}

fs::dir_create(dest)
writeLines(contents, file.path(dest, "orderly.R"))
cli::cli_alert_success("Created 'src/{name}/orderly.R'")
}
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ reference:
- orderly_location_rename
- title: Help for developing
contents:
- orderly_new
- orderly_cleanup
- orderly_cleanup_status
- orderly_gitignore_update
Expand Down
22 changes: 22 additions & 0 deletions inst/template/default.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# This is an orderly script - edit it to suit your needs. You might include
#
# * orderly2::orderly_parameters():
# declare parameters that your report accepts
# * orderly2::orderly_description():
# describe your report with friendly names, descriptions and metadata
# * orderly2::orderly_resource():
# declare files in your source tree that are inputs
# * orderly2::orderly_shared_resource():
# use files from the root directory's 'shared/' directory
# * orderly2::orderly_dependency():
# use files from a previously-run packet
# * orderly2::orderly_artefact():
# declare files that you promise to produce, and describe them
# * orderly2::orderly_strict_mode():
# enable some optional checks
#
# See the docs for more information:
# https://mrc-ide.github.io/orderly2/reference/
#
# To generate templates without this header, pass template = FALSE to
# orderly_new(); this header can be safely deleted if you don't need it.
38 changes: 38 additions & 0 deletions man/orderly_new.Rd

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

81 changes: 81 additions & 0 deletions tests/testthat/test-orderly.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,84 @@ test_that("no candidates returns empty character vector", {
expect_equal(withr::with_dir(path, orderly_list_src()), character())
expect_equal(orderly_list_src(path), character())
})


test_that("can create empty orderly report", {
path <- test_prepare_orderly_example(character())
expect_message(
orderly_new("foo", root = path),
"Created 'src/foo/orderly.R'")
path_orderly <- file.path(path, "src", "foo", "orderly.R")
expect_true(file.exists(path_orderly))
txt <- readLines(path_orderly)
expect_match(txt[[1]], "This is an orderly script")
})


test_that("can create a totally blank orderly report", {
path <- test_prepare_orderly_example(character())
expect_message(
orderly_new("foo", template = FALSE, root = path),
"Created 'src/foo/orderly.R'")
path_orderly <- file.path(path, "src", "foo", "orderly.R")
expect_true(file.exists(path_orderly))
expect_equal(readLines(path_orderly), character())
})


test_that("error if orderly.R exists already", {
path <- test_prepare_orderly_example("data")
expect_error(orderly_new("data", root = path),
"'src/data/orderly.R' already exists")
expect_error(orderly_new("data", force = TRUE, root = path),
"'src/data/orderly.R' already exists")
})


test_that("error if a non-directory file is found in the src dir", {
path <- test_prepare_orderly_example(character())
file.create(file.path(path, "src", "foo"))
err <- expect_error(
orderly_new("foo", template = FALSE, root = path),
"'src/foo' already exists, but is not a directory")
expect_equal(
err$body,
c(i = "This file really should not be here, you might need to tidy up"))
})


test_that("allow creation of orderly.R in existing dir if force is given", {
path <- test_prepare_orderly_example(character())
fs::dir_create(file.path(path, "src", "foo"))
file.create(file.path(path, "src", "foo", "bar"))
err <- expect_error(
orderly_new("foo", root = path),
"'src/foo/' already exists and contains files")
expect_equal(
err$body,
c(i = paste("If you want to add an orderly.R to this directory,",
"rerun `orderly_new()` with `force = TRUE`")))
expect_message(
orderly_new("foo", force = TRUE, root = path),
"Created 'src/foo/orderly.R'")
expect_true(file.exists(file.path(path, "src/foo/orderly.R")))
})


test_that("allow creation of orderly.R in existing empty dir", {
path <- test_prepare_orderly_example(character())
fs::dir_create(file.path(path, "src", "foo"))
expect_message(
orderly_new("foo", root = path),
"Created 'src/foo/orderly.R'")
expect_true(file.exists(file.path(path, "src/foo/orderly.R")))
})


test_that("disallow template arguments", {
path <- test_prepare_orderly_example(character())
expect_error(
orderly_new("foo", template = "other", root = path),
"'template' must be 'NULL' or 'FALSE' for now")
expect_false(file.exists(file.path(path, "src/foo")))
})