Last active
October 18, 2016 23:25
-
-
Save frgomes/fed53e30564962f8734c to your computer and use it in GitHub Desktop.
Scala - Regex on contents of several files
This file contains hidden or 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
/** my projects are named GuinePigModels, GuineaPigController, etc */ | |
private val myProjectsPrefix = "GuineaPig" | |
/** jar names appear on lines which may end by a comma followed by a line continuation mark */ | |
private val jarRef = """^([A-Za-z0-9_.\-]+)(\.jar)(,\\)*""".r | |
/** list files recursively */ | |
private def listOfFiles(dir: File): Seq[File] = { | |
if (dir.exists && dir.isDirectory) { | |
dir.listFiles.filter(f => f.isFile).toList | |
} else { | |
Seq.empty[File] | |
} | |
} | |
/** filter jar names related to internal dependencies, i.e.: matching my project names */ | |
private def onlyInternalJars(name: String): Boolean = | |
name.toLowerCase.startsWith(myProjectsPrefix) | |
/** filter jar names related to external dependencies, i.e.: not matching my project names */ | |
private def onlyExternalJars(name: String): Boolean = | |
!name.toLowerCase.startsWith(myProjectsPrefix) | |
/** parse a single line of text and optionally returns one jar name from it */ | |
private def parse(line: String): Option[String] = | |
line match { | |
case jarRef(name, _*) => Some(name) | |
case _ => None:Option[String] | |
} | |
/** parse a file and returns a Set of jar names from it */ | |
private def parse(f: File): Set[String] = { | |
(for( | |
line <- scala.io.Source.fromFile(f).getLines(); | |
result <- parse(line) if(result != null) | |
) yield { | |
result | |
}).toSet | |
} | |
/** parses several files from a directory and returns a Set of jar names from them */ | |
private def referencedJars(dir: File, jarFilter: (String) => Boolean): Set[String] = | |
(Set.empty[String] /: listOfFiles(dir).map(f => parse(f))) { | |
case (acc, item) => | |
acc ++ item.filter(jarFilter) | |
} | |
/** parses several files from a directory and returns a Set of internal jar names from them */ | |
def referencedInternalJars(dir: File): Set[String] = | |
referencedJars(dir, onlyInternalJars) | |
/** parses several files from a directory and returns a Set of external jar names from them */ | |
def referencedExternalJars(dir: File): Set[String] = | |
referencedJars(dir, onlyExternalJars) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment