<commit_before>package de.saumya.mojo.gems;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.maven.model.Dependency;
import org.apache.maven.model.Developer;
import org.apache.maven.model.License;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.context.Context;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.velocity.VelocityComponent;

import de.saumya.mojo.gems.gem.Gem;
import de.saumya.mojo.gems.gem.GemPackager;
import de.saumya.mojo.gems.spec.GemDependency;
import de.saumya.mojo.gems.spec.GemRequirement;
import de.saumya.mojo.gems.spec.GemSpecification;
import de.saumya.mojo.gems.spec.GemVersion;

/**
 * This is full of "workarounds" here, since for true artifact2gem conversion I
 * would need interpolated POM!
 * 
 * @author cstamas
 * @author mkristian
 */
@Component(role = MavenArtifactConverter.class)
public class DefaultMavenArtifactConverter implements MavenArtifactConverter {
    private static final String LIB_MAVEN_PATH = "lib/maven/";

    enum RubyDependencyType {

        RUNTIME, DEVELOPMENT;

        @Override
        public String toString() {
            return ":" + name().toLowerCase();
        }

        public static RubyDependencyType toRubyDependencyType(
                final String dependencyScope) {
            // ruby scopes
            // :development
            // :runtime
            if ("provided".equals(dependencyScope)
                    || "test".equals(dependencyScope)) {
                return DEVELOPMENT;
            }
            else if ("compile".equals(dependencyScope)
                    || "runtime".equals(dependencyScope)) {
                return RUNTIME;
            }
            else
            // dependencyScope: "system"
            {
                // TODO better throw an exception since there will be no gem for
                // such a dependency or something else
                return RUNTIME;
            }

        }
    }

    /**
     * The Java platform key.
     */
    String                                  PLATFORM_JAVA             = "java";

    @Requirement
    private GemPackager                     gemPackager;

    @Requirement
    private VelocityComponent               velocityComponent;

    private final Maven2GemVersionConverter maven2GemVersionConverter = new Maven2GemVersionConverter();

    public boolean canConvert(final MavenArtifact artifact) {
        // TODO: this is where we filter currently what to convert.
        // for now, we convert only POMs with packaging "pom", or "jar" (but we
        // ensure there is the primary artifact JAR
        // also
        // RELAXING: doing "pom" packagings but also anything that has primary
        // artifact ending with ".jar".
        if (canConvert(artifact, "pom", null)) {
            return true;
        }

        if (canConvert(artifact, artifact.getPom().getPackaging(), "jar")) {
            return true;
        }

        return false;
    }

    private boolean canConvert(final MavenArtifact artifact,
            final String packaging, final String extension) {
        String fixedExtension = null;

        if (extension != null) {
            fixedExtension = extension.startsWith(".") ? extension : "."
                    + extension;
        }

        return StringUtils.equals(packaging, artifact.getPom().getPackaging())
                && ((extension == null && artifact.getArtifactFile() == null) || (extension != null
                        && artifact.getArtifactFile() != null && artifact.getArtifactFile()
                        .getName()
                        .endsWith(fixedExtension)));
    }

    public String getGemFileName(final MavenArtifact artifact) {
        return getGemFileName(artifact.getCoordinates().getGroupId(),
                              artifact.getCoordinates().getArtifactId(),
                              artifact.getCoordinates().getVersion(),
                              this.PLATFORM_JAVA);
    }

