Package com.google.javascript.jscomp
Class CommandLineRunner
- java.lang.Object
-
- com.google.javascript.jscomp.CommandLineRunner
-
public class CommandLineRunner extends java.lang.ObjectCommandLineRunner 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); }This class is totally not thread-safe.@Overrideprotected 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); } } }
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedCommandLineRunner(java.lang.String[] args)Create a new command-line runner.protectedCommandLineRunner(java.lang.String[] args, java.io.PrintStream out, java.io.PrintStream err)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidcheckModuleName(java.lang.String name)Validates the module name.protected CompilercreateCompiler()Returns the instance of the Compiler to use whenrun()is called.protected java.util.List<SourceFile>createExterns()protected java.util.List<SourceFile>createInputs(java.util.List<java.lang.String> files, boolean allowStdIn)Creates inputs from a list of files.protected CompilerOptionscreateOptions()Returns the instance of the Options to use whenrun()is called.protected intdoRun()Parses command-line arguments and runs the compiler.protected java.io.OutputStreamfilenameToOutputStream(java.lang.String fileName)Converts a file name into a Outputstream.protected com.google.javascript.jscomp.AbstractCommandLineRunner.CommandLineConfiggetCommandLineConfig()Get the command line config, so that it can be initialized.protected AgetCompiler()static java.util.List<SourceFile>getDefaultExterns()protected DiagnosticGroupsgetDiagnosticGroups()The warning classes that are available from the command-line.protected java.io.PrintStreamgetErrorPrintStream()Returns the PrintStream for writing errors associated with this AbstractCommandLineRunner.protected booleanisInTestMode()Returns whether we're in test mode.static voidmain(java.lang.String[] args)Runs the Compiler.voidrun()Runs the Compiler and calls System.exit() with the exit status of the compiler.protected voidsetRunOptions(CompilerOptions options)Sets options based on the configurations set flags API.booleanshouldRunCompiler()
-
-
-
Constructor Detail
-
CommandLineRunner
protected CommandLineRunner(java.lang.String[] args)
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
protected CommandLineRunner(java.lang.String[] args, java.io.PrintStream out, java.io.PrintStream err)
-
-
Method Detail
-
createOptions
protected CompilerOptions 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
protected Compiler createCompiler()
Returns the instance of the Compiler to use whenrun()is called.
-
createExterns
protected java.util.List<SourceFile> createExterns() throws com.google.javascript.jscomp.AbstractCommandLineRunner.FlagUsageException, java.io.IOException
- Throws:
com.google.javascript.jscomp.AbstractCommandLineRunner.FlagUsageExceptionjava.io.IOException
-
getDefaultExterns
public static java.util.List<SourceFile> getDefaultExterns() throws java.io.IOException
- Returns:
- a mutable list
- Throws:
java.io.IOException
-
shouldRunCompiler
public boolean shouldRunCompiler()
- Returns:
- Whether the configuration is valid.
-
main
public static void main(java.lang.String[] args)
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
protected DiagnosticGroups getDiagnosticGroups()
The warning classes that are available from the command-line.
-
setRunOptions
protected void setRunOptions(CompilerOptions options) throws com.google.javascript.jscomp.AbstractCommandLineRunner.FlagUsageException, java.io.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.FlagUsageExceptionjava.io.IOException
-
getCompiler
protected final A getCompiler()
-
run
public final void run()
Runs the Compiler and calls System.exit() with the exit status of the compiler.
-
getErrorPrintStream
protected java.io.PrintStream getErrorPrintStream()
Returns the PrintStream for writing errors associated with this AbstractCommandLineRunner.
-
createInputs
protected java.util.List<SourceFile> createInputs(java.util.List<java.lang.String> files, boolean allowStdIn) throws com.google.javascript.jscomp.AbstractCommandLineRunner.FlagUsageException, java.io.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.FlagUsageExceptionjava.io.IOException
-
checkModuleName
protected void checkModuleName(java.lang.String name) throws com.google.javascript.jscomp.AbstractCommandLineRunner.FlagUsageExceptionValidates 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, java.io.IOExceptionParses command-line arguments and runs the compiler.- Returns:
- system exit status
- Throws:
com.google.javascript.jscomp.AbstractCommandLineRunner.FlagUsageExceptionjava.io.IOException
-
filenameToOutputStream
protected java.io.OutputStream filenameToOutputStream(java.lang.String fileName) throws java.io.IOExceptionConverts a file name into a Outputstream. Returns null if the file name is null.- Throws:
java.io.IOException
-
-