Search in sources :

Example 21 with TemplateRegistry

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

the class BytecodeCompilerTest method compileFiles.

private CompiledTemplates compileFiles(String... soyFileContents) {
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(soyFileContents).parse().fileSet();
    TemplateRegistry templateRegistry = new TemplateRegistry(soyTree, ErrorReporter.exploding());
    CompiledTemplates templates = BytecodeCompiler.compile(templateRegistry, false, ErrorReporter.exploding()).get();
    return templates;
}
Also used : TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) CompiledTemplates(com.google.template.soy.jbcsrc.shared.CompiledTemplates)

Example 22 with TemplateRegistry

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

the class BytecodeCompilerTest method testDelCallEscaping_separateCompilation.

// Tests for a bug where we would overescape deltemplates at the call site when the strict
// content kind of the deltemplate was unknown at compile time.
@Test
public void testDelCallEscaping_separateCompilation() throws IOException {
    String soyFileContent1 = Joiner.on("\n").join("{namespace ns}", "", "{template .callerTemplate}", "  {delcall myApp.myDelegate/}", "{/template}", "");
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(soyFileContent1).parse().fileSet();
    // apply an escaping directive to the callsite, just like the autoescaper would
    CallDelegateNode cdn = SoyTreeUtils.getAllNodesOfType(soyTree.getChild(0), CallDelegateNode.class).get(0);
    cdn.setEscapingDirectives(ImmutableList.of(new EscapeHtmlDirective()));
    TemplateRegistry templateRegistry = new TemplateRegistry(soyTree, ErrorReporter.exploding());
    CompiledTemplates templates = BytecodeCompiler.compile(templateRegistry, false, ErrorReporter.exploding()).get();
    CompiledTemplate.Factory caller = templates.getTemplateFactory("ns.callerTemplate");
    try {
        renderWithContext(caller, getDefaultContext(templates));
        fail();
    } catch (IllegalArgumentException iae) {
        assertThat(iae).hasMessageThat().isEqualTo("Found no active impl for delegate call to \"myApp.myDelegate\" (and delcall does " + "not set allowemptydefault=\"true\").");
    }
    String soyFileContent2 = Joiner.on("\n").join("{namespace ns2}", "", "{deltemplate myApp.myDelegate}", "  <span>Hello</span>", "{/deltemplate}", "");
    CompiledTemplates templatesWithDeltemplate = compileFiles(soyFileContent2);
    // By passing an alternate context, we ensure the deltemplate selector contains the delegate
    assertThat(renderWithContext(caller, getDefaultContext(templatesWithDeltemplate))).isEqualTo("<span>Hello</span>");
}
Also used : TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) EscapeHtmlDirective(com.google.template.soy.coredirectives.EscapeHtmlDirective) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) CompiledTemplates(com.google.template.soy.jbcsrc.shared.CompiledTemplates) CallDelegateNode(com.google.template.soy.soytree.CallDelegateNode) CompiledTemplate(com.google.template.soy.jbcsrc.shared.CompiledTemplate) Test(org.junit.Test)

Example 23 with TemplateRegistry

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

the class SoyFileSet method compileToJsSrcFiles.

/**
 * Compiles this Soy file set into JS source code files and writes these JS files to disk.
 *
 * @param outputPathFormat The format string defining how to build the output file path
 *     corresponding to an input file path.
 * @param inputFilePathPrefix The prefix prepended to all input file paths (can be empty string).
 * @param jsSrcOptions The compilation options for the JS Src output target.
 * @param locales The list of locales. Can be an empty list if not applicable.
 * @param msgPlugin The {@link SoyMsgPlugin} to use, or null if not applicable
 * @param messageFilePathFormat The message file path format, or null if not applicable.
 * @throws SoyCompilationException If compilation fails.
 * @throws IOException If there is an error in opening/reading a message file or opening/writing
 *     an output JS file.
 */
