<commit_before>package org.smoothbuild.cli;

import static org.smoothbuild.SmoothConstants.DEFAULT_SCRIPT;
import static org.smoothbuild.SmoothConstants.EXIT_CODE_ERROR;
import static org.smoothbuild.SmoothConstants.EXIT_CODE_SUCCESS;
import static org.smoothbuild.lang.function.base.Name.isLegalName;
import static org.smoothbuild.lang.function.base.Name.name;
import static org.smoothbuild.lang.function.nativ.NativeLibraryLoader.loadBuiltinFunctions;

import java.util.List;
import java.util.Set;

import javax.inject.Inject;

import org.smoothbuild.io.util.TempManager;
import org.smoothbuild.lang.function.Functions;
import org.smoothbuild.lang.function.base.Name;
import org.smoothbuild.parse.ModuleLoader;
import org.smoothbuild.parse.ParsingException;
import org.smoothbuild.task.exec.ExecutionException;
import org.smoothbuild.task.exec.SmoothExecutor;
import org.smoothbuild.util.DuplicatesDetector;

import com.google.common.collect.ImmutableList;

public class Build {
  private final Console console;
  private final TempManager tempManager;
  private final ModuleLoader moduleLoader;
  private final SmoothExecutor smoothExecutor;

  @Inject
  public Build(Console console, TempManager tempManager, ModuleLoader moduleLoader,
      SmoothExecutor smoothExecutor) {
    this.console = console;
    this.tempManager = tempManager;
    this.moduleLoader = moduleLoader;
    this.smoothExecutor = smoothExecutor;
  }

  public int run(String... names) {
    try {
      List<String> argsWithoutFirst = ImmutableList.copyOf(names).subList(1, names.length);
      Set<Name> functionNames = parseArguments(argsWithoutFirst);
      Functions builtinFunctions = loadBuiltinFunctions();
      tempManager.removeTemps();
      Functions definedFunctions = moduleLoader.loadFunctions(builtinFunctions, DEFAULT_SCRIPT);
      Functions functions = builtinFunctions.addAll(definedFunctions);
      smoothExecutor.execute(functions, functionNames);
    } catch (ParsingException | ExecutionException e) {
      return EXIT_CODE_ERROR;
    }

    console.printFinalSummary();
    return console.isErrorReported() ? EXIT_CODE_ERROR : EXIT_CODE_SUCCESS;
  }

  public Set<Name> parseArguments(List<String> args) {
    DuplicatesDetector<Name> duplicatesDetector = new DuplicatesDetector<>();
    for (String argument : args) {
      if (isLegalName(argument)) {
        duplicatesDetector.addValue(name(argument));
      } else {
        console.error("Illegal function name '" + argument + "' passed in command line.");
        throw new ExecutionException();
      }
    }

    for (Name name : duplicatesDetector.getDuplicateValues()) {
      console.error("Function " + name + " has been specified more than once.");
      throw new ExecutionException();
    }
    Set<Name> result = duplicatesDetector.getUniqueValues();
    if (result.isEmpty()) {
      console.error("Specify at least one function to be executed.");
      throw new ExecutionException();
    }
    return result;
  }
}<commit_msg>refactored Build class<commit_after>package org.smoothbuild.cli;

import static org.smoothbuild.SmoothConstants.DEFAULT_SCRIPT;
import static org.smoothbuild.SmoothConstants.EXIT_CODE_ERROR;
import static org.smoothbuild.SmoothConstants.EXIT_CODE_SUCCESS;
import static org.smoothbuild.lang.function.base.Name.isLegalName;
import static org.smoothbuild.lang.function.base.Name.name;
import static org.smoothbuild.lang.function.nativ.NativeLibraryLoader.loadBuiltinFunctions;

import java.util.List;
import java.util.Set;

import javax.inject.Inject;

import org.smoothbuild.io.util.TempManager;
import org.smoothbuild.lang.function.Functions;
import org.smoothbuild.lang.function.base.Name;
import org.smoothbuild.parse.Maybe;
import org.smoothbuild.parse.ModuleLoader;
import org.smoothbuild.parse.ParsingException;
import org.smoothbuild.task.exec.ExecutionException;
import org.smoothbuild.task.exec.SmoothExecutor;
import org.smoothbuild.util.DuplicatesDetector;

import com.google.common.collect.ImmutableList;

public class Build {
  private final Console console;
  private final TempManager tempManager;
  private final ModuleLoader moduleLoader;
  private final SmoothExecutor smoothExecutor;

  @Inject
  public Build(Console console, TempManager tempManager, ModuleLoader moduleLoader,
      SmoothExecutor smoothExecutor) {
    this.console = console;
    this.tempManager = tempManager;
    this.moduleLoader = moduleLoader;
    this.smoothExecutor = smoothExecutor;
  }

  public int run(String... names) {
    List<String> argsWithoutFirst = ImmutableList.copyOf(names).subList(1, names.length);
    Maybe<Set<Name>> functionNames = parseArguments(argsWithoutFirst);
    if (!functionNames.hasResult()) {
      for (Object error : functionNames.errors()) {
        console.rawError(error);
      }
      return EXIT_CODE_ERROR;
    }
    tempManager.removeTemps();
    try {
      Functions functions = loadFunctions();
      smoothExecutor.execute(functions, functionNames.result());
    } catch (ParsingException | ExecutionException e) {
      return EXIT_CODE_ERROR;
    }

    console.printFinalSummary();
    return console.isErrorReported() ? EXIT_CODE_ERROR : EXIT_CODE_SUCCESS;
  }

  private Functions loadFunctions() {
    Functions builtinFunctions = loadBuiltinFunctions();
    Functions definedFunctions = moduleLoader.loadFunctions(builtinFunctions, DEFAULT_SCRIPT);
    return builtinFunctions.addAll(definedFunctions);
  }

  public Maybe<Set<Name>> parseArguments(List<String> args) {
    DuplicatesDetector<Name> duplicatesDetector = new DuplicatesDetector<>();
    for (String argument : args) {
      if (isLegalName(argument)) {
        duplicatesDetector.addValue(name(argument));
      } else {
        return Maybe.error("error: Illegal function name '" + argument
            + "' passed in command line.");
      }
    }

    for (Name name : duplicatesDetector.getDuplicateValues()) {
      return Maybe.error("error: Function " + name + " has been specified more than once.");
    }
    Set<Name> result = duplicatesDetector.getUniqueValues();
    if (result.isEmpty()) {
      return Maybe.error("error: Specify at least one function to be executed.");
    }
    return Maybe.element(result);
  }
}
{'fraction_non_alphanumeric': 0.09610764055742431, 'fraction_numerical': 0.00032035880185808104, 'mean_word_length': 4.47719298245614, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '5315718', 'n_tokens_mistral': 1781, 'n_tokens_neox': 1708, 'n_words': 369}