use of com.google.template.soy.base.internal.SoyJarFileWriter 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.base.internal.SoyJarFileWriter in project closure-templates by google.
the class SoyParseInfoGenerator method compile.
@Override
void compile(SoyFileSet.Builder sfsBuilder) throws IOException {
sfsBuilder.setAllowExternalCalls(allowExternalCalls);
SoyFileSet sfs = sfsBuilder.build();
ImmutableMap<String, String> parseInfo = sfs.generateParseInfo(javaPackage, javaClassNameSource);
if (outputSrcJar == null) {
for (Map.Entry<String, String> entry : parseInfo.entrySet()) {
File outputFile = new File(outputDirectory, entry.getKey());
BaseUtils.ensureDirsExistInPath(outputFile.getPath());
Files.asCharSink(outputFile, UTF_8).write(entry.getValue());
}
} else {
String resourcePath = javaPackage.replace('.', '/') + "/";
try (SoyJarFileWriter writer = new SoyJarFileWriter(new FileOutputStream(outputSrcJar))) {
for (Map.Entry<String, String> entry : parseInfo.entrySet()) {
writer.writeEntry(resourcePath + entry.getKey(), CharSource.wrap(entry.getValue()).asByteSource(UTF_8));
}
}
}
}
use of com.google.template.soy.base.internal.SoyJarFileWriter in project closure-templates by google.
the class BytecodeCompiler method compileToJar.
/**
* Compiles all the templates in the given registry to a jar file written to the given output
* stream.
*
* <p>If errors are encountered, the error reporter will be updated and we will return. The
* contents of any data written to the sink at that point are undefined.
*
* @param registry All the templates to compile
* @param reporter The error reporter
* @param sink The output sink to write the JAR to.
*/
public static void compileToJar(TemplateRegistry registry, ErrorReporter reporter, ByteSink sink) throws IOException {
ErrorReporter.Checkpoint checkpoint = reporter.checkpoint();
if (reporter.errorsSince(checkpoint)) {
return;
}
CompiledTemplateRegistry compilerRegistry = new CompiledTemplateRegistry(registry);
if (reporter.errorsSince(checkpoint)) {
return;
}
try (final SoyJarFileWriter writer = new SoyJarFileWriter(sink.openStream())) {
compileTemplates(compilerRegistry, reporter, new CompilerListener<Void>() {
@Override
void onCompile(ClassData clazz) throws IOException {
writer.writeEntry(clazz.type().internalName() + ".class", ByteSource.wrap(clazz.data()));
}
});
}
}
Aggregations