Search in sources :

Example 31 with ParseResult

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();
}
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) CopyState(com.google.template.soy.basetree.CopyState) IOException(java.io.IOException) SoyMsgBundle(com.google.template.soy.msgs.SoyMsgBundle) File(java.io.File) SoyMsgBundleHandler(com.google.template.soy.msgs.SoyMsgBundleHandler)

Example 32 with ParseResult

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());
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) ParseResult(com.google.template.soy.SoyFileSetParser.ParseResult) ImmutableList(com.google.common.collect.ImmutableList) SoyMsgBundleImpl(com.google.template.soy.msgs.restricted.SoyMsgBundleImpl) PluginResolver(com.google.template.soy.soyparse.PluginResolver) FindTransitiveDepTemplatesVisitor(com.google.template.soy.passes.FindTransitiveDepTemplatesVisitor) SoyFileNode(com.google.template.soy.soytree.SoyFileNode) ExtractMsgsVisitor(com.google.template.soy.msgs.internal.ExtractMsgsVisitor) TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) SoyMsg(com.google.template.soy.msgs.restricted.SoyMsg) ImmutableSet(com.google.common.collect.ImmutableSet) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) TransitiveDepTemplatesInfo(com.google.template.soy.passes.FindTransitiveDepTemplatesVisitor.TransitiveDepTemplatesInfo) SoyMsgBundle(com.google.template.soy.msgs.SoyMsgBundle)

Example 33 with ParseResult

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;
}
Also used : SyntaxVersion(com.google.template.soy.basetree.SyntaxVersion) ParseResult(com.google.template.soy.SoyFileSetParser.ParseResult)

Example 34 with ParseResult

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;
}
Also used : ParseResult(com.google.template.soy.SoyFileSetParser.ParseResult) PassManager(com.google.template.soy.passes.PassManager)

Example 35 with 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();
}
Also used : ParseResult(com.google.template.soy.SoyFileSetParser.ParseResult)

Aggregations

ParseResult (com.google.template.soy.SoyFileSetParser.ParseResult)38 Test (org.junit.Test)21 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)13 TemplateRegistry (com.google.template.soy.soytree.TemplateRegistry)13 TemplateNode (com.google.template.soy.soytree.TemplateNode)10 TransitiveDepTemplatesInfo (com.google.template.soy.passes.FindTransitiveDepTemplatesVisitor.TransitiveDepTemplatesInfo)8 SoyRecord (com.google.template.soy.data.SoyRecord)2 IncrementalDomSrcMain (com.google.template.soy.incrementaldomsrc.IncrementalDomSrcMain)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 SoyTypeRegistry (com.google.template.soy.types.SoyTypeRegistry)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Injector (com.google.inject.Injector)1 SoyFileSetParserBuilder (com.google.template.soy.SoyFileSetParserBuilder)1 SoyModule (com.google.template.soy.SoyModule)1 UniqueNameGenerator (com.google.template.soy.base.internal.UniqueNameGenerator)1 CopyState (com.google.template.soy.basetree.CopyState)1 SyntaxVersion (com.google.template.soy.basetree.SyntaxVersion)1