Skip to content

Commit

Permalink
feat: detect dependencies in namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
ahasverus committed Oct 27, 2023
1 parent fc83dc9 commit eb53dcb
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions R/rdeps-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"_PACKAGE"

# Imports: start ----
#' @importFrom usethis ui_done ui_value ui_oops
# Imports: end ----

NULL
46 changes: 46 additions & 0 deletions R/utils-deps.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit eb53dcb

Please sign in to comment.