Search in sources :

Example 11 with SoyFileNode

use of com.google.template.soy.soytree.SoyFileNode in project closure-templates by google.

the class ContextualAutoescaper method findTemplates.

/**
 * Fills in the {@link Inferences} template name to node map.
 *
 * @param files Modified in place.
 */
private static ImmutableListMultimap<String, TemplateNode> findTemplates(Iterable<? extends SoyFileNode> files) {
    ImmutableListMultimap.Builder<String, TemplateNode> builder = ImmutableListMultimap.builder();
    for (SoyFileNode file : files) {
        for (TemplateNode template : file.getChildren()) {
            String templateName;
            if (template instanceof TemplateBasicNode) {
                templateName = template.getTemplateName();
            } else {
                templateName = ((TemplateDelegateNode) template).getDelTemplateName();
            }
            builder.put(templateName, template);
        }
    }
    return builder.build();
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) TemplateBasicNode(com.google.template.soy.soytree.TemplateBasicNode) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) SoyFileNode(com.google.template.soy.soytree.SoyFileNode)

Example 12 with SoyFileNode

use of com.google.template.soy.soytree.SoyFileNode in project closure-templates by google.

the class PySrcMain method genPyFiles.

/**
 * Generates Python source files given a Soy parse tree, an options object, and information on
 * where to put the output files.
 *
 * @param soyTree The Soy parse tree to generate Python source code for.
 * @param pySrcOptions The compilation options relevant to this backend.
 * @param outputPathFormat The format string defining how to build the output file path
 *     corresponding to an input file path.
 * @param inputPathsPrefix The input path prefix, or empty string if none.
 * @param errorReporter The Soy error reporter that collects errors during code generation.
 * @throws IOException If there is an error in opening/writing an output Python file.
 */
public void genPyFiles(SoyFileSetNode soyTree, SoyPySrcOptions pySrcOptions, String outputPathFormat, String inputPathsPrefix, ErrorReporter errorReporter) throws IOException {
    ImmutableList<SoyFileNode> srcsToCompile = ImmutableList.copyOf(Iterables.filter(soyTree.getChildren(), SoyFileNode.MATCH_SRC_FILENODE));
    // Determine the output paths.
    List<String> soyNamespaces = getSoyNamespaces(soyTree);
    Multimap<String, Integer> outputs = MainEntryPointUtils.mapOutputsToSrcs(null, outputPathFormat, inputPathsPrefix, srcsToCompile);
    // Generate the manifest and add it to the current manifest.
    ImmutableMap<String, String> manifest = generateManifest(soyNamespaces, outputs);
    // Generate the Python source.
    List<String> pyFileContents = genPySrc(soyTree, pySrcOptions, manifest, errorReporter);
    if (srcsToCompile.size() != pyFileContents.size()) {
        throw new AssertionError(String.format("Expected to generate %d code chunk(s), got %d", srcsToCompile.size(), pyFileContents.size()));
    }
    // Write out the Python outputs.
    for (String outputFilePath : outputs.keySet()) {
        try (Writer out = Files.newWriter(new File(outputFilePath), StandardCharsets.UTF_8)) {
            for (int inputFileIndex : outputs.get(outputFilePath)) {
                out.write(pyFileContents.get(inputFileIndex));
            }
        }
    }
    // Write out the manifest file.
    if (pySrcOptions.namespaceManifestFile() != null) {
        try (Writer out = Files.newWriter(new File(pySrcOptions.namespaceManifestFile()), StandardCharsets.UTF_8)) {
            Properties prop = new Properties();
            for (String namespace : manifest.keySet()) {
                prop.put(namespace, manifest.get(namespace));
            }
            prop.store(out, null);
        }
    }
}
Also used : Properties(java.util.Properties) SoyFileNode(com.google.template.soy.soytree.SoyFileNode) File(java.io.File) Writer(java.io.Writer)

Example 13 with SoyFileNode

use of com.google.template.soy.soytree.SoyFileNode in project closure-templates by google.

the class PassManager method doContextualEscaping.

/**
 * Runs the autoescaper and returns whether or not new contextual templates have been added.
 */
private boolean doContextualEscaping(SoyFileSetNode soyTree) {
    List<TemplateNode> extraTemplates = autoescaper.rewrite(soyTree, errorReporter);
    // TODO: Run the redundant template remover here and rename after CL 16642341 is in.
    if (!extraTemplates.isEmpty()) {
        // TODO: pull out somewhere else.  Ideally do the merge as part of the redundant template
        // removal.
        Map<String, SoyFileNode> containingFile = new HashMap<>();
        for (SoyFileNode fileNode : soyTree.getChildren()) {
            for (TemplateNode templateNode : fileNode.getChildren()) {
                String name = templateNode instanceof TemplateDelegateNode ? ((TemplateDelegateNode) templateNode).getDelTemplateName() : templateNode.getTemplateName();
                containingFile.put(DerivedTemplateUtils.getBaseName(name), fileNode);
            }
        }
        for (TemplateNode extraTemplate : extraTemplates) {
            String name = extraTemplate instanceof TemplateDelegateNode ? ((TemplateDelegateNode) extraTemplate).getDelTemplateName() : extraTemplate.getTemplateName();
            containingFile.get(DerivedTemplateUtils.getBaseName(name)).addChild(extraTemplate);
        }
        return true;
    }
    return false;
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) TemplateDelegateNode(com.google.template.soy.soytree.TemplateDelegateNode) HashMap(java.util.HashMap) SoyFileNode(com.google.template.soy.soytree.SoyFileNode)

