Skip to content

Commit 80bdf5f

Browse files
committed
added support for lib directory, fixed bugs, and support for grapes
1 parent 1276fbe commit 80bdf5f

File tree

1 file changed

+97
-52
lines changed

1 file changed

+97
-52
lines changed

GroovyWrapper.groovy

Lines changed: 97 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,145 @@
1-
/*
2-
* Copyright 2002-2007 the original author or authors.
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
1+
/**
2+
* from https://github.com/sdanzan/groovy-wrapper/blob/master/GroovyWrapper.groovy
3+
*
4+
* Ryan: missing features: if i missing, blows up in args
5+
* also, doesn't appear to add main class
76
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
97
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
8+
* Copyright 2002-2007 the original author or authors.
9+
*
10+
* Licensed under the Apache License, Version 2.0 (the "License");
11+
* you may not use this file except in compliance with the License.
12+
* You may obtain a copy of the License at
13+
*
14+
* http://www.apache.org/licenses/LICENSE-2.0
15+
*
16+
* Unless required by applicable law or agreed to in writing, software
17+
* distributed under the License is distributed on an "AS IS" BASIS,
18+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19+
* See the License for the specific language governing permissions and
20+
* limitations under the License.
21+
*/
1622

1723
/*
18-
* Original script at http://groovy.codehaus.org/WrappingGroovyScript
19-
*/
24+
* Original script at http://groovy.codehaus.org/WrappingGroovyScript
25+
*/
2026

2127
/**
22-
* Wrap a script and groovy jars to an executable jar
23-
*/
28+
* Wrap a script and groovy jars to an executable jar
29+
*/
2430
def cli = new CliBuilder()
2531
cli.h( longOpt: 'help', required: false, 'show usage information' )
2632
cli.d( longOpt: 'destfile', argName: 'destfile', required: false, args: 1, 'jar destination filename, defaults to {mainclass}.jar' )
2733
cli.m( longOpt: 'mainclass', argName: 'mainclass', required: true, args: 1, 'fully qualified main class, eg. HelloWorld' )
2834
cli.c( longOpt: 'groovyc', required: false, 'Run groovyc' )
2935
cli.i( longOpt: 'include', required: false, args: 1764, valueSeparator: ' ' as char, 'a list of jars to include into the destination jar' )
3036
cli.x( longOpt: 'exclude', required: false, args: 1764, valueSeparator: ' ' as char, 'a list of file patterns to exclude from included jars' )
31-
37+
cli.g( longOpt: 'grab', required: false, 'Include ~/.groovy/grapes from @Grab statements' )
38+
cli.l( longOpt: 'lib', required: false, 'Include all from ~/.groovy/lib' )
3239
//--------------------------------------------------------------------------
3340
def opt = cli.parse(args)
3441
if (!opt) { return }
3542
if (opt.h) {
36-
cli.usage();
37-
return
43+
cli.usage();
44+
return
3845
}
3946

4047
def mainClass = opt.m
4148
def scriptBase = mainClass.replace( '.', '/' )
4249
def scriptFile = new File( scriptBase + '.groovy' )
4350
if (!scriptFile.canRead()) {
44-
println "Cannot read script file: '${scriptFile}'"
45-
return
51+
println "Cannot read script file: '${scriptFile}'"
52+
return
4653
}
4754
def destFile = scriptBase + '.jar'
4855
if (opt.d) {
49-
destFile = opt.d
56+
destFile = opt.d
5057
}
5158

5259
//--------------------------------------------------------------------------
5360
def ant = new AntBuilder()
5461

5562
if (opt.c) {
56-
ant.echo( "Compiling ${scriptFile}" )
57-
org.codehaus.groovy.tools.FileSystemCompiler.main( [ scriptFile ] as String[] )
63+
ant.echo( "Compiling ${scriptFile}" )
64+
org.codehaus.groovy.tools.FileSystemCompiler.main( [ scriptFile ] as String[] )
5865
}
5966

