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

Add function to return details of report script from parsed exprs #129

Merged
merged 8 commits into from
Mar 8, 2024
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
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: orderly2
Title: Orderly Next Generation
Version: 1.99.12
Version: 1.99.13
Authors@R: c(person("Rich", "FitzJohn", role = c("aut", "cre"),
email = "[email protected]"),
person("Robert", "Ashton", role = "aut"),
Expand All @@ -11,7 +11,7 @@ Description: Reimplementation of orderly based on outpack.
License: MIT + file LICENSE
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
URL: https://github.com/mrc-ide/orderly2
BugReports: https://github.com/mrc-ide/orderly2/issues
Imports:
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export(orderly_metadata_extract)
export(orderly_metadata_read)
export(orderly_new)
export(orderly_parameters)
export(orderly_parse_expr)
export(orderly_parse_file)
export(orderly_plugin_add_metadata)
export(orderly_plugin_context)
export(orderly_plugin_register)
Expand Down
32 changes: 28 additions & 4 deletions R/read.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,35 @@
orderly_read <- function(path, call = NULL) {
entrypoint_filename <- find_entrypoint_filename(path)
orderly_read_r(file.path(path, entrypoint_filename), entrypoint_filename)
orderly_parse_file(file.path(path, entrypoint_filename))
}


orderly_read_r <- function(path, entrypoint_filename) {
#' Parse the orderly entrypoint script
#'
#' For expert use only.
#'
#' Parses details of any calls to the orderly_ in-script functions
#' into intermediate representation for downstream use. Also validates
#' that any calls to `orderly_*` in-script functions are well-formed.
#'
#' @param path Path to `orderly_*` script
#'
#' @return Parsed orderly entrypoint script
#' @export
orderly_parse_file <- function(path) {
assert_file_exists(path)
exprs <- parse(file = path)
orderly_parse_expr(exprs, basename(path))
}


#' @param exprs Parsed AST from `orderly_*` script
#' @param filename Name of `orderly_*` file to include in metadata
#'
#' @rdname orderly_parse_file
#' @export
orderly_parse_expr <- function(exprs, filename) {
assert_is(exprs, "expression")

inputs <- list()
artefacts <- list()
Expand Down Expand Up @@ -50,7 +74,7 @@ orderly_read_r <- function(path, entrypoint_filename) {
## Rename to make things easier below:
names(dat) <- sub("^orderly_", "", names(dat))

ret <- list(entrypoint_filename = entrypoint_filename)
ret <- list(entrypoint_filename = filename)
if (length(dat$strict_mode) > 0) {
ret$strict <- dat$strict_mode[[1]]
} else {
Expand All @@ -76,7 +100,7 @@ orderly_read_r <- function(path, entrypoint_filename) {

if (length(dat$resource) > 0) {
ret$resources <- setdiff(unique(unlist(dat$resource, TRUE, FALSE)),
entrypoint_filename)
filename)
}
if (length(dat$artefact) > 0) {
ret$artefacts <- dat$artefact
Expand Down
2 changes: 2 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,5 @@ reference:
- orderly_validate_archive
- orderly_hash_data
- orderly_hash_file
- orderly_parse_file
- orderly_parse_expr
29 changes: 29 additions & 0 deletions man/orderly_parse_file.Rd

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

18 changes: 12 additions & 6 deletions tests/testthat/test-read.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
test_that("can read file with no helpers", {
expect_equal(orderly_read_r("examples/implicit/implicit.R", "implicit.R"),
test_that("can parse file with no helpers", {
expect_equal(orderly_parse_file("examples/implicit/implicit.R"),
list(entrypoint_filename = "implicit.R",
strict = list(enabled = FALSE)))
})


test_that("can read file with helpers", {
dat <- orderly_read_r("examples/explicit/explicit.R", "explicit.R")
test_that("can parse file with helpers", {
dat <- orderly_parse_file("examples/explicit/explicit.R")
expect_setequal(names(dat),
c("entrypoint_filename", "strict", "resources", "artefacts"))
expect_equal(dat$strict, list(enabled = FALSE))
Expand All @@ -17,9 +17,15 @@ test_that("can read file with helpers", {
})


test_that("can parse file from expression", {
exprs <- parse(file = "examples/explicit/explicit.R")
dat <- orderly_parse_expr(exprs = exprs, filename = "explicit.R")
expect_equal(dat, orderly_parse_file("examples/explicit/explicit.R"))
})


test_that("Skip over computed resources", {
dat <- orderly_read_r("examples/computed-resource/computed-resource.R",
"computed-resource.R")
dat <- orderly_parse_file("examples/computed-resource/computed-resource.R")
expect_null(dat$resources)
})

Expand Down
Loading