Search in sources :

Example 16 with SoyFileNode

use of com.google.template.soy.soytree.SoyFileNode 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 17 with SoyFileNode

use of com.google.template.soy.soytree.SoyFileNode 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)

Example 18 with SoyFileNode

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

the class RenderVisitorTest method testRenderMsgStmtWithFallback.

@Test
public void testRenderMsgStmtWithFallback() throws Exception {
    String templateBody = "" + "  {msg desc=\"\"}\n" + "    blah\n" + "  {fallbackmsg desc=\"\"}\n" + "    bleh\n" + "  {/msg}\n";
    // Without msg bundle.
    assertRender(templateBody, "blah");
    // With msg bundle.
    SoyFileNode file = SoyFileSetParserBuilder.forFileContents("{namespace test}\n{template .foo}\n" + templateBody + "{/template}").parse().fileSet().getChild(0);
    MsgFallbackGroupNode msgGroup = SoyTreeUtils.getAllNodesOfType(file.getChild(0), MsgFallbackGroupNode.class).get(0);
    MsgNode fallbackMsg = msgGroup.getChild(1);
    SoyMsg translatedFallbackMsg = SoyMsg.builder().setId(MsgUtils.computeMsgIdForDualFormat(fallbackMsg)).setLocaleString("x-zz").setParts(ImmutableList.<SoyMsgPart>of(SoyMsgRawTextPart.of("zbleh"))).build();
    SoyMsgBundle msgBundle = new SoyMsgBundleImpl("x-zz", Lists.newArrayList(translatedFallbackMsg));
    assertThat(renderWithDataAndMsgBundle(templateBody, TEST_DATA, msgBundle)).isEqualTo("zbleh");
}
Also used : SoyMsg(com.google.template.soy.msgs.restricted.SoyMsg) SoyMsgBundleImpl(com.google.template.soy.msgs.restricted.SoyMsgBundleImpl) MsgFallbackGroupNode(com.google.template.soy.soytree.MsgFallbackGroupNode) SoyFileNode(com.google.template.soy.soytree.SoyFileNode) SoyMsgBundle(com.google.template.soy.msgs.SoyMsgBundle) MsgNode(com.google.template.soy.soytree.MsgNode) SoyMsgPart(com.google.template.soy.msgs.restricted.SoyMsgPart) Test(org.junit.Test)

Example 19 with SoyFileNode

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

the class GenerateParseInfoVisitorTest method testJavaClassNameSource.

@Test
public void testJavaClassNameSource() {
    SoyFileNode soyFileNode = forFilePathAndNamespace("BooFoo.soy", "aaa.bbb.cccDdd");
    assertEquals("BooFoo", SOY_FILE_NAME.generateBaseClassName(soyFileNode));
    soyFileNode = forFilePathAndNamespace("blah/bleh/boo_foo.soy", "aaa.bbb.cccDdd");
    assertEquals("BooFoo", SOY_FILE_NAME.generateBaseClassName(soyFileNode));
    soyFileNode = forFilePathAndNamespace("boo-FOO.soy", "aaa.bbb.cccDdd");
    assertEquals("BooFoo", SOY_FILE_NAME.generateBaseClassName(soyFileNode));
    soyFileNode = forFilePathAndNamespace("\\BLAH\\BOO_FOO.SOY", "aaa.bbb.cccDdd");
    assertEquals("BooFoo", SOY_FILE_NAME.generateBaseClassName(soyFileNode));
    soyFileNode = forFilePathAndNamespace("", "cccDdd");
    assertEquals("CccDdd", SOY_NAMESPACE_LAST_PART.generateBaseClassName(soyFileNode));
    soyFileNode = forFilePathAndNamespace("", "aaa.bbb.cccDdd");
    assertEquals("CccDdd", SOY_NAMESPACE_LAST_PART.generateBaseClassName(soyFileNode));
    soyFileNode = forFilePathAndNamespace("", "aaa_bbb.ccc_ddd");
    assertEquals("CccDdd", SOY_NAMESPACE_LAST_PART.generateBaseClassName(soyFileNode));
    soyFileNode = forFilePathAndNamespace("", "CccDdd");
    assertEquals("CccDdd", SOY_NAMESPACE_LAST_PART.generateBaseClassName(soyFileNode));
    soyFileNode = forFilePathAndNamespace("", "aaa.bbb.ccc_DDD");
    assertEquals("CccDdd", SOY_NAMESPACE_LAST_PART.generateBaseClassName(soyFileNode));
    soyFileNode = forFilePathAndNamespace("BooFoo.soy", "aaa.bbb.cccDdd");
    assertEquals("File", GENERIC.generateBaseClassName(soyFileNode));
    soyFileNode = forFilePathAndNamespace("blah/bleh/boo-foo.soy", "ccc_ddd");
    assertEquals("File", GENERIC.generateBaseClassName(soyFileNode));
}
Also used : SoyFileNode(com.google.template.soy.soytree.SoyFileNode) Test(org.junit.Test)

Example 20 with SoyFileNode

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

the class FindCalleesNotInFileVisitorTest method testFindCalleesNotInFile.

@Test
public void testFindCalleesNotInFile() {
    String testFileContent = "" + "{namespace boo.foo}\n" + "\n" + "/** Test template 1. */\n" + "{template .goo autoescape=\"deprecated-noncontextual\"}\n" + "  {call .goo data=\"all\" /}\n" + "  {call .moo data=\"all\" /}\n" + "  {call boo.woo.hoo data=\"all\" /}\n" + // not defined in this file
    "{/template}\n" + "\n" + "/** Test template 2. */\n" + "{template .moo autoescape=\"deprecated-noncontextual\"}\n" + "  {for $i in range(8)}\n" + "    {call boo.foo.goo data=\"all\" /}\n" + "    {call .too data=\"all\" /}\n" + // not defined in this file
    "    {call .goo}" + "      {param a}{call .moo /}{/param}" + "      {param b}{call .zoo /}{/param}" + // not defined in this file
    "    {/call}" + "  {/for}\n" + "{/template}\n" + "\n" + "/** Test template 3. */\n" + "{deltemplate booHoo autoescape=\"deprecated-noncontextual\"}\n" + "  {call .goo data=\"all\" /}\n" + "  {call .moo data=\"all\" /}\n" + "  {call boo.hoo.roo data=\"all\" /}\n" + // not defined in this file
    "{/deltemplate}\n";
    ErrorReporter boom = ErrorReporter.exploding();
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(testFileContent).errorReporter(boom).parse().fileSet();
    SoyFileNode soyFile = soyTree.getChild(0);
    Iterable<String> calleesNotInFile = Iterables.transform(new FindCalleesNotInFileVisitor().exec(soyFile), CallBasicNode::getCalleeName);
    assertThat(calleesNotInFile).containsExactly("boo.woo.hoo", "boo.foo.too", "boo.foo.zoo", "boo.hoo.roo");
}
Also used : ErrorReporter(com.google.template.soy.error.ErrorReporter) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) CallBasicNode(com.google.template.soy.soytree.CallBasicNode) SoyFileNode(com.google.template.soy.soytree.SoyFileNode) Test(org.junit.Test)

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