Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Soy Templates Proto import now works #127

Merged
merged 1 commit into from
Apr 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions ToDo.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,15 @@

## High Priority (Next Features)

1. Write a demo model (in YAML) as an example with doc which illustrates
(with `bat` in `docs/use/library/script` auto-scroll PAGER, e.g. https://unix.stackexchange.com/a/77645 ?)
1. Write Web UI MVP (now that Soy templates work)

1. Support top-level `ns: "demo"` in `docs/use/library/model.textproto` and rm others

1. Implement a `FileStoreAspect` and use it in `EnolaServiceProvider`
with test coverage in `EntityServiceProviderTest`
for https://github.com/enola-dev/enola/issues/72; incl. demo+doc about it.
1. Write `docs/use/rosetta`

1. https://github.com/google/closure-templates/issues/1300 for Web UI?
1. Convert Library demo model from TextProto to YAML using `enola rosetta`, and include that YAML on `docs/use/library` MD

1. Connector with remote gRPC
1. Write a `FilestoreRepositoryAspect` demo+doc for library

1. `connectors/demo/` with test to illustrate the Service to get IDs

Expand Down Expand Up @@ -82,7 +79,7 @@

1. Upstream `CommandLineSubject` to Picocli?

1. https://unifiedjs.com ?
1. unifiedjs.com ?

1. Add to https://github.com/SquadcastHub/awesome-sre-tools

Expand Down
16 changes: 16 additions & 0 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ compat_repositories()

grpc_java_repositories()

# https://github.com/bazelbuild/rules_closure/#setup
# TODO This is only useful after https://github.com/bazelbuild/rules_closure/issues/225
#http_archive(
# name = "io_bazel_rules_closure",
# sha256 = "9498e57368efb82b985db1ed426a767cbf1ba0398fd7aed632fc3908654e1b1e",
# strip_prefix = "rules_closure-0.12.0",
# urls = [
# "https://mirror.bazel.build/github.com/bazelbuild/rules_closure/archive/0.12.0.tar.gz",
# "https://github.com/bazelbuild/rules_closure/archive/0.12.0.tar.gz",
# ],
#)

# load("@io_bazel_rules_closure//closure:repositories.bzl", "rules_closure_dependencies", "rules_closure_toolchains")
# rules_closure_dependencies()
# rules_closure_toolchains()

# https://github.com/aignas/rules_shellcheck
# TODO Replace with bazel_dep in MODULE.bazel, see https://github.com/aignas/rules_shellcheck/issues/16
http_archive(
Expand Down
9 changes: 9 additions & 0 deletions core/impl/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ java_library(
],
)

# TODO load("@io_bazel_rules_closure//closure:defs.bzl", "closure_java_template_library")
# TODO This is only useful after https://github.com/bazelbuild/rules_closure/issues/225
# closure_java_template_library(
# name = "soy",
# srcs = glob(["src/main/resources/**/*.soy"]),
# java_package = "dev.enola.core.meta.docgen",
# proto_deps = ["//core/lib:core_java_proto"],
# )

[java_test(
name = name[:-len(".java")],
size = "small",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,13 @@ public EntityKind get(ID id) {
return subMap.get(id.getEntity());
}

public void load(ReadableResource resource) throws IOException, ValidationException {
public EntityKindRepository load(ReadableResource resource)
throws IOException, ValidationException {
var kinds = new ProtoIO().read(resource, EntityKinds.newBuilder()).build();
for (var kind : kinds.getKindsList()) {
put(kind);
}
return this;
}

public Collection<EntityKind> list() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,21 @@
import java.util.List;
import java.util.stream.Collectors;

public class MarkdownDocGenerator { // TODO extends SoyGenerator {
public class MarkdownDocGenerator {

// TODO Convert this to using the (currently un-used) markdown.soy

public static final String FOOTER =
"_This model documentation was generated with ❤️ by"
+ " [Enola.dev](https://www.enola.dev)_\n";

private final Options options;

// Using Protobufs in https://github.com/google/closure-templates (AKA "Soy"
// was not straightforward, see https://github.com/google/closure-templates/issues/1300.
// For now, this is therefore implemented with a sweet good old big fat StringBuffer#append()
// TODO Possibly (re)convert this to using the (currently un-used) markdown.soy maybe later.

public MarkdownDocGenerator(Options options) {
// super("dev/enola/core/docgen/markdown.soy", "dev.enola.markdown.package");
this.options = options;
}
/*
@Override
protected Iterable<? extends Descriptors.GenericDescriptor> protoDescriptors() {
return ImmutableSet.of(ID.getDescriptor());
}
*/
public void render(EntityKindRepository kinds, Appendable md) throws IOException {
// Map<String, ?> data = ImmutableMap.of("package", "test", "kinds",
// kinds.list().stream().map(ek -> ek.getId()).collect(Collectors.toSet()));
// renderer.setData(data).renderText(md).assertDone();

public void render(EntityKindRepository kinds, Appendable md) throws IOException {
md.append("# Models\n");
if (options.diagram.equals(Options.DiagramType.Mermaid)) {
MermaidGenerator.renderMermaid(kinds, md);
Expand Down
72 changes: 72 additions & 0 deletions core/impl/src/main/java/dev/enola/core/meta/docgen/Soy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2023 The Enola <https://enola.dev> Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dev.enola.core.meta.docgen;

import com.google.common.net.MediaType;
import com.google.protobuf.Descriptors;
import com.google.template.soy.SoyFileSet;
import com.google.template.soy.jbcsrc.api.SoySauce;
import com.google.template.soy.parseinfo.TemplateName;

import dev.enola.common.io.resource.ClasspathResource;
import dev.enola.common.io.resource.ReadableResource;

import java.util.Map;

public class Soy {

// TODO Consider using closure_java_template_library Bazel rule?
// See https://github.com/bazelbuild/rules_closure/#closure_java_template_library.
// But must first contribute https://github.com/bazelbuild/rules_closure/issues/225;
// this won't work without that.

public static class Builder {
SoyFileSet.Builder sfsb = SoyFileSet.builder();

Builder addSoy(ReadableResource r) {
sfsb.add(r.charSource(), r.uri().toString());
return this;
}

Builder addSoy(String classpath) {
addSoy(new ClasspathResource(classpath, MediaType.PLAIN_TEXT_UTF_8));
return this;
}

Builder addProto(Descriptors.GenericDescriptor... descriptors) {
sfsb.addProtoDescriptors(descriptors);
return this;
}

Soy build() {
return new Soy(sfsb.build());
}
}

private final SoyFileSet sfs;
private final SoySauce sauce;

private Soy(SoyFileSet sfs) {
this.sfs = sfs;
this.sauce = sfs.compileTemplates();
}

public SoySauce.Renderer newRenderer(String templateName, Map<String, ?> params) {
return sauce.renderTemplate(TemplateName.of(templateName)).setData(params);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{namespace dev.enola.markdown}

// TODO This is not yet used, see comment in MarkdownDocGenerator.java
// TODO Actually use this (it's not, yet) in MarkdownDocGenerator.java

import {ID} from 'dev/enola/core/enola_core.proto';
// see https://github.com/google/closure-templates/issues/1300 re. path
import {ID} from 'core/lib/src/main/java/dev/enola/core/enola_core.proto';

{template helloWorld}
Hello world!
Expand Down
70 changes: 70 additions & 0 deletions core/impl/src/test/java/dev/enola/core/meta/docgen/SoyTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2023 The Enola <https://enola.dev> Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dev.enola.core.meta.docgen;

import static com.google.common.truth.Truth.assertThat;

import com.google.common.collect.ImmutableMap;

import dev.enola.common.io.resource.ClasspathResource;
import dev.enola.common.protobuf.ValidationException;
import dev.enola.core.meta.EntityKindRepository;
import dev.enola.core.meta.proto.EntityKind;
import dev.enola.core.proto.ID;

import org.junit.Test;

import java.io.IOException;
import java.util.Map;
import java.util.stream.Collectors;

public class SoyTest {

// Test coverage for using Protobufs in https://github.com/google/closure-templates (AKA "Soy")
// see https://github.com/google/closure-templates/issues/1300

@Test
public void testRender() throws ValidationException, IOException {
var soy =
new Soy.Builder()
.addSoy("dev/enola/core/docgen/markdown.soy")
.addProto(ID.getDescriptor())
.build();

var ekr = new EntityKindRepository().load(new ClasspathResource("demo-model.yaml"));
Map<String, ?> params =
ImmutableMap.of(
"package",
"test",
"kinds",
ekr.list().stream().map(EntityKind::getId).collect(Collectors.toSet()));
var renderer = soy.newRenderer("dev.enola.markdown.package", params);

var sb = new StringBuffer();
renderer.renderText(sb).assertDone();
var md = sb.toString();
assertThat(md).contains("generated with ❤️ by");
}

@Test
public void testProtoFileDescriptorName() {
assertThat(ID.getDescriptor().getFile().getName())
// This is the "path" used in `import {ID} from` in the *.soy file
.isEqualTo("core/lib/src/main/java/dev/enola/core/enola_core.proto");
}
}
3 changes: 0 additions & 3 deletions core/lib/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,6 @@ buf_proto_lint_test(
java_library(
name = "lib_java",
srcs = glob(["src/main/java/**/*.java"]),
# NB: /java/ not /resources/ for the *.proto!
# TODO Required for https://github.com/google/closure-templates/issues/1300 or not?
# resources = glob(["src/main/java/**/*.proto"]),
visibility = [
"//cli:__subpackages__",
"//connectors:__subpackages__",
Expand Down