From eb53dcb8df2084c984785acfd545e9b2249e7184 Mon Sep 17 00:00:00 2001 From: Nicolas Casajus Date: Fri, 27 Oct 2023 11:32:43 +0200 Subject: [PATCH] feat: detect dependencies in namespace --- NAMESPACE | 3 +++ R/rdeps-package.R | 1 + R/utils-deps.R | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+) diff --git a/NAMESPACE b/NAMESPACE index ba08b14..fffac44 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,3 +1,6 @@ # Generated by roxygen2: do not edit by hand export(print_msg) +importFrom(usethis,ui_done) +importFrom(usethis,ui_oops) +importFrom(usethis,ui_value) diff --git a/R/rdeps-package.R b/R/rdeps-package.R index 60684c1..220e842 100644 --- a/R/rdeps-package.R +++ b/R/rdeps-package.R @@ -2,6 +2,7 @@ "_PACKAGE" # Imports: start ---- +#' @importFrom usethis ui_done ui_value ui_oops # Imports: end ---- NULL diff --git a/R/utils-deps.R b/R/utils-deps.R index ebae8d3..f544a73 100644 --- a/R/utils-deps.R +++ b/R/utils-deps.R @@ -107,3 +107,49 @@ get_deps_in_field <- function(field) { remove_r_min_version(deps) } + + + +#' **Extract and clean list of packages in NAMESPACE** +#' +#' Detect dependencies as `import(pkg)` and `importFrom(pkg,fun)`. +#' +#' @noRd + +get_deps_in_namespace <- function() { + + check_for_descr_file() + + path <- path_proj() + + if (file.exists(file.path(path, "NAMESPACE"))) { + + ui_done("Screening {ui_value('NAMESPACE')} file") + + namespace <- readLines(con = file.path(path, "NAMESPACE"), warn = FALSE) + namespace <- namespace[grep("^\\s{0,}import", namespace)] + + deps <- gsub("importFrom\\s{0,}\\(|import\\s{0,}\\(|\\)", "", namespace) + deps <- gsub("\\s+", " ", deps) + deps <- trimws(deps) + + if (length(deps) == 0) { + + deps <- NULL + + } else { + + deps <- strsplit(deps, "\\s{0,},\\s{0,}") + deps <- lapply(deps, function(x) x[1]) + deps <- sort(unique(unlist(deps))) + } + + } else { + + ui_oops("No {ui_value('NAMESPACE')} file found") + + deps <- NULL + } + + deps +}