From fc83dc913f3697ac0c1dd3552367d496fbbf8428 Mon Sep 17 00:00:00 2001 From: Nicolas Casajus Date: Fri, 27 Oct 2023 11:09:59 +0200 Subject: [PATCH] feat: add utilities functions to get deps in description --- R/utils-deps.R | 109 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 R/utils-deps.R diff --git a/R/utils-deps.R b/R/utils-deps.R new file mode 100644 index 0000000..ebae8d3 --- /dev/null +++ b/R/utils-deps.R @@ -0,0 +1,109 @@ +#' **Detect dependencies in Depends field of DESCRIPTION** +#' +#' @noRd + +get_deps_in_depends <- function() { + + descr <- read_descr_file() + + if (!is.null(descr$"Depends")) { + + deps <- get_deps_in_field(descr$"Depends") + + } else { + + deps <- NULL + } + + deps +} + + + +#' **Detect dependencies in Imports field of DESCRIPTION** +#' +#' @noRd + +get_deps_in_imports <- function() { + + descr <- read_descr_file() + + if (!is.null(descr$"Imports")) { + + deps <- get_deps_in_field(descr$"Imports") + + } else { + + deps <- NULL + } + + deps +} + + + +#' **Detect dependencies in Suggests field of DESCRIPTION** +#' +#' @noRd + +get_deps_in_suggests <- function() { + + descr <- read_descr_file() + + if (!is.null(descr$"Suggests")) { + + deps <- get_deps_in_field(descr$"Suggests") + + } else { + + deps <- NULL + } + + deps +} + + + +#' **Remove R minimum version from list of packages** +#' +#' @noRd + +remove_r_min_version <- function(dependencies) { + + r_version <- grep("^R\\s{0,}\\(", dependencies) + + if (length(r_version)) { + dependencies <- dependencies[-r_version] + } + + dependencies +} + + + +#' **Extract and clean list of packages in a particular DESCRIPTION field** +#' +#' @noRd + +get_deps_in_field <- function(field) { + + deps <- unlist(strsplit(field, "\n\\s{0,}|,|,\\s{0,}")) + + deps <- gsub("\\(\\s{0,}", " (", deps) + deps <- gsub("\\s{0,}\\)", ")", deps) + deps <- gsub("=", "= ", deps) + deps <- gsub(">", "> ", deps) + deps <- gsub("<", "< ", deps) + + deps <- gsub("\\s{0,}\\)", ")", deps) + deps <- gsub("\\s+", " ", deps) + deps <- trimws(deps) + + deps <- gsub("< =", "<=", deps) + deps <- gsub("> =", ">=", deps) + deps <- gsub("= =", "==", deps) + + deps <- deps[!(deps == "")] + + remove_r_min_version(deps) +}