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