@SuppressWarnings("deprecation")
void compileToJsSrcFiles(String outputPathFormat, String inputFilePathPrefix, SoyJsSrcOptions jsSrcOptions, List<String> locales, @Nullable SoyMsgPlugin msgPlugin, @Nullable String messageFilePathFormat) throws IOException {
    ParseResult result = preprocessJsSrcResults(jsSrcOptions);
    SoyFileSetNode soyTree = result.fileSet();
    TemplateRegistry registry = result.registry();
    if (locales.isEmpty()) {
        // Not generating localized JS.
        new JsSrcMain(apiCallScopeProvider, typeRegistry).genJsFiles(soyTree, registry, jsSrcOptions, null, null, outputPathFormat, inputFilePathPrefix, errorReporter);
    } else {
        checkArgument(msgPlugin != null, "a message plugin must be provided when generating localized sources");
        checkArgument(messageFilePathFormat != null, "a messageFilePathFormat must be provided when generating localized sources");
        // Generating localized JS.
        for (String locale : locales) {
            SoyFileSetNode soyTreeClone = soyTree.copy(new CopyState());
            String msgFilePath = MainEntryPointUtils.buildFilePath(messageFilePathFormat, locale, null, inputFilePathPrefix);
            SoyMsgBundle msgBundle = new SoyMsgBundleHandler(msgPlugin).createFromFile(new File(msgFilePath));
            if (msgBundle.getLocaleString() == null) {
                // begins with "en", because falling back to the Soy source will probably be fine.
                if (!locale.startsWith("en")) {
                    throw new IOException("Error opening or reading message file " + msgFilePath);
                }
            }
            new JsSrcMain(apiCallScopeProvider, typeRegistry).genJsFiles(soyTreeClone, registry, jsSrcOptions, locale, msgBundle, outputPathFormat, inputFilePathPrefix, errorReporter);
        }
    }
    throwIfErrorsPresent();
    reportWarnings();
}
Also used : JsSrcMain(com.google.template.soy.jssrc.internal.JsSrcMain) TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) ParseResult(com.google.template.soy.SoyFileSetParser.ParseResult) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) CopyState(com.google.template.soy.basetree.CopyState) IOException(java.io.IOException) SoyMsgBundle(com.google.template.soy.msgs.SoyMsgBundle) File(java.io.File) SoyMsgBundleHandler(com.google.template.soy.msgs.SoyMsgBundleHandler)

Example 24 with TemplateRegistry

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

the class SoyFileSet method pruneTranslatedMsgs.

/**
 * Prunes messages from a given message bundle, keeping only messages used in this Soy file set.
 *
 * <p>Important: Do not use directly. This is subject to change and your code will break.
 *
 * <p>Note: This method memoizes intermediate results to improve efficiency in the case that it is
 * called multiple times (which is a common case). Thus, this method will not work correctly if
 * the underlying Soy files are modified between calls to this method.
 *
 * @param origTransMsgBundle The message bundle to prune.
 * @return The pruned message bundle.
 * @throws SoyCompilationException If compilation fails.
 */
public SoyMsgBundle pruneTranslatedMsgs(SoyMsgBundle origTransMsgBundle) {
    resetErrorReporter();
    if (memoizedExtractedMsgIdsForPruning == null) {
        ParseResult result = parse(passManagerBuilder(SyntaxVersion.V1_0).allowUnknownGlobals().disableAllTypeChecking(), // can't resolve strict types
        SoyTypeRegistry.DEFAULT_UNKNOWN, new PluginResolver(PluginResolver.Mode.ALLOW_UNDEFINED, printDirectives, soyFunctionMap, errorReporter));
        throwIfErrorsPresent();
        SoyFileSetNode soyTree = result.fileSet();
        TemplateRegistry registry = result.registry();
        List<TemplateNode> allPublicTemplates = Lists.newArrayList();
        for (SoyFileNode soyFile : soyTree.getChildren()) {
            for (TemplateNode template : soyFile.getChildren()) {
                if (template.getVisibility() == Visibility.PUBLIC) {
                    allPublicTemplates.add(template);
                }
            }
        }
        Map<TemplateNode, TransitiveDepTemplatesInfo> depsInfoMap = new FindTransitiveDepTemplatesVisitor(registry).execOnMultipleTemplates(allPublicTemplates);
        TransitiveDepTemplatesInfo mergedDepsInfo = TransitiveDepTemplatesInfo.merge(depsInfoMap.values());
        SoyMsgBundle extractedMsgBundle = new ExtractMsgsVisitor().execOnMultipleNodes(mergedDepsInfo.depTemplateSet);
        ImmutableSet.Builder<Long> extractedMsgIdsBuilder = ImmutableSet.builder();
        for (SoyMsg extractedMsg : extractedMsgBundle) {
            extractedMsgIdsBuilder.add(extractedMsg.getId());
        }
        throwIfErrorsPresent();
        memoizedExtractedMsgIdsForPruning = extractedMsgIdsBuilder.build();
    }
    // ------ Prune. ------
    ImmutableList.Builder<SoyMsg> prunedTransMsgsBuilder = ImmutableList.builder();
    for (SoyMsg transMsg : origTransMsgBundle) {
        if (memoizedExtractedMsgIdsForPruning.contains(transMsg.getId())) {
            prunedTransMsgsBuilder.add(transMsg);
        }
    }
    throwIfErrorsPresent();
    return new SoyMsgBundleImpl(origTransMsgBundle.getLocaleString(), prunedTransMsgsBuilder.build());
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) ParseResult(com.google.template.soy.SoyFileSetParser.ParseResult) ImmutableList(com.google.common.collect.ImmutableList) SoyMsgBundleImpl(com.google.template.soy.msgs.restricted.SoyMsgBundleImpl) PluginResolver(com.google.template.soy.soyparse.PluginResolver) FindTransitiveDepTemplatesVisitor(com.google.template.soy.passes.FindTransitiveDepTemplatesVisitor) SoyFileNode(com.google.template.soy.soytree.SoyFileNode) ExtractMsgsVisitor(com.google.template.soy.msgs.internal.ExtractMsgsVisitor) TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) SoyMsg(com.google.template.soy.msgs.restricted.SoyMsg) ImmutableSet(com.google.common.collect.ImmutableSet) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) TransitiveDepTemplatesInfo(com.google.template.soy.passes.FindTransitiveDepTemplatesVisitor.TransitiveDepTemplatesInfo) SoyMsgBundle(com.google.template.soy.msgs.SoyMsgBundle)

