Search in sources :

Example 1 with SuffixMapping

use of org.codehaus.plexus.compiler.util.scan.mapping.SuffixMapping in project st-js by st-js.

the class AbstractSTJSMojo method packFiles.

/**
 * packs all the files in a single file
 *
 * @param generator
 *            a {@link org.stjs.generator.Generator} object.
 * @param gendir
 *            a {@link org.stjs.generator.GenerationDirectory} object.
 * @throws org.apache.maven.plugin.MojoFailureException
 * @throws org.apache.maven.plugin.MojoExecutionException
 */
protected void packFiles(Generator generator, GenerationDirectory gendir) throws MojoFailureException, MojoExecutionException {
    if (!pack) {
        return;
    }
    OutputStream allSourcesFile = null;
    Writer packMapStream = null;
    ClassLoader builtProjectClassLoader = getBuiltProjectClassLoader();
    Map<String, File> currentProjectsFiles = new HashMap<String, File>();
    // pack the files
    try {
        DirectedGraph<String, DefaultEdge> dependencyGraph = new DefaultDirectedGraph<String, DefaultEdge>(DefaultEdge.class);
        File outputFile = new File(gendir.getGeneratedSourcesAbsolutePath(), project.getArtifactId() + ".js");
        allSourcesFile = new BufferedOutputStream(new FileOutputStream(outputFile));
        for (String sourceRoot : getCompileSourceRoots()) {
            File sourceDir = new File(sourceRoot);
            List<File> sources = new ArrayList<File>();
            SourceMapping mapping = new SuffixMapping(".java", ".js");
            SourceMapping stjsMapping = new SuffixMapping(".java", ".stjs");
            // take all the files
            sources = accumulateSources(gendir, sourceDir, mapping, stjsMapping, Integer.MIN_VALUE);
            for (File source : sources) {
                File absoluteTarget = (File) mapping.getTargetFiles(gendir.getGeneratedSourcesAbsolutePath(), source.getPath()).iterator().next();
                String className = getClassNameForSource(source.getPath());
                if (!absoluteTarget.exists()) {
                    getLog().debug(className + " is a bridge. Don't add it to the pack file");
                    continue;
                }
                // add this file to the hashmap to know that this class is part of the project
                currentProjectsFiles.put(className, absoluteTarget);
                if (getLog().isDebugEnabled()) {
                    getLog().debug("Packing " + absoluteTarget);
                }
                ClassWithJavascript cjs = generator.getExistingStjsClass(builtProjectClassLoader, builtProjectClassLoader.loadClass(className));
                dependencyGraph.addVertex(className);
                for (Map.Entry<ClassWithJavascript, DependencyType> dep : cjs.getDirectDependencyMap().entrySet()) {
                    if (dep.getKey() instanceof STJSClass) {
                        dependencyGraph.addVertex(dep.getKey().getJavaClassName());
                        if (dep.getValue() != DependencyType.OTHER) {
                            dependencyGraph.addEdge(dep.getKey().getJavaClassName(), className);
                        }
                    }
                }
            }
        }
        // check for cycles
        detectCycles(dependencyGraph);
        // dump all the files in the dependency order in the pack file
        SourceMapGeneratorV3 packSourceMap = (SourceMapGeneratorV3) SourceMapGeneratorFactory.getInstance(SourceMapFormat.V3);
        int currentLine = 0;
        Iterator<String> it = new TopologicalOrderIterator<String, DefaultEdge>(dependencyGraph);
        while (it.hasNext()) {
            File targetFile = currentProjectsFiles.get(it.next());
            // target file is absolute
            if (targetFile != null) {
                // for this project's files
                if (generateSourceMap) {
                    currentLine = SourceMapUtils.appendFileSkipSourceMap(gendir.getGeneratedSourcesAbsolutePath(), allSourcesFile, targetFile, currentLine, packSourceMap, sourceEncoding);
                } else {
                    Files.copy(targetFile, allSourcesFile);
                }
                allSourcesFile.flush();
            }
        }
        if (generateSourceMap) {
            File packMapFile = new File(gendir.getGeneratedSourcesAbsolutePath(), project.getArtifactId() + ".map");
            packMapStream = new BufferedWriter(new FileWriter(packMapFile));
            packSourceMap.appendTo(packMapStream, project.getArtifactId() + ".js");
            allSourcesFile.write(("//# sourceMappingURL=" + project.getArtifactId() + ".map\n").getBytes());
            allSourcesFile.flush();
        }
    } catch (Exception ex) {
        throw new MojoFailureException("Error when packing files:" + ex.getMessage(), ex);
    } finally {
        try {
            Closeables.close(allSourcesFile, true);
        } catch (IOException e) {
            LOG.log(Level.SEVERE, "IOException should not have been thrown.", e);
        }
        try {
            Closeables.close(packMapStream, true);
        } catch (IOException e) {
            LOG.log(Level.SEVERE, "IOException should not have been thrown.", e);
        }
    }
}
Also used : HashMap(java.util.HashMap) DefaultDirectedGraph(org.jgrapht.graph.DefaultDirectedGraph) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) TopologicalOrderIterator(org.jgrapht.traverse.TopologicalOrderIterator) STJSClass(org.stjs.generator.STJSClass) SuffixMapping(org.codehaus.plexus.compiler.util.scan.mapping.SuffixMapping) SourceMapGeneratorV3(com.google.debugging.sourcemap.SourceMapGeneratorV3) BufferedWriter(java.io.BufferedWriter) DependencyType(org.stjs.generator.name.DependencyType) URLClassLoader(java.net.URLClassLoader) BufferedOutputStream(java.io.BufferedOutputStream) MojoFailureException(org.apache.maven.plugin.MojoFailureException) DefaultEdge(org.jgrapht.graph.DefaultEdge) IOException(java.io.IOException) MultipleFileGenerationException(org.stjs.generator.MultipleFileGenerationException) DependencyResolutionRequiredException(org.apache.maven.artifact.DependencyResolutionRequiredException) JavascriptFileGenerationException(org.stjs.generator.JavascriptFileGenerationException) IOException(java.io.IOException) InclusionScanException(org.codehaus.plexus.compiler.util.scan.InclusionScanException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) FileOutputStream(java.io.FileOutputStream) ClassWithJavascript(org.stjs.generator.ClassWithJavascript) File(java.io.File) SourceMapping(org.codehaus.plexus.compiler.util.scan.mapping.SourceMapping) Map(java.util.Map) HashMap(java.util.HashMap) Writer(java.io.Writer) BufferedWriter(java.io.BufferedWriter) FileWriter(java.io.FileWriter)

Example 2 with SuffixMapping

use of org.codehaus.plexus.compiler.util.scan.mapping.SuffixMapping in project st-js by st-js.

the class AbstractSTJSMojo method execute.

/**
 * {@inheritDoc}
 */
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    GenerationDirectory gendir = getGeneratedSourcesDirectory();
    long t1 = System.currentTimeMillis();
    getLog().info("Generating JavaScript files to " + gendir.getGeneratedSourcesAbsolutePath());
    ClassLoader builtProjectClassLoader = getBuiltProjectClassLoader();
    GeneratorConfigurationBuilder configBuilder = new GeneratorConfigurationBuilder();
    configBuilder.generateArrayHasOwnProperty(generateArrayHasOwnProperty);
    configBuilder.generateSourceMap(generateSourceMap);
    if (sourceEncoding != null) {
        configBuilder.sourceEncoding(sourceEncoding);
    }
    // configBuilder.allowedPackage("org.stjs.javascript");
    configBuilder.allowedPackage("org.junit");
    if (allowedPackages != null) {
        configBuilder.allowedPackages(allowedPackages);
    }
    if (annotations != null) {
        configBuilder.annotations(annotations);
    }
    // scan all the packages
    for (String sourceRoot : getCompileSourceRoots()) {
        File sourceDir = new File(sourceRoot);
        Collection<String> packages = accumulatePackages(sourceDir);
        configBuilder.allowedPackages(packages);
    }
    configBuilder.stjsClassLoader(builtProjectClassLoader);
    configBuilder.targetFolder(getBuildOutputDirectory());
    configBuilder.generationFolder(gendir);
    GeneratorConfiguration configuration = configBuilder.build();
    Generator generator = new Generator(configuration);
    int generatedFiles = 0;
    boolean hasFailures = false;
    // scan the modified sources
    for (String sourceRoot : getCompileSourceRoots()) {
        File sourceDir = new File(sourceRoot);
        SourceMapping mapping = new SuffixMapping(".java", ".js");
        SourceMapping stjsMapping = new SuffixMapping(".java", ".stjs");
        List<File> sources = accumulateSources(gendir, sourceDir, mapping, stjsMapping, staleMillis);
        for (File source : sources) {
            if (source.getName().equals(PACKAGE_INFO_JAVA)) {
                getLog().debug("Skipping " + source);
                continue;
            }
            File absoluteSource = new File(sourceDir, source.getPath());
            try {
                File absoluteTarget = (File) mapping.getTargetFiles(gendir.getGeneratedSourcesAbsolutePath(), source.getPath()).iterator().next();
                if (getLog().isDebugEnabled()) {
                    getLog().debug("Generating " + absoluteTarget);
                }
                buildContext.removeMessages(absoluteSource);
                if (!absoluteTarget.getParentFile().exists() && !absoluteTarget.getParentFile().mkdirs()) {
                    getLog().error("Cannot create output directory:" + absoluteTarget.getParentFile());
                    continue;
                }
                String className = getClassNameForSource(source.getPath());
                ClassWithJavascript stjsClass = generator.generateJavascript(className, sourceDir);
                if (!(stjsClass instanceof BridgeClass)) {
                    ++generatedFiles;
                }
            } catch (InclusionScanException e) {
                throw new MojoExecutionException("Cannot scan the source directory:" + e, e);
            } catch (MultipleFileGenerationException e) {
                for (JavascriptFileGenerationException jse : e.getExceptions()) {
                    buildContext.addMessage(jse.getSourcePosition().getFile(), jse.getSourcePosition().getLine(), jse.getSourcePosition().getColumn(), jse.getMessage(), BuildContext.SEVERITY_ERROR, null);
                }
                hasFailures = true;
            // continue with the next file
            } catch (JavascriptFileGenerationException e) {
                buildContext.addMessage(e.getSourcePosition().getFile(), e.getSourcePosition().getLine(), e.getSourcePosition().getColumn(), e.getMessage(), BuildContext.SEVERITY_ERROR, null);
                hasFailures = true;
            // continue with the next file
            } catch (Exception e) {
                // TODO - maybe should filter more here
                buildContext.addMessage(absoluteSource, 1, 1, e.toString(), BuildContext.SEVERITY_ERROR, e);
                hasFailures = true;
            // throw new MojoExecutionException("Error generating javascript:" + e, e);
            }
        }
    }
    generator.close();
    long t2 = System.currentTimeMillis();
    getLog().info("Generated " + generatedFiles + " JavaScript files in " + (t2 - t1) + " ms");
    if (generatedFiles > 0) {
        filesGenerated(generator, gendir);
    }
    if (hasFailures) {
        throw new MojoFailureException("Errors generating JavaScript");
    }
}
Also used : BridgeClass(org.stjs.generator.BridgeClass) InclusionScanException(org.codehaus.plexus.compiler.util.scan.InclusionScanException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) GenerationDirectory(org.stjs.generator.GenerationDirectory) SuffixMapping(org.codehaus.plexus.compiler.util.scan.mapping.SuffixMapping) JavascriptFileGenerationException(org.stjs.generator.JavascriptFileGenerationException) MultipleFileGenerationException(org.stjs.generator.MultipleFileGenerationException) DependencyResolutionRequiredException(org.apache.maven.artifact.DependencyResolutionRequiredException) JavascriptFileGenerationException(org.stjs.generator.JavascriptFileGenerationException) IOException(java.io.IOException) InclusionScanException(org.codehaus.plexus.compiler.util.scan.InclusionScanException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) MultipleFileGenerationException(org.stjs.generator.MultipleFileGenerationException) URLClassLoader(java.net.URLClassLoader) ClassWithJavascript(org.stjs.generator.ClassWithJavascript) GeneratorConfiguration(org.stjs.generator.GeneratorConfiguration) GeneratorConfigurationBuilder(org.stjs.generator.GeneratorConfigurationBuilder) File(java.io.File) SourceMapping(org.codehaus.plexus.compiler.util.scan.mapping.SourceMapping) Generator(org.stjs.generator.Generator)