Example 14 with SoyFileNode

use of com.google.template.soy.soytree.SoyFileNode in project closure-templates by google.

the class Rewriter method rewrite.

/**
 * @return Derived templates that should be added to the parse tree.
 */
public List<TemplateNode> rewrite(SoyFileSetNode files) {
    RewriterVisitor mutator = new RewriterVisitor();
    // First walk the input files that the caller already knows about.
    for (SoyFileNode file : files.getChildren()) {
        mutator.exec(file);
    }
    // Now walk over anything not reachable from the input files to make sure we get all the derived
    // templates.
    ImmutableList.Builder<TemplateNode> extraTemplates = ImmutableList.builder();
    for (TemplateNode template : inferences.getAllTemplates()) {
        String name = template.getTemplateName();
        if (!visitedTemplateNames.contains(name)) {
            extraTemplates.add(template);
            mutator.exec(template);
        }
    }
    return extraTemplates.build();
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) ImmutableList(com.google.common.collect.ImmutableList) SoyFileNode(com.google.template.soy.soytree.SoyFileNode)

Example 15 with SoyFileNode

use of com.google.template.soy.soytree.SoyFileNode in project closure-templates by google.

the class IncrementalDomSrcMain method genJsFiles.

/**
 * Generates Incremental DOM JS source files given a Soy parse tree, an options object, an
 * optional bundle of translated messages, and information on where to put the output files.
 *
 * @param soyTree The Soy parse tree to generate JS source code for.
 * @param templateRegistry The template registry that contains all the template information.
 * @param jsSrcOptions The compilation options relevant to this backend.
 * @param outputPathFormat The format string defining how to build the output file path
 *     corresponding to an input file path.
 * @param errorReporter The Soy error reporter that collects errors during code generation.
 * @throws IOException If there is an error in opening/writing an output JS file.
 */
public void genJsFiles(SoyFileSetNode soyTree, TemplateRegistry templateRegistry, SoyIncrementalDomSrcOptions jsSrcOptions, String outputPathFormat, ErrorReporter errorReporter) throws IOException {
    List<String> jsFileContents = genJsSrc(soyTree, templateRegistry, jsSrcOptions, errorReporter);
    // try to compare the size between jsFileContents and srcsToCompile.
    if (errorReporter.hasErrors()) {
        return;
    }
    ImmutableList<SoyFileNode> srcsToCompile = ImmutableList.copyOf(Iterables.filter(soyTree.getChildren(), SoyFileNode.MATCH_SRC_FILENODE));
    if (srcsToCompile.size() != jsFileContents.size()) {
        throw new AssertionError(String.format("Expected to generate %d code chunk(s), got %d", srcsToCompile.size(), jsFileContents.size()));
    }
    Multimap<String, Integer> outputs = MainEntryPointUtils.mapOutputsToSrcs(null, /* locale */
    outputPathFormat, "", /* inputPathsPrefix */
    srcsToCompile);
    for (String outputFilePath : outputs.keySet()) {
        Writer out = Files.newWriter(new File(outputFilePath), UTF_8);
        try {
            boolean isFirst = true;
            for (int inputFileIndex : outputs.get(outputFilePath)) {
                if (isFirst) {
                    isFirst = false;
                } else {
                    // Concatenating JS files is not safe unless we know that the last statement from one
                    // couldn't combine with the isFirst statement of the next.  Inserting a semicolon will
                    // prevent this from happening.
                    out.write("\n;\n");
                }
                out.write(jsFileContents.get(inputFileIndex));
            }
        } finally {
            out.close();
        }
    }
}
Also used : SoyFileNode(com.google.template.soy.soytree.SoyFileNode) File(java.io.File) Writer(java.io.Writer)

Aggregations

SoyFileNode (com.google.template.soy.soytree.SoyFileNode)20 TemplateNode (com.google.template.soy.soytree.TemplateNode)7 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)4 Test (org.junit.Test)4 TemplateRegistry (com.google.template.soy.soytree.TemplateRegistry)3 File (java.io.File)3 Writer (java.io.Writer)3 ImmutableList (com.google.common.collect.ImmutableList)2 IncrementingIdGenerator (com.google.template.soy.base.internal.IncrementingIdGenerator)2 CopyState (com.google.template.soy.basetree.CopyState)2 ErrorReporter (com.google.template.soy.error.ErrorReporter)2 SoyMsgBundle (com.google.template.soy.msgs.SoyMsgBundle)2 SoyMsg (com.google.template.soy.msgs.restricted.SoyMsg)2 SoyMsgBundleImpl (com.google.template.soy.msgs.restricted.SoyMsgBundleImpl)2 HashSet (java.util.HashSet)2 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 ParseResult (com.google.template.soy.SoyFileSetParser.ParseResult)1 SourceLocation (com.google.template.soy.base.SourceLocation)1 IdGenerator (com.google.template.soy.base.internal.IdGenerator)1