dev/checkEnds.R

# checkEnds.R

#' checkEnds
#'
#' \code{checkEnds} Utility to check source files for the presence of
#' an \code{# [END]} comment.
#'
#' @section Details: whenever \code{# [END]} comments are used in a project, ALL
#' source files MUST include this tag. The function checks all files with an
#' \code{.R} extension recursively from \code{path} and reports any files that
#' do not have an \code{# [END]} tag as the last line of the script. If no such
#'files are present, a message is printed.
#'
#' @param path (char) path to directory that is to be checked recursively.
#' Defaults to \code{getwd()}.
#' @param excl (char) Vector of regular expressions for files and directories
#'                    that are excluded from checking. Defaults to exclude
#'                    \code{./doc} and \code{./R/RcppExports.R} since these
#'                    are/contain autogenerated scripts.
#' @return NULL (invisible) Invoked for the side-effect of printing a
#'                          report to console.
#'
#' @author \href{https://orcid.org/0000-0002-1134-6758}{Boris Steipe} (aut)
#'
#' @examples
#' # Check all files in the project
#' checkEnds()
#'
#' # Check file in and below the ./dev directory only
#' checkEnds(path = "./dev")
#'

checkEnds <- function(path = getwd(), excl = c("^doc/",
                                               "^R/RcppExports\\.R$")) {

  fileNames <-  list.files(path = path,
                           pattern = ".*\\.R$",
                           recursive = TRUE,
                           all.files = TRUE,
                           include.dirs = TRUE)

  # remove files and directories listed in excl
  sel <- grepl(paste("(", excl, ")", sep = "", collapse = "|"), fileNames)
  fileNames <- fileNames[! sel]

  allIsGood <- TRUE
  for (fN in fileNames) {
    x <- readLines(paste0(path, "/", fN))
    if (! grepl("^# \\[[Ee][Nn][Dd]\\]$", x[length(x)])) {
      message(sprintf("Malformed or missing [END] tag in \"./%s\"\n", fN))
      allIsGood <- FALSE
    }
  }
  if (allIsGood) {
    message(sprintf("%d files checked, no [END] tags missing.\n",
                length(fileNames)))
  }

  return(invisible(NULL))

}

# [END]
hyginn/rptPlus documentation built on May 30, 2019, 2:11 p.m.