Example 3 with SuffixMapping

use of org.codehaus.plexus.compiler.util.scan.mapping.SuffixMapping in project antlr4 by tunnelvisionlabs.

the class Antlr4Mojo method getImportFiles.

private Set<File> getImportFiles(File sourceDirectory) throws InclusionScanException {
    if (!libDirectory.exists())
        return Collections.emptySet();
    Set<String> includes = new HashSet<String>();
    includes.add("*.g4");
    includes.add("*.tokens");
    SourceInclusionScanner scan = new SimpleSourceInclusionScanner(includes, Collections.<String>emptySet());
    scan.addSourceMapping(new SuffixMapping("G4", "g4"));
    return scan.getIncludedSources(libDirectory, null);
}
Also used : SourceInclusionScanner(org.codehaus.plexus.compiler.util.scan.SourceInclusionScanner) SimpleSourceInclusionScanner(org.codehaus.plexus.compiler.util.scan.SimpleSourceInclusionScanner) SuffixMapping(org.codehaus.plexus.compiler.util.scan.mapping.SuffixMapping) SimpleSourceInclusionScanner(org.codehaus.plexus.compiler.util.scan.SimpleSourceInclusionScanner) HashSet(java.util.HashSet)

Example 4 with SuffixMapping

use of org.codehaus.plexus.compiler.util.scan.mapping.SuffixMapping in project antlr4 by antlr.

the class Antlr4Mojo method getGrammarFiles.

