<commit_before>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.tool.extension.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Model;
import org.apache.maven.model.building.ModelBuildingRequest;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.DefaultProjectBuildingRequest;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuilder;
import org.apache.maven.project.ProjectBuildingException;
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.project.ProjectBuildingResult;
import org.xwiki.component.embed.EmbeddableComponentManager;
import org.xwiki.component.manager.ComponentLookupException;
import org.xwiki.component.util.DefaultParameterizedType;
import org.xwiki.extension.Extension;
import org.xwiki.extension.internal.ExtensionUtils;
import org.xwiki.extension.internal.converter.ExtensionIdConverter;
import org.xwiki.extension.repository.internal.ExtensionSerializer;
import org.xwiki.extension.repository.internal.local.DefaultLocalExtension;
import org.xwiki.properties.converter.Converter;
import org.xwiki.tool.extension.ExtensionOverride;

/**
 * Base class for Maven plugins manipulating extensions.
 * 
 * @version $Id$
 * @since 8.4RC1
 */
public abstract class AbstractExtensionMojo extends AbstractMojo
{
    /**
     * The current Maven session being executed.
     */
    @Parameter(defaultValue = "${session}", required = true, readonly = true)
    private MavenSession session;

    /**
     * Project builder -- builds a model from a pom.xml.
     */
    @Component
    protected ProjectBuilder projectBuilder;

    @Parameter
    private List<ExtensionOverride> extensionOverrides;

    protected ExtensionSerializer extensionSerializer;

    protected Converter<Extension> extensionConverter;

    protected void initializeComponents() throws MojoExecutionException
    {
        // Initialize ComponentManager
        EmbeddableComponentManager componentManager = new EmbeddableComponentManager();
        componentManager.initialize(this.getClass().getClassLoader());

        // Initialize components
        try {
            this.extensionSerializer = componentManager.getInstance(ExtensionSerializer.class);
            this.extensionConverter =
                componentManager.getInstance(new DefaultParameterizedType(null, Converter.class, Extension.class));
        } catch (ComponentLookupException e) {
            throw new MojoExecutionException("Failed to load components", e);
        }
    }

