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

import groovy.lang.Closure;
import groovy.lang.GroovyObjectSupport;
import org.gradle.api.file.FileCollection;
import org.gradle.api.internal.project.IsolatedAntBuilder;
import org.gradle.api.internal.tasks.SimpleWorkResult;
import org.gradle.api.tasks.WorkResult;
import org.gradle.language.base.internal.compile.Compiler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AntGosuCompiler implements Compiler<DefaultGosuCompileSpec> {

  private static final Logger LOGGER = LoggerFactory.getLogger(AntGosuCompiler.class);
  
  private final IsolatedAntBuilder _antBuilder;
  private Iterable<File> _compileClasspath;
  private Iterable<File> _gosuClasspath;
  
  public AntGosuCompiler( IsolatedAntBuilder antBuilder, Iterable<File> compileClasspath, Iterable<File> gosuClasspath) {
    _antBuilder = antBuilder;
    _compileClasspath = compileClasspath;
    _gosuClasspath = gosuClasspath;
  }
      
  @Override
  public WorkResult execute(DefaultGosuCompileSpec spec) {
    final String gosuClasspathRefId = "gosu.classpath";
    final File destinationDir = spec.getDestinationDir();
    final GosuCompileOptions options = spec.getGosuCompileOptions();
    Map<String, Object> optionsMap = options.optionMap();

    final String taskName = "gosuc";

    LOGGER.info("Compiling with Ant gosuc task.");
    LOGGER.info("Ant gosuc task options: {}", optionsMap);
    LOGGER.info("_compileClasspath: {}", _compileClasspath);
    LOGGER.info("_gosuClasspath: {}", _gosuClasspath);

    List<File> jointClasspath = new ArrayList<>();
    _compileClasspath.forEach(jointClasspath::add);
    _gosuClasspath.forEach(jointClasspath::add);

    LOGGER.info("jointClasspath: {}", jointClasspath);
    
    _antBuilder.withClasspath(jointClasspath).execute(new Closure<Object>(this, this) {
      @SuppressWarnings("UnusedDeclaration")
      public Object doCall( Object it ) {
        final GroovyObjectSupport antBuilder = (GroovyObjectSupport) it;
        
        LOGGER.debug("About to call antBuilder.invokeMethod(\"taskdef\")");
        
//        antBuilder.invokeMethod("taskdef", ImmutableMap.of(
//            "resource", "gosu/tools/ant/antlib.xml"
//        ));
        Map<String, Object> taskdefMap = new HashMap<>();
        taskdefMap.put("name", taskName);
        taskdefMap.put("classname", "gosu.tools.ant.Gosuc");  //TODO load from antlib.xml

        antBuilder.invokeMethod("taskdef", taskdefMap);

        LOGGER.debug("Finished calling antBuilder.invokeMethod(\"taskdef\")");
        
        //define the PATH for the classpath
        List<String> gosuClasspathAsStrings = new ArrayList<>();
        jointClasspath.forEach(file -> gosuClasspathAsStrings.add(file.getAbsolutePath()));

        Map<String, Object> classpath = new HashMap<>();
        classpath.put("id", gosuClasspathRefId);
        classpath.put("path", String.join(":", gosuClasspathAsStrings));

        LOGGER.debug("About to call antBuilder.invokeMethod(\"path\")");
        LOGGER.debug("classpath map {}", classpath);
        
        antBuilder.invokeMethod("path", classpath);

        LOGGER.debug("Finished calling antBuilder.invokeMethod(\"path\")");

        optionsMap.put("destdir", destinationDir.getAbsolutePath());
        optionsMap.put("classpathref", gosuClasspathRefId);

        LOGGER.debug("Dumping optionsMap:");
        optionsMap.forEach(( key, value ) -> LOGGER.debug('\t' + key + '=' + value));
        
        LOGGER.debug("About to call antBuilder.invokeMethod(\"" + taskName + "\")");

        antBuilder.invokeMethod(taskName, new Object[]{ optionsMap, new Closure<Object>(this, this) {
          public Object doCall(Object ignore) {
            spec.getSource().addToAntBuilder(antBuilder, "src", FileCollection.AntType.ResourceCollection);

            return null;
          }
        }});

        return null;
      }
    });

    return new SimpleWorkResult(true);
  }

}<commit_msg>Fix tests   * Turns out that Gradle's recommendation to use FileCollection.AntType.ResourceCollection is without precedent   * Other compilation/javadoc assembly tasks using a Gradle closure to nest a <src> attribute are using MatchingTask<commit_after>package org.gosulang.gradle.tasks.compile;

