Merge pull request #194 from dubek/literal-empty-list
[jackhill/mal.git] / groovy / GroovyWrapper.groovy
CommitLineData
8e2d4a4c
JM
1/* From:
2 * http://groovy.jmiguel.eu/groovy.codehaus.org/WrappingGroovyScript.html
3 */
4/*
5 * Copyright 2002-2007 the original author or authors.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20/**
21 * Wrap a script and groovy jars to an executable jar
22 */
23def cli = new CliBuilder()
24cli.h( longOpt: 'help', required: false, 'show usage information' )
25cli.d( longOpt: 'destfile', argName: 'destfile', required: false, args: 1, 'jar destintation filename, defaults to {mainclass}.jar' )
26cli.m( longOpt: 'mainclass', argName: 'mainclass', required: true, args: 1, 'fully qualified main class, eg. HelloWorld' )
27cli.c( longOpt: 'groovyc', required: false, 'Run groovyc' )
28
29//--------------------------------------------------------------------------
30def opt = cli.parse(args)
31if (!opt) { return }
32if (opt.h) {
33 cli.usage();
34 return
35}
36
37def mainClass = opt.m
38def scriptBase = mainClass.replace( '.', '/' )
39def scriptFile = new File( scriptBase + '.groovy' )
40if (!scriptFile.canRead()) {
41 println "Cannot read script file: '${scriptFile}'"
42 return
43}
44def destFile = scriptBase + '.jar'
45if (opt.d) {
46 destFile = opt.d
47}
48
49//--------------------------------------------------------------------------
50def ant = new AntBuilder()
51
52if (opt.c) {
53 ant.echo( "Compiling ${scriptFile}" )
54 org.codehaus.groovy.tools.FileSystemCompiler.main( [ scriptFile ] as String[] )
55}
56
57def GROOVY_HOME = new File( System.getenv('GROOVY_HOME') )
58if (!GROOVY_HOME.canRead()) {
59 ant.echo( "Missing environment variable GROOVY_HOME: '${GROOVY_HOME}'" )
60 return
61}
62
63ant.jar( destfile: destFile, compress: true, index: true ) {
64 //fileset( dir: '.', includes: scriptBase + '*.class' )
65 fileset( dir: '.', includes: '*.class' )
66
67 zipgroupfileset( dir: GROOVY_HOME, includes: 'embeddable/groovy-all-*.jar' )
68 zipgroupfileset( dir: GROOVY_HOME, includes: 'lib/commons*.jar' )
69 // add more jars here
70
71 manifest {
72 attribute( name: 'Main-Class', value: mainClass )
73 }
74}
75
76ant.echo( "Run script using: \'java -jar ${destFile} ...\'" )