|
| 1 | +##' detect usage of using::pkg in a source file |
| 2 | +##' |
| 3 | +##' Intended for use when building lock files (e.g. for {renv}) from source |
| 4 | +##' files. |
| 5 | +##' |
| 6 | +##' returns a data.frame of all information in using::pkg calls with columns: |
| 7 | +##' `package`, `min_version`, `repo`. The value of a row may be NA if the |
| 8 | +##' argument was not supplied. |
| 9 | +##' |
| 10 | +##' Each result row is guaranteed to be unique, however the result data frame may |
| 11 | +##' contain near duplicates, e.g. using::pkg(janitor, min_version = "2.0.1") and |
| 12 | +##' using::pkg(janitor, min_version = "1.0.0") would each create a row in the |
| 13 | +##' result data frame if they appeared in the same file, due to differing |
| 14 | +##' min_version. |
| 15 | +##' |
| 16 | +##' Each using::pkg call is parsed based on it's literal expression and no variable |
| 17 | +##' substitution is done. So using(janitor, min_version = janitor_ver) would |
| 18 | +##' place "janitor_ver" in the min_row column of the result for this dependency. |
| 19 | +##' |
| 20 | +##' @title detect_dependencies |
| 21 | +##' @param file_path a length 1 character vector file path to an .R or .Rmd file |
| 22 | +##' @return a data.frame summarising found using::pkg calls in the supplied file. |
| 23 | +##' @author Miles McBain |
| 24 | +##' @export |
| 25 | +detect_dependencies <- function(file_path) { |
| 26 | + if (length(file_path) > 1) stop("file_path must be single file path not a vector of length > 1") |
| 27 | + |
| 28 | + if (!file.exists(file_path)) stop("could not find file ", file_path) |
| 29 | + |
| 30 | + file_type <- |
| 31 | + tolower( |
| 32 | + regmatches( |
| 33 | + file_path, |
| 34 | + regexpr("\\.[A-Za-z0-9]{1,3}$", file_path) |
| 35 | + ) |
| 36 | + ) |
| 37 | + |
| 38 | + if (!(file_type %in% c(".r", ".rmd"))) stop("detect_dependencies only supported for .R and .Rmd") |
| 39 | + |
| 40 | + deps <- switch(file_type, |
| 41 | + .r = parse_detect_deps(file_path, parse), |
| 42 | + .rmd = parse_detect_deps(file_path, parse_rmd), |
| 43 | + NULL |
| 44 | + ) |
| 45 | + |
| 46 | + deps[!duplicated(deps), ] |
| 47 | +} |
| 48 | + |
| 49 | +parse_detect_deps <- function(file_path, file_parser) { |
| 50 | + syntax_tree <- tryCatch(file_parser(file = file_path), |
| 51 | + error = function(e) stop("Could not detect usage of using::pkg in due to invalid R code. The parser returned: \n", e$message) |
| 52 | + ) |
| 53 | + |
| 54 | + get_using(syntax_tree) |
| 55 | +} |
| 56 | + |
| 57 | +parse_rmd <- function(file_path) { |
| 58 | + R_temp <- tempfile(fileext = ".R") |
| 59 | + on.exit(unlink(R_temp)) |
| 60 | + |
| 61 | + withr::with_options( |
| 62 | + list(knitr.purl.inline = TRUE), |
| 63 | + knitr::purl(file_path, |
| 64 | + output = R_temp, |
| 65 | + quiet = TRUE |
| 66 | + ) |
| 67 | + ) |
| 68 | + |
| 69 | + parse(file = R_temp) |
| 70 | +} |
| 71 | + |
| 72 | + |
| 73 | +is_using_node <- function(ast_node) { |
| 74 | + node_list <- as.list(ast_node) |
| 75 | + name_node <- as.character(node_list[[1]]) |
| 76 | + |
| 77 | + length(name_node) == 3 && |
| 78 | + name_node[[1]] == "::" && |
| 79 | + name_node[[2]] == "using" && |
| 80 | + name_node[[3]] == "pkg" |
| 81 | +} |
| 82 | + |
| 83 | +extract_using_data <- function(ast_node) { |
| 84 | + node_list <- as.list(ast_node) |
| 85 | + node_list <- node_list[-1] |
| 86 | + char_nodes <- stats::setNames( |
| 87 | + lapply(node_list, as.character), |
| 88 | + names(node_list) |
| 89 | + ) |
| 90 | + |
| 91 | + |
| 92 | + do.call( |
| 93 | + function(package = NA_character_, |
| 94 | + min_version = NA_character_, |
| 95 | + repo = NA_character_) { |
| 96 | + data.frame( |
| 97 | + package = package, |
| 98 | + min_version = min_version, |
| 99 | + repo = repo, |
| 100 | + stringsAsFactors = FALSE |
| 101 | + ) |
| 102 | + }, |
| 103 | + char_nodes |
| 104 | + ) |
| 105 | +} |
| 106 | + |
| 107 | +get_using <- function(syntax_tree) { |
| 108 | + get_using_recurse <- function(syntax_tree_expr) { |
| 109 | + if (is.call(syntax_tree_expr)) { |
| 110 | + if (is_using_node(syntax_tree_expr)) { |
| 111 | + return(extract_using_data(syntax_tree_expr)) |
| 112 | + } else { |
| 113 | + make_data_frame(lapply(syntax_tree_expr, get_using_recurse)) |
| 114 | + } |
| 115 | + } else { |
| 116 | + return(NULL) |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + make_data_frame(lapply(syntax_tree, get_using_recurse)) |
| 121 | +} |
| 122 | + |
| 123 | +make_data_frame <- function(x) { |
| 124 | + result_dfs <- x[!unlist(lapply(x, is.null))] |
| 125 | + |
| 126 | + if (length(result_dfs) > 0) { |
| 127 | + do.call(rbind, c(result_dfs, stringsAsFactors = FALSE)) |
| 128 | + } else { |
| 129 | + data.frame( |
| 130 | + package = character(0), |
| 131 | + min_version = character(0), |
| 132 | + repo = character(0), stringsAsFactors = FALSE |
| 133 | + ) |
| 134 | + } |
| 135 | +} |
0 commit comments