Search in sources :

Example 1 with SoyFileNode

use of com.google.template.soy.soytree.SoyFileNode in project closure-templates by google.

the class JsSrcMain method genJsFiles.

/**
 * Generates JS source files given a Soy parse tree, an options object, an optional bundle of
 * translated messages, and information on where to put the output files.
 *
 * @param soyTree The Soy parse tree to generate JS source code for.
 * @param templateRegistry The template registry that contains all the template information.
 * @param jsSrcOptions The compilation options relevant to this backend.
 * @param locale The current locale that we're generating JS for, or null if not applicable.
 * @param msgBundle The bundle of translated messages, or null to use the messages from the Soy
 *     source.
 * @param outputPathFormat The format string defining how to build the output file path
 *     corresponding to an input file path.
 * @param inputPathsPrefix The input path prefix, or empty string if none.
 * @param errorReporter The Soy error reporter that collects errors during code generation.
 * @throws IOException If there is an error in opening/writing an output JS file.
 */
public void genJsFiles(SoyFileSetNode soyTree, TemplateRegistry templateRegistry, SoyJsSrcOptions jsSrcOptions, @Nullable String locale, @Nullable SoyMsgBundle msgBundle, String outputPathFormat, String inputPathsPrefix, ErrorReporter errorReporter) throws IOException {
    List<String> jsFileContents = genJsSrc(soyTree, templateRegistry, jsSrcOptions, msgBundle, errorReporter);
    ImmutableList<SoyFileNode> srcsToCompile = ImmutableList.copyOf(Iterables.filter(soyTree.getChildren(), SoyFileNode.MATCH_SRC_FILENODE));
    if (srcsToCompile.size() != jsFileContents.size()) {
        throw new AssertionError(String.format("Expected to generate %d code chunk(s), got %d", srcsToCompile.size(), jsFileContents.size()));
    }
    Multimap<String, Integer> outputs = MainEntryPointUtils.mapOutputsToSrcs(locale, outputPathFormat, inputPathsPrefix, srcsToCompile);
    for (String outputFilePath : outputs.keySet()) {
        Writer out = Files.newWriter(new File(outputFilePath), UTF_8);
        try {
            boolean isFirst = true;
            for (int inputFileIndex : outputs.get(outputFilePath)) {
                if (isFirst) {
                    isFirst = false;
                } else {
                    // Concatenating JS files is not safe unless we know that the last statement from one
                    // couldn't combine with the isFirst statement of the next.  Inserting a semicolon will
                    // prevent this from happening.
                    out.write("\n;\n");
                }
                out.write(jsFileContents.get(inputFileIndex));
            }
        } finally {
            out.close();
        }
    }
}
Also used : SoyFileNode(com.google.template.soy.soytree.SoyFileNode) File(java.io.File) Writer(java.io.Writer)

Example 2 with SoyFileNode

use of com.google.template.soy.soytree.SoyFileNode in project closure-templates by google.

the class BytecodeCompiler method writeSrcJar.

/**
 * Writes the source files out to a {@code -src.jar}. This places the soy files at the same
 * classpath relative location as their generated classes. Ultimately this can be used by
 * debuggers for source level debugging.
 *
 * <p>It is a little weird that the relative locations of the generated classes are not identical
 * to the input source files. This is due to the disconnect between java packages and soy
 * namespaces. We should consider using the soy namespace directly as a java package in the
 * future.
 *
 * @param registry All the templates in the current compilation unit
 * @param files The source files by file path
 * @param sink The source to write the jar file
 */
public static void writeSrcJar(TemplateRegistry registry, ImmutableMap<String, SoyFileSupplier> files, ByteSink sink) throws IOException {
    Set<SoyFileNode> seenFiles = new HashSet<>();
    try (SoyJarFileWriter writer = new SoyJarFileWriter(sink.openStream())) {
        for (TemplateNode template : registry.getAllTemplates()) {
            SoyFileNode file = template.getParent();
            if (file.getSoyFileKind() == SoyFileKind.SRC && seenFiles.add(file)) {
                String namespace = file.getNamespace();
                String fileName = file.getFileName();
                writer.writeEntry(Names.javaFileName(namespace, fileName), files.get(file.getFilePath()).asCharSource().asByteSource(UTF_8));
            }
        }
    }
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) SoyJarFileWriter(com.google.template.soy.base.internal.SoyJarFileWriter) SoyFileNode(com.google.template.soy.soytree.SoyFileNode) HashSet(java.util.HashSet)

