Search in sources :

Example 11 with CompiledTemplates

use of com.google.template.soy.jbcsrc.shared.CompiledTemplates in project closure-templates by google.

the class BytecodeCompiler method compile.

/**
 * Compiles all the templates in the given registry.
 *
 * @param registry All the templates to compile
 * @param developmentMode Whether or not we are in development mode. In development mode we
 *     compile classes lazily
 * @param reporter The error reporter
 * @return CompiledTemplates or {@code absent()} if compilation fails, in which case errors will
 *     have been reported to the error reporter.
 */
public static Optional<CompiledTemplates> compile(final TemplateRegistry registry, boolean developmentMode, ErrorReporter reporter) {
    final Stopwatch stopwatch = Stopwatch.createStarted();
    ErrorReporter.Checkpoint checkpoint = reporter.checkpoint();
    if (reporter.errorsSince(checkpoint)) {
        return Optional.absent();
    }
    CompiledTemplateRegistry compilerRegistry = new CompiledTemplateRegistry(registry);
    if (developmentMode) {
        CompiledTemplates templates = new CompiledTemplates(compilerRegistry.getDelegateTemplateNames(), new CompilingClassLoader(compilerRegistry));
        // TODO(lukes): consider spawning a thread to load all the generated classes in the background
        return Optional.of(templates);
    }
    // TODO(lukes): once most internal users have moved to precompilation eliminate this and just
    // use the 'developmentMode' path above.  This hybrid only makes sense for production services
    // that are doing runtime compilation.  Hopefully, this will become an anomaly.
    List<ClassData> classes = compileTemplates(compilerRegistry, reporter, new CompilerListener<List<ClassData>>() {

        final List<ClassData> compiledClasses = new ArrayList<>();

        int numBytes = 0;

        int numFields = 0;

        int numDetachStates = 0;

        @Override
        public void onCompile(ClassData clazz) {
            numBytes += clazz.data().length;
            numFields += clazz.numberOfFields();
            numDetachStates += clazz.numberOfDetachStates();
            compiledClasses.add(clazz);
        }

        @Override
        public List<ClassData> getResult() {
            logger.log(Level.INFO, "Compilation took {0}\n" + "     templates: {1}\n" + "       classes: {2}\n" + "         bytes: {3}\n" + "        fields: {4}\n" + "  detachStates: {5}", new Object[] { stopwatch.toString(), registry.getAllTemplates().size(), compiledClasses.size(), numBytes, numFields, numDetachStates });
            return compiledClasses;
        }
    });
    if (reporter.errorsSince(checkpoint)) {
        return Optional.absent();
    }
    CompiledTemplates templates = new CompiledTemplates(compilerRegistry.getDelegateTemplateNames(), new MemoryClassLoader(classes));
    stopwatch.reset().start();
    templates.loadAll(compilerRegistry.getTemplateNames());
    logger.log(Level.INFO, "Loaded all classes in {0}", stopwatch);
    return Optional.of(templates);
}
Also used : Stopwatch(com.google.common.base.Stopwatch) ArrayList(java.util.ArrayList) ErrorReporter(com.google.template.soy.error.ErrorReporter) ClassData(com.google.template.soy.jbcsrc.internal.ClassData) MemoryClassLoader(com.google.template.soy.jbcsrc.internal.MemoryClassLoader) ArrayList(java.util.ArrayList) List(java.util.List) CompiledTemplates(com.google.template.soy.jbcsrc.shared.CompiledTemplates)

Example 12 with CompiledTemplates

use of com.google.template.soy.jbcsrc.shared.CompiledTemplates in project closure-templates by google.

the class VeLoggingTest method renderTemplate.

