Skip to content

Instantly share code, notes, and snippets.

@alpoza
Forked from masanobuimai/jinspect.groovy
Created July 4, 2013 22:40
Show Gist options
  • Save alpoza/5930647 to your computer and use it in GitHub Desktop.
Save alpoza/5930647 to your computer and use it in GitHub Desktop.
listFilter = []
jarfile = ""
fileList = []
cli = new CliBuilder(usage:"jinspect [-hHvxXoawmISMWjJrgGcCpPlLZZZ] jarfile [filename ...]")
cli.with {
h(longOpt:'help', 'show help')
v(longOpt:'verbose', 'be more verbose')
X(longOpt:'save', 'save specified files (including path) to the current directory instead of printing them')
l(longOpt:'war', 'list war contents; specify again to include more information')
x(longOpt:'xml', 'list xml files')
o(longOpt:'properties', 'list properties files')
c(longOpt:'class', 'list class files')
j(longOpt:'java', 'list java files')
J(longOpt:'jsp', 'list jsp files')
S(longOpt:'js', 'list js files')
g(longOpt:'groovy', 'list groovy files')
G(longOpt:'gsp', 'list gsp files')
H(longOpt:'html', 'list html files')
C(longOpt:'css', 'list css files')
I(longOpt:'image', 'list image files')
L(longOpt:'lib', 'list files in lib directory')
M(longOpt:'meta-inf', 'list files in META-INF directory')
W(longOpt:'web-inf', 'list files in WEB-INF directory')
a(longOpt:'applicationcontext.xml', 'show WEB-INF/applicationContext.xml')
w(longOpt:'web.xml', 'show WEB-INF/web.xml')
r(longOpt:'grails.xml', 'show WEB-INF/grails.xml')
m(longOpt:'manifest.mf', 'show META-INF/MANIFEST.MF')
p(longOpt:'plugin.xml', 'show plugin.xml')
P(longOpt:'grailsplugin.groovy', 'show *GrailsPlugin.groovy')
}
opt = cli.parse(args)
if (!opt) System.exit 1
if (opt.h) { cli.usage(); System.exit 0 }
//if (opt.v)
//if (opt.X)
//if (opt.l)
if (opt.x) listFilter << '\\.xml'
if (opt.o) listFilter << '\\.properties'
if (opt.c) listFilter << '\\.class'
if (opt.j) listFilter << '\\.java'
if (opt.J) listFilter << '\\.jsp'
if (opt.S) listFilter << '\\.js'
if (opt.g) listFilter << '\\.groovy'
if (opt.G) listFilter << '\\.gsp'
if (opt.H) listFilter << '\\.html'
if (opt.C) listFilter << '\\.css'
if (opt.I) listFilter += ['\\.png', '\\.gif', '\\.jpg' ]
if (opt.M) listFilter << 'META-INF/'
if (opt.W) listFilter << 'WEB-INF/'
if (opt.L) listFilter << 'lib/'
if (opt.m) fileList << 'META-INF/MANIFEST.MF'
if (opt.a) fileList << 'WEB-INF/applicationContext.xml'
if (opt.w) fileList << 'WEB-INF/web.xml'
if (opt.r) fileList << 'WEB-INF/grails.xml'
if (opt.p) fileList << 'plugin.xml'
if (opt.P) fileList << '*GrailsPlugin.groovy'
params = opt.arguments()
if (params.size() < 1) { cli.usage(); System.exit 0 }
jarfile = params.remove(0)
fileList += params
ant = new AntBuilder()
// war/jar/zipから指定したファイルを表示する
if (fileList) {
if (opt.v) println "=== extracting $fileList from $jarfile"
fileList.each { file ->
if (opt.v) println "=== $jarfile:$file"
ant.unzip(src: jarfile, dest: '.') {
patternset { include(name: "**/$file")}
mapper(type: 'flatten') // <-- 注目!!
}
f = new File(file)
if (f.exists()) f.eachLine { println it }
if (!opt.x) f.delete() // <-- -xオプションが無い場合は,解凍したファイルを削除する
}
}
// -lオプションがあれば,war/jar/zipの内容一覧を表示する
if (opt.l) {
if (opt.v) println "=== listing contents of $jarfile: ${listFilter ? listFilter.collect{ it.replace("\\", "") } : ''}"
// jarコマンド使って,一覧取得を手抜き
"jar tf $jarfile".execute().in.eachLine { line ->
if (listFilter) {
if (listFilter.findAll { line =~ /${it}/ }) println line
} else {
println line
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment