Search in sources :

Example 1 with PluginResolver

use of com.google.template.soy.soyparse.PluginResolver in project closure-templates by google.

the class SoyFileSet method generateParseInfo.

/**
 * Generates Java classes containing parse info (param names, template names, meta info). There
 * will be one Java class per Soy file.
 *
 * @param javaPackage The Java package for the generated classes.
 * @param javaClassNameSource Source of the generated class names. Must be one of "filename",
 *     "namespace", or "generic".
 * @return A map from generated file name (of the form "<*>SoyInfo.java") to generated file
 *     content.
 * @throws SoyCompilationException If compilation fails.
 */
ImmutableMap<String, String> generateParseInfo(String javaPackage, String javaClassNameSource) {
    resetErrorReporter();
    // TODO(lukes): see if we can enforce that globals are provided at compile time here. given that
    // types have to be, this should be possible.  Currently it is disabled for backwards
    // compatibility
    // N.B. we do not run the optimizer here for 2 reasons:
    // 1. it would just waste time, since we are not running code generation the optimization work
    // doesn't help anything
    // 2. it potentially removes metadata from the tree by precalculating expressions. For example,
    // trivial print nodes are evaluated, which can remove globals from the tree, but the
    // generator requires data about globals to generate accurate proto descriptors.  Also, the
    // ChangeCallsToPassAllData pass will change the params of templates.
    ParseResult result = parse(passManagerBuilder(SyntaxVersion.V2_0).allowUnknownGlobals().optimize(false), typeRegistry, new PluginResolver(// we allow undefined plugins since they typically aren't provided :(
    PluginResolver.Mode.ALLOW_UNDEFINED, printDirectives, soyFunctionMap, errorReporter));
    throwIfErrorsPresent();
    SoyFileSetNode soyTree = result.fileSet();
    TemplateRegistry registry = result.registry();
    // Do renaming of package-relative class names.
    ImmutableMap<String, String> parseInfo = new GenerateParseInfoVisitor(javaPackage, javaClassNameSource, registry).exec(soyTree);
    throwIfErrorsPresent();
    reportWarnings();
    return parseInfo;
}
Also used : TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) ParseResult(com.google.template.soy.SoyFileSetParser.ParseResult) GenerateParseInfoVisitor(com.google.template.soy.parseinfo.passes.GenerateParseInfoVisitor) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) PluginResolver(com.google.template.soy.soyparse.PluginResolver)

Example 2 with PluginResolver

use of com.google.template.soy.soyparse.PluginResolver 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 3 with PluginResolver

use of com.google.template.soy.soyparse.PluginResolver in project closure-templates by google.

the class SoyFileSet method doExtractMsgs.

/**
 * Performs the parsing and extraction logic.
 */
private SoyMsgBundle doExtractMsgs() {
    // extractMsgs disables a bunch of passes since it is typically not configured with things
    // like global definitions, type definitions, etc.
    SoyFileSetNode soyTree = parse(passManagerBuilder(SyntaxVersion.V1_0).allowUnknownGlobals().setTypeRegistry(SoyTypeRegistry.DEFAULT_UNKNOWN).disableAllTypeChecking(), // can't resolve strict types
    SoyTypeRegistry.DEFAULT_UNKNOWN, new PluginResolver(PluginResolver.Mode.ALLOW_UNDEFINED, printDirectives, soyFunctionMap, errorReporter)).fileSet();
    throwIfErrorsPresent();
    SoyMsgBundle bundle = new ExtractMsgsVisitor().exec(soyTree);
    throwIfErrorsPresent();
    return bundle;
}
Also used : SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) PluginResolver(com.google.template.soy.soyparse.PluginResolver) SoyMsgBundle(com.google.template.soy.msgs.SoyMsgBundle) ExtractMsgsVisitor(com.google.template.soy.msgs.internal.ExtractMsgsVisitor)

Aggregations

PluginResolver (com.google.template.soy.soyparse.PluginResolver)3 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)3 ParseResult (com.google.template.soy.SoyFileSetParser.ParseResult)2 SoyMsgBundle (com.google.template.soy.msgs.SoyMsgBundle)2 ExtractMsgsVisitor (com.google.template.soy.msgs.internal.ExtractMsgsVisitor)2 TemplateRegistry (com.google.template.soy.soytree.TemplateRegistry)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 SoyMsg (com.google.template.soy.msgs.restricted.SoyMsg)1 SoyMsgBundleImpl (com.google.template.soy.msgs.restricted.SoyMsgBundleImpl)1 GenerateParseInfoVisitor (com.google.template.soy.parseinfo.passes.GenerateParseInfoVisitor)1 FindTransitiveDepTemplatesVisitor (com.google.template.soy.passes.FindTransitiveDepTemplatesVisitor)1 TransitiveDepTemplatesInfo (com.google.template.soy.passes.FindTransitiveDepTemplatesVisitor.TransitiveDepTemplatesInfo)1 SoyFileNode (com.google.template.soy.soytree.SoyFileNode)1 TemplateNode (com.google.template.soy.soytree.TemplateNode)1