Skip to content

Commit

Permalink
Prepare parsing Rmd/qmd files
Browse files Browse the repository at this point in the history
  • Loading branch information
gaborcsardi committed Oct 20, 2024
1 parent c38ecc0 commit 37a2490
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
35 changes: 32 additions & 3 deletions R/scan-deps.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
#' Scan R code for dependent packages
#'
#' @keywords internal

scan_deps <- function(path = ".") {
paths <- dir(path, pattern = "[.]R$", recursive = TRUE)
do.call("rbind", lapply(paths, scan_path_deps))
paths <- dir(path, pattern = "[.](R|r|Rmd|rmd)$", recursive = TRUE)
full_paths <- normalizePath(file.path(path, paths))
deps_list <- lapply(full_paths, scan_path_deps)
deps <- do.call("rbind", c(list(scan_path_deps_empty()), deps_list))
# write back the relative paths
deps$path <- paths[match(deps$path, full_paths)]
deps
}

# -------------------------------------------------------------------------

# needs to increase as the deps discovry code changes, otherwise we don't
# apply the new discovery code
deps_cache_version <- 1L
Expand Down Expand Up @@ -61,13 +72,25 @@ scan_path_deps_empty <- function() {
}

scan_path_deps_do <- function(code, path) {
ext <- tolower(file_ext(path))
switch(
ext,
".r" = scan_path_deps_do_r(code, path),
".qmd" = ,
".rmd" = scan_path_deps_do_rmd(code, path),
stop("Cannot parse ", ext, " file for dependencies, internal error")
)
}

# -------------------------------------------------------------------------

scan_path_deps_do_r <- function(code, path) {
hits <- code_query(code, q_deps())
# q_library_0 hits are generic ones, only use them if they are not hit
gen_pat <- hits$patterns$id[hits$patterns$name == "q_library_0"]
gen_hits <- hits$matched_captures[hits$matched_captures$pattern %in% gen_pat, ]
ok_hits <- hits$matched_captures[! hits$matched_captures$pattern %in% gen_pat, ]
rbind(
scan_path_deps_empty(),
if (nrow(ok_hits) > 0) scan_path_deps_do_ok_hits(ok_hits, path),
if (nrow(gen_hits) > 0) scan_path_deps_do_gen_hits(gen_hits, path)
)
Expand Down Expand Up @@ -123,3 +146,9 @@ parse_pkg_from_library_call <- function(fn, code) {

NA_character_
}

# -------------------------------------------------------------------------

scan_path_deps_do_rmd <- function(code, path) {
# TODO
}
4 changes: 4 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -503,3 +503,7 @@ collapse <- function(x, ...) {
na_omit <- function(x) {
x[!is.na(x)]
}

file_ext <- function(x) {
re_match(x, "[.]([[:alnum:]]+)$")[[".match"]]
}

0 comments on commit 37a2490

Please sign in to comment.