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

[ggj][bazel] feat: initial Bazel rules for a raw srcjar #304

Merged
merged 10 commits into from
Sep 12, 2020
2 changes: 2 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ load(
"google_java_format_verification",
)

package(default_visibility = ["//visibility:public"])

JAVA_SRCS = [
"//src/main/java/com/google/api/generator:generator_files",
"//src/main/java/com/google/api/generator/engine:engine_files",
Expand Down
3 changes: 2 additions & 1 deletion dependencies.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ version.io_grpc_java=1.30.2

# Common deps.
maven.com_google_guava_guava=com.google.guava:guava:26.0-jre
maven.javax_validation_javax_validation_api=javax.validation:validation-api:2.0.1.Final
maven.com_google_code_findbugs_jsr305=com.google.code.findbugs:jsr305:3.0.0
maven.com_google_auto_value_auto_value=com.google.auto.value:auto-value:1.7.2
maven.com_google_auto_value_auto_value_annotations=com.google.auto.value:auto-value-annotations:1.7.2
maven.com_google_protobuf_protobuf_java=com.google.protobuf:protobuf-java:3.12.2
maven.javax_annotation_api=javax.annotation:javax.annotation-api:1.3.2
maven.javax_validation_javax_validation_api=javax.validation:validation-api:2.0.1.Final

# Gapic YAML parsing for batching settings.
maven.org_yaml_snakeyaml=org.yaml:snakeyaml:1.26
Expand Down
Empty file added rules_java_gapic/BUILD.bazel
Empty file.
55 changes: 55 additions & 0 deletions rules_java_gapic/java_gapic.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Copyright 2019 Google LLC
#
# 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.

load("@com_google_api_codegen//rules_gapic:gapic.bzl", "proto_custom_library", "unzipped_srcjar")

def java_gapic_library(
name,
srcs,
package = None,
service_yaml = None,
grpc_service_config = None,
gapic_yaml = None,
deps = [],
test_deps = [],
**kwargs):
file_args_dict = {}

if grpc_service_config:
file_args_dict[grpc_service_config] = "grpc-service-config"

if gapic_yaml:
file_args_dict[gapic_yaml] = "gapic-config"

# Currently a no-op.
if service_yaml:
file_args_dict[service_yaml] = "gapic-service-config"

#file_args = ["%s=%s" % (k, v) for k, v in file_args_dict.items()]

srcjar_name = name + "_srcjar"
raw_srcjar_name = srcjar_name + "_raw"
output_suffix = ".srcjar"

_java_generator_name = "java_gapic"
proto_custom_library(
name = raw_srcjar_name,
deps = srcs,
plugin = Label("@com_google_api_generator//:protoc-gen-%s" % _java_generator_name),
plugin_file_args = {},
opt_file_args = file_args_dict,
output_type = _java_generator_name,
output_suffix = output_suffix,
**kwargs
)
3 changes: 1 addition & 2 deletions src/main/java/com/google/api/generator/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ public static void main(String[] args)
ExtensionRegistry registry = ExtensionRegistry.newInstance();
registerAllExtensions(registry);
CodeGeneratorRequest request = CodeGeneratorRequest.parseFrom(System.in, registry);
String outputFilePath = "temp-gen.srcjar";
CodeGeneratorResponse response = Generator.generateGapic(request, outputFilePath);
CodeGeneratorResponse response = Generator.generateGapic(request);
response.writeTo(System.out);
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/google/api/generator/gapic/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
import java.util.List;

public class Generator {
public static CodeGeneratorResponse generateGapic(
CodeGeneratorRequest request, String outputFilePath) {
public static CodeGeneratorResponse generateGapic(CodeGeneratorRequest request) {
GapicContext context = Parser.parse(request);
List<GapicClass> clazzes = Composer.composeServiceClasses(context);
CodeGeneratorResponse response = Writer.writeCode(clazzes, outputFilePath);
String outputFilename = "temp-codegen.srcjar";
CodeGeneratorResponse response = Writer.writeCode(clazzes, outputFilename);
return response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ java_library(
"@io_grpc_grpc_java//api",
"@io_grpc_grpc_java//protobuf",
"@io_grpc_grpc_java//stub",
"@javax_annotation_api//jar",
"@junit_junit//jar",
"@org_threeten_threetenbp//jar",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1081,13 +1081,19 @@ private static MethodDefinition createToStringMethod(
if (!hasVariants) {
String token = getTokenSet(tokenHierarchies).stream().collect(Collectors.toList()).get(0);
String javaTokenVarName = JavaStyle.toLowerCamelCase(token);
Preconditions.checkNotNull(
patternTokenVarExprs.get(token),
String.format(
"No expression found for token %s amongst values %s",
javaTokenVarName, patternTokenVarExprs.toString()));

MethodInvocationExpr returnInstantiateExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(templateFinalVarExprs.get(0))
.setMethodName("instantiate")
.setArguments(
ValueExpr.withValue(StringObjectValue.withValue(token)),
patternTokenVarExprs.get(javaTokenVarName))
patternTokenVarExprs.get(token))
.setReturnType(TypeNode.STRING)
.build();
return MethodDefinition.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.Generated;
import javax.annotation.Nullable;
import org.threeten.bp.Duration;

// TODO(miraleung): Refactor ClassComposer's interface.
Expand Down Expand Up @@ -153,7 +154,10 @@ public static ServiceStubSettingsClassComposer instance() {
}

public GapicClass generate(
Service service, GapicServiceConfig serviceConfig, Map<String, Message> messageTypes) {
Service service,
@Nullable GapicServiceConfig serviceConfig,
Map<String, Message> messageTypes) {
// TODO(miraleung): Robustify this against a null serviceConfig.
String pakkage = String.format("%s.stub", service.pakkage());
Map<String, TypeNode> types = createDynamicTypes(service, pakkage);
Map<String, VariableExpr> methodSettingsMemberVarExprs =
Expand Down Expand Up @@ -215,7 +219,8 @@ private static Map<String, VariableExpr> createMethodSettingsClassMemberVarExprs
// Creates class variables <method>Settings, e.g. echoSettings.
// TODO(miraleung): Handle batching here.
for (Method method : service.methods()) {
boolean hasBatchingSettings = serviceConfig.hasBatchingSetting(service, method);
boolean hasBatchingSettings =
!Objects.isNull(serviceConfig) && serviceConfig.hasBatchingSetting(service, method);
TypeNode settingsType =
getCallSettingsType(method, types, hasBatchingSettings, isNestedClass);
String varName = JavaStyle.toLowerCamelCase(String.format("%sSettings", method.name()));
Expand Down Expand Up @@ -304,7 +309,9 @@ private static List<Statement> createClassStatements(

for (Method method : service.methods()) {
Optional<GapicBatchingSettings> batchingSettingOpt =
serviceConfig.getBatchingSetting(service, method);
Objects.isNull(serviceConfig)
? Optional.empty()
: serviceConfig.getBatchingSetting(service, method);
if (batchingSettingOpt.isPresent()) {
statements.add(
exprToStatementFn.apply(
Expand Down Expand Up @@ -1125,7 +1132,8 @@ private static MethodDefinition createClassConstructor(
}

private static ClassDefinition createNestedBuilderClass(
Service service, GapicServiceConfig serviceConfig, Map<String, TypeNode> types) {
Service service, @Nullable GapicServiceConfig serviceConfig, Map<String, TypeNode> types) {
// TODO(miraleung): Robustify this against a null serviceConfig.
String thisClassName = getThisClassName(service.name());
TypeNode outerThisClassType = types.get(thisClassName);

Expand Down Expand Up @@ -1235,7 +1243,8 @@ private static List<MethodDefinition> createNestedClassMethods(
}

private static MethodDefinition createNestedClassInitDefaultsMethod(
Service service, GapicServiceConfig serviceConfig, Map<String, TypeNode> types) {
Service service, @Nullable GapicServiceConfig serviceConfig, Map<String, TypeNode> types) {
// TODO(miraleung): Robustify this against a null serviceConfig.
TypeNode builderType = types.get(NESTED_BUILDER_CLASS_NAME);
VariableExpr builderVarExpr =
VariableExpr.withVariable(
Expand All @@ -1248,7 +1257,7 @@ private static MethodDefinition createNestedClassInitDefaultsMethod(
if (streamKind.equals(Method.Stream.CLIENT) || streamKind.equals(Method.Stream.BIDI)) {
continue;
}
if (serviceConfig.hasBatchingSetting(service, method)) {
if (!Objects.isNull(serviceConfig) && serviceConfig.hasBatchingSetting(service, method)) {
Optional<GapicBatchingSettings> batchingSettingOpt =
serviceConfig.getBatchingSetting(service, method);
Preconditions.checkState(
Expand Down