<commit_before>package org.gosulang.gradle.tasks;

import org.gradle.api.Buildable;
import org.gradle.api.GradleException;
import org.gradle.api.Nullable;
import org.gradle.api.Project;
import org.gradle.api.file.FileCollection;
import org.gradle.api.internal.artifacts.dependencies.DefaultExternalModuleDependency;
import org.gradle.api.internal.file.FileCollectionInternal;
import org.gradle.api.internal.file.collections.LazilyInitializedFileCollection;
import org.gradle.api.tasks.TaskDependency;
import org.gradle.internal.Cast;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GosuRuntime {

  private static final Pattern GOSU_JAR_PATTERN = Pattern.compile("gosu-(\\w.*?)-(\\d.*).jar");
  private static final String LF = System.lineSeparator();

  private final Project _project;

  public GosuRuntime(Project project) {
    _project = project;
  }

  /**
   * Searches the specified classpath for a 'gosu-core-api' Jar, and returns a classpath
   * containing a corresponding (same version) 'gosu-ant-compiler' Jar and its dependencies.
   * 
   * <p>The returned class path may be empty, or may fail to resolve when asked for its contents.
   *
   * @param classpath a classpath containing a 'gosu-core-api' Jar
   * @return a classpath containing a corresponding 'gosu-core' Jar and its dependencies
   */
  public FileCollection inferGosuClasspath(final Iterable<File> classpath) {

    return new LazilyInitializedFileCollection() {
      @Override
      public String getDisplayName() {
        return "Gosu runtime classpath";
      }

      @Override
      public FileCollectionInternal createDelegate() {
        if (_project.getRepositories().isEmpty()) {
          throw new GradleException("Cannot infer Gosu classpath because no repository is declared in " + _project);
        }

        File gosuCoreApiJar = findGosuJar(classpath, "core-api");

        if(gosuCoreApiJar == null) {
          List<String> classpathAsStrings = new ArrayList<>();
          classpath.forEach(file -> classpathAsStrings.add(file.getAbsolutePath()));
          String flattenedClasspath = String.join(":", classpathAsStrings);
          throw new GradleException(String.format("Cannot infer Gosu classpath because the Gosu Core API Jar was not found." + LF +
              "Does %s declare dependency to gosu-core-api? Searched classpath: %s.", _project, flattenedClasspath) + LF +
              "An example dependencies closure may resemble the following:" + LF +
              LF +
              "dependencies {" + LF +
              "    compile 'org.gosu-lang.gosu:gosu-core-api:1.8.1'" + LF +
              "}" + LF);
        }

        String gosuCoreApiVersion = getGosuVersion(gosuCoreApiJar);

        if (gosuCoreApiVersion == null ) {
          throw new AssertionError(String.format("Unexpectedly failed to parse version of Gosu Jar file: %s in %s", gosuCoreApiJar, _project));
        }

        return Cast.cast(FileCollectionInternal.class, _project.getConfigurations().detachedConfiguration(
            new DefaultExternalModuleDependency("org.gosu-lang.gosu", "gosu-ant-compiler", gosuCoreApiVersion)));
      }

      // let's override this so that delegate isn't created at autowiring time (which would mean on every build)
      @Override
      public TaskDependency getBuildDependencies() {
        if (classpath instanceof Buildable) {
          return ((Buildable) classpath).getBuildDependencies();
        }
        return task -> Collections.emptySet();
      }
    };

  }

  /**
   * Searches the specified class path for a Gosu Jar file (gosu-core, gosu-core-api, etc.) with the specified appendix (compiler, library, jdbc, etc.).
   * If no such file is found, {@code null} is returned.
   *
   * @param classpath the class path to search<commit_msg>Small change for Gradle 2.8 API change<commit_after>package org.gosulang.gradle.tasks;

import org.gradle.api.Buildable;
import org.gradle.api.GradleException;
import org.gradle.api.Nullable;
import org.gradle.api.Project;
import org.gradle.api.file.FileCollection;
import org.gradle.api.internal.artifacts.dependencies.DefaultExternalModuleDependency;
import org.gradle.api.internal.file.FileCollectionInternal;
import org.gradle.api.internal.file.collections.LazilyInitializedFileCollection;
import org.gradle.api.internal.tasks.TaskDependencyResolveContext;
import org.gradle.internal.Cast;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GosuRuntime {

  private static final Pattern GOSU_JAR_PATTERN = Pattern.compile("gosu-(\\w.*?)-(\\d.*).jar");
  private static final String LF = System.lineSeparator();

  private final Project _project;

  public GosuRuntime(Project project) {
    _project = project;
  }

  /**
   * Searches the specified classpath for a 'gosu-core-api' Jar, and returns a classpath
   * containing a corresponding (same version) 'gosu-ant-compiler' Jar and its dependencies.
   * 
   * <p>The returned class path may be empty, or may fail to resolve when asked for its contents.
   *
   * @param classpath a classpath containing a 'gosu-core-api' Jar
   * @return a classpath containing a corresponding 'gosu-core' Jar and its dependencies
   */
  public FileCollection inferGosuClasspath(final Iterable<File> classpath) {

    return new LazilyInitializedFileCollection() {
      @Override
      public String getDisplayName() {
        return "Gosu runtime classpath";
      }

      @Override
      public FileCollectionInternal createDelegate() {
        if (_project.getRepositories().isEmpty()) {
          throw new GradleException("Cannot infer Gosu classpath because no repository is declared in " + _project);
        }

        File gosuCoreApiJar = findGosuJar(classpath, "core-api");

        if(gosuCoreApiJar == null) {
          List<String> classpathAsStrings = new ArrayList<>();
          classpath.forEach(file -> classpathAsStrings.add(file.getAbsolutePath()));
          String flattenedClasspath = String.join(":", classpathAsStrings);
          throw new GradleException(String.format("Cannot infer Gosu classpath because the Gosu Core API Jar was not found." + LF +
              "Does %s declare dependency to gosu-core-api? Searched classpath: %s.", _project, flattenedClasspath) + LF +
              "An example dependencies closure may resemble the following:" + LF +
              LF +
              "dependencies {" + LF +
              "    compile 'org.gosu-lang.gosu:gosu-core-api:1.8.1'" + LF +
              "}" + LF);
        }

        String gosuCoreApiVersion = getGosuVersion(gosuCoreApiJar);

        if (gosuCoreApiVersion == null ) {
          throw new AssertionError(String.format("Unexpectedly failed to parse version of Gosu Jar file: %s in %s", gosuCoreApiJar, _project));
        }

        return Cast.cast(FileCollectionInternal.class, _project.getConfigurations().detachedConfiguration(
            new DefaultExternalModuleDependency("org.gosu-lang.gosu", "gosu-ant-compiler", gosuCoreApiVersion)));
      }

      // let's override this so that delegate isn't created at autowiring time (which would mean on every build)
      @Override
      public void visitDependencies(TaskDependencyResolveContext context) {
        if (classpath instanceof Buildable) {
          context.add(classpath);
        }
      }
    };

  }

  /**
   * Searches the specified class path for a Gosu Jar file (gosu-core, gosu-core-api, etc.) with the specified appendix (compiler, library, jdbc, etc.).
   * If no such file is found, {@code null} is returned.
   *
   * @param classpath the class path to search
{'fraction_non_alphanumeric': 0.0991661321359846, 'fraction_numerical': 0.0010262989095574087, 'mean_word_length': 3.2976846747519293, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 14, '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': '3105407', 'n_tokens_mistral': 2263, 'n_tokens_neox': 2158, 'n_words': 617}