private Set<File> getGrammarFiles(File sourceDirectory) throws InclusionScanException {
    // Which files under the source set should we be looking for as grammar files
    SourceMapping mapping = new SuffixMapping("g4", Collections.<String>emptySet());
    // What are the sets of includes (defaulted or otherwise).
    Set<String> includes = getIncludesPatterns();
    // Now, to the excludes, we need to add the imports directory
    // as this is autoscanned for imported grammars and so is auto-excluded from the
    // set of grammar fields we should be analyzing.
    excludes.add("imports/**");
    SourceInclusionScanner scan = new SimpleSourceInclusionScanner(includes, excludes);
    scan.addSourceMapping(mapping);
    return scan.getIncludedSources(sourceDirectory, null);
}
Also used : SourceInclusionScanner(org.codehaus.plexus.compiler.util.scan.SourceInclusionScanner) SimpleSourceInclusionScanner(org.codehaus.plexus.compiler.util.scan.SimpleSourceInclusionScanner) SuffixMapping(org.codehaus.plexus.compiler.util.scan.mapping.SuffixMapping) SourceMapping(org.codehaus.plexus.compiler.util.scan.mapping.SourceMapping) SimpleSourceInclusionScanner(org.codehaus.plexus.compiler.util.scan.SimpleSourceInclusionScanner)

Example 5 with SuffixMapping

use of org.codehaus.plexus.compiler.util.scan.mapping.SuffixMapping in project jangaroo-tools by CoreMedia.

the class MavenPluginHelper method computeStaleSources.

public List<File> computeStaleSources(List<File> compileSourceRoots, Set<String> includes, Set<String> excludes, File outputDirectory, String inputFileSuffix, String outputFileSuffix, int staleMillis) throws MojoExecutionException {
    SourceInclusionScanner scanner = createSourceInclusionScanner(includes, excludes, inputFileSuffix, staleMillis);
    scanner.addSourceMapping(new SuffixMapping(inputFileSuffix, outputFileSuffix));
    log.debug("Searching for");
    Set<File> staleSources = new LinkedHashSet<File>();
    for (File rootFile : compileSourceRoots) {
        if (!rootFile.isDirectory()) {
            continue;
        }
        try {
            log.debug("scanner.getIncludedSources(" + rootFile + ", " + outputDirectory + ")");
            // noinspection unchecked
            staleSources.addAll(scanner.getIncludedSources(rootFile, outputDirectory));
        } catch (InclusionScanException e) {
            throw new MojoExecutionException("Error scanning source root: \'" + rootFile.getAbsolutePath() + "\' " + "for stale files to recompile.", e);
        }
    }
    return Collections.unmodifiableList(new ArrayList<File>(staleSources));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) InclusionScanException(org.codehaus.plexus.compiler.util.scan.InclusionScanException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) SimpleSourceInclusionScanner(org.codehaus.plexus.compiler.util.scan.SimpleSourceInclusionScanner) SourceInclusionScanner(org.codehaus.plexus.compiler.util.scan.SourceInclusionScanner) SuffixMapping(org.codehaus.plexus.compiler.util.scan.mapping.SuffixMapping) File(java.io.File)

Aggregations

SuffixMapping (org.codehaus.plexus.compiler.util.scan.mapping.SuffixMapping)9 SourceMapping (org.codehaus.plexus.compiler.util.scan.mapping.SourceMapping)6 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)5 SimpleSourceInclusionScanner (org.codehaus.plexus.compiler.util.scan.SimpleSourceInclusionScanner)5 SourceInclusionScanner (org.codehaus.plexus.compiler.util.scan.SourceInclusionScanner)5 File (java.io.File)4 InclusionScanException (org.codehaus.plexus.compiler.util.scan.InclusionScanException)4 HashSet (java.util.HashSet)3 IOException (java.io.IOException)2 URLClassLoader (java.net.URLClassLoader)2 DependencyResolutionRequiredException (org.apache.maven.artifact.DependencyResolutionRequiredException)2 MojoFailureException (org.apache.maven.plugin.MojoFailureException)2 CompilerOutputStyle (org.codehaus.plexus.compiler.CompilerOutputStyle)2 SingleTargetSourceMapping (org.codehaus.plexus.compiler.util.scan.mapping.SingleTargetSourceMapping)2 ClassWithJavascript (org.stjs.generator.ClassWithJavascript)2 JavascriptFileGenerationException (org.stjs.generator.JavascriptFileGenerationException)2 MultipleFileGenerationException (org.stjs.generator.MultipleFileGenerationException)2 SourceMapGeneratorV3 (com.google.debugging.sourcemap.SourceMapGeneratorV3)1 BufferedOutputStream (java.io.BufferedOutputStream)1 BufferedWriter (java.io.BufferedWriter)1