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);
}
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());
}
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");
}
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");
}
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");
}
Aggregations