Created
February 21, 2014 14:38
-
-
Save ytoshima/9135392 to your computer and use it in GitHub Desktop.
FindJar
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
import java.io._ | |
import collection.JavaConversions._ | |
case class MatchedEnt(path: String, names: List[String]) | |
class FindJar(path: String, patternStr: String) { | |
import scala.util.matching.Regex | |
val pattern = patternStr.r | |
if (!(new File(path)).exists()) { | |
throw new IllegalArgumentException( | |
"E: path %s does not exist".format(path)) | |
} | |
def getJarPaths: List[String] = { | |
def findJar(path: String): List[String] = { | |
new File(path) match { | |
case d if d.isDirectory => { | |
val jarFiles = d.listFiles(new FileFilter { | |
override def accept(f: File): Boolean = { | |
f.isFile && f.getPath.endsWith(".jar") | |
} | |
}).map(_.getPath) | |
val subDirs = d.listFiles(new FileFilter { | |
override def accept(f: File): Boolean = f.isDirectory | |
}).map(_.getPath) | |
Array.concat(jarFiles, subDirs.flatMap(findJar(_))).toList | |
} | |
} | |
} | |
findJar(path) | |
} | |
def jarEntNames(jarPath: String): List[String] = { | |
import java.util.jar.JarFile | |
(new JarFile(jarPath)).entries.toList.map(_.getName) | |
} | |
def candInJar(jarPath: String, re: Regex): List[String] = { | |
jarEntNames(jarPath).filter(re.findFirstIn(_)!=None) | |
} | |
def processFiles: List[MatchedEnt] = | |
getJarPaths.map(p => | |
MatchedEnt(p, candInJar(p, pattern))).filter(_.names.size > 0) | |
} | |
object FindJar extends App { | |
def usage() { | |
val m = "usage: scala FindJar <path> <pattern>" | |
println(m) | |
} | |
if (args.size < 2) { | |
usage(); | |
} else { | |
val fj = new FindJar(args(0), args(1)) | |
val ents = fj.processFiles | |
for (e <- ents) { | |
println(e.path) | |
for (n <- e.names) { | |
println(" %s".format(n)) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment