Skip to content

Commit 5a24d02

Browse files
authored
[ggj][bazel] feat: initial Bazel rules for a raw srcjar (#304)
* fix: refactor and rename Bazel rules to use googleapis' toolchain * fix: simplify Bazel rules post-refactoring * fix: rename protoc plugin binary * fix!: use key=value pairs for the plugin arguments (for Bazel) * feat: prevent null args in MethodInvocationExpr * feat: prevent null args in ReferenceConstructorExpr * feat: prevent null args in NewObjectExpr * feat: Bazel rules for a raw srcjar * fix: build file visibility
1 parent 24e1d79 commit 5a24d02

File tree

9 files changed

+86
-13
lines changed

9 files changed

+86
-13
lines changed

BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ load(
44
"google_java_format_verification",
55
)
66

7+
package(default_visibility = ["//visibility:public"])
8+
79
JAVA_SRCS = [
810
"//src/main/java/com/google/api/generator:generator_files",
911
"//src/main/java/com/google/api/generator/engine:engine_files",

dependencies.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ version.io_grpc_java=1.30.2
1616

1717
# Common deps.
1818
maven.com_google_guava_guava=com.google.guava:guava:26.0-jre
19-
maven.javax_validation_javax_validation_api=javax.validation:validation-api:2.0.1.Final
2019
maven.com_google_code_findbugs_jsr305=com.google.code.findbugs:jsr305:3.0.0
2120
maven.com_google_auto_value_auto_value=com.google.auto.value:auto-value:1.7.2
2221
maven.com_google_auto_value_auto_value_annotations=com.google.auto.value:auto-value-annotations:1.7.2
2322
maven.com_google_protobuf_protobuf_java=com.google.protobuf:protobuf-java:3.12.2
23+
maven.javax_annotation_api=javax.annotation:javax.annotation-api:1.3.2
24+
maven.javax_validation_javax_validation_api=javax.validation:validation-api:2.0.1.Final
2425

2526
# Gapic YAML parsing for batching settings.
2627
maven.org_yaml_snakeyaml=org.yaml:snakeyaml:1.26

rules_java_gapic/BUILD.bazel

Whitespace-only changes.

rules_java_gapic/java_gapic.bzl

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Copyright 2019 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
load("@com_google_api_codegen//rules_gapic:gapic.bzl", "proto_custom_library", "unzipped_srcjar")
16+
17+
def java_gapic_library(
18+
name,
19+
srcs,
20+
package = None,
21+
service_yaml = None,
22+
grpc_service_config = None,
23+
gapic_yaml = None,
24+
deps = [],
25+
test_deps = [],
26+
**kwargs):
27+
file_args_dict = {}
28+
29+
if grpc_service_config:
30+
file_args_dict[grpc_service_config] = "grpc-service-config"
31+
32+
if gapic_yaml:
33+
file_args_dict[gapic_yaml] = "gapic-config"
34+
35+
# Currently a no-op.
36+
if service_yaml:
37+
file_args_dict[service_yaml] = "gapic-service-config"
38+
39+
#file_args = ["%s=%s" % (k, v) for k, v in file_args_dict.items()]
40+
41+
srcjar_name = name + "_srcjar"
42+
raw_srcjar_name = srcjar_name + "_raw"
43+
output_suffix = ".srcjar"
44+
45+
_java_generator_name = "java_gapic"
46+
proto_custom_library(
47+
name = raw_srcjar_name,
48+
deps = srcs,
49+
plugin = Label("@com_google_api_generator//:protoc-gen-%s" % _java_generator_name),
50+
plugin_file_args = {},
51+
opt_file_args = file_args_dict,
52+
output_type = _java_generator_name,
53+
output_suffix = output_suffix,
54+
**kwargs
55+
)

src/main/java/com/google/api/generator/Main.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ public static void main(String[] args)
3232
ExtensionRegistry registry = ExtensionRegistry.newInstance();
3333
registerAllExtensions(registry);
3434
CodeGeneratorRequest request = CodeGeneratorRequest.parseFrom(System.in, registry);
35-
String outputFilePath = "temp-gen.srcjar";
36-
CodeGeneratorResponse response = Generator.generateGapic(request, outputFilePath);
35+
CodeGeneratorResponse response = Generator.generateGapic(request);
3736
response.writeTo(System.out);
3837
}
3938

src/main/java/com/google/api/generator/gapic/Generator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
import java.util.List;
2525

2626
public class Generator {
27-
public static CodeGeneratorResponse generateGapic(
28-
CodeGeneratorRequest request, String outputFilePath) {
27+
public static CodeGeneratorResponse generateGapic(CodeGeneratorRequest request) {
2928
GapicContext context = Parser.parse(request);
3029
List<GapicClass> clazzes = Composer.composeServiceClasses(context);
31-
CodeGeneratorResponse response = Writer.writeCode(clazzes, outputFilePath);
30+
String outputFilename = "temp-codegen.srcjar";
31+
CodeGeneratorResponse response = Writer.writeCode(clazzes, outputFilename);
3232
return response;
3333
}
3434
}

src/main/java/com/google/api/generator/gapic/composer/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ java_library(
3030
"@io_grpc_grpc_java//api",
3131
"@io_grpc_grpc_java//protobuf",
3232
"@io_grpc_grpc_java//stub",
33+
"@javax_annotation_api//jar",
3334
"@junit_junit//jar",
3435
"@org_threeten_threetenbp//jar",
3536
],

src/main/java/com/google/api/generator/gapic/composer/ResourceNameHelperClassComposer.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1081,13 +1081,19 @@ private static MethodDefinition createToStringMethod(
10811081
if (!hasVariants) {
10821082
String token = getTokenSet(tokenHierarchies).stream().collect(Collectors.toList()).get(0);
10831083
String javaTokenVarName = JavaStyle.toLowerCamelCase(token);
1084+
Preconditions.checkNotNull(
1085+
patternTokenVarExprs.get(token),
1086+
String.format(
1087+
"No expression found for token %s amongst values %s",
1088+
javaTokenVarName, patternTokenVarExprs.toString()));
1089+
10841090
MethodInvocationExpr returnInstantiateExpr =
10851091
MethodInvocationExpr.builder()
10861092
.setExprReferenceExpr(templateFinalVarExprs.get(0))
10871093
.setMethodName("instantiate")
10881094
.setArguments(
10891095
ValueExpr.withValue(StringObjectValue.withValue(token)),
1090-
patternTokenVarExprs.get(javaTokenVarName))
1096+
patternTokenVarExprs.get(token))
10911097
.setReturnType(TypeNode.STRING)
10921098
.build();
10931099
return MethodDefinition.builder()

src/main/java/com/google/api/generator/gapic/composer/ServiceStubSettingsClassComposer.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
import java.util.function.Function;
109109
import java.util.stream.Collectors;
110110
import javax.annotation.Generated;
111+
import javax.annotation.Nullable;
111112
import org.threeten.bp.Duration;
112113

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

155156
public GapicClass generate(
156-
Service service, GapicServiceConfig serviceConfig, Map<String, Message> messageTypes) {
157+
Service service,
158+
@Nullable GapicServiceConfig serviceConfig,
159+
Map<String, Message> messageTypes) {
160+
// TODO(miraleung): Robustify this against a null serviceConfig.
157161
String pakkage = String.format("%s.stub", service.pakkage());
158162
Map<String, TypeNode> types = createDynamicTypes(service, pakkage);
159163
Map<String, VariableExpr> methodSettingsMemberVarExprs =
@@ -215,7 +219,8 @@ private static Map<String, VariableExpr> createMethodSettingsClassMemberVarExprs
215219
// Creates class variables <method>Settings, e.g. echoSettings.
216220
// TODO(miraleung): Handle batching here.
217221
for (Method method : service.methods()) {
218-
boolean hasBatchingSettings = serviceConfig.hasBatchingSetting(service, method);
222+
boolean hasBatchingSettings =
223+
!Objects.isNull(serviceConfig) && serviceConfig.hasBatchingSetting(service, method);
219224
TypeNode settingsType =
220225
getCallSettingsType(method, types, hasBatchingSettings, isNestedClass);
221226
String varName = JavaStyle.toLowerCamelCase(String.format("%sSettings", method.name()));
@@ -304,7 +309,9 @@ private static List<Statement> createClassStatements(
304309

305310
for (Method method : service.methods()) {
306311
Optional<GapicBatchingSettings> batchingSettingOpt =
307-
serviceConfig.getBatchingSetting(service, method);
312+
Objects.isNull(serviceConfig)
313+
? Optional.empty()
314+
: serviceConfig.getBatchingSetting(service, method);
308315
if (batchingSettingOpt.isPresent()) {
309316
statements.add(
310317
exprToStatementFn.apply(
@@ -1125,7 +1132,8 @@ private static MethodDefinition createClassConstructor(
11251132
}
11261133

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

@@ -1235,7 +1243,8 @@ private static List<MethodDefinition> createNestedClassMethods(
12351243
}
12361244

12371245
private static MethodDefinition createNestedClassInitDefaultsMethod(
1238-
Service service, GapicServiceConfig serviceConfig, Map<String, TypeNode> types) {
1246+
Service service, @Nullable GapicServiceConfig serviceConfig, Map<String, TypeNode> types) {
1247+
// TODO(miraleung): Robustify this against a null serviceConfig.
12391248
TypeNode builderType = types.get(NESTED_BUILDER_CLASS_NAME);
12401249
VariableExpr builderVarExpr =
12411250
VariableExpr.withVariable(
@@ -1248,7 +1257,7 @@ private static MethodDefinition createNestedClassInitDefaultsMethod(
12481257
if (streamKind.equals(Method.Stream.CLIENT) || streamKind.equals(Method.Stream.BIDI)) {
12491258
continue;
12501259
}
1251-
if (serviceConfig.hasBatchingSetting(service, method)) {
1260+
if (!Objects.isNull(serviceConfig) && serviceConfig.hasBatchingSetting(service, method)) {
12521261
Optional<GapicBatchingSettings> batchingSettingOpt =
12531262
serviceConfig.getBatchingSetting(service, method);
12541263
Preconditions.checkState(

0 commit comments

Comments
 (0)