import groovy.lang.Closure;
import groovy.lang.GroovyObjectSupport;
import org.gradle.api.file.FileCollection;
import org.gradle.api.internal.project.IsolatedAntBuilder;
import org.gradle.api.internal.tasks.SimpleWorkResult;
import org.gradle.api.tasks.WorkResult;
import org.gradle.language.base.internal.compile.Compiler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AntGosuCompiler implements Compiler<DefaultGosuCompileSpec> {

  private static final Logger LOGGER = LoggerFactory.getLogger(AntGosuCompiler.class);
  
  private final IsolatedAntBuilder _antBuilder;
  private Iterable<File> _compileClasspath;
  private Iterable<File> _gosuClasspath;
  
  public AntGosuCompiler( IsolatedAntBuilder antBuilder, Iterable<File> compileClasspath, Iterable<File> gosuClasspath) {
    _antBuilder = antBuilder;
    _compileClasspath = compileClasspath;
    _gosuClasspath = gosuClasspath;
  }
      
  @Override
  public WorkResult execute(DefaultGosuCompileSpec spec) {
    final String gosuClasspathRefId = "gosu.classpath";
    final File destinationDir = spec.getDestinationDir();
    final GosuCompileOptions options = spec.getGosuCompileOptions();
    Map<String, Object> optionsMap = options.optionMap();

    final String taskName = "gosuc";

    LOGGER.info("Compiling with Ant gosuc task.");
    LOGGER.info("Ant gosuc task options: {}", optionsMap);
    LOGGER.info("_compileClasspath: {}", _compileClasspath);
    LOGGER.info("_gosuClasspath: {}", _gosuClasspath);

    List<File> jointClasspath = new ArrayList<>();
    _compileClasspath.forEach(jointClasspath::add);
    _gosuClasspath.forEach(jointClasspath::add);

    LOGGER.info("jointClasspath: {}", jointClasspath);
    
    _antBuilder.withClasspath(jointClasspath).execute(new Closure<Object>(this, this) {
      @SuppressWarnings("UnusedDeclaration")
      public Object doCall( Object it ) {
        final GroovyObjectSupport antBuilder = (GroovyObjectSupport) it;
        
        LOGGER.debug("About to call antBuilder.invokeMethod(\"taskdef\")");
        
//        antBuilder.invokeMethod("taskdef", ImmutableMap.of(
//            "resource", "gosu/tools/ant/antlib.xml"
//        ));
        Map<String, Object> taskdefMap = new HashMap<>();
        taskdefMap.put("name", taskName);
        taskdefMap.put("classname", "gosu.tools.ant.Gosuc");  //TODO load from antlib.xml

        antBuilder.invokeMethod("taskdef", taskdefMap);

        LOGGER.debug("Finished calling antBuilder.invokeMethod(\"taskdef\")");
        
        //define the PATH for the classpath
        List<String> gosuClasspathAsStrings = new ArrayList<>();
        jointClasspath.forEach(file -> gosuClasspathAsStrings.add(file.getAbsolutePath()));

        Map<String, Object> classpath = new HashMap<>();
        classpath.put("id", gosuClasspathRefId);
        classpath.put("path", String.join(":", gosuClasspathAsStrings));

        LOGGER.debug("About to call antBuilder.invokeMethod(\"path\")");
        LOGGER.debug("classpath map {}", classpath);
        
        antBuilder.invokeMethod("path", classpath);

        LOGGER.debug("Finished calling antBuilder.invokeMethod(\"path\")");

        optionsMap.put("destdir", destinationDir.getAbsolutePath());
        optionsMap.put("classpathref", gosuClasspathRefId);

        LOGGER.debug("Dumping optionsMap:");
        optionsMap.forEach(( key, value ) -> LOGGER.debug('\t' + key + '=' + value));
        
        LOGGER.debug("About to call antBuilder.invokeMethod(\"" + taskName + "\")");

        antBuilder.invokeMethod(taskName, new Object[]{ optionsMap, new Closure<Object>(this, this) {
          public Object doCall(Object ignore) {
            spec.getSource().addToAntBuilder(antBuilder, "src", FileCollection.AntType.MatchingTask);

            return null;
          }
        }});

        return null;
      }
    });

    return new SimpleWorkResult(true);
  }

}
{'fraction_non_alphanumeric': 0.12103268593083846, 'fraction_numerical': 0.0004737091425864519, 'mean_word_length': 3.7874149659863945, 'pattern_counts': {'":': 2, '<': 36, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '5452441', 'n_tokens_mistral': 2527, 'n_tokens_neox': 2488, 'n_words': 476}