Search in sources :

Example 66 with SoyFileSetNode

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;
}
Also used : TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) CompiledTemplates(com.google.template.soy.jbcsrc.shared.CompiledTemplates)

Example 67 with SoyFileSetNode

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>");
}
Also used : TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) EscapeHtmlDirective(com.google.template.soy.coredirectives.EscapeHtmlDirective) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) CompiledTemplates(com.google.template.soy.jbcsrc.shared.CompiledTemplates) CallDelegateNode(com.google.template.soy.soytree.CallDelegateNode) CompiledTemplate(com.google.template.soy.jbcsrc.shared.CompiledTemplate) Test(org.junit.Test)

Example 68 with SoyFileSetNode

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);
}
Also used : SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode)

Example 69 with SoyFileSetNode

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();
}
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 70 with SoyFileSetNode

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

Aggregations

SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)106 Test (org.junit.Test)81 TemplateNode (com.google.template.soy.soytree.TemplateNode)35 TemplateRegistry (com.google.template.soy.soytree.TemplateRegistry)29 ErrorReporter (com.google.template.soy.error.ErrorReporter)19 ParseResult (com.google.template.soy.SoyFileSetParser.ParseResult)13 TransitiveDepTemplatesInfo (com.google.template.soy.passes.FindTransitiveDepTemplatesVisitor.TransitiveDepTemplatesInfo)8 RawTextNode (com.google.template.soy.soytree.RawTextNode)8 MsgNode (com.google.template.soy.soytree.MsgNode)7 SoyNode (com.google.template.soy.soytree.SoyNode)6 CompiledTemplates (com.google.template.soy.jbcsrc.shared.CompiledTemplates)5 SoyMsgBundle (com.google.template.soy.msgs.SoyMsgBundle)5 PrintNode (com.google.template.soy.soytree.PrintNode)5 MsgFallbackGroupNode (com.google.template.soy.soytree.MsgFallbackGroupNode)4 SoyFileNode (com.google.template.soy.soytree.SoyFileNode)4 CompiledTemplate (com.google.template.soy.jbcsrc.shared.CompiledTemplate)3 SoyMsg (com.google.template.soy.msgs.restricted.SoyMsg)3 SoyMsgBundleImpl (com.google.template.soy.msgs.restricted.SoyMsgBundleImpl)3 PluginResolver (com.google.template.soy.soyparse.PluginResolver)3 ImmutableList (com.google.common.collect.ImmutableList)2