<commit_before>package cuke4duke.mojo;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;
import org.apache.tools.ant.BuildEvent;
import org.apache.tools.ant.BuildListener;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Java;
import org.apache.tools.ant.types.Commandline;
import org.apache.tools.ant.types.Environment;
import org.apache.tools.ant.types.Path;
import org.codehaus.plexus.util.StringUtils;

/**
 * Base for all JRuby mojos.
 *
 * @requiresDependencyResolution test
 */
public abstract class AbstractJRubyMojo extends AbstractMojo {

    protected boolean shouldFork = true;

    /**
     * @parameter expression="${project}"
     */
    protected MavenProject mavenProject;

    /**
     * @parameter expression="${project.basedir}"
     * @required
     */
    protected File launchDirectory;

    /**
     * The project compile classpath.
     *
     * @parameter default-value="${project.compileClasspathElements}"
     * @required
     * @readonly
     */
    protected List<String> compileClasspathElements;

    /**
     * The plugin dependencies.
     *
     * @parameter expression="${plugin.artifacts}"
     * @required
     * @readonly
     */
    protected List<Artifact> pluginArtifacts;

    /**
     * The project test classpath
     *
     * @parameter expression="${project.testClasspathElements}"
     * @required
     * @readonly
     */
    protected List<String> testClasspathElements;

    /**
     * @parameter expression="${localRepository}"
     * @required
     * @readonly
     */
    protected ArtifactRepository localRepository;

    protected Java jruby(List<String> args) throws MojoExecutionException {
        launchDirectory.mkdirs();
        Project project;
        try {
            project = getProject();
        } catch (DependencyResolutionRequiredException e) {
            throw new MojoExecutionException("error resolving dependencies", e);
        }

        Java java = new Java();
        java.setProject(project);
        java.setClassname("org.jruby.Main");
        java.setFailonerror(true);

        Commandline.Argument arg;

        if (shouldFork) {
            java.setFork(true);
            java.setDir(launchDirectory);

            for (String jvmArg : getJvmArgs()) {
                arg = java.createJvmarg();
                arg.setValue(jvmArg);
            }

            Environment.Variable classpath = new Environment.Variable();

            Path p = new Path(java.getProject());
            p.add((Path) project.getReference("maven.plugin.classpath"));
            p.add((Path) project.getReference("maven.compile.classpath"));
            p.add((Path) project.getReference("maven.test.classpath"));
            classpath.setKey("JRUBY_PARENT_CLASSPATH");
            classpath.setValue(p.toString());

            java.addEnv(classpath);
        }

        Environment.Variable gemPathVar = new Environment.Variable();
        gemPathVar.setKey("GEM_PATH");
        gemPathVar.setValue(gemHome().getAbsolutePath());
        java.addEnv(gemPathVar);

        Path p = java.createClasspath();
        p.add((Path) project.getReference("maven.plugin.classpath"));
        p.add((Path) project.getReference("maven.compile.classpath"));
        p.add((Path) project.getReference("maven.test.classpath"));
        getLog().debug("java classpath: " + p.toString());

        for (String s : args) {
            arg = java.createArg();
            arg.setValue(s);
        }

        return java;
    }

    protected abstract String[] getJvmArgs();

    /**
     * Installs a gem.
     *
     * @param gemSpec name and optional version and location separated by colon. Example:
     *                <ul>
     *                <li>name</li>
     *                <li>name:version</li>
     *                <li>name:version:github</li>
     *                </ul>
     * @throws org.apache.maven.plugin.MojoExecutionException
     *          if gem installation fails.
     */
    protected void installGem(String gemSpec) throws MojoExecutionException {
        List<String> args = new ArrayList<String>();
        args.add("-S");
        args.add("gem");
        args.add("install");
        args.add("--no-ri");
        args.add("--no-rdoc");
        args.add("--install-dir");
        args.add(gemHome().getAbsolutePath());
        args.addAll(parseGem(gemSpec));

        Java jruby = jruby(args);
        // We have to override HOME to make RubyGems install gems
        // where we want it. Setting GEM_HOME and using --install-dir
        // is not enough.
        Environment.Variable homeVar = new Environment.Variable();
        homeVar.setKey("HOME");
        homeVar.setValue(dotGemParent().getAbsolutePath());
        jruby.addEnv(homeVar);
        dotGemParent().mkdirs();
        jruby.execute();
    }

    /**
     * @param gemSpec colon separated string. See installGem.
     * @return arguments that the gem command understands.
     * @throws MojoExecutionException if gem installation fails.<commit_msg>add rubyforge and github sources to gem install<commit_after>package cuke4duke.mojo;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;
import org.apache.tools.ant.BuildEvent;
import org.apache.tools.ant.BuildListener;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Java;
import org.apache.tools.ant.types.Commandline;
import org.apache.tools.ant.types.Environment;
import org.apache.tools.ant.types.Path;
import org.codehaus.plexus.util.StringUtils;

/**
 * Base for all JRuby mojos.
 *
 * @requiresDependencyResolution test
 */
public abstract class AbstractJRubyMojo extends AbstractMojo {