Example 25 with TemplateRegistry

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

the class SoyFileSetParser method parseWithVersions.

/**
 * Parses a set of Soy files, returning a structure containing the parse tree and template
 * registry.
 */
private ParseResult parseWithVersions() throws IOException {
    IdGenerator nodeIdGen = (cache() != null) ? cache().getNodeIdGenerator() : new IncrementingIdGenerator();
    SoyFileSetNode soyTree = new SoyFileSetNode(nodeIdGen.genId(), nodeIdGen);
    boolean filesWereSkipped = false;
    // generator but fail to lock on it.  Eliminate the id system to avoid this whole issue.
    synchronized (nodeIdGen) {
        // Avoid using the same ID generator in multiple threads.
        for (SoyFileSupplier fileSupplier : soyFileSuppliers().values()) {
            SoyFileSupplier.Version version = fileSupplier.getVersion();
            VersionedFile cachedFile = cache() != null ? cache().get(fileSupplier.getFilePath(), version) : null;
            SoyFileNode node;
            if (cachedFile == null) {
                node = parseSoyFileHelper(fileSupplier, nodeIdGen, typeRegistry());
                // a malformed parse tree.
                if (node == null) {
                    filesWereSkipped = true;
                    continue;
                }
                // Run passes that are considered part of initial parsing.
                passManager().runSingleFilePasses(node, nodeIdGen);
                // Run passes that check the tree.
                if (cache() != null) {
                    cache().put(fileSupplier.getFilePath(), VersionedFile.of(node, version));
                }
            } else {
                node = cachedFile.file();
            }
            soyTree.addChild(node);
        }
        TemplateRegistry registry;
        // Run passes that check the tree iff we successfully parsed every file.
        if (!filesWereSkipped) {
            registry = passManager().runWholeFilesetPasses(soyTree);
        } else {
            registry = new TemplateRegistry(soyTree, errorReporter());
        }
        return ParseResult.create(soyTree, registry);
    }
}
Also used : TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) IdGenerator(com.google.template.soy.base.internal.IdGenerator) IncrementingIdGenerator(com.google.template.soy.base.internal.IncrementingIdGenerator) SoyFileSupplier(com.google.template.soy.base.internal.SoyFileSupplier) IncrementingIdGenerator(com.google.template.soy.base.internal.IncrementingIdGenerator) SoyFileNode(com.google.template.soy.soytree.SoyFileNode) VersionedFile(com.google.template.soy.shared.SoyAstCache.VersionedFile)

Aggregations

TemplateRegistry (com.google.template.soy.soytree.TemplateRegistry)31 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)29 Test (org.junit.Test)20 TemplateNode (com.google.template.soy.soytree.TemplateNode)19 ParseResult (com.google.template.soy.SoyFileSetParser.ParseResult)13 TransitiveDepTemplatesInfo (com.google.template.soy.passes.FindTransitiveDepTemplatesVisitor.TransitiveDepTemplatesInfo)8 CompiledTemplates (com.google.template.soy.jbcsrc.shared.CompiledTemplates)5 CompiledTemplate (com.google.template.soy.jbcsrc.shared.CompiledTemplate)3 SoyFileNode (com.google.template.soy.soytree.SoyFileNode)3 IncrementingIdGenerator (com.google.template.soy.base.internal.IncrementingIdGenerator)2 ErrorReporter (com.google.template.soy.error.ErrorReporter)2 JsSrcMain (com.google.template.soy.jssrc.internal.JsSrcMain)2 SoyMsgBundle (com.google.template.soy.msgs.SoyMsgBundle)2 PluginResolver (com.google.template.soy.soyparse.PluginResolver)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 SoyFileSetParserBuilder (com.google.template.soy.SoyFileSetParserBuilder)1 IdGenerator (com.google.template.soy.base.internal.IdGenerator)1 SoyFileSupplier (com.google.template.soy.base.internal.SoyFileSupplier)1