001: /*
002: $Id: FileSystemCompiler.java 3514 2006-02-07 20:00:41Z dierk $
003:
004: Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
005:
006: Redistribution and use of this software and associated documentation
007: ("Software"), with or without modification, are permitted provided
008: that the following conditions are met:
009:
010: 1. Redistributions of source code must retain copyright
011: statements and notices. Redistributions must also contain a
012: copy of this document.
013:
014: 2. Redistributions in binary form must reproduce the
015: above copyright notice, this list of conditions and the
016: following disclaimer in the documentation and/or other
017: materials provided with the distribution.
018:
019: 3. The name "groovy" must not be used to endorse or promote
020: products derived from this Software without prior written
021: permission of The Codehaus. For written permission,
022: please contact info@codehaus.org.
023:
024: 4. Products derived from this Software may not be called "groovy"
025: nor may "groovy" appear in their names without prior written
026: permission of The Codehaus. "groovy" is a registered
027: trademark of The Codehaus.
028:
029: 5. Due credit should be given to The Codehaus -
030: http://groovy.codehaus.org/
031:
032: THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
033: ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
034: NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
035: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
036: THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
037: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
038: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
039: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
040: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
041: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
042: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
043: OF THE POSSIBILITY OF SUCH DAMAGE.
044:
045: */
046: package org.codehaus.groovy.tools;
047:
048: import java.io.File;
049:
050: import org.apache.commons.cli.CommandLine;
051: import org.apache.commons.cli.OptionBuilder;
052: import org.apache.commons.cli.Options;
053: import org.apache.commons.cli.PosixParser;
054: import org.codehaus.groovy.control.CompilationUnit;
055: import org.codehaus.groovy.control.CompilerConfiguration;
056: import org.codehaus.groovy.control.ConfigurationException;
057:
058: public class FileSystemCompiler {
059: private CompilationUnit unit;
060:
061: public FileSystemCompiler(CompilerConfiguration configuration)
062: throws ConfigurationException {
063: this .unit = new CompilationUnit(configuration);
064: }
065:
066: public void compile(String[] paths) throws Exception {
067: unit.addSources(paths);
068: unit.compile();
069: }
070:
071: public void compile(File[] files) throws Exception {
072: unit.addSources(files);
073: unit.compile();
074: }
075:
076: public static void displayHelp() // todo: use HelpFormatter to avoid duplication between help and OptionBuilder
077: {
078: System.err.println("Usage: groovyc <options> <source files>");
079: System.err.println("where possible options include: ");
080: System.err
081: .println(" --classpath <path> Specify where to find user class files");
082: System.err
083: .println(" -d <directory> Specify where to place generated class files");
084: System.err
085: .println(" --encoding <encoding> Specify the encoding of the user class files");
086: // System.err.println(" --strict Turn on strict type safety");
087: System.err
088: .println(" --version Print the verion");
089: System.err
090: .println(" --help Print a synopsis of standard options");
091: System.err
092: .println(" --exception Print stack trace on error");
093: System.err.println("");
094: }
095:
096: public static void displayVersion() {
097: System.err.println("groovy compiler version 1.0-rc1");
098: System.err
099: .println("Copyright 2003-2004 The Codehaus. http://groovy.codehaus.org/");
100: System.err.println("");
101: }
102:
103: public static int checkFiles(String[] filenames) {
104: int errors = 0;
105:
106: for (int i = 0; i < filenames.length; ++i) {
107: File file = new File(filenames[i]);
108:
109: if (!file.exists()) {
110: System.err.println("error: file not found: " + file);
111: ++errors;
112: } else if (!file.canRead()) {
113: System.err.println("error: file not readable: " + file);
114: ++errors;
115: } else {
116: String name = file.getName();
117: int p = name.lastIndexOf(".");
118: if (p++ >= 0) {
119: if (name.substring(p).equals("java")) {
120: System.err
121: .println("error: cannot compile file with .java extension: "
122: + file);
123: ++errors;
124: }
125: }
126: }
127: }
128:
129: return errors;
130: }
131:
132: /**
133: * Primary entry point for compiling from the command line
134: * (using the groovyc script).
135: */
136:
137: public static void main(String[] args) {
138: boolean displayStackTraceOnError = false;
139:
140: try {
141: //
142: // Parse the command line
143:
144: Options options = new Options();
145:
146: options.addOption(OptionBuilder.withLongOpt("classpath")
147: .hasArg().withArgName("classpath").create());
148: options.addOption(OptionBuilder.withLongOpt("sourcepath")
149: .hasArg().withArgName("sourcepath").create());
150: options.addOption(OptionBuilder.withLongOpt("encoding")
151: .hasArg().withArgName("encoding").create());
152: options.addOption(OptionBuilder.hasArg().create('d'));
153: // options.addOption(OptionBuilder.withLongOpt("strict").create('s'));
154: options.addOption(OptionBuilder.withLongOpt("help").create(
155: 'h'));
156: options.addOption(OptionBuilder.withLongOpt("version")
157: .create('v'));
158: options.addOption(OptionBuilder.withLongOpt("exception")
159: .create('e'));
160:
161: PosixParser cliParser = new PosixParser();
162:
163: CommandLine cli = cliParser.parse(options, args);
164:
165: if (cli.hasOption('h')) {
166: displayHelp();
167: return;
168: }
169:
170: if (cli.hasOption('v')) {
171: displayVersion();
172: }
173:
174: //
175: // Setup the configuration data
176:
177: CompilerConfiguration configuration = new CompilerConfiguration();
178:
179: if (cli.hasOption("classpath")) {
180: configuration.setClasspath(cli
181: .getOptionValue("classpath"));
182: }
183:
184: if (cli.hasOption('d')) {
185: configuration.setTargetDirectory(cli
186: .getOptionValue('d'));
187: }
188:
189: if (cli.hasOption("encoding")) {
190: configuration.setSourceEncoding(cli
191: .getOptionValue("encoding"));
192: }
193:
194: displayStackTraceOnError = cli.hasOption('e');
195:
196: //
197: // Load the file name list
198:
199: String[] filenames = cli.getArgs();
200: if (filenames.length == 0) {
201: displayHelp();
202: return;
203: }
204:
205: int errors = checkFiles(filenames);
206:
207: //
208: // Create and start the compiler
209:
210: if (errors == 0) {
211: FileSystemCompiler compiler = new FileSystemCompiler(
212: configuration);
213: compiler.compile(filenames);
214: }
215: } catch (Throwable e) {
216: new ErrorReporter(e, displayStackTraceOnError)
217: .write(System.err);
218: }
219: }
220:
221: }
|