Class CommandLineRunner

java.lang.Object
com.google.javascript.jscomp.CommandLineRunner

public class CommandLineRunner extends 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 Details

    • CommandLineRunner

      protected CommandLineRunner(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(String[] args, PrintStream out, PrintStream err)
  • Method Details

    • 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 List<SourceFile> createExterns() throws com.google.javascript.jscomp.AbstractCommandLineRunner.FlagUsageException, IOException
      Throws:
      com.google.javascript.jscomp.AbstractCommandLineRunner.FlagUsageException
      IOException
    • getDefaultExterns

      public static List<SourceFile> getDefaultExterns() throws IOException
      Returns:
      a mutable list
      Throws:
      IOException
    • shouldRunCompiler

      public boolean shouldRunCompiler()
      Returns:
      Whether the configuration is valid.
    • main

      public static void main(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, 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

      protected final Compiler getCompiler()
    • run

      public final void run()
      Runs the Compiler and calls System.exit() with the exit status of the compiler.
    • getErrorPrintStream

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

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