View Javadoc

1   /*
2    $Id: FileSystemCompiler.java,v 1.8 2004/07/10 03:31:45 bran Exp $
3   
4    Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
5   
6    Redistribution and use of this software and associated documentation
7    ("Software"), with or without modification, are permitted provided
8    that the following conditions are met:
9   
10   1. Redistributions of source code must retain copyright
11      statements and notices.  Redistributions must also contain a
12      copy of this document.
13  
14   2. Redistributions in binary form must reproduce the
15      above copyright notice, this list of conditions and the
16      following disclaimer in the documentation and/or other
17      materials provided with the distribution.
18  
19   3. The name "groovy" must not be used to endorse or promote
20      products derived from this Software without prior written
21      permission of The Codehaus.  For written permission,
22      please contact info@codehaus.org.
23  
24   4. Products derived from this Software may not be called "groovy"
25      nor may "groovy" appear in their names without prior written
26      permission of The Codehaus. "groovy" is a registered
27      trademark of The Codehaus.
28  
29   5. Due credit should be given to The Codehaus -
30      http://groovy.codehaus.org/
31  
32   THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
33   ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
34   NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
35   FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
36   THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
37   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
43   OF THE POSSIBILITY OF SUCH DAMAGE.
44  
45   */
46  package org.codehaus.groovy.tools;
47  
48  import java.io.File;
49  
50  import org.apache.commons.cli.CommandLine;
51  import org.apache.commons.cli.OptionBuilder;
52  import org.apache.commons.cli.Options;
53  import org.apache.commons.cli.PosixParser;
54  import org.codehaus.groovy.control.CompilationUnit;
55  import org.codehaus.groovy.control.CompilerConfiguration;
56  import org.codehaus.groovy.control.ConfigurationException;
57  
58  public class FileSystemCompiler  
59  {
60      private CompilationUnit unit;
61  
62      
63      public FileSystemCompiler( CompilerConfiguration configuration ) throws ConfigurationException
64      {
65          this.unit = new CompilationUnit( configuration );
66      }
67  
68      
69      public void compile( String[] paths ) throws Exception 
70      {
71          unit.addSources( paths );
72          unit.compile( );
73      }
74  
75      
76      public void compile( File[] files ) throws Exception 
77      {
78          unit.addSources( files );
79          unit.compile( );
80      }
81  
82  
83      public static void displayHelp() 
84      {
85          System.err.println("Usage: groovy <options> <source files>");
86          System.err.println("where possible options include: ");
87          System.err.println("  --classpath <path>        Specify where to find user class files");
88          System.err.println("  -d <directory>            Specify where to place generated class files");
89          System.err.println("  --encoding <encoding>     Specify the encoding of the user class files");
90          System.err.println("  --strict                  Turn on strict type safety");
91          System.err.println("  --version                 Print the verion");
92          System.err.println("  --help                    Print a synopsis of standard options");
93          System.err.println("  --exception               Print stack trace on error");
94          System.err.println("");
95      }
96  
97      public static void displayVersion() 
98      {
99          System.err.println("groovy compiler version 1.0-rc1");
100         System.err.println("Copyright 2003-2004 The Codehaus. http://groovy.codehaus.org/");
101         System.err.println("");
102     }
103 
104     public static int checkFiles( String[] filenames ) 
105     {
106         int errors = 0;
107 
108         for(int i = 0; i < filenames.length; ++i ) 
109         {
110             File file = new File( filenames[i] );
111 
112             if( !file.exists() ) 
113             {
114                 System.err.println( "error: file not found: " + file );
115                 ++errors;
116             }
117             else if( !file.canRead() ) 
118             {
119                 System.err.println( "error: file not readable: " + file );
120                 ++errors;
121             } else {
122                 String name = file.getName();
123                 int p = name.lastIndexOf(".");
124                 if ( p++ >= 0) {
125                     if (name.substring(p).equals("java")) {
126                         System.err.println( "error: cannot compile file with .java extension: " + file );
127                         ++errors;
128                     }
129                 }
130             }
131         }
132 
133         return errors;
134     }
135 
136     
137     
138    /***
139     *  Primary entry point for compiling from the command line
140     *  (using the groovyc script).
141     */
142     
143     public static void main( String[] args )
144     {
145         boolean displayStackTraceOnError = false;
146         
147         try
148         {
149             //
150             // Parse the command line
151             
152             Options options = new Options();
153     
154             options.addOption(OptionBuilder.withLongOpt("classpath").hasArg().withArgName("classpath").create());
155             options.addOption(OptionBuilder.withLongOpt("sourcepath").hasArg().withArgName("sourcepath").create());
156             options.addOption(OptionBuilder.withLongOpt("encoding").hasArg().withArgName("encoding").create());
157             options.addOption(OptionBuilder.hasArg().create('d'));
158             options.addOption(OptionBuilder.withLongOpt("strict").create('s'));
159             options.addOption(OptionBuilder.withLongOpt("help").create('h'));
160             options.addOption(OptionBuilder.withLongOpt("version").create('v'));
161             options.addOption(OptionBuilder.withLongOpt("exception").create('e'));
162     
163             PosixParser cliParser = new PosixParser();
164     
165             CommandLine cli = cliParser.parse(options, args);
166     
167             if( cli.hasOption('h') ) 
168             {
169                 displayHelp();
170                 return;
171             }
172     
173             if( cli.hasOption('v') ) 
174             {
175                 displayVersion();
176             }
177     
178             
179             //
180             // Setup the configuration data
181             
182             CompilerConfiguration configuration = new CompilerConfiguration();
183     
184             if( cli.hasOption("classpath") ) 
185             {
186                 configuration.setClasspath( cli.getOptionValue("classpath") );
187             }
188     
189             if( cli.hasOption('d') ) 
190             {
191                 configuration.setTargetDirectory( cli.getOptionValue('d') );
192             }
193 
194             if (cli.hasOption("encoding")) {
195                 configuration.setSourceEncoding(cli.getOptionValue("encoding"));
196             }
197 
198             displayStackTraceOnError = cli.hasOption('e');
199             
200             
201             //
202             // Load the file name list
203             
204             String[] filenames = cli.getArgs();
205             if( filenames.length == 0 ) 
206             {
207                 displayHelp();
208                 return;
209             }
210     
211             int errors = checkFiles( filenames );
212     
213             
214             //
215             // Create and start the compiler
216             
217             if( errors == 0 ) 
218             {
219                 FileSystemCompiler compiler = new FileSystemCompiler( configuration );
220                 compiler.compile( filenames );
221             }
222         }
223         catch( Throwable e ) 
224         {
225             new ErrorReporter( e, displayStackTraceOnError ).write( System.err );
226         }
227     }
228     
229 }