Search in sources :

Example 1 with TemplateRegistry

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

the class BytecodeCompilerTest method testDelCall_delPackageSelections.

@Test
public void testDelCall_delPackageSelections() throws IOException {
    String soyFileContent1 = Joiner.on("\n").join("{namespace ns1}", "", "/***/", "{template .callerTemplate}", "  {delcall myApp.myDelegate}", "    {param boo: 'aaaaaah' /}", "  {/delcall}", "{/template}", "", "/** */", // default implementation (doesn't use $boo)
    "{deltemplate myApp.myDelegate}", "  {@param boo : string}", "  default", "{/deltemplate}", "");
    String soyFileContent2 = Joiner.on("\n").join("{delpackage SecretFeature}", "{namespace ns2}", "", "/** */", // implementation in SecretFeature
    "{deltemplate myApp.myDelegate}", "  {@param boo : string}", "  SecretFeature {$boo}", "{/deltemplate}", "");
    String soyFileContent3 = Joiner.on("\n").join("{delpackage AlternateSecretFeature}", "{namespace ns3}", "", "/** */", // implementation in AlternateSecretFeature
    "{deltemplate myApp.myDelegate}", "  {@param boo : string}", "  AlternateSecretFeature {call .helper data=\"all\" /}", "{/deltemplate}", "");
    String soyFileContent4 = Joiner.on("\n").join("{namespace ns3}", "", "/** */", "{template .helper}", "  {@param boo : string}", "  {$boo}", "{/template}", "");
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(soyFileContent1, soyFileContent2, soyFileContent3, soyFileContent4).parse().fileSet();
    TemplateRegistry templateRegistry = new TemplateRegistry(soyTree, ErrorReporter.exploding());
    CompiledTemplates templates = BytecodeCompiler.compile(templateRegistry, false, ErrorReporter.exploding()).get();
    CompiledTemplate.Factory factory = templates.getTemplateFactory("ns1.callerTemplate");
    Predicate<String> activePackages = Predicates.alwaysFalse();
    assertThat(renderWithContext(factory, getDefaultContext(templates, activePackages))).isEqualTo("default");
    activePackages = Predicates.equalTo("SecretFeature");
    assertThat(renderWithContext(factory, getDefaultContext(templates, activePackages))).isEqualTo("SecretFeature aaaaaah");
    activePackages = Predicates.equalTo("AlternateSecretFeature");
    assertThat(renderWithContext(factory, getDefaultContext(templates, activePackages))).isEqualTo("AlternateSecretFeature aaaaaah");
    activePackages = Predicates.equalTo("NonexistentFeature");
    assertThat(renderWithContext(factory, getDefaultContext(templates, activePackages))).isEqualTo("default");
}
Also used : TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) CompiledTemplates(com.google.template.soy.jbcsrc.shared.CompiledTemplates) CompiledTemplate(com.google.template.soy.jbcsrc.shared.CompiledTemplate) Test(org.junit.Test)

Example 2 with TemplateRegistry

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

the class SoyFileSet method generateParseInfo.

/**
 * Generates Java classes containing parse info (param names, template names, meta info). There
 * will be one Java class per Soy file.
 *
 * @param javaPackage The Java package for the generated classes.
 * @param javaClassNameSource Source of the generated class names. Must be one of "filename",
 *     "namespace", or "generic".
 * @return A map from generated file name (of the form "<*>SoyInfo.java") to generated file
 *     content.
 * @throws SoyCompilationException If compilation fails.
 */
ImmutableMap<String, String> generateParseInfo(String javaPackage, String javaClassNameSource) {
    resetErrorReporter();
    // TODO(lukes): see if we can enforce that globals are provided at compile time here. given that
    // types have to be, this should be possible.  Currently it is disabled for backwards
    // compatibility
    // N.B. we do not run the optimizer here for 2 reasons:
    // 1. it would just waste time, since we are not running code generation the optimization work
    // doesn't help anything
    // 2. it potentially removes metadata from the tree by precalculating expressions. For example,
    // trivial print nodes are evaluated, which can remove globals from the tree, but the
    // generator requires data about globals to generate accurate proto descriptors.  Also, the
    // ChangeCallsToPassAllData pass will change the params of templates.
    ParseResult result = parse(passManagerBuilder(SyntaxVersion.V2_0).allowUnknownGlobals().optimize(false), typeRegistry, new PluginResolver(// we allow undefined plugins since they typically aren't provided :(
    PluginResolver.Mode.ALLOW_UNDEFINED, printDirectives, soyFunctionMap, errorReporter));
    throwIfErrorsPresent();
    SoyFileSetNode soyTree = result.fileSet();
    TemplateRegistry registry = result.registry();
    // Do renaming of package-relative class names.
    ImmutableMap<String, String> parseInfo = new GenerateParseInfoVisitor(javaPackage, javaClassNameSource, registry).exec(soyTree);
    throwIfErrorsPresent();
    reportWarnings();
    return parseInfo;
}
Also used : TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) ParseResult(com.google.template.soy.SoyFileSetParser.ParseResult) GenerateParseInfoVisitor(com.google.template.soy.parseinfo.passes.GenerateParseInfoVisitor) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) PluginResolver(com.google.template.soy.soyparse.PluginResolver)