Example 3 with SoyFileNode

use of com.google.template.soy.soytree.SoyFileNode in project closure-templates by google.

the class MainEntryPointUtils method mapOutputsToSrcs.

/**
 * Maps output paths to indices of inputs that should be emitted to them.
 *
 * @param locale The locale for the file path, or null if not applicable.
 * @param outputPathFormat The format string defining how to format output file paths.
 * @param inputPathsPrefix The input path prefix, or empty string if none.
 * @param fileNodes A list of the SoyFileNodes being written.
 * @return A map of output file paths to their respective input indicies.
 */
public static Multimap<String, Integer> mapOutputsToSrcs(@Nullable String locale, String outputPathFormat, String inputPathsPrefix, ImmutableList<SoyFileNode> fileNodes) {
    Multimap<String, Integer> outputs = ArrayListMultimap.create();
    // contains no wildcards.
    for (int i = 0; i < fileNodes.size(); ++i) {
        SoyFileNode inputFile = fileNodes.get(i);
        String inputFilePath = inputFile.getFilePath();
        String outputFilePath = MainEntryPointUtils.buildFilePath(outputPathFormat, locale, inputFilePath, inputPathsPrefix);
        BaseUtils.ensureDirsExistInPath(outputFilePath);
        outputs.put(outputFilePath, i);
    }
    return outputs;
}
Also used : SoyFileNode(com.google.template.soy.soytree.SoyFileNode)

Example 4 with SoyFileNode

use of com.google.template.soy.soytree.SoyFileNode in project closure-templates by google.

the class HtmlRewritePassTest method runPass.

/**
 * Parses the given input as a template content.
 */
private static TemplateNode runPass(String input, ErrorReporter errorReporter) {
    String soyFile = Joiner.on('\n').join("{namespace ns}", "", "{template .t stricthtml=\"false\"}", input, "{/template}");
    SoyFileNode node = SoyFileSetParserBuilder.forFileContents(soyFile).desugarHtmlNodes(false).errorReporter(errorReporter).parse().fileSet().getChild(0);
    if (node != null) {
        return node.getChild(0);
    }
    return null;
}
Also used : SoyFileNode(com.google.template.soy.soytree.SoyFileNode)

Example 5 with SoyFileNode

use of com.google.template.soy.soytree.SoyFileNode in project closure-templates by google.

the class HtmlRewritePassTest method assertThatASTString.

private static StringSubject assertThatASTString(TemplateNode node) {
    SoyFileNode parent = node.getParent().copy(new CopyState());
    new CombineConsecutiveRawTextNodesPass().run(parent);
    return assertThat(SoyTreeUtils.buildAstString(parent.getChild(0), 0, new StringBuilder()).toString());
}
Also used : CopyState(com.google.template.soy.basetree.CopyState) SoyFileNode(com.google.template.soy.soytree.SoyFileNode)

Aggregations

SoyFileNode (com.google.template.soy.soytree.SoyFileNode)20 TemplateNode (com.google.template.soy.soytree.TemplateNode)7 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)4 Test (org.junit.Test)4 TemplateRegistry (com.google.template.soy.soytree.TemplateRegistry)3 File (java.io.File)3 Writer (java.io.Writer)3 ImmutableList (com.google.common.collect.ImmutableList)2 IncrementingIdGenerator (com.google.template.soy.base.internal.IncrementingIdGenerator)2 CopyState (com.google.template.soy.basetree.CopyState)2 ErrorReporter (com.google.template.soy.error.ErrorReporter)2 SoyMsgBundle (com.google.template.soy.msgs.SoyMsgBundle)2 SoyMsg (com.google.template.soy.msgs.restricted.SoyMsg)2 SoyMsgBundleImpl (com.google.template.soy.msgs.restricted.SoyMsgBundleImpl)2 HashSet (java.util.HashSet)2 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 ParseResult (com.google.template.soy.SoyFileSetParser.ParseResult)1 SourceLocation (com.google.template.soy.base.SourceLocation)1 IdGenerator (com.google.template.soy.base.internal.IdGenerator)1