    protected boolean shouldFork = true;

    /**
     * @parameter expression="${project}"
     */
    protected MavenProject mavenProject;

    /**
     * @parameter expression="${project.basedir}"
     * @required
     */
    protected File launchDirectory;

    /**
     * The project compile classpath.
     *
     * @parameter default-value="${project.compileClasspathElements}"
     * @required
     * @readonly
     */
    protected List<String> compileClasspathElements;

    /**
     * The plugin dependencies.
     *
     * @parameter expression="${plugin.artifacts}"
     * @required
     * @readonly
     */
    protected List<Artifact> pluginArtifacts;

    /**
     * The project test classpath
     *
     * @parameter expression="${project.testClasspathElements}"
     * @required
     * @readonly
     */
    protected List<String> testClasspathElements;

    /**
     * @parameter expression="${localRepository}"
     * @required
     * @readonly
     */
    protected ArtifactRepository localRepository;

    protected Java jruby(List<String> args) throws MojoExecutionException {
        launchDirectory.mkdirs();
        Project project;
        try {
            project = getProject();
        } catch (DependencyResolutionRequiredException e) {
            throw new MojoExecutionException("error resolving dependencies", e);
        }

        Java java = new Java();
        java.setProject(project);
        java.setClassname("org.jruby.Main");
        java.setFailonerror(true);

        Commandline.Argument arg;

        if (shouldFork) {
            java.setFork(true);
            java.setDir(launchDirectory);

            for (String jvmArg : getJvmArgs()) {
                arg = java.createJvmarg();
                arg.setValue(jvmArg);
            }

            Environment.Variable classpath = new Environment.Variable();

            Path p = new Path(java.getProject());
            p.add((Path) project.getReference("maven.plugin.classpath"));
            p.add((Path) project.getReference("maven.compile.classpath"));
            p.add((Path) project.getReference("maven.test.classpath"));
            classpath.setKey("JRUBY_PARENT_CLASSPATH");
            classpath.setValue(p.toString());

            java.addEnv(classpath);
        }

        Environment.Variable gemPathVar = new Environment.Variable();
        gemPathVar.setKey("GEM_PATH");
        gemPathVar.setValue(gemHome().getAbsolutePath());
        java.addEnv(gemPathVar);

        Path p = java.createClasspath();
        p.add((Path) project.getReference("maven.plugin.classpath"));
        p.add((Path) project.getReference("maven.compile.classpath"));
        p.add((Path) project.getReference("maven.test.classpath"));
        getLog().debug("java classpath: " + p.toString());

        for (String s : args) {
            arg = java.createArg();
            arg.setValue(s);
        }

        return java;
    }

    protected abstract String[] getJvmArgs();

    /**
     * Installs a gem.
     *
     * @param gemSpec name and optional version and location separated by colon. Example:
     *                <ul>
     *                <li>name</li>
     *                <li>name:version</li>
     *                <li>name:version:github</li>
     *                </ul>
     * @throws org.apache.maven.plugin.MojoExecutionException
     *          if gem installation fails.
     */
    protected void installGem(String gemSpec) throws MojoExecutionException {
        List<String> args = new ArrayList<String>();
        args.add("-S");
        args.add("gem");
        args.add("install");
        args.add("--no-ri");
        args.add("--no-rdoc");
        args.add("--source");
        args.add("http://gems.rubyforge.org");
        args.add("--source");
        args.add("http://gems.github.com");
        args.add("--install-dir");
        args.add(gemHome().getAbsolutePath());
        args.addAll(parseGem(gemSpec));

        Java jruby = jruby(args);
        // We have to override HOME to make RubyGems install gems
        // where we want it. Setting GEM_HOME and using --install-dir
        // is not enough.
        Environment.Variable homeVar = new Environment.Variable();
        homeVar.setKey("HOME");
        homeVar.setValue(dotGemParent().getAbsolutePath());
        jruby.addEnv(homeVar);
        dotGemParent().mkdirs();
        jruby.execute();
    }

    /**
     * @param gemSpec colon separated string. See installGem.
     * @return arguments that the gem command understands.
     * @throws MojoExecutionException if gem installation fails.
{'fraction_non_alphanumeric': 0.12331858806536065, 'fraction_numerical': 0.00018055430170623815, 'mean_word_length': 2.4946372239747636, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, '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': '1795065', 'n_tokens_mistral': 3150, 'n_tokens_neox': 3020, 'n_words': 600}