001: /*
002: * Spoon - http://spoon.gforge.inria.fr/
003: * Copyright (C) 2006 INRIA Futurs <renaud.pawlak@inria.fr>
004: *
005: * This software is governed by the CeCILL-C License under French law and
006: * abiding by the rules of distribution of free software. You can use, modify
007: * and/or redistribute the software under the terms of the CeCILL-C license as
008: * circulated by CEA, CNRS and INRIA at http://www.cecill.info.
009: *
010: * This program is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
013: *
014: * The fact that you are presently reading this means that you have had
015: * knowledge of the CeCILL-C license and that you accept its terms.
016: */
017: package spoon;
018:
019: import spoon.processing.FileGenerator;
020: import spoon.reflect.Factory;
021: import spoon.support.ByteCodeOutputProcessor;
022: import spoon.support.JavaOutputProcessor;
023: import spoon.support.gui.SpoonModelTree;
024:
025: import com.martiansoftware.jsap.FlaggedOption;
026: import com.martiansoftware.jsap.JSAP;
027: import com.martiansoftware.jsap.JSAPException;
028: import com.martiansoftware.jsap.Switch;
029: import com.martiansoftware.jsap.stringparsers.FileStringParser;
030:
031: /**
032: * This class implements an integrated command-line launcher for processing
033: * programs at compile-time using the JDT-based builder (Eclipse). It takes
034: * arguments that allow building, processing, printing, and compiling Java
035: * programs. Launch with no arguments (see {@link #main(String[])}) for
036: * detailed usage.
037: *
038: *
039: * @see spoon.processing.Environment
040: * @see spoon.reflect.Factory
041: * @see spoon.processing.Builder
042: * @see spoon.processing.ProcessingManager
043: * @see spoon.processing.Processor
044: */
045: public class Launcher extends AbstractLauncher {
046: /**
047: * A default program entry point (instantiates a launcher with the given
048: * arguments and calls {@link #run()}).
049: */
050: public static void main(String[] args) throws Exception {
051: try {
052: new Launcher(args).run();
053: } catch (Exception exc) {
054: exc.printStackTrace();
055: throw exc;
056: }
057: }
058:
059: /**
060: * Constructor.
061: */
062: public Launcher(String[] args) throws JSAPException {
063: super (args);
064: }
065:
066: /**
067: * Adds some specific arguments to the common ones.
068: */
069: @Override
070: protected JSAP defineArgs() throws JSAPException {
071: JSAP jsap = super .defineArgs();
072:
073: // Disable output generation
074: Switch sw1 = new Switch("nooutput");
075: sw1.setLongFlag("no");
076: sw1.setHelp("disable output printing");
077: sw1.setDefault("false");
078: jsap.registerParameter(sw1);
079:
080: // Compile Output files
081: sw1 = new Switch("compile");
082: sw1.setShortFlag('c');
083: sw1.setLongFlag("compile");
084: sw1.setHelp("compile generated sources");
085: jsap.registerParameter(sw1);
086:
087: // build output directory
088: FlaggedOption opt2 = new FlaggedOption("build");
089: opt2.setShortFlag('b');
090: opt2.setLongFlag("build");
091: opt2.setDefault("spoonBuild");
092: opt2.setHelp("specify where to place generated class files");
093: opt2.setStringParser(FileStringParser.getParser());
094: opt2.setRequired(false);
095: jsap.registerParameter(opt2);
096:
097: // show GUI
098: sw1 = new Switch("gui");
099: sw1.setShortFlag('g');
100: sw1.setLongFlag("gui");
101: sw1.setHelp("show spoon model after processing");
102: jsap.registerParameter(sw1);
103:
104: return jsap;
105: }
106:
107: /**
108: * Creates the factory and associated environment for constructing the
109: * model, initialized with the launcher's arguments.
110: */
111: @Override
112: protected Factory createFactory() {
113: Factory f = super .createFactory();
114:
115: if (getArguments().getBoolean("compile")) {
116: FileGenerator<?> printer = f.getEnvironment()
117: .getDefaultFileGenerator();
118: ByteCodeOutputProcessor p = new ByteCodeOutputProcessor(
119: (JavaOutputProcessor) printer, getArguments()
120: .getFile("build"));
121: f.getEnvironment().setDefaultFileGenerator(p);
122: }
123:
124: return f;
125: }
126:
127: /**
128: * Prints out the built model into files.
129: */
130: @Override
131: protected void print() {
132: if (!getArguments().getBoolean("nooutput"))
133: super .print();
134: }
135:
136: /**
137: * Starts the Spoon processing.
138: */
139: @Override
140: public void run() throws Exception {
141: super .run();
142:
143: // display GUI
144: if (getArguments().getBoolean("gui"))
145: new SpoonModelTree(getFactory());
146:
147: }
148:
149: }
|