private void renderTemplate(Map<String, ?> params, OutputAppendable output, String... templateBodyLines) throws IOException {
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents("{namespace ns}\n" + "{template .foo}\n" + Joiner.on("\n").join(templateBodyLines) + "\n{/template}").typeRegistry(new SoyTypeRegistry.Builder().addDescriptors(ImmutableList.of(com.google.template.soy.testing.Foo.getDescriptor())).build()).setLoggingConfig(config).addSoyFunction(new DepthFunction()).runAutoescaper(true).parse().fileSet();
    TemplateRegistry templateRegistry = new TemplateRegistry(soyTree, ErrorReporter.exploding());
    CompiledTemplates templates = BytecodeCompiler.compile(templateRegistry, false, ErrorReporter.exploding()).get();
    RenderContext ctx = TemplateTester.getDefaultContext(templates).toBuilder().hasLogger(true).build();
    RenderResult result = templates.getTemplateFactory("ns.foo").create(TemplateTester.asRecord(params), EMPTY_DICT).render(output, ctx);
    assertThat(result).isEqualTo(RenderResult.done());
}
Also used : TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) RenderContext(com.google.template.soy.jbcsrc.shared.RenderContext) SoyFileSetParserBuilder(com.google.template.soy.SoyFileSetParserBuilder) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) RenderResult(com.google.template.soy.jbcsrc.api.RenderResult) CompiledTemplates(com.google.template.soy.jbcsrc.shared.CompiledTemplates)

Example 13 with CompiledTemplates

use of com.google.template.soy.jbcsrc.shared.CompiledTemplates in project closure-templates by google.

the class DetachStateTest method testDetach_multipleNodes.

@Test
public void testDetach_multipleNodes() throws IOException {
    CompiledTemplates templates = TemplateTester.compileTemplateBody("hello", // get merged
    "{' '}", "world");
    CompiledTemplate.Factory factory = templates.getTemplateFactory("ns.foo");
    RenderContext context = getDefaultContext(templates);
    CompiledTemplate template = factory.create(EMPTY_DICT, EMPTY_DICT);
    // Basic stuff works
    TestAppendable output = new TestAppendable();
    assertThat(template.render(output, context)).isEqualTo(RenderResult.done());
    assertThat(output.toString()).isEqualTo("hello world");
    output = new TestAppendable();
    output.softLimitReached = true;
    // detached!!!
    assertThat(template.render(output, context)).isEqualTo(RenderResult.limited());
    assertThat(output.toString()).isEqualTo("hello");
    assertThat(template.render(output, context)).isEqualTo(RenderResult.limited());
    assertThat(output.toString()).isEqualTo("hello ");
    assertThat(template.render(output, context)).isEqualTo(RenderResult.limited());
    assertThat(output.toString()).isEqualTo("hello world");
    assertThat(template.render(output, context)).isEqualTo(RenderResult.done());
    // nothing was added
    assertThat(output.toString()).isEqualTo("hello world");
}
Also used : RenderContext(com.google.template.soy.jbcsrc.shared.RenderContext) CompiledTemplates(com.google.template.soy.jbcsrc.shared.CompiledTemplates) CompiledTemplate(com.google.template.soy.jbcsrc.shared.CompiledTemplate) Test(org.junit.Test)

Example 14 with CompiledTemplates

use of com.google.template.soy.jbcsrc.shared.CompiledTemplates in project closure-templates by google.

the class DetachStateTest method testDetachOnUnResolvedProvider.

@Test
public void testDetachOnUnResolvedProvider() throws IOException {
    SettableFuture<String> future = SettableFuture.create();
    CompiledTemplates templates = TemplateTester.compileTemplateBody("{@param foo : string}", "prefix{sp}{$foo}{sp}suffix");
    CompiledTemplate.Factory factory = templates.getTemplateFactory("ns.foo");
    RenderContext context = getDefaultContext(templates);
    CompiledTemplate template = factory.create(asRecord(ImmutableMap.of("foo", future)), EMPTY_DICT);
    BufferingAppendable output = LoggingAdvisingAppendable.buffering();
    RenderResult result = template.render(output, context);
    assertThat(result.type()).isEqualTo(RenderResult.Type.DETACH);
    assertThat(result.future()).isEqualTo(future);
    assertThat(output.toString()).isEqualTo("prefix ");
    // No progress is made, our caller is an idiot and didn't wait for the future
    result = template.render(output, context);
    assertThat(result.type()).isEqualTo(RenderResult.Type.DETACH);
    assertThat(result.future()).isEqualTo(future);
    assertThat(output.toString()).isEqualTo("prefix ");
    future.set("future");
    result = template.render(output, context);
    assertThat(result).isEqualTo(RenderResult.done());
    assertThat(output.toString()).isEqualTo("prefix future suffix");
}
Also used : RenderContext(com.google.template.soy.jbcsrc.shared.RenderContext) BufferingAppendable(com.google.template.soy.data.LoggingAdvisingAppendable.BufferingAppendable) RenderResult(com.google.template.soy.jbcsrc.api.RenderResult) CompiledTemplates(com.google.template.soy.jbcsrc.shared.CompiledTemplates) CompiledTemplate(com.google.template.soy.jbcsrc.shared.CompiledTemplate) Test(org.junit.Test)