    protected MavenProject getMavenProject(Artifact artifact) throws MojoExecutionException
    {
        try {
            ProjectBuildingRequest request = new DefaultProjectBuildingRequest(this.session.getProjectBuildingRequest())
                // We don't want to execute any plugin here
                .setProcessPlugins(false)
                // It's not this plugin job to validate this pom.xml
                .setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL)
                // Use the repositories configured for the built project instead of the default Maven ones
                .setRemoteRepositories(this.session.getCurrentProject().getRemoteArtifactRepositories());
            // Note: build() will automatically get the POM artifact corresponding to the passed artifact.
            ProjectBuildingResult result = this.projectBuilder.build(artifact, request);
            return result.getProject();
        } catch (ProjectBuildingException e) {
            throw new MojoExecutionException(String.format("Failed to build project for [%s]", artifact), e);
        }
    }

    protected Extension toExtension(Artifact artifact) throws MojoExecutionException
    {
        MavenProject mavenProject = getMavenProject(artifact);

        return toExtension(mavenProject.getModel());
    }

    protected Extension toExtension(Model model)
    {
        return this.extensionConverter.convert(Extension.class, model);
    }

    protected void saveExtension(File path, Artifact artifact)
        throws MojoExecutionException, IOException, ParserConfigurationException, TransformerException
    {
        // Get MavenProject instance
        MavenProject mavenProject = getMavenProject(artifact);

        saveExtension(path, mavenProject.getModel());
    }

    protected void saveExtension(File path, Model model)
        throws IOException, ParserConfigurationException, TransformerException
    {
        // Get Extension instance
        Extension mavenExtension = this.extensionConverter.convert(Extension.class, model);
        DefaultLocalExtension extension = new DefaultLocalExtension(null, mavenExtension);

        if (!path.exists()) {
            // Apply overrides
            override(extension);

            // Save the Extension descriptor
            try (FileOutputStream stream = new FileOutputStream(path)) {
                this.extensionSerializer.saveExtensionDescriptor(extension, stream);
            }
        }
    }

    protected void override(DefaultLocalExtension extension)
    {
        for (ExtensionOverride extensionOverride : this.extensionOverrides) {
            String id = extensionOverride.get(Extension.FIELD_ID);
            if (id != null) {
                // Override features
                String featuresString = extensionOverride.get(Extension.FIELD_FEATURES);
                if (featuresString != null) {
                    Collection<String> features = ExtensionUtils.importPropertyStringList(featuresString, true);
                    extension.setExtensionFeatures(
                        ExtensionIdConverter.toExtensionIdList(features, extension.getId().getVersion()));
                }
            }
        }
    }

    protected void saveExtension(Artifact artifact, File directory) throws MojoExecutionException
    {
        // Get path
        File path = new File(directory, artifact.getArtifactId() + '-' + artifact.getVersion() + ".xed");

        try {
            saveExtension(path, artifact);
        } catch (Exception e) {
            throw new MojoExecutionException("Failed to write descriptor for artifact [" + artifact + "]", e);
        }
    }

    protected void saveExtensions(Collection<Artifact> artifacts, File directory, String type)
        throws MojoExecutionException
    {<commit_msg>XCOMMONS-1098: Introduce Maven plugin to generate extension descriptos in a WAR<commit_after>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.tool.extension.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Model;
import org.apache.maven.model.building.ModelBuildingRequest;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.DefaultProjectBuildingRequest;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuilder;
import org.apache.maven.project.ProjectBuildingException;
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.project.ProjectBuildingResult;
import org.xwiki.component.embed.EmbeddableComponentManager;
import org.xwiki.component.manager.ComponentLookupException;
import org.xwiki.component.util.DefaultParameterizedType;
import org.xwiki.extension.AbstractExtension;
import org.xwiki.extension.Extension;
import org.xwiki.extension.internal.ExtensionUtils;
import org.xwiki.extension.internal.converter.ExtensionIdConverter;
import org.xwiki.extension.repository.internal.ExtensionSerializer;
import org.xwiki.extension.repository.internal.local.DefaultLocalExtension;
import org.xwiki.properties.converter.Converter;
import org.xwiki.tool.extension.ExtensionOverride;

/**
 * Base class for Maven plugins manipulating extensions.
 * 
 * @version $Id$
 * @since 8.4RC1
 */
public abstract class AbstractExtensionMojo extends AbstractMojo
{
    /**
     * The current Maven session being executed.
     */
    @Parameter(defaultValue = "${session}", required = true, readonly = true)
    protected MavenSession session;

    /**
     * Project builder -- builds a model from a pom.xml.
     */
    @Component
    protected ProjectBuilder projectBuilder;

    @Parameter
    protected List<ExtensionOverride> extensionOverrides;

    protected ExtensionSerializer extensionSerializer;

    protected Converter<Extension> extensionConverter;

    protected void initializeComponents() throws MojoExecutionException
    {
        // Initialize ComponentManager
        EmbeddableComponentManager componentManager = new EmbeddableComponentManager();
        componentManager.initialize(this.getClass().getClassLoader());

        // Initialize components
        try {
            this.extensionSerializer = componentManager.getInstance(ExtensionSerializer.class);
            this.extensionConverter =
                componentManager.getInstance(new DefaultParameterizedType(null, Converter.class, Extension.class));
        } catch (ComponentLookupException e) {
            throw new MojoExecutionException("Failed to load components", e);
        }
    }

    protected MavenProject getMavenProject(Artifact artifact) throws MojoExecutionException
    {
        try {
            ProjectBuildingRequest request = new DefaultProjectBuildingRequest(this.session.getProjectBuildingRequest())
                // We don't want to execute any plugin here
                .setProcessPlugins(false)
                // It's not this plugin job to validate this pom.xml
                .setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL)
                // Use the repositories configured for the built project instead of the default Maven ones
                .setRemoteRepositories(this.session.getCurrentProject().getRemoteArtifactRepositories());
            // Note: build() will automatically get the POM artifact corresponding to the passed artifact.
            ProjectBuildingResult result = this.projectBuilder.build(artifact, request);
            return result.getProject();
        } catch (ProjectBuildingException e) {
            throw new MojoExecutionException(String.format("Failed to build project for [%s]", artifact), e);
        }
    }

    protected Extension toExtension(Artifact artifact) throws MojoExecutionException
    {
        MavenProject mavenProject = getMavenProject(artifact);

        return toExtension(mavenProject.getModel());
    }

    protected Extension toExtension(Model model)
    {
        return this.extensionConverter.convert(Extension.class, model);
    }

    protected void saveExtension(File path, Artifact artifact)
        throws MojoExecutionException, IOException, ParserConfigurationException, TransformerException
    {
        // Get MavenProject instance
        MavenProject mavenProject = getMavenProject(artifact);

        saveExtension(path, mavenProject.getModel());
    }

    protected void saveExtension(File path, Model model)
        throws IOException, ParserConfigurationException, TransformerException
    {
        // Get Extension instance
        Extension mavenExtension = this.extensionConverter.convert(Extension.class, model);
        AbstractExtension mutableExtension;
        if (mavenExtension instanceof AbstractExtension) {
            mutableExtension = (AbstractExtension) mavenExtension;
        } else {
            mutableExtension = new DefaultLocalExtension(null, mavenExtension);
        }

        if (!path.exists()) {
            // Apply overrides
            override(mutableExtension);

            // Save the Extension descriptor
            try (FileOutputStream stream = new FileOutputStream(path)) {
                this.extensionSerializer.saveExtensionDescriptor(mavenExtension, stream);
            }
        }
    }

    protected void override(AbstractExtension extension)
    {
        if (this.extensionOverrides != null) {
            for (ExtensionOverride extensionOverride : this.extensionOverrides) {
                String id = extensionOverride.get(Extension.FIELD_ID);
                if (id != null) {
                    // Override features
                    String featuresString = extensionOverride.get(Extension.FIELD_FEATURES);
                    if (featuresString != null) {
                        Collection<String> features = ExtensionUtils.importPropertyStringList(featuresString, true);
                        extension.setExtensionFeatures(
                            ExtensionIdConverter.toExtensionIdList(features, extension.getId().getVersion()));
                    }
                }
            }
        }
    }

    protected void saveExtension(Artifact artifact, File directory) throws MojoExecutionException
    {
        // Get path
        File path = new File(directory, artifact.getArtifactId() + '-' + artifact.getVersion() + ".xed");

        try {
            saveExtension(path, artifact);
        } catch (Exception e) {
            throw new MojoExecutionException("Failed to write descriptor for artifact [" + artifact + "]", e);
        }
    }

    protected void saveExtensions(Collection<Artifact> artifacts, File directory, String type)
        throws MojoExecutionException
    {
{'fraction_non_alphanumeric': 0.07770676453837878, 'fraction_numerical': 0.0022780484718091503, 'mean_word_length': 3.2713513513513512, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 8}, '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': '1380243', 'n_tokens_mistral': 3843, 'n_tokens_neox': 3657, 'n_words': 1044}