com.google.template.soy.SoyFileSet Java Examples

The following examples show how to use com.google.template.soy.SoyFileSet. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SoyTemplateUtils.java    From nomulus with Apache License 2.0 6 votes vote down vote up
/** Returns a memoized supplier containing compiled tofu. */
public static Supplier<SoyTofu> createTofuSupplier(final SoyFileInfo... soyInfos) {
  return memoize(
      () -> {
        ConsoleDebug debugMode = ConsoleDebug.get();
        SoyFileSet.Builder builder = SoyFileSet.builder();
        for (SoyFileInfo soyInfo : soyInfos) {
          builder.add(getResource(soyInfo.getClass(), soyInfo.getFileName()));
        }
        Map<String, Object> globals;
        try {
          globals =
              new HashMap<>(SoyUtils.parseCompileTimeGlobals(asCharSource(SOY_GLOBALS, UTF_8)));
        } catch (IOException e) {
          throw new RuntimeException("Failed to load soy globals", e);
        }
        globals.put("DEBUG", debugMode.ordinal());
        builder.setCompileTimeGlobals(globals);
        return builder.build().compileToTofu();
      });
}
 
Example #2
Source File: Renderer.java    From js-dossier with Apache License 2.0 6 votes vote down vote up
@Inject
Renderer(
    Provider<SoyFileSet.Builder> filesetBuilderProvider,
    ImmutableSet<Descriptors.GenericDescriptor> descriptors,
    JsonRenderer jsonRenderer) {
  this.filesetBuilderProvider = filesetBuilderProvider;
  this.tofu =
      filesetBuilderProvider
          .get()
          .add(Renderer.class.getResource("resources/types.soy"))
          .add(Renderer.class.getResource("resources/dossier.soy"))
          .addProtoDescriptors(descriptors)
          .build()
          .compileToTofu();
  this.jsonRenderer = jsonRenderer;
}
 
Example #3
Source File: TemplateTest.java    From gcp-token-broker with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setup() {
    SoyFileSet sfs = SoyFileSet.builder()
        .add(Resources.getResource("index.soy"))
        .add(Resources.getResource("success.soy"))
        .build();
    soySauce = sfs.compileTemplates();
}
 
Example #4
Source File: CoverPageGenerator.java    From nomulus with Apache License 2.0 5 votes vote down vote up
/** Returns a lazily created soy renderer */
private SoyTofu getTofu() {
  if (tofu == null) {
    tofu =
        SoyFileSet.builder()
            .add(getResource(CoverPageGenerator.class, "soy/coverpage.soy"))
            .build()
            .compileToTofu();
  }
  return tofu;
}
 
Example #5
Source File: EppToolCommand.java    From nomulus with Apache License 2.0 5 votes vote down vote up
protected void addSoyRecord(String clientId, SoyRecord record) {
  checkNotNull(soyFileInfo, "SoyFileInfo is missing, cannot add record.");
  checkNotNull(soyRenderer, "SoyRenderer is missing, cannot add record.");
  addXmlCommand(clientId, SoyFileSet.builder()
      .add(getResource(soyFileInfo.getClass(), soyFileInfo.getFileName()))
      .build()
      .compileToTofu()
      .newRenderer(soyRenderer)
      .setData(record)
      .render());
}
 
Example #6
Source File: ClosureTemplateEngine.java    From spark-template-engines with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
}
 
Example #7
Source File: DefaultTofuCompiler.java    From spring-soy-view with Apache License 2.0 5 votes vote down vote up
private void addCompileTimeGlobalModel(final SoyFileSet.Builder sfsBuilder) {
    final Optional<SoyMapData> soyMapData = compileTimeGlobalModelResolver.resolveData();
    if (soyMapData.isPresent()) {
        final Map<String, ?> mapData = soyMapData.get().asMap();
        if (mapData.size() > 0) {
            logger.debug("Setting compile time globals, entries number:" + mapData.size());
            sfsBuilder.setCompileTimeGlobals(mapData);
        }
    }
}
 
Example #8
Source File: DefaultTofuCompiler.java    From spring-soy-view with Apache License 2.0 5 votes vote down vote up
private SoyFileSet buildSoyFileSetFrom(final Collection<URL> urls) {
    final SoyFileSet.Builder builder = new SoyFileSet.Builder();

    for (final URL url : urls) {
        builder.setAllowExternalCalls(true);
        builder.add(url);
    }

    addCompileTimeGlobalModel(builder);

    return builder.build();
}
 