Example 15 with CompiledTemplates

use of com.google.template.soy.jbcsrc.shared.CompiledTemplates in project closure-templates by google.

the class DetachStateTest method testDetachOnParamTransclusion.

@Test
public void testDetachOnParamTransclusion() throws IOException {
    CompiledTemplates templates = TemplateTester.compileFile("{namespace ns}", "", "/** */", "{template .caller}", "  {@param callerParam : string}", "  {call .callee}", "    {param calleeParam kind=\"text\"}", "      prefix {$callerParam} suffix", "    {/param}", "  {/call}", "{/template}", "", "/** */", "{template .callee}", "  {@param calleeParam : string}", "  {$calleeParam}", "{/template}", "");
    CompiledTemplate.Factory factory = templates.getTemplateFactory("ns.caller");
    RenderContext context = getDefaultContext(templates);
    SettableFuture<String> param = SettableFuture.create();
    SoyRecord params = asRecord(ImmutableMap.of("callerParam", param));
    CompiledTemplate template = factory.create(params, EMPTY_DICT);
    BufferingAppendable output = LoggingAdvisingAppendable.buffering();
    assertThat(template.render(output, context)).isEqualTo(RenderResult.continueAfter(param));
    assertThat(output.toString()).isEqualTo("prefix ");
    param.set("foo");
    assertThat(template.render(output, context)).isEqualTo(RenderResult.done());
    assertThat(output.toString()).isEqualTo("prefix foo suffix");
}
Also used : RenderContext(com.google.template.soy.jbcsrc.shared.RenderContext) SoyRecord(com.google.template.soy.data.SoyRecord) BufferingAppendable(com.google.template.soy.data.LoggingAdvisingAppendable.BufferingAppendable) CompiledTemplates(com.google.template.soy.jbcsrc.shared.CompiledTemplates) CompiledTemplate(com.google.template.soy.jbcsrc.shared.CompiledTemplate) Test(org.junit.Test)

Aggregations

CompiledTemplates (com.google.template.soy.jbcsrc.shared.CompiledTemplates)33 Test (org.junit.Test)30 CompiledTemplate (com.google.template.soy.jbcsrc.shared.CompiledTemplate)22 RenderContext (com.google.template.soy.jbcsrc.shared.RenderContext)20 BufferingAppendable (com.google.template.soy.data.LoggingAdvisingAppendable.BufferingAppendable)14 RenderResult (com.google.template.soy.jbcsrc.api.RenderResult)6 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)5 TemplateRegistry (com.google.template.soy.soytree.TemplateRegistry)5 SoyRecord (com.google.template.soy.data.SoyRecord)3 SoyDict (com.google.template.soy.data.SoyDict)2 IOException (java.io.IOException)2 Stopwatch (com.google.common.base.Stopwatch)1 SettableFuture (com.google.common.util.concurrent.SettableFuture)1 ByteString (com.google.protobuf.ByteString)1 SoyFileSetParserBuilder (com.google.template.soy.SoyFileSetParserBuilder)1 EscapeHtmlDirective (com.google.template.soy.coredirectives.EscapeHtmlDirective)1 BasicParamStore (com.google.template.soy.data.internal.BasicParamStore)1 ParamStore (com.google.template.soy.data.internal.ParamStore)1 ErrorReporter (com.google.template.soy.error.ErrorReporter)1 ClassData (com.google.template.soy.jbcsrc.internal.ClassData)1