<commit_before><commit_msg>Factored out code common to SoyTo{Java,Js}<commit_after>package com.google.common.html.plugin.soy;

import java.io.File;
import java.io.IOException;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;

import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
import com.google.common.html.plugin.Sources.Source;
import com.google.common.html.plugin.common.Ingredients.DirScanFileSetIngredient;
import com.google.common.html.plugin.common.Ingredients.FileIngredient;
import com.google.common.html.plugin.common.Ingredients.FileSetIngredient;
import com.google.common.html.plugin.common.Ingredients.OptionsIngredient;
import com.google.common.html.plugin.common.Ingredients.PathValue;
import com.google.common.html.plugin.plan.Ingredient;
import com.google.common.html.plugin.plan.Step;
import com.google.common.html.plugin.plan.StepSource;
import com.google.common.io.Files;
import com.google.template.soy.SoyFileSet;

abstract class AbstractSoyStep extends Step {

  AbstractSoyStep(
      String keyPrefix,
      OptionsIngredient<SoyOptions> options,
      FileSetIngredient soySources,
      PathValue dest) {
    super(
        keyPrefix + ":[" + options.key + "];" + soySources.key + ";" + dest.key,
        ImmutableList.<Ingredient>of(options, soySources, dest),
        Sets.immutableEnumSet(StepSource.SOY_GENERATED, StepSource.SOY_SRC),
        Sets.immutableEnumSet(StepSource.JS_GENERATED));
  }

  @Override
  public void execute(Log log) throws MojoExecutionException {
    OptionsIngredient<SoyOptions> optionsIng =
        ((OptionsIngredient<?>) inputs.get(0)).asSuperType(SoyOptions.class);
    DirScanFileSetIngredient soySources =
        (DirScanFileSetIngredient) inputs.get(1);
    PathValue dest = (PathValue) inputs.get(2);

    try {
      soySources.resolve(log);
    } catch (IOException ex) {
      throw new MojoExecutionException("Failed to find .soy sources", ex);
    }
    Iterable<FileIngredient> soySourceFiles = soySources.mainSources();

    if (!Iterables.isEmpty(soySourceFiles)) {
      SoyOptions options = optionsIng.getOptions();
      SoyFileSet.Builder sfsBuilder = options.toSoyFileSetBuilder(log);

      ImmutableList.Builder<Source> sources = ImmutableList.builder();
      for (FileIngredient soySource : soySourceFiles) {
        File relPath = soySource.source.relativePath;
        try {
          sfsBuilder.add(
              Files.toString(soySource.source.canonicalPath, Charsets.UTF_8),
              relPath.getPath());
        } catch (IOException ex) {
          throw new MojoExecutionException(
              "Failed to read soy source: " + relPath, ex);
        }
        sources.add(soySource.source);
      }

      compileSoy(log, options, sfsBuilder, sources.build(), dest);
    }
  }

  protected abstract void compileSoy(
      Log log, SoyOptions options, SoyFileSet.Builder sfsBuilder,
      ImmutableList<Source> sources, PathValue dest)
  throws MojoExecutionException;

  @Override
  public void skip(Log log) throws MojoExecutionException {
    // Done
  }

  @Override
  public ImmutableList<Step> extraSteps(Log log) throws MojoExecutionException {
    return ImmutableList.of();
  }

}

{'fraction_non_alphanumeric': 0.10521609538002981, 'fraction_numerical': 0.0011922503725782414, 'mean_word_length': 3.899270072992701, 'pattern_counts': {'":': 1, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6890037', 'n_tokens_mistral': 1056, 'n_tokens_neox': 1029, 'n_words': 180}