/** * Compile up our filesets */ private void compile() { // Compile and store (Note: We don't generate JS as it will be rare to // use this here) tofu = generateSoyFileSet(SOY_EXTENSION).compileToTofu(); }
@Override public Collection<String> compileToJsSrc(Collection<URL> templates, @Nullable SoyMsgBundle soyMsgBundle) { Preconditions.checkNotNull("soyJsSrcOptions", soyJsSrcOptions); logger.debug("SoyJavaScript compilation of template:" + templates); final long time1 = System.currentTimeMillis(); final SoyFileSet soyFileSet = buildSoyFileSetFrom(templates); final List<String> ret = soyFileSet.compileToJsSrc(soyJsSrcOptions, soyMsgBundle); final long time2 = System.currentTimeMillis(); logger.debug("SoyJavaScript compilation complete." + (time2 - time1) + " ms"); return ret; }
/** * Constructs a closure template engine with the given soy file paths. * * @param soyFilePaths one or more paths to .soy files relative to /templates in the resources folder. * @throws IllegalArgumentException if the input is null. */ public ClosureTemplateEngine(final String... soyFilePaths) { Preconditions.checkNotNull(soyFilePaths, "at least one path to soy file must be specified"); SoyFileSet.Builder sfsBuilder = SoyFileSet.builder(); for (final String soyFilePath : soyFilePaths) { sfsBuilder = sfsBuilder.add(ClosureTemplateEngine.class.getResource("/templates/" + soyFilePath)); } soyTofu = sfsBuilder.build().compileToTofu(); }
@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)); } } } } }
@Override void compile(SoyFileSet.Builder sfsBuilder) throws IOException { sfsBuilder.setAllowExternalCalls(allowExternalCalls); SoyFileSet sfs = sfsBuilder.build(); // Create SoyJsSrcOptions. SoyJsSrcOptions jsSrcOptions = new SoyJsSrcOptions(); jsSrcOptions.setShouldAllowDeprecatedSyntax(shouldAllowDeprecatedSyntax); jsSrcOptions.setShouldGenerateGoogMsgDefs(shouldGenerateGoogMsgDefs); jsSrcOptions.setGoogMsgsAreExternal(googMsgsAreExternal); jsSrcOptions.setBidiGlobalDir(bidiGlobalDir); jsSrcOptions.setUseGoogIsRtlForBidiGlobalDir(useGoogIsRtlForBidiGlobalDir); // Compile. boolean generateLocalizedJs = !locales.isEmpty(); if (generateLocalizedJs) { sfs.compileToJsSrcFiles( outputPathFormat, jsSrcOptions, locales, messagePlugin, messageFilePathFormat); } else { sfs.compileToJsSrcFiles(outputPathFormat, jsSrcOptions, locales, null, null); } } }
sfsBuilder = injector.getInstance(SoyFileSet.Builder.class); } else { sfsBuilder = SoyFileSet.builder();
@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)); } } } } }
sfs.compileToJsSrcFiles( outputPathFormat, inputPrefix, messageFilePathFormat); } else { sfs.compileToJsSrcFiles(outputPathFormat, inputPrefix, jsSrcOptions, locales, null, null);
/** * Compiles this Soy file set into a Java object (type {@code SoyTofu}) capable of rendering the * compiled templates. The resulting {@code SoyTofu} does not cache intermediate results after * substitutions from the SoyMsgBundle and the SoyCssRenamingMap. * * @see #compileToTofu(com.google.template.soy.tofu.SoyTofuOptions) * * @return The resulting {@code SoyTofu} object. * @throws SoySyntaxException If a syntax error is found. */ public SoyTofu compileToTofu() throws SoySyntaxException { return compileToTofu(new SoyTofuOptions()); }
/** * Prints the generated HTML to stdout. * * @param args Not used. */ public static void main(String[] args) { // Compile the template. SoyFileSet sfs = SoyFileSet.builder().add(Resources.getResource("simple.soy")).build(); SoyTofu tofu = sfs.compileToTofu(); // Example 1. writeExampleHeader(); System.out.println(tofu.newRenderer("soy.examples.simple.helloWorld").render()); // Create a namespaced tofu object to make calls more concise. SoyTofu simpleTofu = tofu.forNamespace("soy.examples.simple"); // Example 2. writeExampleHeader(); System.out.println( simpleTofu.newRenderer(".helloName").setData(ImmutableMap.of("name", "Ana")).render()); // Example 3. writeExampleHeader(); System.out.println( simpleTofu .newRenderer(".helloNames") .setData(ImmutableMap.of("names", ImmutableList.of("Bob", "Cid", "Dee"))) .render()); }
public List<String> renderJs(List<File> files) { SoyFileSet sfs = createSoyFileSet(files); return sfs.compileToJsSrc(jsSrcOptions, null); } }
sfs.generateParseInfo(javaPackage, javaClassNameSource);
sfs.compileToJsSrcFiles(outputPathFormat, inputPrefix, jsSrcOptions, locales, null); sfs.compileToJsSrcFiles( outputPathFormat, inputPrefix, jsSrcOptions, locales, messageFilePathFormat);
/** * Compiles this Soy file set into a Java object (type {@code SoyTofu}) capable of rendering the * compiled templates. The resulting {@code SoyTofu} does not cache intermediate results after * substitutions from the SoyMsgBundle and the SoyCssRenamingMap. * * @see #compileToTofu() * * @return The result of compiling this Soy file set into a Java object. * @throws SoySyntaxException If a syntax error is found. * @deprecated Use {@link #compileToTofu()}. */ @Deprecated public SoyTofu compileToJavaObj() throws SoySyntaxException { return compileToTofu(new SoyTofuOptions()); }
/** * Prints the generated HTML to stdout. * * @param args Not used. */ public static void main(String[] args) { // Compile the template. SoyFileSet sfs = SoyFileSet.builder().add(Resources.getResource("simple.soy")).build(); SoyTofu tofu = sfs.compileToTofu(); // Example 1. writeExampleHeader(); System.out.println(tofu.newRenderer("soy.examples.simple.helloWorld").render()); // Create a namespaced tofu object to make calls more concise. SoyTofu simpleTofu = tofu.forNamespace("soy.examples.simple"); // Example 2. writeExampleHeader(); System.out.println( simpleTofu.newRenderer(".helloName").setData(new SoyMapData("name", "Ana")).render()); // Example 3. writeExampleHeader(); System.out.println( simpleTofu .newRenderer(".helloNames") .setData(new SoyMapData("names", new SoyListData("Bob", "Cid", "Dee"))) .render()); }
public String load(Locale locale) { SoyJsSrcOptions opts = new SoyJsSrcOptions(); // FUTURE: Allow configuration of opts via config? List<String> js = generateSoyFileSet(JS_SOY_EXTENSION).compileToJsSrc(opts, msgBundleCache.getUnchecked(locale)); StringBuilder sb = new StringBuilder(); for (String s : js) { sb.append(s).append("\r\n"); } return sb.toString(); }
@Override public SoyTofu compile(@Nullable final Collection<URL> urls) throws IOException { Preconditions.checkNotNull("compileTimeGlobalModelResolver", compileTimeGlobalModelResolver); if (urls == null || urls.isEmpty()) { throw new IOException("Unable to compile, no urls found"); } logger.debug("SoyTofu compilation of templates:" + urls.size()); final long time1 = System.currentTimeMillis(); final SoyFileSet.Builder sfsBuilder = new SoyFileSet.Builder(); for (final URL url : urls) { sfsBuilder.add(url); } addCompileTimeGlobalModel(sfsBuilder); final SoyFileSet soyFileSet = sfsBuilder.build(); final SoyTofuOptions soyTofuOptions = createSoyTofuOptions(); final SoyTofu soyTofu = soyFileSet.compileToTofu(soyTofuOptions); final long time2 = System.currentTimeMillis(); logger.debug("SoyTofu compilation complete." + (time2 - time1) + " ms"); return soyTofu; }
SoyFileSet.Builder sfsBuilder = SoyFileSet.builder(); SoyFileSet sfs = sfsBuilder .setCompileTimeGlobals(Resources.getResource("FeaturesUsage_globals.txt")) .build(); SoyTofu tofu = sfs.compileToTofu().forNamespace("soy.examples.features");
@Override protected String transform(String originalContent) { List<Module> guiceModules = new ArrayList<Module>(); guiceModules.add(new SoyModule()); guiceModules.add(new XliffMsgPluginModule()); guiceModules.add(new PicoBridgeModule()); guiceModules.add(new OurFunctionsModule()); final Injector injector = Guice.createInjector(guiceModules); SoyFileSet.Builder sfsBuilder = injector.getInstance(SoyFileSet.Builder.class); SoyJsSrcOptions jsSrcOptions = new SoyJsSrcOptions(); jsSrcOptions.setShouldGenerateJsdoc(false); sfsBuilder.add(originalContent, location); SoyFileSet sfs = sfsBuilder.build(); final List<String> output = sfs.compileToJsSrc(jsSrcOptions, null); if (output.size() != 1) { throw new IllegalStateException(i18n.getText("wfshare.exception.soy.did.not.manage.to.compile.soy.template", output.size())); } return output.get(0); } }
SoyTofuOptions options = new SoyTofuOptions(); options.setUseCaching(useCaching); return compileToTofu(options);