<commit_before>package org.alfresco.maven.plugin.amp.packaging;

import org.alfresco.maven.plugin.amp.Overlay;
import org.alfresco.maven.plugin.amp.util.PathSet;

import org.apache.maven.model.Resource;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;

import org.codehaus.plexus.util.DirectoryScanner;
import org.codehaus.plexus.util.StringUtils;

import java.io.File;
import java.io.IOException;

/**
 * Handles the project own resources, that is:
 * <ul
 * <li>The list of web resources, if any</li>
 * <li>The content of the webapp directory if it exists</li>
 * <li>The custom deployment descriptor(s), if any</li>
 * <li>The content of the classes directory if it exists</li>
 * <li>The dependencies of the project</li>
 * </ul>
 *
 * @author <NAME>
 */
public class AmpProjectPackagingTask
    extends AbstractAmpPackagingTask
{
    private static final String MODULE_PROPERTIES = "module.properties";

	private static final String WEB_PATH = "web/";

	private static final String CONFIG_PATH = "config/";

	private Resource[] webResources = new Resource[0];

    private final File moduleProperties;
    
    private final String id;


    public AmpProjectPackagingTask( Resource[] webResource, File moduleProperties)
    {
        if ( webResources != null )
        {
            this.webResources = webResources;
        }
        this.moduleProperties = moduleProperties;
        this.id = Overlay.currentProjectInstance().getId();
    }

    public void performPackaging( AmpPackagingContext context )
        throws MojoExecutionException, MojoFailureException
    {

        context.getLog().info( "Processing amp project" );

        File metainfDir = new File( context.getAmpDirectory(), META_INF_PATH );
        
        metainfDir.mkdirs();

        handleArtifacts( context );
        
        handleWebResources( context );
        
        handleClassesDirectory( context );

        handeAmpConfigDirectory( context );

        handeWebAppSourceDirectory( context );
        
        
        // Notice: this will work only in case we are copying only this AMP or this AMP is
        // set as last overlay of the maven-amp-plugin
        handleDeploymentDescriptors(context);
        
    }


	/**
     * Handles the web resources.
     *
     * @param context the packaging context
     * @throws MojoExecutionException if a resource could not be copied
     */
    protected void handleWebResources( AmpPackagingContext context )
        throws MojoExecutionException
    {
        for ( int i = 0; i < webResources.length; i++ )
        {
            Resource resource = webResources[i];
            if ( !( new File( resource.getDirectory() ) ).isAbsolute() )
            {
                resource.setDirectory( context.getProject().getBasedir() + File.separator + resource.getDirectory() );
            }

            // Make sure that the resource directory is not the same as the webappDirectory
            if ( !resource.getDirectory().equals( context.getAmpDirectory().getPath() ) )
            {

                try
                {
                    copyResources( context, resource );
                }
                catch ( IOException e )
                {
                    throw new MojoExecutionException( "Could not copy resource[" + resource.getDirectory() + "]", e );
                }
            }
        }
    }

    /**
     * Handles the webapp sources.
     *
     * @param context the packaging context
     * @throws MojoExecutionException if the sources could not be copied
     */
    protected void handeWebAppSourceDirectory( AmpPackagingContext context )
        throws MojoExecutionException
    {
        if ( !context.getAmpWebDirectory().exists() )
        {
            context.getLog().debug( "AMP sources directory does not exist - skipping." );
        }
        else
        if ( !context.getAmpWebDirectory().getAbsolutePath().equals( context.getAmpDirectory().getPath() ) )
        {
            final PathSet sources = getFilesToIncludes( context.getAmpWebDirectory(),
                                                        context.getAmpWebIncludes(),
                                                        context.getAmpWebExcludes() );

            try
            {
            	context.getLog().info("Copying AMP web data into " + WEB_PATH);
                copyFiles( id, context, context.getAmpWebDirectory(), sources, WEB_PATH );
            }
            catch ( IOException e )
            {
                throw new MojoExecutionException(
                    "Could not copy AMP sources[" + context.getAmpDirectory().getAbsolutePath() + "]", e );
            }
        }
    }

    /**
     * Handles the webapp sources.
     *
     * @param context the packaging context
     * @throws MojoExecutionException if the sources could not be copied
     */
    protected void handeAmpConfigDirectory( AmpPackagingContext context )
        throws MojoExecutionException
    {
        if ( !context.getAmpConfigDirectory().exists() )
        {
            context.getLog().debug( "AMP config directory does not exist - skipping." );
        }
        else
        {
            if ( !context.getAmpConfigDirectory().getAbsolutePath().equals( context.getAmpDirectory().getPath() ) )
            {
                final PathSet sources = getFilesToIncludes( context.getAmpConfigDirectory(),
                                                            new String[0],
                                                            new String[] {"**/*.class"} );

                try
                {
                    context.getLog().info("packaging AMP config items into " + CONFIG_PATH);<commit_msg>Fixed issue #5 - added a hardcoded file-mappings.properties that allows overriding resources in /WEB-INF/ (which is not recommended, but may be necessary in some cases).<commit_after>package org.alfresco.maven.plugin.amp.packaging;

import org.alfresco.maven.plugin.amp.Overlay;
import org.alfresco.maven.plugin.amp.util.PathSet;

import org.apache.maven.model.Resource;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;

import org.codehaus.plexus.util.DirectoryScanner;
import org.codehaus.plexus.util.StringUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * Handles the project own resources, that is:
 * <ul
 * <li>The list of web resources, if any</li>
 * <li>The content of the webapp directory if it exists</li>
 * <li>The custom deployment descriptor(s), if any</li>
 * <li>The content of the classes directory if it exists</li>
 * <li>The dependencies of the project</li>
 * </ul>
 *
 * @author <NAME>
 */
public class AmpProjectPackagingTask
    extends AbstractAmpPackagingTask
{
    private static final String MODULE_PROPERTIES = "module.properties";
    private static final String FILE_MAPPINGS     = "file-mappings.properties";

	private static final String WEB_PATH = "web/";

	private static final String CONFIG_PATH = "config/";
	
	private static final String FILE_MAPPINGS_CONTENT = "# Add mapping for /WEB-INF/, since MMT doesn't support it by default\n" +
                                                        "/web/WEB-INF=/WEB-INF\n";


	private Resource[] webResources = new Resource[0];

    private final File moduleProperties;
    
    private final String id;


    public AmpProjectPackagingTask( Resource[] webResource, File moduleProperties)
    {
        if ( webResources != null )
        {
            this.webResources = webResources;
        }
        this.moduleProperties = moduleProperties;
        this.id = Overlay.currentProjectInstance().getId();
    }

    public void performPackaging( AmpPackagingContext context )
        throws MojoExecutionException, MojoFailureException
    {

        context.getLog().info( "Processing amp project" );

        File metainfDir = new File( context.getAmpDirectory(), META_INF_PATH );
        
        metainfDir.mkdirs();

        handleArtifacts( context );
        
        handleWebResources( context );
        
        handleClassesDirectory( context );

        handeAmpConfigDirectory( context );

        handeWebAppSourceDirectory( context );
        
        synthesiseFileMappings(context);
        
        
        // Notice: this will work only in case we are copying only this AMP or this AMP is
        // set as last overlay of the maven-amp-plugin
        handleDeploymentDescriptors(context);
        
    }


	/**
     * Handles the web resources.
     *
     * @param context the packaging context
     * @throws MojoExecutionException if a resource could not be copied
     */
    protected void handleWebResources( AmpPackagingContext context )
        throws MojoExecutionException
    {
        for ( int i = 0; i < webResources.length; i++ )
        {
            Resource resource = webResources[i];
            if ( !( new File( resource.getDirectory() ) ).isAbsolute() )
            {
                resource.setDirectory( context.getProject().getBasedir() + File.separator + resource.getDirectory() );
            }

            // Make sure that the resource directory is not the same as the webappDirectory
            if ( !resource.getDirectory().equals( context.getAmpDirectory().getPath() ) )
            {

                try
                {
                    copyResources( context, resource );
                }
                catch ( IOException e )
                {
                    throw new MojoExecutionException( "Could not copy resource[" + resource.getDirectory() + "]", e );
                }
            }
        }
    }

    /**
     * Handles the webapp sources.
     *
     * @param context the packaging context
     * @throws MojoExecutionException if the sources could not be copied
     */
    protected void handeWebAppSourceDirectory( AmpPackagingContext context )
        throws MojoExecutionException
    {
        if ( !context.getAmpWebDirectory().exists() )
        {
            context.getLog().debug( "AMP sources directory does not exist - skipping." );
        }
        else
        if ( !context.getAmpWebDirectory().getAbsolutePath().equals( context.getAmpDirectory().getPath() ) )
        {
            final PathSet sources = getFilesToIncludes( context.getAmpWebDirectory(),
                                                        context.getAmpWebIncludes(),
                                                        context.getAmpWebExcludes() );

            try
            {
            	context.getLog().info("Copying AMP web data into " + WEB_PATH);
                copyFiles( id, context, context.getAmpWebDirectory(), sources, WEB_PATH );
            }
            catch ( IOException e )
            {
                throw new MojoExecutionException(
                    "Could not copy AMP sources[" + context.getAmpDirectory().getAbsolutePath() + "]", e );
            }
        }
    }
    
    
    /**
     * Synthesizes (creates) a standard file-mappings.properties file so that resources in webapp/WEB-INF
     * get written to the correct location by the MMT.
     * 
     * @param context The packaging context
     * @throws MojoExecutionException if the file could not be created
     */
    protected void synthesiseFileMappings(final AmpPackagingContext context)
        throws MojoExecutionException
    {
        try
        {
            File         fileMappings = new File(context.getAmpDirectory(), FILE_MAPPINGS);
            OutputStream out          = null;
            
            fileMappings.createNewFile();  // Note: ignore if the file already exists - we simply overwrite its existing contents
            
            try
            {
                out = new FileOutputStream(fileMappings, false);
                
                out.write(FILE_MAPPINGS_CONTENT.getBytes());
            }
            finally
            {
                if (out != null)
                {
                    out.flush();
                    out.close();
                }
            }
        }
        catch (final IOException ioe)
        {
            throw new MojoExecutionException("Could not create file-mappings.properties in [" + context.getAmpDirectory().getAbsolutePath() + "]", ioe);
        }
    }
    

    /**
     * Handles the webapp sources.
     *
     * @param context the packaging context
     * @throws MojoExecutionException if the sources could not be copied
     */
    protected void handeAmpConfigDirectory( AmpPackagingContext context )
        throws MojoExecutionException
    {
        if ( !context.getAmpConfigDirectory().exists() )
        {
            context.getLog().debug( "AMP config directory does not exist - skipping." );
        }
        else
        {
            if ( !context.getAmpConfigDirectory().getAbsolutePath().equals( context.getAmpDirectory().getPath() ) )
            {
                final PathSet sources = getFilesToIncludes( context.getAmpConfigDirectory(),
                                                            new String[0],
                                                            new String[] {"**/*.class"} );

                try
                {
                    context.getLog().info("packaging AMP config items into " + CONFIG_PATH);
{'fraction_non_alphanumeric': 0.0886236478925774, 'fraction_numerical': 0.0005221932114882506, 'mean_word_length': 1.9232446576537288, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 27, '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': '1095491', 'n_tokens_mistral': 3432, 'n_tokens_neox': 3298, 'n_words': 891}