Skip to content

Commit

Permalink
streamline roclet.R code to fix #38
Browse files Browse the repository at this point in the history
  • Loading branch information
mpadge committed Mar 13, 2024
1 parent a334617 commit 1b9bac7
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 96 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
@@ -1,6 +1,6 @@
Package: srr
Title: 'rOpenSci' Review Roclets
Version: 0.1.1.001
Version: 0.1.2
Authors@R:
person("Mark", "Padgham", , "mark@ropensci.org", role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-2172-5265"))
Expand Down
23 changes: 4 additions & 19 deletions R/pre-submit.R
Expand Up @@ -61,27 +61,12 @@ srr_stats_pre_submit <- function (path = ".", quiet = FALSE) {

get_stds_from_code <- function (path) {

flist <- get_all_file_names (path)

suppressWarnings ({
blocks <- lapply (flist, function (i) {
tryCatch (roxygen2::parse_file (i, env = NULL),
error = function (e) list ()
)
})
})
names (blocks) <- flist
blocks <- do.call (c, blocks)
blocks <- collect_blocks (blocks, path)

msgs <- collect_one_tag (path, blocks, tag = "srrstats")
msgs_na <- collect_one_tag (path, blocks, tag = "srrstatsNA")
msgs_todo <- collect_one_tag (path, blocks, tag = "srrstatsTODO")
msgs <- get_all_msgs (path) # in report.R

ret <- list (
stds = parse_std_refs (msgs, "std"),
stds_na = parse_std_refs (msgs_na, "na"),
stds_todo = parse_std_refs (msgs_todo, "todo")
stds = parse_std_refs (msgs$msgs, "std"),
stds_na = parse_std_refs (msgs$msgs_na, "na"),
stds_todo = parse_std_refs (msgs$msgs_todo, "todo")
)

do.call (rbind, ret)
Expand Down
17 changes: 9 additions & 8 deletions R/report.R
Expand Up @@ -206,20 +206,21 @@ get_all_msgs <- function (path = ".") {

flist <- get_all_file_names (path)

pkg_name <- paste0 ("package:", pkg_name_from_desc (path))
if (!pkg_name %in% search ()) {
pkgload::load_all (path)
}
pkg_env <- as.environment (pkg_name)

blocks <- lapply (flist, function (i) {
this_file <- i
if (grepl ("\\.Rmd$", i)) {
is_rmd <- grepl ("\\.Rmd$", i)
if (is_rmd) {
fout <- tempfile ()
rcpp_parse_rmd (i, fout)
this_file <- fout
}
res <- try (roxygen2::parse_file (this_file, env = pkg_env))
res <- try (roxygen2::parse_file (this_file, env = NULL))
if (is_rmd) {
res <- lapply (res, function (j) {
j$file <- i
return (j)
})
}
return (res)
})

Expand Down
79 changes: 19 additions & 60 deletions R/roclet.R
@@ -1,4 +1,3 @@

#' srr_stats_roclet
#'
#' Get values of all `srrstats` tags in function documentation
Expand Down Expand Up @@ -90,13 +89,27 @@ collect_blocks <- function (blocks, base_path) {
)
rcpp_blocks <- blocks [which (rcpp)]
blocks <- blocks [which (!rcpp)]
test_blocks <- get_test_blocks (base_path)
readme_blocks <- get_readme_blocks (base_path)

file_paths <- vapply (blocks, function (i) {
gsub (base_path, "", i$file)
}, character (1), USE.NAMES = FALSE)
re <- regexpr ("^.*\\/", file_paths)
file_dirs <- rep (".", length (file_paths))
index <- which (re > 0)
file_dirs [index] <- regmatches (file_paths, re)
file_dirs <- gsub ("^\\/", "", file_dirs)
file_dirs <- gsub ("\\/.*$", "", file_dirs)

readme_blocks <- blocks [which (file_dirs == ".")]
test_blocks <- blocks [which (file_dirs == "tests")]
r_blocks <- blocks [which (file_dirs == "R")]
vignette_blocks <- blocks [which (file_dirs == "vignettes")]

blocks <- list (
R = blocks,
R = r_blocks,
src = rcpp_blocks,
tests = test_blocks,
vignettes = vignette_blocks,
readme = readme_blocks
)

Expand Down Expand Up @@ -135,44 +148,6 @@ get_verbose_flag <- function (blocks) {
return (as.logical (flag))
}

get_readme_blocks <- function (base_path) {

blocks <- NULL

f <- list.files (
base_path,
pattern = "readme\\.rmd$",
ignore.case = TRUE,
full.names = TRUE
)
if (length (f) != 1L) {
return (blocks)
}

if (file.exists (f)) {
f <- normalizePath (f)
fout <- tempfile ()
rcpp_parse_rmd (f, fout)
blocks <- tryCatch (
roxygen2::parse_file (fout, env = NULL),
error = function (e) e
)

if (methods::is (blocks, "error")) {

blocks <- NULL

} else {

blocks <- lapply (blocks, function (i) {
i$file <- f
return (i) })
}
}

return (blocks)
}

parse_one_msg_list <- function (msgs, block, tag, fn_name = TRUE, dir = "R") {

if (length (roxygen2::block_get_tags (block, tag)) > 0L) {
Expand All @@ -198,8 +173,7 @@ print_one_msg_list <- function (msgs) {
}

# Collect all messages for one tag
collect_one_tag <- function (base_path, blocks, test_blocks, rcpp_blocks,
tag = "srrstats") {
collect_one_tag <- function (base_path, blocks, tag = "srrstats") {

msgs <- list ()
for (block in blocks$R) {
Expand All @@ -211,6 +185,7 @@ collect_one_tag <- function (base_path, blocks, test_blocks, rcpp_blocks,
)
msgs <- c (msgs, get_src_tags (blocks$src, base_path, tag = tag))
msgs <- c (msgs, get_other_tags (blocks$readme, tag = tag, dir = "."))
msgs <- c (msgs, get_other_tags (blocks$vignettes, tag = tag, dir = "vignettes"))

return (msgs)
}
Expand Down Expand Up @@ -448,22 +423,6 @@ get_src_tags <- function (blocks, base_path, tag = "srrstats") {
return (msgs)
}

get_test_blocks <- function (base_path) {


flist <- list.files (file.path (base_path, "tests"),
pattern = "\\.R$",
recursive = TRUE,
full.names = TRUE
)

blocks <- lapply (flist, function (i) roxygen2::parse_file (i, env = NULL))
names (blocks) <- flist
blocks <- do.call (c, blocks)

return (blocks)
}

get_other_tags <- function (blocks, tag = "srrstats", dir = "tests") {

msgs <- list ()
Expand Down
13 changes: 5 additions & 8 deletions codemeta.json
Expand Up @@ -8,13 +8,13 @@
"codeRepository": "https://github.com/ropensci-review-tools/srr",
"issueTracker": "https://github.com/ropensci-review-tools/srr/issues",
"license": "https://spdx.org/licenses/MIT",
"version": "0.1.1.001",
"version": "0.1.2",
"programmingLanguage": {
"@type": "ComputerLanguage",
"name": "R",
"url": "https://r-project.org"
},
"runtimePlatform": "R version 4.3.2 (2023-10-31)",
"runtimePlatform": "R version 4.3.3 (2024-02-29)",
"author": [
{
"@type": "Person",
Expand Down Expand Up @@ -186,9 +186,9 @@
},
"sameAs": "https://CRAN.R-project.org/package=roxygen2"
},
"SystemRequirements": {}
"SystemRequirements": null
},
"fileSize": "1414.674KB",
"fileSize": "1414.08KB",
"citation": [
{
"@type": "SoftwareSourceCode",
Expand All @@ -206,9 +206,6 @@
}
],
"readme": "https://github.com/ropensci-review-tools/srr/blob/main/README.md",
"contIntegration": [
"https://github.com/ropensci-review-tools/srr/actions",
"https://codecov.io/gh/ropensci-review-tools/srr"
],
"contIntegration": ["https://github.com/ropensci-review-tools/srr/actions", "https://codecov.io/gh/ropensci-review-tools/srr"],
"developmentStatus": "https://www.repostatus.org/#concept"
}

0 comments on commit 1b9bac7

Please sign in to comment.