    public GemSpecification createSpecification(final MavenArtifact artifact) {
        final GemSpecification result = new GemSpecification();

        // this is fix
        result.setPlatform(this.PLATFORM_JAVA);

        // the must ones
        result.setName(createGemName(artifact.getCoordinates().getGroupId(),
                                     artifact.getCoordinates().getArtifactId(),
                                     artifact.getCoordinates().getVersion()));
        result.setVersion(new GemVersion(createGemVersion(artifact.getCoordinates()
                .getVersion())));

        // dependencies
        if (artifact.getPom().getDependencies().size() > 0) {
            for (final Dependency dependency : artifact.getPom()
                    .getDependencies()) {
                result.getDependencies().add(convertDependency(artifact,
                                                               dependency));
            }
        }

        // and other stuff "nice to have"
        result.setDate(new Date()); // now
        result.setDescription(sanitizeStringValue(artifact.getPom()
                .getDescription() != null
                ? artifact.getPom().getDescription()
                : artifact.getPom().getName()));
        result.setSummary(sanitizeStringValue(artifact.getPom().getName()));
        result.setHomepage(sanitizeStringValue(artifact.getPom().getUrl()));

        if (artifact.getPom().getLicenses().size() > 0) {
            for (final License license : artifact.getPom().getLicenses()) {
                result.getLicenses().add(sanitizeStringValue(license.getName()
                        + " (" + license.getUrl() + ")"));
            }
        }
        if (artifact.getPom().getDevelopers().size() > 0) {
            for (final Developer developer : artifact.getPom().getDevelopers()) {
                result.getAuthors().add(sanitizeStringValue(developer.getName()
                        + " (" + developer.getEmail() + ")"));
            }
        }
<commit_msg>omit optional dependencies<commit_after>package de.saumya.mojo.gems;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.maven.model.Dependency;
import org.apache.maven.model.Developer;
import org.apache.maven.model.License;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.context.Context;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.velocity.VelocityComponent;

import de.saumya.mojo.gems.gem.Gem;
import de.saumya.mojo.gems.gem.GemPackager;
import de.saumya.mojo.gems.spec.GemDependency;
import de.saumya.mojo.gems.spec.GemRequirement;
import de.saumya.mojo.gems.spec.GemSpecification;
import de.saumya.mojo.gems.spec.GemVersion;

/**
 * This is full of "workarounds" here, since for true artifact2gem conversion I
 * would need interpolated POM!
 * 
 * @author cstamas
 * @author mkristian
 */
@Component(role = MavenArtifactConverter.class)
public class DefaultMavenArtifactConverter implements MavenArtifactConverter {
    private static final String LIB_MAVEN_PATH = "lib/maven/";

    enum RubyDependencyType {

        RUNTIME, DEVELOPMENT;

        @Override
        public String toString() {
            return ":" + name().toLowerCase();
        }

        public static RubyDependencyType toRubyDependencyType(
                final String dependencyScope) {
            // ruby scopes
            // :development
            // :runtime
            if ("provided".equals(dependencyScope)
                    || "test".equals(dependencyScope)) {
                return DEVELOPMENT;
            }
            else if ("compile".equals(dependencyScope)
                    || "runtime".equals(dependencyScope)) {
                return RUNTIME;
            }
            else
            // dependencyScope: "system"
            {
                // TODO better throw an exception since there will be no gem for
                // such a dependency or something else
                return RUNTIME;
            }

        }
    }

    /**
     * The Java platform key.
     */
    String                                  PLATFORM_JAVA             = "java";

    @Requirement
    private GemPackager                     gemPackager;

    @Requirement
    private VelocityComponent               velocityComponent;

    private final Maven2GemVersionConverter maven2GemVersionConverter = new Maven2GemVersionConverter();

    public boolean canConvert(final MavenArtifact artifact) {
        // TODO: this is where we filter currently what to convert.
        // for now, we convert only POMs with packaging "pom", or "jar" (but we
        // ensure there is the primary artifact JAR
        // also
        // RELAXING: doing "pom" packagings but also anything that has primary
        // artifact ending with ".jar".
        if (canConvert(artifact, "pom", null)) {
            return true;
        }

        if (canConvert(artifact, artifact.getPom().getPackaging(), "jar")) {
            return true;
        }

        return false;
    }

    private boolean canConvert(final MavenArtifact artifact,
            final String packaging, final String extension) {
        String fixedExtension = null;

        if (extension != null) {
            fixedExtension = extension.startsWith(".") ? extension : "."
                    + extension;
        }

        return StringUtils.equals(packaging, artifact.getPom().getPackaging())
                && ((extension == null && artifact.getArtifactFile() == null) || (extension != null
                        && artifact.getArtifactFile() != null && artifact.getArtifactFile()
                        .getName()
                        .endsWith(fixedExtension)));
    }

    public String getGemFileName(final MavenArtifact artifact) {
        return getGemFileName(artifact.getCoordinates().getGroupId(),
                              artifact.getCoordinates().getArtifactId(),
                              artifact.getCoordinates().getVersion(),
                              this.PLATFORM_JAVA);
    }

    public GemSpecification createSpecification(final MavenArtifact artifact) {
        final GemSpecification result = new GemSpecification();

        // this is fix
        result.setPlatform(this.PLATFORM_JAVA);

        // the must ones
        result.setName(createGemName(artifact.getCoordinates().getGroupId(),
                                     artifact.getCoordinates().getArtifactId(),
                                     artifact.getCoordinates().getVersion()));
        result.setVersion(new GemVersion(createGemVersion(artifact.getCoordinates()
                .getVersion())));

        // dependencies
        if (artifact.getPom().getDependencies().size() > 0) {
            for (final Dependency dependency : artifact.getPom()
                    .getDependencies()) {
                if (!dependency.isOptional()) {
                    result.getDependencies().add(convertDependency(artifact,
                                                                   dependency));
                }
            }
        }

        // and other stuff "nice to have"
        result.setDate(new Date()); // now
        result.setDescription(sanitizeStringValue(artifact.getPom()
                .getDescription() != null
                ? artifact.getPom().getDescription()
                : artifact.getPom().getName()));
        result.setSummary(sanitizeStringValue(artifact.getPom().getName()));
        result.setHomepage(sanitizeStringValue(artifact.getPom().getUrl()));

        if (artifact.getPom().getLicenses().size() > 0) {
            for (final License license : artifact.getPom().getLicenses()) {
                result.getLicenses().add(sanitizeStringValue(license.getName()
                        + " (" + license.getUrl() + ")"));
            }
        }
        if (artifact.getPom().getDevelopers().size() > 0) {
            for (final Developer developer : artifact.getPom().getDevelopers()) {
                result.getAuthors().add(sanitizeStringValue(developer.getName()
                        + " (" + developer.getEmail() + ")"));
            }
        }

{'fraction_non_alphanumeric': 0.10422996027109138, 'fraction_numerical': 0.0010905974916257692, 'mean_word_length': 2.0421800947867297, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3406128', 'n_tokens_mistral': 3478, 'n_tokens_neox': 3217, 'n_words': 682}