Search in sources :

Example 1 with SourceMapGeneratorV3

use of com.google.debugging.sourcemap.SourceMapGeneratorV3 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 SourceMapGeneratorV3

use of com.google.debugging.sourcemap.SourceMapGeneratorV3 in project closure-compiler by google.

the class SourceMapGeneratorV3Test method testSourceMapMergeExtensions.

public void testSourceMapMergeExtensions() throws Exception {
    SourceMapGeneratorV3 mapper = new SourceMapGeneratorV3();
    mapper.mergeMapSection(0, 0, "{\n" + "\"version\":3,\n" + "\"file\":\"testcode\",\n" + "\"lineCount\":1,\n" + "\"mappings\":\"AAAAA,QAASA,UAAS,EAAG;\",\n" + "\"sources\":[\"testcode\"],\n" + "\"names\":[\"__BASIC__\"],\n" + "\"x_company_foo\":2\n" + "}\n");
    assertThat(mapper.hasExtension("x_company_foo")).isFalse();
    mapper.addExtension("x_company_baz", 2);
    mapper.mergeMapSection(0, 0, "{\n" + "\"version\":3,\n" + "\"file\":\"testcode2\",\n" + "\"lineCount\":0,\n" + "\"mappings\":\"\",\n" + "\"sources\":[\"testcode2\"],\n" + "\"names\":[],\n" + "\"x_company_baz\":3,\n" + "\"x_company_bar\":false\n" + "}\n", new ExtensionMergeAction() {

        @Override
        public Object merge(String extensionKey, Object currentValue, Object newValue) {
            return (Integer) currentValue + ((JsonPrimitive) newValue).getAsInt();
        }
    });
    assertThat(mapper.getExtension("x_company_baz")).isEqualTo(5);
    assertThat(((JsonPrimitive) mapper.getExtension("x_company_bar")).getAsBoolean()).isFalse();
}
Also used : JsonPrimitive(com.google.gson.JsonPrimitive) ExtensionMergeAction(com.google.debugging.sourcemap.SourceMapGeneratorV3.ExtensionMergeAction) JsonObject(com.google.gson.JsonObject)

Example 3 with SourceMapGeneratorV3

use of com.google.debugging.sourcemap.SourceMapGeneratorV3 in project closure-compiler by google.

the class CompilerTest method sourcemap.

private SourceMapInput sourcemap(String sourceMapPath, String originalSource, FilePosition originalSourcePosition) throws Exception {
    SourceMapGeneratorV3 sourceMap = new SourceMapGeneratorV3();
    sourceMap.addMapping(originalSource, "testSymbolName", originalSourcePosition, new FilePosition(1, 1), new FilePosition(100, 1));
    StringBuilder output = new StringBuilder();
    sourceMap.appendTo(output, "unused.js");
    return new SourceMapInput(SourceFile.fromCode(sourceMapPath, output.toString()));
}
Also used : FilePosition(com.google.debugging.sourcemap.FilePosition) SourceMapGeneratorV3(com.google.debugging.sourcemap.SourceMapGeneratorV3)

Example 4 with SourceMapGeneratorV3

use of com.google.debugging.sourcemap.SourceMapGeneratorV3 in project closure-compiler by google.

the class JsMessageVisitorTest method testJsMessagesWithSrcMap.

public void testJsMessagesWithSrcMap() throws Exception {
    SourceMapGeneratorV3 sourceMap = new SourceMapGeneratorV3();
    sourceMap.addMapping("source1.html", null, new FilePosition(10, 0), new FilePosition(0, 0), new FilePosition(0, 100));
    sourceMap.addMapping("source2.html", null, new FilePosition(10, 0), new FilePosition(1, 0), new FilePosition(1, 100));
    StringBuilder output = new StringBuilder();
    sourceMap.appendTo(output, "unused.js");
    compilerOptions = new CompilerOptions();
    compilerOptions.inputSourceMaps = ImmutableMap.of("[testcode]", new SourceMapInput(SourceFile.fromCode("example.srcmap", output.toString())));
    extractMessagesSafely("/** @desc Hello */ var MSG_HELLO = goog.getMsg('a');\n" + "/** @desc Hi */ var MSG_HI = goog.getMsg('b');\n");
    assertThat(compiler.getWarnings()).isEmpty();
    assertThat(messages).hasSize(2);
    JsMessage msg1 = messages.get(0);
    assertEquals("MSG_HELLO", msg1.getKey());
    assertEquals("Hello", msg1.getDesc());
    assertEquals("source1.html", msg1.getSourceName());
    JsMessage msg2 = messages.get(1);
    assertEquals("MSG_HI", msg2.getKey());
    assertEquals("Hi", msg2.getDesc());
    assertEquals("source2.html", msg2.getSourceName());
}
Also used : FilePosition(com.google.debugging.sourcemap.FilePosition) SourceMapGeneratorV3(com.google.debugging.sourcemap.SourceMapGeneratorV3)

Aggregations

SourceMapGeneratorV3 (com.google.debugging.sourcemap.SourceMapGeneratorV3)3 FilePosition (com.google.debugging.sourcemap.FilePosition)2 ExtensionMergeAction (com.google.debugging.sourcemap.SourceMapGeneratorV3.ExtensionMergeAction)1 JsonObject (com.google.gson.JsonObject)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 BufferedOutputStream (java.io.BufferedOutputStream)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 Writer (java.io.Writer)1 URLClassLoader (java.net.URLClassLoader)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 DependencyResolutionRequiredException (org.apache.maven.artifact.DependencyResolutionRequiredException)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1