Class AbstractCommandLineRunner<A extends Compiler,B extends CompilerOptions>

java.lang.Object
com.google.javascript.jscomp.AbstractCommandLineRunner<A,B>
Direct Known Subclasses:
CommandLineRunner

public abstract class AbstractCommandLineRunner<A extends Compiler,B extends CompilerOptions> extends Object
Implementations of AbstractCommandLineRunner translate flags into Java API calls on the Compiler. AbstractCompiler contains common flags and logic to make that happen.

This class may be extended and used to create other Java classes that behave the same as running the Compiler from the command line. Example:

 class MyCommandLineRunner extends
     AbstractCommandLineRunner<MyCompiler, MyOptions> {
   MyCommandLineRunner(String[] args) {
     super(args);
   }

   @Override
   protected MyOptions createOptions() {
     MyOptions options = new MyOptions();
     CompilerFlagTranslator.setOptionsFromFlags(options);
     addMyCrazyCompilerPassThatOutputsAnExtraFile(options);
     return options;
   }

   @Override
   protected MyCompiler createCompiler() {
     return new MyCompiler();
   }

   public static void main(String[] args) {
     (new MyCommandLineRunner(args)).run();
   }
 }
 
  • Method Details

    • setExitCodeReceiver

      public void setExitCodeReceiver(com.google.common.base.Function<Integer,Void> newExitCodeReceiver)
      Parameters:
      newExitCodeReceiver - receives a non-zero integer to indicate a problem during execution or 0i to indicate success.
    • isInTestMode

      protected boolean isInTestMode()
      Returns whether we're in test mode.
    • getCommandLineConfig

      protected AbstractCommandLineRunner.CommandLineConfig getCommandLineConfig()
      Get the command line config, so that it can be initialized.
    • createCompiler

      protected abstract A createCompiler()
      Returns the instance of the Compiler to use when run() is called.
    • prepForBundleAndAppendTo

      protected abstract void prepForBundleAndAppendTo(Appendable out, CompilerInput input, String content) throws IOException
      Performs any transformation needed on the given compiler input and appends it to the given output bundle.
      Throws:
      IOException
    • appendRuntimeTo

      protected abstract void appendRuntimeTo(Appendable out) throws IOException
      Writes whatever runtime libraries are needed to bundle.
      Throws:
      IOException
    • createOptions

      protected abstract B 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.
    • getDiagnosticGroups

      protected DiagnosticGroups getDiagnosticGroups()
      The warning classes that are available from the command-line.
    • addAllowlistWarningsGuard

      protected abstract void addAllowlistWarningsGuard(CompilerOptions options, File allowlistFile)
    • setWarningGuardOptions

      protected static void setWarningGuardOptions(CompilerOptions options, ArrayList<AbstractCommandLineRunner.FlagEntry<CheckLevel>> warningGuards, DiagnosticGroups diagnosticGroups)
    • setRunOptions

      protected void setRunOptions(CompilerOptions options) throws 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:
      IOException
    • getCompiler

      protected final A getCompiler()
    • getBuiltinExterns

      public static List<SourceFile> getBuiltinExterns(CompilerOptions.Environment env) throws IOException
      Returns:
      a mutable list
      Throws:
      IOException
    • getVersionText

      protected abstract String getVersionText()
      Some text identifying this binary and its version.

      At minimum, this is what will be printed when `--version` is passed.

    • run

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

      protected final PrintStream getErrorPrintStream()
      Returns the PrintStream for writing errors associated with this AbstractCommandLineRunner.
    • parseJsonFilesFromInputStream

      public List<AbstractCommandLineRunner.JsonFileSpec> parseJsonFilesFromInputStream() throws IOException
      Throws:
      IOException
    • createInputs

      Creates inputs from a list of source files, zips and json files.

      Can be overridden by subclasses who want to pull files from different places.

      Parameters:
      files - A list of flag entries indicates js and zip file names
      jsonFiles - A list of json encoded files.
      allowStdIn - Whether '-' is allowed appear as a filename to represent stdin. If true, '-' is only allowed to appear once.
      jsChunkSpecs - A list chunk specs.
      Returns:
      An array of inputs
      Throws:
      IOException
    • createJsChunks

      public static List<JSChunk> createJsChunks(List<AbstractCommandLineRunner.JsChunkSpec> specs, List<CompilerInput> inputs)
      Creates chunk objects from a list of chunk specifications.
      Parameters:
      specs - A list of chunk specifications, not null or empty.
      inputs - A list of JS file paths, not null
      Returns:
      An array of module objects
    • checkModuleName

      protected void checkModuleName(String name)
      Validates the module name. Can be overridden by subclasses.
      Parameters:
      name - The module name
    • parseModuleWrappers

      public static Map<String,String> parseModuleWrappers(List<String> specs, Iterable<JSChunk> chunks)
      Parses module wrapper specifications.
      Parameters:
      specs - A list of module wrapper specifications, not null. The spec format is: name:wrapper. Wrappers.
      chunks - The JS chunks whose wrappers are specified
      Returns:
      A map from module name to module wrapper. Modules with no wrapper will have the empty string as their value in this map.
    • doRun

      protected int doRun() throws IOException
      Parses command-line arguments and runs the compiler.
      Returns:
      system exit status
      Throws:
      IOException
    • getCompileMetricsRecorder

      protected CompileMetricsRecorderInterface getCompileMetricsRecorder()
      Child classes should override this if they want to actually record metrics about the compilation.
    • createExterns

      protected List<SourceFile> createExterns(CompilerOptions options) throws IOException
      Throws:
      IOException
    • shouldGenerateMapPerModule

      protected boolean shouldGenerateMapPerModule(B options)
      Returns true if and only if a source map file should be generated for each module, as opposed to one unified map. This is specified by having the source map pattern include the %outname% variable.
    • filenameToOutputStream

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

      public static void createDefineReplacements(List<String> definitions, CompilerOptions options)
      Create a map of constant names to constant values from a textual description of the map.
      Parameters:
      definitions - A list of overriding definitions for defines in the form <name>[=<val>], where <val> is a number, boolean, or single-quoted string without single quotes.