Skip to content

Commit

Permalink
Merge pull request #8610 from jhipster/protobuf
Browse files Browse the repository at this point in the history
New module: protobuf support
  • Loading branch information
murdos authored Jan 20, 2024
2 parents c29c0ff + 3e77b57 commit 00f57bb
Show file tree
Hide file tree
Showing 21 changed files with 528 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package tech.jhipster.lite.generator.server.javatool.protobuf.application;

import org.springframework.stereotype.Service;
import tech.jhipster.lite.generator.server.javatool.protobuf.domain.ProtobufModuleFactory;
import tech.jhipster.lite.module.domain.JHipsterModule;
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;

@Service
public class ProtobufApplicationService {

private final ProtobufModuleFactory factory;

public ProtobufApplicationService() {
factory = new ProtobufModuleFactory();
}

public JHipsterModule buildProtobufModule(JHipsterModuleProperties properties) {
return factory.buildProtobufModule(properties);
}

public JHipsterModule buildProtobufBackwardsCompatibilityCheckModule(JHipsterModuleProperties properties) {
return factory.buildProtobufBackwardsCompatibilityCheckModule(properties);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package tech.jhipster.lite.generator.server.javatool.protobuf.domain;

import static tech.jhipster.lite.module.domain.JHipsterModule.*;

import tech.jhipster.lite.module.domain.JHipsterModule;
import tech.jhipster.lite.module.domain.file.JHipsterDestination;
import tech.jhipster.lite.module.domain.file.JHipsterSource;
import tech.jhipster.lite.module.domain.gradleplugin.GradleCommunityPlugin;
import tech.jhipster.lite.module.domain.javabuild.GroupId;
import tech.jhipster.lite.module.domain.javabuild.VersionSlug;
import tech.jhipster.lite.module.domain.mavenplugin.MavenPlugin;
import tech.jhipster.lite.module.domain.mavenplugin.MavenPlugin.MavenPluginOptionalBuilder;
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;
import tech.jhipster.lite.shared.error.domain.Assert;

public class ProtobufModuleFactory {

private static final JHipsterSource SOURCE = from("server/javatool/protobuf");
private static final JHipsterSource MAIN_SOURCE = SOURCE.append("main");
private static final JHipsterSource TEST_SOURCE = SOURCE.append("test");

private static final String PROTOBUF_PACKAGE = "shared/protobuf";
private static final VersionSlug PROTOBUF_VERSION_SLUG = versionSlug("protobuf");
private static final GroupId PROTOBUF_GROUPID = groupId("com.google.protobuf");

public JHipsterModule buildProtobufModule(JHipsterModuleProperties properties) {
Assert.notNull("properties", properties);

JHipsterDestination mainDestination = toSrcMainJava().append(properties.packagePath()).append(PROTOBUF_PACKAGE);
JHipsterDestination testDestination = toSrcTestJava().append(properties.packagePath()).append(PROTOBUF_PACKAGE);

//@formatter:off
return moduleBuilder(properties)
.files()
.add(MAIN_SOURCE.template("package-info.java"), mainDestination.append("package-info.java"))
.add(MAIN_SOURCE.template("ProtobufDatesReader.java"), mainDestination.append("infrastructure/primary/ProtobufDatesReader.java"))
.add(MAIN_SOURCE.template("ProtobufDatesWriter.java"), mainDestination.append("infrastructure/secondary/ProtobufDatesWriter.java"))
.add(MAIN_SOURCE.append(".gitkeep"), to("src/main/proto/.gitkeep"))
.add(
TEST_SOURCE.template("ProtobufDatesReaderTest.java"),
testDestination.append("infrastructure/primary/ProtobufDatesReaderTest.java")
)
.add(
TEST_SOURCE.template("ProtobufDatesWriterTest.java"),
testDestination.append("infrastructure/secondary/ProtobufDatesWriterTest.java")
)
.and()
.javaDependencies()
.addDependency(PROTOBUF_GROUPID, artifactId("protobuf-java"), PROTOBUF_VERSION_SLUG)
.addTestDependency(PROTOBUF_GROUPID, artifactId("protobuf-java-util"), PROTOBUF_VERSION_SLUG)
.and()
.mavenPlugins()
.pluginManagement(protobufMavenPluginManagement())
.plugin(protobufMavenPluginBuilder().build())
.and()
.mavenBuildExtensions()
.addExtension(groupId("kr.motd.maven"), artifactId("os-maven-plugin"), versionSlug("os-maven-plugin"))
.and()
.gradlePlugins()
.plugin(protobufGradlePlugin())
.and()
.build();
//@formatter:on
}

private static GradleCommunityPlugin protobufGradlePlugin() {
return gradleCommunityPlugin()
.id("com.google.protobuf")
.pluginSlug("protobuf")
.versionSlug("protobuf-plugin")
.configuration(
"""
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:${libs.versions.protobuf.asProvider().get()}"
}
}
"""
)
.build();
}

private MavenPlugin protobufMavenPluginManagement() {
return protobufMavenPluginBuilder()
.versionSlug("protobuf-maven-plugin")
.configuration(
"""
<protocArtifact>com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier}</protocArtifact>
"""
)
.addExecution(pluginExecution().goals("compile"))
.build();
}

private MavenPluginOptionalBuilder protobufMavenPluginBuilder() {
return mavenPlugin().groupId("org.xolstice.maven.plugins").artifactId("protobuf-maven-plugin");
}

public JHipsterModule buildProtobufBackwardsCompatibilityCheckModule(JHipsterModuleProperties properties) {
//@formatter:off
return moduleBuilder(properties)
.mavenPlugins()
.pluginManagement(protoBackwardsCompatibilityMavenPluginManagement())
.plugin(protoBackwardsCompatibilityMavenPluginBuilder().build())
.and()
.build();
//@formatter:on
}

private MavenPlugin protoBackwardsCompatibilityMavenPluginManagement() {
return protoBackwardsCompatibilityMavenPluginBuilder()
.versionSlug("proto-backwards-compatibility")
.addExecution(pluginExecution().goals("backwards-compatibility-check"))
.build();
}

private MavenPluginOptionalBuilder protoBackwardsCompatibilityMavenPluginBuilder() {
return mavenPlugin().groupId("com.salesforce.servicelibs").artifactId("proto-backwards-compatibility");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package tech.jhipster.lite.generator.server.javatool.protobuf.infrastructure.primary;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import tech.jhipster.lite.generator.server.javatool.protobuf.application.ProtobufApplicationService;
import tech.jhipster.lite.generator.slug.domain.JHLiteFeatureSlug;
import tech.jhipster.lite.generator.slug.domain.JHLiteModuleSlug;
import tech.jhipster.lite.module.domain.resource.JHipsterModuleOrganization;
import tech.jhipster.lite.module.domain.resource.JHipsterModulePropertiesDefinition;
import tech.jhipster.lite.module.domain.resource.JHipsterModuleResource;

@Configuration
class ProtobufModuleConfiguration {

@Bean
JHipsterModuleResource protobufModule(ProtobufApplicationService protobuf) {
return JHipsterModuleResource
.builder()
.slug(JHLiteModuleSlug.PROTOBUF)
.propertiesDefinition(JHipsterModulePropertiesDefinition.builder().addBasePackage().addIndentation().build())
.apiDoc("Java", "Add protobuf support")
.organization(JHipsterModuleOrganization.builder().addDependency(JHLiteFeatureSlug.JAVA_BUILD_TOOL).build())
.tags("server", "protobuf")
.factory(protobuf::buildProtobufModule);
}

@Bean
JHipsterModuleResource protobufBackwardsCompatibilityCheckModule(ProtobufApplicationService protobuf) {
return JHipsterModuleResource
.builder()
.slug(JHLiteModuleSlug.PROTOBUF_BACKWARDS_COMPATIBILITY_CHECK)
.propertiesDefinition(JHipsterModulePropertiesDefinition.builder().addBasePackage().addIndentation().build())
.apiDoc("Java", "Add protobuf backwards compatibility check")
.organization(
JHipsterModuleOrganization.builder().addDependency(JHLiteModuleSlug.PROTOBUF).addDependency(JHLiteModuleSlug.MAVEN_JAVA).build()
)
.tags("server", "protobuf")
.factory(protobuf::buildProtobufBackwardsCompatibilityCheckModule);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ public JHipsterModule buildModule(JHipsterModuleProperties properties) {
.put("baseName", properties.projectBaseName().capitalized())
.and()
.documentation(documentationTitle("Dummy"), SOURCE.file("dummy.md"))
.mavenBuildExtensions()
.addExtension(groupId("kr.motd.maven"), artifactId("os-maven-plugin"), versionSlug("os-maven-plugin"))
.and()
.files()
.batch(MAIN_SOURCE, mainDestination)
.addTemplate("package-info.java")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public enum JHLiteModuleSlug implements JHipsterModuleSlugFactory {
PLAYWRIGHT("playwright"),
POSTGRESQL("postgresql"),
PRETTIER("prettier"),
PROTOBUF("protobuf"),
PROTOBUF_BACKWARDS_COMPATIBILITY_CHECK("protobuf-backwards-compatibility-check"),
REACT_CORE("react-core"),
REACT_JWT("react-jwt"),
REST_PAGINATION("rest-pagination"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package tech.jhipster.lite.module.domain.javadependency;

import static tech.jhipster.lite.module.domain.javadependency.JavaDependencyScope.TEST;

import java.util.ArrayList;
import java.util.Collection;
import java.util.function.Function;
Expand Down Expand Up @@ -124,6 +126,18 @@ public JHipsterModuleJavaDependenciesBuilder addDependency(GroupId groupId, Arti
return addDependency(dependency);
}

public JHipsterModuleJavaDependenciesBuilder addTestDependency(GroupId groupId, ArtifactId artifactId, VersionSlug versionSlug) {
JavaDependency dependency = JavaDependency
.builder()
.groupId(groupId)
.artifactId(artifactId)
.versionSlug(versionSlug)
.scope(TEST)
.build();

return addDependency(dependency);
}

public JHipsterModuleJavaDependenciesBuilder addDependency(JavaDependency dependency) {
Assert.notNull(DEPENDENCY, dependency);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ public boolean notMatchIn(String content) {
@Override
public BiFunction<String, String, String> replacement() {
return (content, replacement) ->
linePattern().matcher(content).replaceAll(result -> result.group() + JHipsterModule.LINE_BREAK + replacement);
linePattern()
.matcher(content)
.replaceAll(result -> result.group() + JHipsterModule.LINE_BREAK + escapeSpecialCharacters(replacement));
}

private String escapeSpecialCharacters(String replacement) {
return replacement.replace("$", "\\$");
}

private Pattern linePattern() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ public boolean notMatchIn(String content) {
@Override
public BiFunction<String, String, String> replacement() {
return (content, replacement) ->
linePattern().matcher(content).replaceAll(result -> replacement + JHipsterModule.LINE_BREAK + result.group());
linePattern()
.matcher(content)
.replaceAll(result -> escapeSpecialCharacters(replacement) + JHipsterModule.LINE_BREAK + result.group());
}

private String escapeSpecialCharacters(String replacement) {
return replacement.replace("$", "\\$");
}

private Pattern linePattern() {
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/generator/dependencies/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
java
checkstyle
alias(libs.plugins.jib)
alias(libs.plugins.protobuf)
// jhipster-needle-gradle-plugin
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
checkstyle = "10.12.7"
jib = "3.4.0"
git-properties = "2.4.1"
protobuf-plugin = "0.9.4"

[libraries]
[libraries.checkstyle]
Expand All @@ -17,3 +18,7 @@ version.ref = "jib"
[plugins.git-properties]
id = "com.gorylenko.gradle-git-properties"
version.ref = "git-properties"

[plugins.protobuf]
id = "com.google.cloud.tools.jib"
version.ref = "protobuf-plugin"
18 changes: 18 additions & 0 deletions src/main/resources/generator/dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
<htmx-webjars.version>1.9.10</htmx-webjars.version>
<alpinejs-webjars.version>3.13.3</alpinejs-webjars.version>
<os-maven-plugin.version>1.7.1</os-maven-plugin.version>
<protobuf.version>3.23.3</protobuf.version>
<protobuf-maven-plugin.version>0.6.1</protobuf-maven-plugin.version>
<proto-backwards-compatibility.version>1.0.7</proto-backwards-compatibility.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -305,6 +308,21 @@
<artifactId>neo4j-migrations</artifactId>
<version>${neo4j-migrations.version}</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>${protobuf.version}</version>
</dependency>
<dependency>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>${protobuf-maven-plugin.version}</version>
</dependency>
<dependency>
<groupId>com.salesforce.servicelibs</groupId>
<artifactId>proto-backwards-compatibility</artifactId>
<version>${proto-backwards-compatibility.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package {{packageName}}.shared.protobuf.infrastructure.primary;

import com.google.protobuf.Timestamp;
import java.time.Instant;

public final class ProtobufDatesReader {
private ProtobufDatesReader() {}

public static Instant readInstant(Timestamp timestamp) {
if (timestamp == null || timestamp.getSeconds() == 0) {
return null;
}

return Instant.ofEpochSecond(timestamp.getSeconds(), timestamp.getNanos());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package {{packageName}}.shared.protobuf.infrastructure.secondary;

import com.google.protobuf.Timestamp;
import java.time.Instant;

public final class ProtobufDatesWriter {
private ProtobufDatesWriter() {}

public static Timestamp buildTimestamp(Instant instant) {
if (instant == null) {
return null;
}

return Timestamp.newBuilder().setSeconds(instant.getEpochSecond()).setNanos(instant.getNano()).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@{{packageName}}.SharedKernel
package {{packageName}}.shared.protobuf;
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package {{packageName}}.shared.protobuf.infrastructure.primary;

import static org.assertj.core.api.Assertions.*;

import com.google.protobuf.util.Timestamps;
import {{packageName}}.UnitTest;
import java.util.Date;
import org.junit.jupiter.api.Test;

@UnitTest
class ProtobufDatesReaderTest {
@Test
void shouldConvertNullTimestampToNull() {
assertThat(ProtobufDatesReader.readInstant(null)).isNull();
}

@Test
void shouldConvertDefaultDateToNull() {
assertThat(ProtobufDatesReader.readInstant(Timestamps.fromDate(new Date(0)))).isNull();
}

@Test
void shouldGetInstantFromTimestampFromDate() {
Date date = new Date();
assertThat(ProtobufDatesReader.readInstant(Timestamps.fromDate(date))).isEqualTo(date.toInstant());
}
}
Loading

0 comments on commit 00f57bb

Please sign in to comment.