6067
def GROOVY_HOME = new File( System.getenv('GROOVY_HOME') )
61-
if (!GROOVY_HOME.canRead()) {
62-
ant.echo( "Missing environment variable GROOVY_HOME: '${GROOVY_HOME}'" )
63-
return
68+
def HOME = new File(System.getenv('HOME'))
69+
if (!GROOVY_HOME.canRead() || GROOVY_HOME == "") {
70+
ant.echo( "Missing environment variable GROOVY_HOME: '${GROOVY_HOME}'" )
71+
return
6472
}
6573

6674
def supJars = []
6775

76+
def grapes = []
77+
def libs = []
78+
6879
ant.jar( destfile: destFile, compress: true, index: true ) {
69-
classes = new File( scriptBase ).getParent() + '/*.class'
70-
if ( classes.startsWith( '/' ) ) classes = '*.class'
71-
fileset( dir: '.', includes: classes )
72-
73-
// Embedded Groovy jars
74-
zipgroupfileset( dir: GROOVY_HOME, includes: 'embeddable/groovy-all-*.jar' )
75-
zipgroupfileset( dir: GROOVY_HOME, includes: 'lib/commons*.jar' )
76-
77-
// Other jars to include from -i option
78-
opt.is.each {
79-
if (it.endsWith( '.jar' )) {
80-
jarFile = new File( it )
81-
zipfileset( src: it ) {
82-
// exclude patterns from -x option
83-
opt.xs.each {
84-
exclude( name: it )
80+
81+
classes = ((scriptFile.getParent()) ? scriptFile.getParent() : "") + '/*.class'
82+
if ( classes.startsWith( '/' ) ) classes = '*.class'
83+
fileset( dir: '.', includes: classes )
84+
85+
// Embedded Groovy jars
86+
zipgroupfileset( dir: GROOVY_HOME, includes: 'embeddable/groovy-all-*.jar' )
87+
zipgroupfileset( dir: GROOVY_HOME, includes: 'lib/commons*.jar' )
88+
if (opt.g) {
89+
// Selective Grap (doesn't do dependencies, but good enough). Generally the @Grab will be commented out when compiling to jar
90+
sfLines = scriptFile.readLines()
91+
for (a in sfLines) {
92+
if (a.contains("@Grab")) {
93+
s = a.indexOf("(")
94+
e = a.indexOf(")")
95+
c = a.substring(s, e + 1)
96+
c = c.replace("(", "").replace(")", "").replace('"', "").replace("'", "") // stripping
97+
g = c.split(":") // this is what I want to get from the grapes dir
98+
j = "${g[0]}/${g[1]}/jars/${g[1]}-${g[2]}.jar"
99+
grapes.add(j)
100+
zipgroupfileset(dir: new File(HOME, ".groovy/grapes"), includes: "${g[0]}/${g[1]}/jars/${g[1]}-${g[2]}.jar")
101+
102+
}
103+
}
104+
}
105+
if (opt.l) {
106+
// All in lib
107+
zipgroupfileset(dir: HOME, includes: '.groovy/lib/*.jar')
108+
libs = new File(HOME, '.groovy/lib').list()
109+
}
110+
111+
112+
// Other jars to include from -i option
113+
if (opt.is) {
114+
opt.is.each {
115+
if (it.endsWith('.jar')) {
116+
jarFile = new File(it)
117+
zipfileset(src: it) {
118+
// exclude patterns from -x option
119+
if (opt.xs) {
120+
opt.xs.each {
121+
exclude(name: it)
122+
}
123+
}
124+
}
125+
supJars << new File(it).getName()
126+
}
85127
}
86-
}
87-
supJars << new File( it ).getName()
88128
}
89-
}
90129

91-
manifest {
92-
attribute( name: 'Main-Class', value: mainClass )
93-
}
130+
manifest {
131+
attribute( name: 'Main-Class', value: mainClass )
132+
}
94133
}
95134

96135
supJars.each {
97-
ant.echo( "Added supplemental jar: " + it )
136+
ant.echo( "Added supplemental jar: " + it )
137+
}
138+
grapes.each{
139+
ant.echo("Added jar from grapes: $it")
98140
}
99-
ant.echo( "Run script using: \'java -jar ${destFile} ...\'" )
100141

142+
libs.each{
143+
ant.echo("Added jar from lib: $it")
144+
}
145+
ant.echo( "Run script using: \'java -jar ${destFile} ...\'" )

0 commit comments

Comments
 (0)