Skip to content

Commit

Permalink
Add entry for spring-projects-experimental/spring-grpc
Browse files Browse the repository at this point in the history
Closes gh-1673
  • Loading branch information
mhalbritter committed Dec 17, 2024
1 parent 0eefa6f commit 3a28aa7
Show file tree
Hide file tree
Showing 12 changed files with 650 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2012-2024 the original author or 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 io.spring.start.site.extension.dependency.springgrpc;

import io.spring.initializr.generator.buildsystem.gradle.GradleBuild;
import io.spring.initializr.generator.buildsystem.gradle.GradleExtensionContainer;
import io.spring.initializr.generator.spring.build.BuildCustomizer;

/**
* Abstract base class for {@link BuildCustomizer} to deal withj Gradle based gRPC
* projects.
*
* @author Moritz Halbritter
*/
abstract class AbstractGrpcGradleBuildCustomizer implements BuildCustomizer<GradleBuild> {

private static final String GRPC_PLUGIN_VERSION = "0.9.4";

private final char quote;

AbstractGrpcGradleBuildCustomizer(char quote) {
this.quote = quote;
}

@Override
public void customize(GradleBuild build) {
build.plugins().add("com.google.protobuf", (plugin) -> plugin.setVersion(GRPC_PLUGIN_VERSION));
customizeExtensions(build.extensions());
}

protected abstract void customizeExtensions(GradleExtensionContainer extensions);

protected String quote(String value) {
return this.quote + value + this.quote;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2012-2024 the original author or 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 io.spring.start.site.extension.dependency.springgrpc;

import io.spring.initializr.generator.buildsystem.Build;
import io.spring.initializr.generator.buildsystem.DependencyScope;
import io.spring.initializr.generator.spring.build.BuildCustomizer;

/**
* {@link BuildCustomizer} to add additional dependencies for Spring gRPC.
*
* @author Moritz Halbritter
*/
class GrpcAdditionalDependenciesBuildCustomizer implements BuildCustomizer<Build> {

@Override
public void customize(Build build) {
build.dependencies().add("grpc-services", "io.grpc", "grpc-services", DependencyScope.COMPILE);
build.dependencies()
.add("spring-grpc-test", "org.springframework.grpc", "spring-grpc-test", DependencyScope.TEST_COMPILE);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2012-2024 the original author or 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 io.spring.start.site.extension.dependency.springgrpc;

import io.spring.initializr.generator.buildsystem.gradle.GradleExtensionContainer;
import io.spring.initializr.generator.spring.build.BuildCustomizer;

/**
* {@link BuildCustomizer} to customize the Groovy DSL Gradle build to build gRPC
* projects.
*
* @author Moritz Halbritter
*/
class GrpcGradleGroovyBuildCustomizer extends AbstractGrpcGradleBuildCustomizer {

GrpcGradleGroovyBuildCustomizer() {
super('\'');
}

@Override
protected void customizeExtensions(GradleExtensionContainer extensions) {
extensions.customize("protobuf", (protobuf) -> {
protobuf.nested("protoc", (protoc) -> protoc.attribute("artifact", quote("com.google.protobuf:protoc")));
protobuf.nested("plugins", (plugins) -> plugins.nested("grpc",
(grpc) -> grpc.attribute("artifact", quote("io.grpc:protoc-gen-grpc-java"))));
protobuf.nested("generateProtoTasks", (generateProtoTasks) -> generateProtoTasks.nested("all()*.plugins",
(plugins) -> plugins.nested("grpc", (grpc) -> grpc.invoke("option", quote("jakarta_omit")))));
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2012-2024 the original author or 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 io.spring.start.site.extension.dependency.springgrpc;

import io.spring.initializr.generator.buildsystem.gradle.GradleExtensionContainer;
import io.spring.initializr.generator.spring.build.BuildCustomizer;

/**
* {@link BuildCustomizer} to customize the Kotlin DSL Gradle build to build gRPC
* projects.
*
* @author Moritz Halbritter
*/
class GrpcGradleKotlinBuildCustomizer extends AbstractGrpcGradleBuildCustomizer {

GrpcGradleKotlinBuildCustomizer() {
super('\"');
}

@Override
protected void customizeExtensions(GradleExtensionContainer extensions) {
extensions.customize("protobuf", (protobuf) -> {
protobuf.nested("protoc", (protoc) -> protoc.attribute("artifact", quote("com.google.protobuf:protoc")));
protobuf.importType("com.google.protobuf.gradle.id");
protobuf.nested("plugins", (plugins) -> plugins.nested("id(\"grpc\")",
(grpc) -> grpc.attribute("artifact", quote("io.grpc:protoc-gen-grpc-java"))));
protobuf.nested("generateProtoTasks",
(generateProtoTasks) -> generateProtoTasks.nested("all().forEach",
(forEach) -> forEach.nested("it.plugins", (plugins) -> plugins.nested("id(\"grpc\")",
(grpc) -> grpc.invoke("option", quote("jakarta_omit"))))));
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright 2012-2024 the original author or 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 io.spring.start.site.extension.dependency.springgrpc;

import io.spring.initializr.generator.buildsystem.PropertyContainer;
import io.spring.initializr.generator.buildsystem.maven.MavenBuild;
import io.spring.initializr.generator.buildsystem.maven.MavenPluginContainer;
import io.spring.initializr.generator.spring.build.BuildCustomizer;
import io.spring.initializr.generator.version.VersionProperty;

/**
* {@link BuildCustomizer} to customize the Maven build to build gRPC projects.
*
* @author Moritz Halbritter
*/
class GrpcMavenBuildCustomizer implements BuildCustomizer<MavenBuild> {

private static final String OS_PLUGIN_VERSION = "1.7.1";

private static final String PROTOBUF_PLUGIN_VERSION = "0.6.1";

private final String protobufJavaVersion;

private final String grpcVersion;

GrpcMavenBuildCustomizer(String protobufJavaVersion, String grpcVersion) {
this.protobufJavaVersion = protobufJavaVersion;
this.grpcVersion = grpcVersion;
}

@Override
public void customize(MavenBuild build) {
VersionProperty protobufJava = VersionProperty.of("protobuf-java.version");
VersionProperty grpc = VersionProperty.of("grpc.version");
addVersionProperties(build.properties(), protobufJava, grpc);
addOsPlugin(build.plugins());
addProtobufPlugin(build.plugins(), protobufJava, grpc);
}

private void addVersionProperties(PropertyContainer properties, VersionProperty protobufJava,
VersionProperty grpc) {
properties.version(protobufJava, this.protobufJavaVersion);
properties.version(grpc, this.grpcVersion);
}

private void addOsPlugin(MavenPluginContainer plugins) {
plugins.add("kr.motd.maven", "os-maven-plugin", (plugin) -> {
plugin.version(OS_PLUGIN_VERSION);
plugin.execution("initialize", (execution) -> {
execution.phase("initialize");
execution.goal("detect");
});
});
}

private void addProtobufPlugin(MavenPluginContainer plugins, VersionProperty protobufJava, VersionProperty grpc) {
plugins.add("org.xolstice.maven.plugins", "protobuf-maven-plugin", (plugin) -> {
plugin.version(PROTOBUF_PLUGIN_VERSION);
plugin.configuration((configuration) -> {
configuration.add("protocArtifact", "com.google.protobuf:protoc:${%s}:exe:${os.detected.classifier}"
.formatted(protobufJava.toStandardFormat()));
configuration.add("pluginId", "grpc-java");
configuration.add("pluginArtifact", "io.grpc:protoc-gen-grpc-java:${%s}:exe:${os.detected.classifier}"
.formatted(grpc.toStandardFormat()));
});
plugin.execution("compile", (execution) -> {
execution.goal("compile").goal("compile-custom");
execution.configuration((configuration) -> configuration.add("pluginParameter", "jakarta_omit"));
});
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2012-2024 the original author or 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 io.spring.start.site.extension.dependency.springgrpc;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import io.spring.initializr.generator.project.contributor.ProjectContributor;

/**
* A {@link ProjectContributor} that creates the "src/main/proto" directory.
*
* @author Moritz Halbritter
*/
class GrpcProjectContributor implements ProjectContributor {

@Override
public void contribute(Path projectRoot) throws IOException {
Path protoDirectory = projectRoot.resolve("src/main/proto");
Files.createDirectories(protoDirectory);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2012-2024 the original author or 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 io.spring.start.site.extension.dependency.springgrpc;

import java.util.Map;

import io.spring.initializr.versionresolver.MavenVersionResolver;

import org.springframework.util.function.SingletonSupplier;

/**
* Resolves dependency versions from 'org.springframework.grpc:spring-grpc-dependencies'.
*
* @author Moritz Halbritter
*/
class GrpcVersionResolver {

private final SingletonSupplier<Map<String, String>> versions;

GrpcVersionResolver(MavenVersionResolver versionResolver, String springGrpcVersion) {
this.versions = SingletonSupplier.of(() -> versionResolver.resolveDependencies("org.springframework.grpc",
"spring-grpc-dependencies", springGrpcVersion));
}

String resolveProtobufJavaVersion() {
return this.versions.obtain().get("com.google.protobuf:protobuf-java");
}

String resolveGrpcVersion() {
return this.versions.obtain().get("io.grpc:grpc-core");
}

}
Loading

0 comments on commit 3a28aa7

Please sign in to comment.