All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.tenderowls.opensource.haxemojos.components.HaxeCompiler Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (C) 2012 https://github.com/tenderowls/haxemojos
 *
 * 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 com.tenderowls.opensource.haxemojos.components;

import com.tenderowls.opensource.haxemojos.components.nativeProgram.NativeProgram;
import com.tenderowls.opensource.haxemojos.utils.*;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.archiver.zip.ZipUnArchiver;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.logging.Logger;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Component(role = HaxeCompiler.class)
public final class HaxeCompiler {

    @Requirement(hint = "haxe")
    private NativeProgram haxe;

    @Requirement(hint = "haxelib")
    private NativeProgram haxelib;

    @Requirement
    private Logger logger;

    private File outputDirectory;

    public void compile(MavenProject project, Map targets, String main, boolean debug, boolean includeTestSources) throws Exception
    {
        compile(project, targets, main, debug, includeTestSources, null);
    }

    public void compile(MavenProject project, Map targets, String main, boolean debug, boolean includeTestSources, ArtifactFilter artifactFilter) throws Exception
    {
        compile(project, targets, main, debug, includeTestSources, artifactFilter, null);
    }

    public void compile(MavenProject project, Map targets, String main, boolean debug, boolean includeTestSources, ArtifactFilter artifactFilter, List additionalArguments) throws Exception
    {
        List args = new ArrayList();

        for (String sourceRoot : project.getCompileSourceRoots()) {
            addSourcePath(args, sourceRoot);
        }

        if (includeTestSources) {
            for (String sourceRoot : project.getTestCompileSourceRoots()) {
                addSourcePath(args, sourceRoot);
            }
        }

        for (File resourceDir: ProjectHelper.getResourceDirectories(project)) {
            addSourcePath(args, resourceDir.getAbsolutePath());
        }

        addLibs(args, project, artifactFilter);
        addHars(args, project, targets.keySet(), artifactFilter);
        addDebug(args, debug);

        if (main != null)
            addMain(args, main);

        if (additionalArguments != null)
            args.addAll(additionalArguments);

        for (CompileTarget target : targets.keySet())
        {
            String output = targets.get(target);
            List argsClone = new ArrayList();
            argsClone.addAll(args);
            addTarget(argsClone, target);
            argsClone.add(output);

            CompilerLogger compilerLogger = new CompilerLogger(logger);
            int returnCode = haxe.execute(argsClone, compilerLogger);

            if (compilerLogger.getErrors().size() > 0)
            {
                logger.info("-------------------------------------------------------------");
                logger.error("COMPILATION ERROR :");
                logger.info("-------------------------------------------------------------");

                for (String error: compilerLogger.getErrors()) {
                    logger.error(error);
                }

                throw new Exception("Compilation failure");
            }
            else if (returnCode > 0)
            {
	        throw new Exception("Compilation failure");
            }
        }
    }

    public void setOutputDirectory(File outputDirectory)
    {
        this.outputDirectory = outputDirectory;
    }

    private void addLibs(List argumentsList, MavenProject project, ArtifactFilter artifactFilter) throws Exception
    {
        for (Artifact artifact : project.getArtifacts())
        {
            boolean filtered = artifactFilter != null && !artifactFilter.include(artifact);

            if (!filtered && artifact.getType().equals(HaxeFileExtensions.HAXELIB))
            {
                String haxelibId = artifact.getArtifactId() + ":" + artifact.getVersion();
                argumentsList.add("-lib");
                argumentsList.add(haxelibId);
            }
        }
    }

    private void addTarget(List args, CompileTarget target)
    {
        switch (target)
        {
            case java: {
                args.add("-java");
                break;
            }
            case neko: {
                args.add("-neko");
                break;
            }
            case swf:
            case swc: {
                args.add("-swf");
                break;
            }
        }
    }

    private void addDebug(List argumentsList, boolean debug)
    {
        if (debug)
            argumentsList.add("-debug");
    }

    private void addHars(List argumentsList, MavenProject project, Set targets, ArtifactFilter artifactFilter)
    {
        Map projectReferences = project.getProjectReferences();

        for (Artifact artifact: project.getArtifacts())
        {
            boolean filtered = artifactFilter != null && !artifactFilter.include(artifact);

            if (!filtered && artifact.getType().equals(HaxeFileExtensions.HAR))
            {
                String artifactKey = getProjectReferenceKey(artifact, ":");
                MavenProject reference = projectReferences.get(artifactKey);

                if (reference == null)
                {
                    File harUnpackDirectory = new File(getDependenciesDirectory(), getProjectReferenceKey(artifact, "-"));
                    unpackHar(artifact, harUnpackDirectory);
                    validateHarMetadata(targets, artifact, new File(harUnpackDirectory, HarMetadata.METADATA_FILE_NAME));
                    addSourcePath(argumentsList, harUnpackDirectory.getAbsolutePath());
                }
                else
                {
                    String dirName = OutputNamesHelper.getHarValidationOutput(artifact);
                    File validationDirectory = new File(reference.getBuild().getDirectory(), dirName);
                    validateHarMetadata(targets, artifact, new File(validationDirectory, HarMetadata.METADATA_FILE_NAME));

                    for (String cp : reference.getCompileSourceRoots()) {
                        addSourcePath(argumentsList, cp);
                    }
                }
            }
        }
    }

    private File getDependenciesDirectory()
    {
        File dependenciesDirectory = new File(outputDirectory, "dependencies");

        if (!dependenciesDirectory.exists())
            dependenciesDirectory.mkdir();

        return dependenciesDirectory;
    }

    private void validateHarMetadata(Set targets, Artifact artifact, File metadataFile)
    {
        try
        {
            JAXBContext jaxbContext = JAXBContext.newInstance(HarMetadata.class, CompileTarget.class);
            HarMetadata metadata = (HarMetadata) jaxbContext.createUnmarshaller().unmarshal(metadataFile);

            if (!metadata.target.containsAll(targets))
                logger.warn("Dependency " + artifact + " is not compatible with your compile targets.");
        }
        catch (JAXBException e)
        {
            logger.warn("Can't read " + artifact + " metadata", e);
        }

    }

    private void unpackHar(Artifact artifact, File harUnpackDirectory)
    {
        if (!harUnpackDirectory.exists())
        {
            harUnpackDirectory.mkdir();
            ZipUnArchiver unArchiver = new ZipUnArchiver();
            unArchiver.enableLogging(logger);
            unArchiver.setSourceFile(artifact.getFile());
            unArchiver.setDestDirectory(harUnpackDirectory);
            unArchiver.extract();
        }
    }

    private String getProjectReferenceKey(Artifact artifact, String separator)
    {
        StringBuilder sb = new StringBuilder();
        sb.append(artifact.getGroupId());
        sb.append(separator);
        sb.append(artifact.getArtifactId());
        sb.append(separator);
        sb.append(artifact.getVersion());

        return sb.toString();
    }


    private void addMain(List argumentsList, String main)
    {
        argumentsList.add("-main");
        argumentsList.add(main);
    }

    private void addSourcePath(List argumentsList, String sourcePath)
    {
        argumentsList.add("-cp");
        argumentsList.add(sourcePath);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy