use of com.google.template.soy.soytree.SoyFileSetNode 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;
}
use of com.google.template.soy.soytree.SoyFileSetNode 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>");
}
use of com.google.template.soy.soytree.SoyFileSetNode in project closure-templates by google.
the class SourceLocationTest method assertSourceLocations.
private void assertSourceLocations(String asciiArtExpectedOutput, String soySourceCode) {
SoyFileSetNode soyTree = SoyFileSetParserBuilder.forSuppliers(SoyFileSupplier.Factory.create(soySourceCode, SoyFileKind.SRC, "/example/file.soy")).parse().fileSet();
String actual = new AsciiArtVisitor().exec(soyTree);
assertEquals(// fixing source locations bugs.
"REPLACE_WITH:\n\"" + actual.replaceAll("\n", "\",\n\"") + "\"\n\n", asciiArtExpectedOutput, actual);
}
use of com.google.template.soy.soytree.SoyFileSetNode 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();
}
use of com.google.template.soy.soytree.SoyFileSetNode 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());
}
Aggregations