From 3eb7906ed21e3f0a53c2923ce8dc850d8dc880fd Mon Sep 17 00:00:00 2001 From: Clemens Schmid Date: Sat, 31 Aug 2019 18:50:57 +0200 Subject: [PATCH] first draft of a update_description_file() function #12 --- DESCRIPTION | 1 + NAMESPACE | 1 + R/update_description_file.R | 30 ++++++++++++++++++++++++++++++ man/update_description_file.Rd | 19 +++++++++++++++++++ 4 files changed, 51 insertions(+) create mode 100644 R/update_description_file.R create mode 100644 man/update_description_file.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 4c47c64..1cd0da9 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -14,6 +14,7 @@ RoxygenNote: 6.1.1 Depends: R (>= 3.1.0) Imports: + desc, dplyr, formatR, knitr, diff --git a/NAMESPACE b/NAMESPACE index 001a9a4..5d1f42e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -8,5 +8,6 @@ export(install_deps_file) export(install_package_guess) export(make_deps_file) export(parse_packages) +export(update_description_file) importFrom(magrittr,"%>%") importFrom(utils,packageDescription) diff --git a/R/update_description_file.R b/R/update_description_file.R new file mode 100644 index 0000000..6553082 --- /dev/null +++ b/R/update_description_file.R @@ -0,0 +1,30 @@ +#' add missing packages to package DESCRIPTION file +#' +#' @details uses \code{get_dependent_packages()} to find dependencies and +#' adds them to the description file with the desc package +#' +#' @param directory folder to search for R and Rmd files. +#' The DESCRIPTION file is expected to be in this directory. +#' +#' @export +update_description_file <- function(directory = getwd()) { + description_file_path <- file.path(directory, "DESCRIPTION") + # check if a descripton file is available + if (!file.exists(description_file_path)) { + stop("There is no DESCRIPTION file in this directory.") + } + # get dependent packages + dependent_packages <- get_dependent_packages(directory) + # get description object + desc <- desc::description$new(description_file_path) + # add packages to description object + purrr::walk( + .x = dependent_packages, + .f = function(x, y) { + y$set_dep(x) + }, + desc + ) + # write description object to file system + desc$write(description_file_path) +} diff --git a/man/update_description_file.Rd b/man/update_description_file.Rd new file mode 100644 index 0000000..a21fa3d --- /dev/null +++ b/man/update_description_file.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/update_description_file.R +\name{update_description_file} +\alias{update_description_file} +\title{add missing packages to package DESCRIPTION file} +\usage{ +update_description_file(directory = getwd()) +} +\arguments{ +\item{directory}{folder to search for R and Rmd files. +The DESCRIPTION file is expected to be in this directory.} +} +\description{ +add missing packages to package DESCRIPTION file +} +\details{ +uses \code{get_dependent_packages()} to find dependencies and +adds them to the description file with the desc package +}