Example 3 with TemplateRegistry

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

the class SoyFileSet method compileForServerRendering.

/**
 * Runs common compiler logic shared by tofu and jbcsrc backends.
 */
private ServerCompilationPrimitives compileForServerRendering() {
    ParseResult result = parse(SyntaxVersion.V2_0);
    throwIfErrorsPresent();
    SoyFileSetNode soyTree = result.fileSet();
    TemplateRegistry registry = result.registry();
    // which case it is pointless.
    if (cache == null) {
        new ClearSoyDocStringsVisitor().exec(soyTree);
    }
    throwIfErrorsPresent();
    return new ServerCompilationPrimitives(registry, soyTree);
}
Also used : TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) ClearSoyDocStringsVisitor(com.google.template.soy.passes.ClearSoyDocStringsVisitor) ParseResult(com.google.template.soy.SoyFileSetParser.ParseResult) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode)

Example 4 with TemplateRegistry

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

the class SoyFileSet method compileToJsSrc.

/**
 * Compiles this Soy file set into JS source code files and returns these JS files as a list of
 * strings, one per file.
 *
 * @param jsSrcOptions The compilation options for the JS Src output target.
 * @param msgBundle The bundle of translated messages, or null to use the messages from the Soy
 *     source.
 * @return A list of strings where each string represents the JS source code that belongs in one
 *     JS file. The generated JS files correspond one-to-one to the original Soy source files.
 * @throws SoyCompilationException If compilation fails.
 */
@SuppressWarnings("deprecation")
public List<String> compileToJsSrc(SoyJsSrcOptions jsSrcOptions, @Nullable SoyMsgBundle msgBundle) {
    ParseResult result = preprocessJsSrcResults(jsSrcOptions);
    TemplateRegistry registry = result.registry();
    SoyFileSetNode fileSet = result.fileSet();
    List<String> generatedSrcs = new JsSrcMain(apiCallScopeProvider, typeRegistry).genJsSrc(fileSet, registry, jsSrcOptions, msgBundle, errorReporter);
    throwIfErrorsPresent();
    reportWarnings();
    return generatedSrcs;
}
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)

Example 5 with TemplateRegistry

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

the class FindTransitiveDepTemplatesVisitorTest method testTwoPathsToSameRecursiveCycle.

@Test
public void testTwoPathsToSameRecursiveCycle() {
    // aaa -> {bbb, ccc}, bbb -> ddd, ccc -> ddd, ddd -> bbb.
    String fileContent = "" + "{namespace ns}\n" + "\n" + "/***/\n" + "{template .aaa}\n" + "  {$ij.boo} {$ij.foo} {call .bbb /} {call .ccc /}\n" + "{/template}\n" + "\n" + "/***/\n" + "{template .bbb}\n" + "  {$ij.boo} {$ij.goo} {call .ddd /}\n" + "{/template}\n" + "\n" + "/***/\n" + "{template .ccc}\n" + "  {$ij.boo} {$ij.moo} {call .ddd /}\n" + "{/template}\n" + "\n" + "/***/\n" + "{template .ddd}\n" + "  {$ij.boo} {$ij.too} {call .bbb /}\n" + "{/template}\n";
    ParseResult result = SoyFileSetParserBuilder.forFileContents(fileContent).errorReporter(FAIL).parse();
    TemplateRegistry templateRegistry = result.registry();
    SoyFileSetNode soyTree = result.fileSet();
    TemplateNode aaa = soyTree.getChild(0).getChild(0);
    TemplateNode bbb = soyTree.getChild(0).getChild(1);
    TemplateNode ccc = soyTree.getChild(0).getChild(2);
    TemplateNode ddd = soyTree.getChild(0).getChild(3);
    // Test with exec(aaa).
    // Exercises: processCalleeHelper case 4 with incorporateCalleeVisitInfo case 4 (ccc -> ddd).
    FindTransitiveDepTemplatesVisitor visitor = new FindTransitiveDepTemplatesVisitor(templateRegistry);
    Map<TemplateNode, TransitiveDepTemplatesInfo> memoizedInfoMap = visitor.templateToFinishedInfoMap;
    visitor.exec(aaa);
    assertThat(memoizedInfoMap).hasSize(4);
    assertThat(memoizedInfoMap.get(bbb).depTemplateSet).isEqualTo(ImmutableSet.of(bbb, ddd));
    assertThat(memoizedInfoMap.get(ddd).depTemplateSet).isEqualTo(ImmutableSet.of(ddd, bbb));
    assertThat(memoizedInfoMap.get(ccc).depTemplateSet).isEqualTo(ImmutableSet.of(ccc, ddd, bbb));
    assertThat(memoizedInfoMap.get(aaa).depTemplateSet).isEqualTo(ImmutableSet.of(aaa, bbb, ccc, ddd));
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) ParseResult(com.google.template.soy.SoyFileSetParser.ParseResult) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) TransitiveDepTemplatesInfo(com.google.template.soy.passes.FindTransitiveDepTemplatesVisitor.TransitiveDepTemplatesInfo) Test(org.junit.Test)

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