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

feat(bazel): detach spring bazel rules #1065

Merged
merged 8 commits into from
Nov 16, 2022
6 changes: 6 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ java_binary(
runtime_deps = [":gapic_generator_java"] + MAIN_DEPS,
)

java_binary(
name = "protoc-gen-java_gapic_spring",
main_class = "com.google.api.generator.spring.Main",
runtime_deps = [":gapic_generator_java"] + MAIN_DEPS,
zhumin8 marked this conversation as resolved.
Show resolved Hide resolved
)

# Request dumper binary, which dumps the CodeGeneratorRequest to a file on disk
# which will be identical to the one passed to the protoc-gen-java_gapic during
# normal execution. The dumped file then can be used to run this gapic-generator
Expand Down
78 changes: 1 addition & 77 deletions rules_java_gapic/java_gapic.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ def _java_gapic_postprocess_srcjar_impl(ctx):
output_main = ctx.outputs.main
output_test = ctx.outputs.test
output_samples = ctx.outputs.samples
output_spring = ctx.outputs.spring
output_resource_name = ctx.outputs.resource_name
formatter = ctx.executable.formatter

Expand Down Expand Up @@ -66,21 +65,10 @@ def _java_gapic_postprocess_srcjar_impl(ctx):

cd $WORKING_DIR

unzip -q temp-codegen-spring.srcjar -d {output_dir_path}/spring
# This may fail if there are spaces and/or too many files (exceed max length of command length).
{formatter} --replace $(find {output_dir_path}/spring -type f -printf "%p ")

# Spring source files.
cd {output_dir_path}/spring
zip -r $WORKING_DIR/{output_srcjar_name}-spring.srcjar ./

cd $WORKING_DIR

mv {output_srcjar_name}.srcjar {output_main}
mv {output_srcjar_name}-resource-name.srcjar {output_resource_name}
mv {output_srcjar_name}-tests.srcjar {output_test}
mv {output_srcjar_name}-samples.srcjar {output_samples}
mv {output_srcjar_name}-spring.srcjar {output_spring}
""".format(
gapic_srcjar = gapic_srcjar.path,
output_srcjar_name = output_srcjar_name,
Expand All @@ -91,14 +79,13 @@ def _java_gapic_postprocess_srcjar_impl(ctx):
output_resource_name = output_resource_name.path,
output_test = output_test.path,
output_samples = output_samples.path,
output_spring = output_spring.path,
)

ctx.actions.run_shell(
inputs = [gapic_srcjar],
tools = [formatter],
command = script,
outputs = [output_main, output_resource_name, output_test, output_samples, output_spring],
outputs = [output_main, output_resource_name, output_test, output_samples],
)

_java_gapic_postprocess_srcjar = rule(
Expand All @@ -115,7 +102,6 @@ _java_gapic_postprocess_srcjar = rule(
"resource_name": "%{name}-resource-name.srcjar",
"test": "%{name}-test.srcjar",
"samples": "%{name}-samples.srcjar",
"spring": "%{name}-spring.srcjar",
},
implementation = _java_gapic_postprocess_srcjar_impl,
)
Expand Down Expand Up @@ -175,62 +161,6 @@ _java_gapic_samples_srcjar = rule(
implementation = _java_gapic_samples_srcjar_impl,
)


def _java_gapic_spring_srcjar_impl(ctx):
gapic_srcjar = ctx.file.gapic_srcjar
output_srcjar_name = ctx.label.name
output_spring = ctx.outputs.spring
formatter = ctx.executable.formatter

output_dir_name = ctx.label.name
output_dir_path = "%s/%s" % (output_spring.dirname, output_dir_name)

script = """
unzip -q {gapic_srcjar}
# Sync'd to the output file name in Writer.java.
unzip -q temp-codegen-spring.srcjar -d {output_dir_path}
# This may fail if there are spaces and/or too many files (exceed max length of command length).
{formatter} --replace $(find {output_dir_path}/spring -type f -printf "%p ")
WORKING_DIR=`pwd`

# Spring source files.
cd $WORKING_DIR/{output_dir_path}
zip -r $WORKING_DIR/{output_srcjar_name}-spring.srcjar ./

cd $WORKING_DIR

mv {output_srcjar_name}-spring.srcjar {output_spring}
""".format(
gapic_srcjar = gapic_srcjar.path,
output_srcjar_name = output_srcjar_name,
formatter = formatter,
output_dir_name = output_dir_name,
output_dir_path = output_dir_path,
output_spring = output_spring.path,
)

ctx.actions.run_shell(
inputs = [gapic_srcjar],
tools = [formatter],
command = script,
outputs = [output_spring],
)

_java_gapic_spring_srcjar = rule(
attrs = {
"gapic_srcjar": attr.label(mandatory = True, allow_single_file = True),
"formatter": attr.label(
default = Label("//:google_java_format_binary"),
executable = True,
cfg = "host",
),
},
outputs = {
"spring": "%{name}-spring.srcjar",
},
implementation = _java_gapic_spring_srcjar_impl,
)

def _extract_common_proto_dep(dep):
return dep[dep.index("/"):] if "//google" in dep else dep

Expand Down Expand Up @@ -343,12 +273,6 @@ def java_gapic_library(
**kwargs
)

_java_gapic_spring_srcjar(
name = "%s_spring" % name,
gapic_srcjar = "%s.srcjar" % raw_srcjar_name,
**kwargs
)

print("postprocessing done.\n")

resource_name_name = "%s_resource_name" % name
Expand Down
146 changes: 146 additions & 0 deletions rules_java_gapic/java_gapic_spring.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# Copyright 2022 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("@rules_gapic//:gapic.bzl", "proto_custom_library")
diegomarquezp marked this conversation as resolved.
Show resolved Hide resolved

def _java_gapic_spring_postprocess_srcjar_impl(ctx):
gapic_srcjar = ctx.file.gapic_srcjar
output_srcjar_name = ctx.label.name
srcjar_name = output_srcjar_name + "_raw.srcjar"
output_spring = ctx.outputs.spring
formatter = ctx.executable.formatter

output_dir_name = ctx.label.name
output_dir_path = "%s/%s" % (output_spring.dirname, output_dir_name)

script = """
WORKING_DIR=`pwd`
cd $WORKING_DIR
unzip -q {gapic_srcjar}
unzip -q temp-codegen-spring.srcjar -d {output_dir_path}
# This may fail if there are spaces and/or too many files (exceed max length of command length).
{formatter} --replace $(find {output_dir_path} -type f -printf "%p ")

# Spring source files.
cd {output_dir_path}
zip -r $WORKING_DIR/{output_srcjar_name}.srcjar ./

cd $WORKING_DIR

mv $WORKING_DIR/{output_srcjar_name}.srcjar {output_spring}
""".format(
output_srcjar_name = output_srcjar_name,
gapic_srcjar = gapic_srcjar.path,
srcjar_name = srcjar_name,
formatter = formatter,
output_dir_name = output_dir_name,
output_dir_path = output_dir_path,
output_spring = output_spring.path,
)

ctx.actions.run_shell(
inputs = [gapic_srcjar],
tools = [formatter],
command = script,
outputs = [output_spring],
)

_java_gapic_spring_postprocess_srcjar = rule(
attrs = {
"gapic_srcjar": attr.label(mandatory = True, allow_single_file = True),
"formatter": attr.label(
default = Label("//:google_java_format_binary"),
executable = True,
cfg = "host",
),
},
outputs = {
"spring": "%{name}-spring.srcjar",
},
implementation = _java_gapic_spring_postprocess_srcjar_impl,
)

def java_gapic_spring_library(
name,
srcs,
grpc_service_config = None,
gapic_yaml = None,
service_yaml = None,
transport = None,
**kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose this is derived from _java_gapic_library() in java_gapic.bzl and you removed a couple of options. Can you double check if they don't affect sping-codegen for us?
E.g. I noticed transport is removed, If I get it right, this is where transport is specified as grpc or grpc+rest or rest, which get parsed in and eventually plays a part in #1078. Are we losing this info here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, we indeed need transport to be specified! I'll double check the impact of the other options.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, the other option is rest_numeric_enums, which is used by HttpJsonServiceStubClassComposer to determine how to serialize enums (by number or value). I see that our spring composers do not mention enums, so it should be safe to remove.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is probably not in scope now, but once SpringCodeGen decide to support both grpc and rest, we need a way to pass the correct value to this Spring specific bazel rule, and currently the only source of truth is the Bazel files in googleapis.

library_name = name + "-spring"
raw_srcjar_name = name + "_raw"

_java_gapic_spring_srcjar(
name = raw_srcjar_name,
srcs = srcs,
grpc_service_config = grpc_service_config,
gapic_yaml = gapic_yaml,
service_yaml = service_yaml,
transport = transport,
**kwargs
)

_java_gapic_spring_postprocess_srcjar(
name = name,
gapic_srcjar = "%s.srcjar" % raw_srcjar_name,
**kwargs
)

def _java_gapic_spring_srcjar(
name,
srcs,
grpc_service_config,
gapic_yaml,
service_yaml,
transport,
# Can be used to provide a java_library with a customized generator,
# like the one which dumps descriptor to a file for future debugging.
java_generator_name = "java_gapic_spring",
**kwargs):
file_args_dict = {}

diegomarquezp marked this conversation as resolved.
Show resolved Hide resolved
if grpc_service_config:
file_args_dict[grpc_service_config] = "grpc-service-config"
elif not transport or transport == "grpc":
if "library" not in name:
fail("Missing a gRPC service config file")

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

if service_yaml:
file_args_dict[service_yaml] = "api-service-config"

opt_args = []

if transport:
opt_args.append("transport=%s" % transport)

# Produces the GAPIC metadata file if this flag is set. to any value.
# Protoc invocation: --java_gapic_opt=metadata
plugin_args = ["metadata"]

proto_custom_library(
name = name,
deps = srcs,
plugin = Label("@gapic_generator_java//:protoc-gen-%s" % java_generator_name),
plugin_args = plugin_args,
plugin_file_args = {},
opt_file_args = file_args_dict,
output_type = java_generator_name,
output_suffix = ".srcjar",
opt_args = opt_args,
**kwargs
)
31 changes: 31 additions & 0 deletions src/main/java/com/google/api/generator/spring/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2022 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
//
// http://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 com.google.api.generator.spring;

import com.google.api.generator.ProtoRegistry;
import com.google.protobuf.ExtensionRegistry;
import com.google.protobuf.compiler.PluginProtos.CodeGeneratorRequest;
import com.google.protobuf.compiler.PluginProtos.CodeGeneratorResponse;
import java.io.IOException;

public class Main {
public static void main(String[] args) throws IOException {
ExtensionRegistry registry = ExtensionRegistry.newInstance();
ProtoRegistry.registerAllExtensions(registry);
CodeGeneratorRequest request = CodeGeneratorRequest.parseFrom(System.in, registry);
CodeGeneratorResponse springResponse = SpringGenerator.generateSpring(request);
springResponse.writeTo(System.out);
}
}