Class CommandLineRunner


  • public class CommandLineRunner
    extends java.lang.Object
    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

      Constructors 
      Modifier Constructor Description
      protected CommandLineRunner​(java.lang.String[] args)
      Create a new command-line runner.
      protected CommandLineRunner​(java.lang.String[] args, java.io.PrintStream out, java.io.PrintStream err)  
    • 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 when run() 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 when run() 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.FlagUsageException
        java.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.FlagUsageException
        java.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 filenames
        allowStdIn - 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
        java.io.IOException
      • checkModuleName

        protected void checkModuleName​(java.lang.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,
                            java.io.IOException
        Parses command-line arguments and runs the compiler.
        Returns:
        system exit status
        Throws:
        com.google.javascript.jscomp.AbstractCommandLineRunner.FlagUsageException
        java.io.IOException
      • filenameToOutputStream

        protected java.io.OutputStream filenameToOutputStream​(java.lang.String fileName)
                                                       throws java.io.IOException
        Converts a file name into a Outputstream. Returns null if the file name is null.
        Throws:
        java.io.IOException