001: /**
002: * Main entry point for the script compiler
003: *
004: * @author steele@osteele.com
005: * @author ptw@openlaszlo.org
006: */package org.openlaszlo.compiler;
007:
008: import org.openlaszlo.server.LPS;
009: import org.openlaszlo.utils.FileUtils;
010: import org.openlaszlo.sc.ScriptCompiler;
011: import java.io.*;
012: import java.util.*;
013: import org.apache.log4j.*;
014: import org.apache.log4j.spi.*;
015: import org.apache.log4j.varia.NullAppender;
016:
017: public class Main {
018: private static String[] USAGE = {
019: "Usage: lzc [OPTION]... FILE...",
020: "",
021: "Options:",
022: "-D<name>=<value>",
023: " Set the name/var property to value (See Compiler.getProperties).",
024: "-D<name>",
025: " Short for -Dname=true.",
026: "-v",
027: " Write progress information to standard output.",
028: "--mcache on|off",
029: " Turns on/off media cache. Default is off.",
030: "--onerror [throw|warn]",
031: " Action to take on compilation errors. Defaults to warn.",
032: "--help",
033: " Prints this message.",
034: "--flush-script-cache",
035: " Doesn't flush script cache before compiling.",
036: "",
037: "Output options:",
038: "--runtime=[swf7|swf8|swf9|dhtml|j2me|svg|null]",
039: " Compile to swf7, swf8, swf9, dhtml, j2me, svg, null",
040: "--dir outputdir",
041: " Output directory.",
042: "-c | --compile",
043: " Compile and assemble, but do not link",
044: "-g1 | --debug",
045: " Add debugging support into the output object.",
046: "-g | -g2 | --backtrace",
047: " Add debugging and backtrace support into the output object.",
048: "-p | --profile",
049: " Add profiling information into the output object.", "",
050: "Logging options:", "-l<loglevel>",
051: " Logging level (See org.apache.log4j.Level)",
052: "-l<loggerName>=<loglevel>",
053: " Logging level (See org.apache.log4j.Level)", "-lp file",
054: " Log4j properties files", "--log logfile",
055: " Specify logfile (output still goes to console as well)",
056: "--schema", " Writes the schema to standard output.",
057: "--script", " Writes JavaScript to standard output." };
058:
059: /** Compiles each file base.ext to the output file base.swf (or .js),
060: * writing progress information to standard output. This method
061: * is intended for testing the compiler.
062: *
063: * <p>See the usage string or execute <code>lzc --help</code>
064: * to see a list of options.
065: *
066: * @param args the command line arguments
067: */
068: public static void main(String args[]) throws IOException {
069: System.exit(lzc(args, null, null, null));
070: }
071:
072: /** This method implements the behavior described in main
073: * but also returns an integer error code.
074: */
075: public static int lzc(String args[], String logFile,
076: String outFileName, String outDir) throws IOException {
077: Logger logger = Logger.getRootLogger();
078: List files = new Vector();
079: // Configure logging
080: logger.setLevel(Level.ERROR);
081: PatternLayout layout = new PatternLayout("%m%n");
082: logger.removeAllAppenders();
083: if (logFile == null) {
084: logger.addAppender(new ConsoleAppender(layout));
085: } else {
086: logger
087: .addAppender(new FileAppender(layout, logFile,
088: false));
089: }
090:
091: Compiler compiler = new Compiler();
092: CompilerMediaCache cache;
093: String cacheDir = LPS.getWorkDirectory() + File.separator
094: + "cache" + File.separator + "cmcache";
095: cache = new CompilerMediaCache(new File(cacheDir),
096: new Properties());
097:
098: // Make sure we always retranscode media
099: //cache.setProperty("forcetranscode", "true");
100: compiler.setMediaCache(cache);
101:
102: String scacheDir = LPS.getWorkDirectory() + File.separator
103: + "scache";
104: ScriptCompiler.initScriptCompilerCache(new File(scacheDir),
105: new Properties());
106: // Set default runtime to compiler.runtime.default
107: compiler.setProperty(CompilationEnvironment.RUNTIME_PROPERTY,
108: LPS.getProperty("compiler.runtime.default", LPS
109: .getRuntimeDefault()));
110: boolean flushScriptCache = false;
111:
112: for (int i = 0; i < args.length; i++) {
113: String arg = args[i].intern();
114: if (arg.startsWith("-")) {
115: if (arg == "-v") {
116: logger.setLevel(Level.ALL);
117: } else if (arg == "-lp") {
118: PropertyConfigurator.configure(args[++i]);
119: String lhome = System.getProperty("LPS_HOME");
120: if (lhome == null || lhome.equals("")) {
121: lhome = System.getenv("LPS_HOME");
122: }
123: LPS.setHome(lhome);
124: } else if (arg == "--schema") {
125: compiler.SchemaLogger.setLevel(Level.ALL);
126: } else if (arg == "--keepscriptcache") {
127: flushScriptCache = false;
128: System.err
129: .println("--keepscriptcache is deprecated. This is now the default behavior.");
130: } else if (arg == "--flush-script-cache") {
131: flushScriptCache = true;
132: } else if (arg == "--onerror") {
133: String value = args[++i];
134: CompilationError.ThrowCompilationErrors = "throw"
135: .equals(value);
136: } else if (arg.startsWith("--runtime=")) {
137: String value = arg.substring("--runtime=".length());
138: if ((new HashSet(Compiler.KNOWN_RUNTIMES))
139: .contains(value)) {
140: compiler
141: .setProperty(
142: CompilationEnvironment.RUNTIME_PROPERTY,
143: value);
144: } else {
145: System.err
146: .println("Invalid value for --runtime");
147: return 1;
148: }
149: } else if (arg == "-S" || arg == "--script") {
150: // TODO: [2007-05-25 ptw] This is bogus -- should write out a .lzs file
151: Logger.getLogger(
152: org.openlaszlo.sc.ScriptCompiler.class)
153: .setLevel(Level.ALL);
154: } else if (arg == "-mcache" || arg == "--mcache") {
155: String value = args[++i];
156: if (value.equals("on")) {
157: cache.getProperties().setProperty(
158: "forcetranscode", "false");
159: } else if (value.equals("off")) {
160: cache.getProperties().setProperty(
161: "forcetranscode", "true");
162: }
163: } else if (arg == "-log" || arg == "--log") {
164: String log = args[++i];
165: logger.removeAllAppenders();
166: logger.addAppender(new FileAppender(layout, log,
167: false));
168: } else if (arg == "-dir" || arg == "--dir") {
169: outDir = args[++i];
170: } else if (arg.startsWith("-D")) {
171: String key = arg.substring(2);
172: String value = "true";
173: int offset = key.indexOf('=');
174: if (offset >= 0) {
175: value = key.substring(offset + 1).intern();
176: key = key.substring(0, offset);
177: }
178: compiler.setProperty(key, value);
179: LPS.setProperty(key, value);
180: } else if (arg.startsWith("-l")) {
181: Logger this Logger = logger;
182: String level = arg.substring(2);
183: if (level.indexOf('=') > -1) {
184: String key = level.substring(0, level
185: .indexOf('='));
186: level = level.substring(level.indexOf('=') + 1);
187: this Logger = Logger.getLogger(key);
188: }
189: if (level != "" && level != null) {
190: this Logger.setLevel(Level.toLevel(level));
191: }
192: } else if (arg == "-g1" || arg == "--debug") {
193: compiler.setProperty(
194: CompilationEnvironment.DEBUG_PROPERTY,
195: "true");
196: } else if (arg == "-g" || arg == "-g2"
197: || arg == "--backtrace") {
198: compiler.setProperty(
199: CompilationEnvironment.DEBUG_PROPERTY,
200: "true");
201: compiler.setProperty(
202: CompilationEnvironment.BACKTRACE_PROPERTY,
203: "true");
204: } else if (arg == "-p" || arg == "--profile") {
205: compiler.setProperty(
206: CompilationEnvironment.PROFILE_PROPERTY,
207: "true");
208: } else if (arg == "-c" || arg == "--compile") {
209: compiler.setProperty(
210: CompilationEnvironment.LINK_PROPERTY,
211: "false");
212: } else if (arg == "--help") {
213: for (int j = 0; j < USAGE.length; j++) {
214: System.err.println(USAGE[j]);
215: }
216: return 0;
217: } else {
218: System.err
219: .println("Usage: lzc [OPTION]... file...");
220: System.err
221: .println("Try `lzc --help' for more information.");
222: return 1;
223: }
224: continue;
225: }
226: String sourceName = args[i];
227: files.add(sourceName);
228: }
229:
230: if (flushScriptCache) {
231: ScriptCompiler.clearCacheStatic();
232: }
233:
234: LPS.initialize();
235:
236: int status = 0;
237: for (Iterator iter = files.iterator(); iter.hasNext();) {
238: String sourceName = (String) iter.next();
239: if (files.size() > 1)
240: System.err.println("Compiling " + sourceName);
241: status += compile(compiler, logger, sourceName,
242: outFileName, outDir);
243: }
244: return status;
245: }
246:
247: static private int compile(Compiler compiler, Logger logger,
248: String sourcePath, String outName, String outDir) {
249: File sourceFile = new File(sourcePath);
250: String objExtension = null;
251: String finalExtension = null;
252: String finalName = null;
253: if ("false".equals(compiler
254: .getProperty(CompilationEnvironment.LINK_PROPERTY))) {
255: objExtension = ".gz";
256: finalExtension = ".lzo";
257: } else {
258: String runtime = compiler
259: .getProperty(CompilationEnvironment.RUNTIME_PROPERTY);
260: objExtension = compiler
261: .getObjectFileExtensionForRuntime(runtime);
262: }
263: if (outName == null) {
264: String baseName = FileUtils.getBase(sourceFile.getName());
265: outName = baseName + objExtension;
266: if (finalExtension != null) {
267: finalName = baseName + finalExtension;
268: }
269: }
270: if (outDir == null) {
271: outDir = sourceFile.getParent();
272: }
273: File objectFile = new File(outDir, outName);
274: try {
275: System.err.println("Compiling: " + sourceFile + " to "
276: + objectFile);
277: compiler.compile(sourceFile, objectFile, new Properties());
278: if (finalName != null) {
279: File finalFile = new File(outDir, finalName);
280: if (finalFile.exists()) {
281: finalFile.delete();
282: }
283: if (!objectFile.renameTo(finalFile)) {
284: throw new CompilationError("Could not rename "
285: + objectFile + " to " + finalFile);
286: }
287: }
288: } catch (CompilationError e) {
289: logger.error(
290: /* (non-Javadoc)
291: * @i18n.test
292: * @org-mes="Compilation errors occurred:"
293: */
294: org.openlaszlo.i18n.LaszloMessages.getMessage(Main.class
295: .getName(), "051018-249"));
296: logger.error(e.getMessage());
297: return 2;
298: } catch (IOException e) {
299: logger.error(
300: /* (non-Javadoc)
301: * @i18n.test
302: * @org-mes="IO exception: " + p[0]
303: */
304: org.openlaszlo.i18n.LaszloMessages.getMessage(Main.class
305: .getName(), "051018-259", new Object[] { e
306: .getMessage() }));
307: return 3;
308: }
309: return 0;
310: }
311: }
312:
313: /**
314: * @copyright Copyright 2001-2007 Laszlo Systems, Inc. All Rights
315: * Reserved. Use is subject to license terms.
316: */
|