1 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 142 143 public static void main( String [] args ) 144 { 145 boolean displayStackTraceOnError = false; 146 147 try 148 { 149 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 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 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 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 } 230 | Popular Tags |