use of com.google.template.soy.SoyFileSetParser.ParseResult 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.SoyFileSetParser.ParseResult 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());
}
use of com.google.template.soy.SoyFileSetParser.ParseResult in project closure-templates by google.
the class SoyFileSet method preprocessIncrementalDOMResults.
/**
* Prepares the parsed result for use in generating Incremental DOM source code.
*/
@SuppressWarnings("deprecation")
private ParseResult preprocessIncrementalDOMResults() {
SyntaxVersion declaredSyntaxVersion = generalOptions.getDeclaredSyntaxVersion(SyntaxVersion.V2_0);
Preconditions.checkState(declaredSyntaxVersion.num >= SyntaxVersion.V2_0.num, "Incremental DOM code generation only supports syntax version of V2 or higher.");
requireStrictAutoescaping();
// For incremental dom backend, we don't desugar HTML nodes since it requires HTML context.
ParseResult result = parse(passManagerBuilder(SyntaxVersion.V2_0).desugarHtmlNodes(false));
throwIfErrorsPresent();
return result;
}
use of com.google.template.soy.SoyFileSetParser.ParseResult in project closure-templates by google.
the class SoyFileSet method preprocessJsSrcResults.
@SuppressWarnings("deprecation")
private ParseResult preprocessJsSrcResults(SoyJsSrcOptions jsSrcOptions) {
resetErrorReporter();
// Synchronize old and new ways to declare syntax version V1.
if (jsSrcOptions.shouldAllowDeprecatedSyntax()) {
generalOptions.setDeclaredSyntaxVersionName("1.0");
}
// JS has traditionally allowed unknown globals, as a way for soy to reference normal js enums
// and constants. For consistency/reusability of templates it would be nice to not allow that
// but the cat is out of the bag.
PassManager.Builder builder = passManagerBuilder(SyntaxVersion.V2_0).allowUnknownGlobals().desugarHtmlNodes(false);
ParseResult parseResult = parse(builder);
throwIfErrorsPresent();
return parseResult;
}
use of com.google.template.soy.SoyFileSetParser.ParseResult in project closure-templates by google.
the class SimplifyVisitorTest method simplifySoyFiles.
private SoyFileSetNode simplifySoyFiles(String... soyFileContents) throws Exception {
ParseResult parse = SoyFileSetParserBuilder.forFileContents(soyFileContents).parse();
SimplifyVisitor simplifyVisitor = SimplifyVisitor.create();
simplifyVisitor.simplify(parse.fileSet(), parse.registry());
return parse.fileSet();
}
Aggregations