Example #9
Source File: TemplateRenderer.java    From deploymentmanager-autogen with Apache License 2.0 4 votes vote down vote up
/** Use {@link FileSet#builder} to create an instance. */
@Inject
private Builder(SoyFileSet.Builder soyFileSetBuilder) {
  this.soyFileSetBuilder = soyFileSetBuilder;
}
 
Example #10
Source File: Renderer.java    From js-dossier with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws IOException {
  checkArgument(args.length > 0, "no output directory specified");

  Path outputDir = FileSystems.getDefault().getPath(args[0]);
  checkArgument(Files.isDirectory(outputDir), "not a directory: %s", outputDir);

  Injector injector = Guice.createInjector(new DossierSoyModule());

  ImmutableSet<Descriptors.GenericDescriptor> descriptors =
      injector.getInstance(
          Key.get(new TypeLiteral<ImmutableSet<Descriptors.GenericDescriptor>>() {}));
  SoyFileSet fileSet =
      injector
          .getInstance(SoyFileSet.Builder.class)
          .add(Renderer.class.getResource("resources/dossier.soy"))
          .add(Renderer.class.getResource("resources/nav.soy"))
          .add(Renderer.class.getResource("resources/types.soy"))
          .addProtoDescriptors(descriptors)
          .build();

  SoyJsSrcOptions options = new SoyJsSrcOptions();

  options.setShouldGenerateGoogModules(true);

  Pattern googModulePattern = Pattern.compile("(goog\\.module\\('.*'\\);)");
  String missingContent =
      "\n/** @suppress {extraRequire} */\n"
          + "goog.require('dossier.soyplugins');\n"
          + "/** @suppress {extraRequire} */\n"
          + "goog.require('goog.soy.data.SanitizedContent');\n";

  Iterator<Path> files =
      ImmutableList.of(
              outputDir.resolve("dossier.soy.js"),
              outputDir.resolve("nav.soy.js"),
              outputDir.resolve("types.soy.js"))
          .iterator();
  for (String string : fileSet.compileToJsSrc(options, null)) {
    Matcher matcher = googModulePattern.matcher(string);
    if (matcher.find()) {
      string = matcher.replaceFirst("$1\n" + missingContent);
    }

    Path file = files.next();
    Files.write(file, string.getBytes(StandardCharsets.UTF_8));
  }
}
 
Example #11
Source File: SanitizeHtmlFunction.java    From js-dossier with Apache License 2.0 4 votes vote down vote up
@Inject
SanitizeHtmlFunction(Provider<SoyFileSet.Builder> sfsBuilder) {
  this.sfsBuilder = sfsBuilder;
}
 
Example #12
Source File: ProtoDescriptorsToJs.java    From js-dossier with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws IOException {
  checkArgument(args.length > 0, "no output directory specified");

  final Path output = FileSystems.getDefault().getPath(args[0]);
  Injector injector =
      Guice.createInjector(
          new DossierSoyModule(),
          new AbstractModule() {
            @Override
            protected void configure() {
              Multibinder<SoyFunction> binder =
                  Multibinder.newSetBinder(binder(), SoyFunction.class);
              binder.addBinding().to(DynamicJsFunction.class);
              binder.addBinding().to(TypeNameFunction.class);
              binder.addBinding().to(ToLowerCamelCaseFunction.class);
              binder.addBinding().to(ToUpperCamelCaseFunction.class);
            }

            @Provides
            @Internal
            Path provideOutput() {
              return output;
            }

            @Provides
            @Internal
            SoyTofu provideTofu(
                SoyFileSet.Builder builder,
                ImmutableSet<Descriptors.GenericDescriptor> descriptors) {
              return builder
                  .add(ProtoDescriptorsToJs.class.getResource("resources/proto.soy"))
                  .addProtoDescriptors(descriptors)
                  .build()
                  .compileToTofu();
            }
          });

  injector
      .getInstance(ProtoDescriptorsToJs.class)
      .processFiles(
          TrustedResourceUrlProto.getDescriptor().getFile(),
          Dossier.getDescriptor(),
          Expression.getDescriptor(),
          State.getDescriptor());
}
 
Example #13
Source File: DefaultTofuCompiler.java    From spring-soy-view with Apache License 2.0 4 votes vote down vote up
@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;
}
 
Example #14
Source File: DefaultTofuCompiler.java    From spring-soy-view with Apache License 2.0 3 votes vote down vote up
@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;
}