Package com.google.javascript.jscomp
Class CommandLineRunner
java.lang.Object
com.google.javascript.jscomp.CommandLineRunner
CommandLineRunner translates flags into Java API calls on the Compiler.
This class may be extended and used to create other Java classes
that behave the same as running the Compiler from the command line. If you
want to run the compiler in-process in Java, you should look at this class
for hints on what API calls to make, but you should not use this class
directly.
Example:
class MyCommandLineRunner extends CommandLineRunner {
MyCommandLineRunner(String[] args) {
super(args);
}
@Override
protected CompilerOptions createOptions() {
CompilerOptions options = super.createOptions();
addMyCrazyCompilerPassThatOutputsAnExtraFile(options);
return options;
}
public static void main(String[] args) {
MyCommandLineRunner runner = new MyCommandLineRunner(args);
if (runner.shouldRunCompiler()) {
runner.run();
} else {
System.exit(-1);
}
}
}
This class is totally not thread-safe.-
Constructor Summary
ModifierConstructorDescriptionprotected
CommandLineRunner
(String[] args) Create a new command-line runner.protected
CommandLineRunner
(String[] args, PrintStream out, PrintStream err) -
Method Summary
Modifier and TypeMethodDescriptionprotected void
checkModuleName
(String name) Validates the module name.protected Compiler
Returns the instance of the Compiler to use whenrun()
is called.protected List<SourceFile>
protected List<SourceFile>
createInputs
(List<String> files, boolean allowStdIn) Creates inputs from a list of files.protected CompilerOptions
Returns the instance of the Options to use whenrun()
is called.protected int
doRun()
Parses command-line arguments and runs the compiler.protected OutputStream
filenameToOutputStream
(String fileName) Converts a file name into a Outputstream.protected com.google.javascript.jscomp.AbstractCommandLineRunner.CommandLineConfig
Get the command line config, so that it can be initialized.protected final Compiler
static List<SourceFile>
protected DiagnosticGroups
The warning classes that are available from the command-line.protected PrintStream
Returns the PrintStream for writing errors associated with this AbstractCommandLineRunner.protected boolean
Returns whether we're in test mode.static void
Runs the Compiler.final void
run()
Runs the Compiler and calls System.exit() with the exit status of the compiler.protected void
setRunOptions
(CompilerOptions options) Sets options based on the configurations set flags API.boolean
-
Constructor Details
-
CommandLineRunner
Create a new command-line runner. You should only need to call the constructor if you're extending this class. Otherwise, the main method should instantiate it. -
CommandLineRunner
-
-
Method Details
-
createOptions
Returns the instance of the Options to use whenrun()
is called. createCompiler() is called before createOptions(), so getCompiler() will not return null when createOptions() is called. -
createCompiler
Returns the instance of the Compiler to use whenrun()
is called. -
createExterns
protected List<SourceFile> createExterns() throws com.google.javascript.jscomp.AbstractCommandLineRunner.FlagUsageException, IOException- Throws:
com.google.javascript.jscomp.AbstractCommandLineRunner.FlagUsageException
IOException
-
getDefaultExterns
- Returns:
- a mutable list
- Throws:
IOException
-
shouldRunCompiler
public boolean shouldRunCompiler()- Returns:
- Whether the configuration is valid.
-
main
Runs the Compiler. Exits cleanly in the event of an error. -
isInTestMode
protected boolean isInTestMode()Returns whether we're in test mode. -
getCommandLineConfig
protected com.google.javascript.jscomp.AbstractCommandLineRunner.CommandLineConfig getCommandLineConfig()Get the command line config, so that it can be initialized. -
getDiagnosticGroups
The warning classes that are available from the command-line. -
setRunOptions
protected void setRunOptions(CompilerOptions options) throws com.google.javascript.jscomp.AbstractCommandLineRunner.FlagUsageException, IOException Sets options based on the configurations set flags API. Called during the run() run() method. If you want to ignore the flags API, or interpret flags your own way, then you should override this method.- Throws:
com.google.javascript.jscomp.AbstractCommandLineRunner.FlagUsageException
IOException
-
getCompiler
-
run
public final void run()Runs the Compiler and calls System.exit() with the exit status of the compiler. -
getErrorPrintStream
Returns the PrintStream for writing errors associated with this AbstractCommandLineRunner. -
createInputs
protected List<SourceFile> createInputs(List<String> files, boolean allowStdIn) throws com.google.javascript.jscomp.AbstractCommandLineRunner.FlagUsageException, IOException Creates inputs from a list of files. Can be overridden by subclasses who want to pull files from different places.- Parameters:
files
- A list of filenamesallowStdIn
- Whether '-' is allowed appear as a filename to represent stdin. If true, '-' is only allowed to appear once.- Returns:
- An array of inputs
- Throws:
com.google.javascript.jscomp.AbstractCommandLineRunner.FlagUsageException
IOException
-
checkModuleName
protected void checkModuleName(String name) throws com.google.javascript.jscomp.AbstractCommandLineRunner.FlagUsageException Validates the module name. Can be overridden by subclasses.- Parameters:
name
- The module name- Throws:
com.google.javascript.jscomp.AbstractCommandLineRunner.FlagUsageException
- if the validation fails
-
doRun
protected int doRun() throws com.google.javascript.jscomp.AbstractCommandLineRunner.FlagUsageException, IOExceptionParses command-line arguments and runs the compiler.- Returns:
- system exit status
- Throws:
com.google.javascript.jscomp.AbstractCommandLineRunner.FlagUsageException
IOException
-
filenameToOutputStream
Converts a file name into a Outputstream. Returns null if the file name is null.- Throws:
IOException
-