Search in sources :

Example 1 with JsSrcMain

use of com.google.template.soy.jssrc.internal.JsSrcMain in project closure-templates by google.

the class SoyFileSet method compileToJsSrc.

/**
 * Compiles this Soy file set into JS source code files and returns these JS files as a list of
 * strings, one per file.
 *
 * @param jsSrcOptions The compilation options for the JS Src output target.
 * @param msgBundle The bundle of translated messages, or null to use the messages from the Soy
 *     source.
 * @return A list of strings where each string represents the JS source code that belongs in one
 *     JS file. The generated JS files correspond one-to-one to the original Soy source files.
 * @throws SoyCompilationException If compilation fails.
 */
@SuppressWarnings("deprecation")
public List<String> compileToJsSrc(SoyJsSrcOptions jsSrcOptions, @Nullable SoyMsgBundle msgBundle) {
    ParseResult result = preprocessJsSrcResults(jsSrcOptions);
    TemplateRegistry registry = result.registry();
    SoyFileSetNode fileSet = result.fileSet();
    List<String> generatedSrcs = new JsSrcMain(apiCallScopeProvider, typeRegistry).genJsSrc(fileSet, registry, jsSrcOptions, msgBundle, errorReporter);
    throwIfErrorsPresent();
    reportWarnings();
    return generatedSrcs;
}
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)

Example 2 with JsSrcMain

use of com.google.template.soy.jssrc.internal.JsSrcMain 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)

Aggregations

ParseResult (com.google.template.soy.SoyFileSetParser.ParseResult)2 JsSrcMain (com.google.template.soy.jssrc.internal.JsSrcMain)2 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)2 TemplateRegistry (com.google.template.soy.soytree.TemplateRegistry)2 CopyState (com.google.template.soy.basetree.CopyState)1 SoyMsgBundle (com.google.template.soy.msgs.SoyMsgBundle)1 SoyMsgBundleHandler (com.google.template.soy.msgs.SoyMsgBundleHandler)1 File (java.io.File)1 IOException (java.io.IOException)1