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

Simplify orderly initialisation #118

Merged
merged 1 commit into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 R/example.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ orderly_example <- function(name, ..., dest = NULL) {
}
src <- orderly2_file("example")
fs::dir_copy(src, dest)
orderly2::orderly_init(..., path = dest)
orderly2::orderly_init(..., root = dest)
invisible(dest)
}
41 changes: 23 additions & 18 deletions R/root.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@
##'
##' @title Initialise an orderly repository
##'
##' @param path The path to initialise the repository at. If the
##' repository is already initialised, this operation does nothing.
##' @param root The path to initialise the repository root at. If the
##' repository is already initialised, this operation checks that
##' the options passed in are the same as those set in the
##' repository (erroring if not), but otherwise does nothing. The
##' default path is the current working directory.
##'
##' @param path_archive Path to the archive directory, used to store
##' human-readable copies of packets. If `NULL`, no such copy is
Expand Down Expand Up @@ -59,27 +62,27 @@
##' orderly2::orderly_init(path, use_file_store = TRUE)
##'
##' # fs::dir_tree(path, all = TRUE)
orderly_init <- function(path,
orderly_init <- function(root = ".",
path_archive = "archive",
use_file_store = FALSE,
require_complete_tree = FALSE) {
assert_scalar_character(path)
has_orderly_config <- file.exists(file.path(path, "orderly_config.yml"))
if (!has_orderly_config && file.exists(path)) {
if (!is_directory(path)) {
cli::cli_abort("'path' exists but is not a directory")
assert_scalar_character(root)
has_orderly_config <- file.exists(file.path(root, "orderly_config.yml"))
if (!has_orderly_config && file.exists(root)) {
if (!is_directory(root)) {
cli::cli_abort("'root' exists but is not a directory")
}
if (!file.exists(file.path(path, ".outpack"))) {
if (!file.exists(file.path(root, ".outpack"))) {
## We may need to relax this, but it's not really clear to me
## how the user gets into this position; they have a bunch of
## files there and they want to a root into it?
##
## One option is provide a boolean arg to proceed anyway in this
## case, at the moment there's not a lot that can be done to
## undo this situation.
if (length(dir(path, all.files = TRUE, no.. = TRUE)) > 0) {
if (length(dir(root, all.files = TRUE, no.. = TRUE)) > 0) {
cli::cli_abort(c(
"'path' exists but is not empty, or an outpack archive",
"'root' exists but is not empty, or an outpack archive",
i = "Please have a chat with us if this is something you need to do"))
}
}
Expand All @@ -88,25 +91,27 @@ orderly_init <- function(path,
config <- config_new(path_archive, use_file_store, require_complete_tree,
call = environment())

path_outpack <- file.path(path, ".outpack")
path_outpack <- file.path(root, ".outpack")
if (file.exists(path_outpack)) {
root <- root_open(path, locate = FALSE, require_orderly = FALSE)
root <- root_open(root, locate = FALSE, require_orderly = FALSE)
root_validate_same_configuration(match.call(), config, root, environment())
} else {
fs::dir_create(path_outpack)
fs::dir_create(file.path(path_outpack, "metadata"))
fs::dir_create(file.path(path_outpack, "location"))
config_write(config, path)
root <- outpack_root$new(path)
cli::cli_alert_success("Created orderly root at '{path}'")
config_write(config, root)
root <- outpack_root$new(root)
cli::cli_alert_success("Created orderly root at '{root$path}'")
}

if (!has_orderly_config) {
writeLines(empty_config_contents(), file.path(path, "orderly_config.yml"))
writeLines(empty_config_contents(),
file.path(root$path, "orderly_config.yml"))
}

root <- root_open(path, locate = FALSE, require_orderly = TRUE,
root <- root_open(root, locate = FALSE, require_orderly = TRUE,
call = environment())

invisible(root$path)
}

Expand Down
9 changes: 6 additions & 3 deletions man/orderly_init.Rd

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

12 changes: 10 additions & 2 deletions tests/testthat/test-init.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ test_that("Initialisation requires empty directory", {
on.exit(unlink(tmp, recursive = TRUE))
file.create(file.path(tmp, "file"))
expect_error(orderly_init_quietly(tmp),
"'path' exists but is not empty, or an outpack archive")
"'root' exists but is not empty, or an outpack archive")
})


Expand Down Expand Up @@ -99,7 +99,7 @@ test_that("Initialisation can't be done into a file", {
tmp <- withr::local_tempfile()
file.create(tmp)
expect_error(orderly_init_quietly(tmp),
"'path' exists but is not a directory")
"'root' exists but is not a directory")
})


Expand Down Expand Up @@ -178,3 +178,11 @@ test_that("inform about weirdly nested roots: orderly in outpack", {
x = "outpack is nested within orderly at 'a/b'",
i = "How did you even do this? Please let us know!"))
})


test_that("create root in wd by default", {
path <- withr::local_tempdir()
root <- withr::with_dir(path, suppressMessages(orderly_init()))
expect_true(file.exists(file.path(path, ".outpack")))
expect_true(file.exists(file.path(path, "orderly_config.yml")))
})