<commit_before>/*-
 * Copyright (c) 2012 Red Hat, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.fedoraproject.maven.rpminstall.plugin;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
import org.apache.maven.model.Parent;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.PluginExecution;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
import org.codehaus.plexus.util.xml.XMLWriter;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.fedoraproject.maven.Configuration;
import org.fedoraproject.maven.Rule;

@Mojo( name = "install", aggregator = true, requiresDependencyResolution = ResolutionScope.NONE )
public class InstallMojo
    extends AbstractMojo
{
    @Parameter( defaultValue = "${project}", readonly = true, required = true )
    private MavenProject rootProject;

    @Parameter( defaultValue = "${reactorProjects}", readonly = true, required = true )
    private List<MavenProject> reactorProjects;

    private static Model getRawModel( MavenProject project )
        throws MojoExecutionException
    {
        try
        {
            try (Reader reader = new FileReader( project.getFile() ))
            {
                return new MavenXpp3Reader().read( reader );
            }
        }
        catch ( XmlPullParserException | IOException e )
        {
            throw new MojoExecutionException( "Failed to read POM", e );
        }
    }

    private void writeSimpleEffectiveModel( Path path, Model pom )
        throws MojoExecutionException
    {
        pom.setParent( null );

        try
        {
            Writer sWriter = new StringWriter();
            MavenXpp3Writer pomWriter = new MavenXpp3Writer();
            pomWriter.write( sWriter, pom );

            try (Writer fWriter = new FileWriter( path.toFile() ))
            {
                XMLWriter writer = new PrettyPrintXMLWriter( fWriter, "  ", "UTF-8", null );
                writer.writeMarkup( sWriter.toString().replaceAll( "<\\?xml[^>]+\\?>", "" ) );
            }
        }
        catch ( IOException e )
        {
            throw new MojoExecutionException( "Failed to write effective POM", e );
        }
    }

    private void generateEffectiveRequires( Model pom, Package targetPackage )
    {
        for ( Dependency dep : pom.getDependencies() )
        {
            String scope = dep.getScope();
            if ( scope.equals( "compile" ) || scope.equals( "runtime" ) || scope.equals( "provided" ) )
                targetPackage.addRequires( dep.getGroupId(), dep.getArtifactId() );
        }
    }

    private void generateRawRequires( Model pom, Package targetPackage )
    {
        Parent parent = pom.getParent();
        if ( parent != null )
        {
            String groupId = parent.getGroupId();
            if ( groupId == null )
                groupId = pom.getGroupId();
            targetPackage.addDevelRequires( groupId, parent.getArtifactId() );
        }

        if ( pom.getPackaging().equals( "pom" ) && pom.getBuild() != null )
        {
            for ( Plugin plugin : pom.getBuild().getPlugins() )
            {
                String groupId = plugin.getGroupId();
                if ( groupId == null )
                    groupId = "org.apache.maven.plugins";
                targetPackage.addDevelRequires( groupId, plugin.getArtifactId() );
            }<commit_msg>Don't hardcode dependency scopes<commit_after>/*-
 * Copyright (c) 2012 Red Hat, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.fedoraproject.maven.rpminstall.plugin;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
import org.apache.maven.model.Parent;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.PluginExecution;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
import org.codehaus.plexus.util.xml.XMLWriter;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.fedoraproject.maven.Configuration;
import org.fedoraproject.maven.Rule;

@Mojo( name = "install", aggregator = true, requiresDependencyResolution = ResolutionScope.NONE )
public class InstallMojo
    extends AbstractMojo
{
    @Parameter( defaultValue = "${project}", readonly = true, required = true )
    private MavenProject rootProject;

    @Parameter( defaultValue = "${reactorProjects}", readonly = true, required = true )
    private List<MavenProject> reactorProjects;

    // List of dependency scopes for which auto-requires are generated. Must be in ascending order.
    private static final String[] dependencyScopes = new String[] { "compile", "provided", "runtime" };

    private static Model getRawModel( MavenProject project )
        throws MojoExecutionException
    {
        try
        {
            try (Reader reader = new FileReader( project.getFile() ))
            {
                return new MavenXpp3Reader().read( reader );
            }
        }
        catch ( XmlPullParserException | IOException e )
        {
            throw new MojoExecutionException( "Failed to read POM", e );
        }
    }

    private void writeSimpleEffectiveModel( Path path, Model pom )
        throws MojoExecutionException
    {
        pom.setParent( null );

        try
        {
            Writer sWriter = new StringWriter();
            MavenXpp3Writer pomWriter = new MavenXpp3Writer();
            pomWriter.write( sWriter, pom );

            try (Writer fWriter = new FileWriter( path.toFile() ))
            {
                XMLWriter writer = new PrettyPrintXMLWriter( fWriter, "  ", "UTF-8", null );
                writer.writeMarkup( sWriter.toString().replaceAll( "<\\?xml[^>]+\\?>", "" ) );
            }
        }
        catch ( IOException e )
        {
            throw new MojoExecutionException( "Failed to write effective POM", e );
        }
    }

    private void generateEffectiveRequires( Model pom, Package targetPackage )
    {
        for ( Dependency dep : pom.getDependencies() )
        {
            String scope = dep.getScope();
            if ( Arrays.binarySearch( dependencyScopes, scope ) >= 0 )
                targetPackage.addRequires( dep.getGroupId(), dep.getArtifactId() );
        }
    }

    private void generateRawRequires( Model pom, Package targetPackage )
    {
        Parent parent = pom.getParent();
        if ( parent != null )
        {
            String groupId = parent.getGroupId();
            if ( groupId == null )
                groupId = pom.getGroupId();
            targetPackage.addDevelRequires( groupId, parent.getArtifactId() );
        }

        if ( pom.getPackaging().equals( "pom" ) && pom.getBuild() != null )
        {
            for ( Plugin plugin : pom.getBuild().getPlugins() )
            {
                String groupId = plugin.getGroupId();
                if ( groupId == null )
                    groupId = "org.apache.maven.plugins";
                targetPackage.addDevelRequires( groupId, plugin.getArtifactId() );
            }
{'fraction_non_alphanumeric': 0.10412926391382406, 'fraction_numerical': 0.0034909235986435266, 'mean_word_length': 2.7852019630049076, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 10}, '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': '5110625', 'n_tokens_mistral': 2879, 'n_tokens_neox': 2769, 'n_words': 709}