Created
January 1, 2019 03:27
-
-
Save hyginn/144dff923a2f362bebf24c2e26a20a4b to your computer and use it in GitHub Desktop.
`grepPackage.R` - when I develop packages, I often need to look for code in a variety of files, only to realize I missed one of them after I push to GitHub. This tool helps. It searches through the package directories and files and reports if a regex pattern was found in a file, and where.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# grepPackage.R | |
# Author: Boris Steipe (ORCID: 0000-0002-1134-6758) | |
# License: (c) Author (2018) + MIT | |
# Date: 2018-12-31 | |
# | |
# This R-script grep()s a pattern in all relevant files of an RStudio | |
# project/package. For expected directories and files, see: | |
# https://cran.r-project.org/doc/manuals/r-release/R-exts.html | |
# | |
# Issues: | |
# None at this time | |
grepPackage <- function(patt) { | |
# build list of files to include | |
MAXLEN <- 70 # max nchar() of output lines to print | |
fNames <- character() | |
fNames <- c(fNames, "./.Rprofile") | |
fNames <- c(fNames, "./DESCRIPTION") | |
fNames <- c(fNames, "./INDEX") | |
fNames <- c(fNames, "./NAMESPACE") | |
fNames <- c(fNames, "./LICENSE") | |
fNames <- c(fNames, "./LICENCE") | |
fNames <- c(fNames, "./NEWS") | |
fNames <- c(fNames, "./inst/NEWS") | |
fNames <- c(fNames, "./BinaryFiles") | |
fNames <- c(fNames, "./configure") | |
fNames <- c(fNames, "./configure.ac") | |
fNames <- c(fNames, "./cleanup") | |
fNames <- c(fNames, "./configure.win") | |
fNames <- c(fNames, "./cleanup.win") | |
fNames <- c(fNames, "./README.md") | |
fNames <- c(fNames, "./inst/CITATION") | |
fNames <- c(fNames, "./inst/AUTHORS") | |
fNames <- c(fNames, "./inst/COPYRIGHTS") | |
fNames <- c(fNames, "./src/Makevars") | |
fNames <- c(fNames, "./src/Makefile") | |
fNames <- c(fNames, "./src/Makefile.win") | |
dirNames <- c("./dev", | |
"./[Rr]", | |
"./data", | |
"./demo", | |
"./exec", | |
"./inst", | |
"./man", | |
"./po", | |
"./src", | |
"./tests", | |
"./tools", | |
"./vignettes") | |
fileExtensions <- paste0("(\\.txt$)", "|", | |
"(\\.po$)", "|", | |
"(\\.bib$)", "|", | |
"(\\.R$)", "|", | |
"(\\.cpp$)", "|", | |
"(\\.cc?$)", "|", | |
"(\\.h$)", "|", | |
"(\\.f(9[05])?$)", "|", | |
"(\\.[Mm]m?$)", "|", | |
"(\\.def$)", "|", | |
"(\\.R?md$)", "|", | |
"(\\.[RS]nw$)", "|", | |
"(\\.[RS]tex$)", | |
collapse = "") | |
for (dirName in dirNames) { | |
if (dir.exists(dirName)) { | |
fNames <- c(fNames, list.files(path = dirName, | |
recursive = TRUE, | |
pattern = fileExtensions, | |
no.. = TRUE, | |
full.names = TRUE, | |
all.files = TRUE)) | |
} | |
} | |
for (fName in fNames) { | |
if (file.exists(fName)) { | |
src <- readLines(fName) # sourcecode | |
indices <- grep(patt, src) # line numbers | |
if (length(indices) > 0) { | |
cat(sprintf("%s\n", fName)) | |
for (idx in indices) { | |
if (nchar(src[idx]) > MAXLEN) { # truncate | |
cat(sprintf("%d:\t%s ...\n", idx, substr(src[idx], 1, MAXLEN))) | |
} else { | |
cat(sprintf("%d:\t%s\n", idx, src[idx])) | |
} | |
} | |
cat("\n") | |
} | |
} | |
} | |
} | |
# try it | |
if (FALSE) { | |
# A string that appears in diverse file types: | |
grepPackage("author") | |
} | |
# [END] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment