Search in sources :

Example 1 with SoyJarFileWriter

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));
            }
        }
    }
}
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 2 with SoyJarFileWriter

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));
            }
        }
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) SoyJarFileWriter(com.google.template.soy.base.internal.SoyJarFileWriter) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) File(java.io.File)

Example 3 with SoyJarFileWriter

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()));
            }
        });
    }
}
Also used : ErrorReporter(com.google.template.soy.error.ErrorReporter) ClassData(com.google.template.soy.jbcsrc.internal.ClassData) IOException(java.io.IOException) SoyJarFileWriter(com.google.template.soy.base.internal.SoyJarFileWriter)

Aggregations

SoyJarFileWriter (com.google.template.soy.base.internal.SoyJarFileWriter)3 ImmutableMap (com.google.common.collect.ImmutableMap)1 ErrorReporter (com.google.template.soy.error.ErrorReporter)1 ClassData (com.google.template.soy.jbcsrc.internal.ClassData)1 SoyFileNode (com.google.template.soy.soytree.SoyFileNode)1 TemplateNode (com.google.template.soy.soytree.TemplateNode)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1