Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,34 @@ java_binary(
],
)

java_binary(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@miraleung (from #767): Nit: Can we add a summary comment above these two rules, which also indicate these are debugging tools?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These are not rules, these are java_binaries used by one of the rules only. I clarified the purpose of the binaries in the comments

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for adding the comments. Could we copy-paste these into the Java src files as well? Up to you whether to replace these comments with a pointer to the Java source, or leave these as they are.

Could we also add a usage example? Up to you whether that goes here or in the Java source comments.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

name = "protoc-gen-code_generator_request_dumper",
main_class = "com.google.api.generator.debug.CodeGeneratorRequestDumper",
runtime_deps = [
"//src/main/java/com/google/api/generator",
"//src/main/java/com/google/api/generator/debug",
"//src/main/java/com/google/api/generator/gapic",
"@com_google_googleapis//google/api:api_java_proto",
"@com_google_googleapis//google/longrunning:longrunning_java_proto",
"@com_google_guava_guava",
"@com_google_protobuf//:protobuf_java",
],
)

java_binary(
name = "protoc-gen-java_gapic_from_file",
main_class = "com.google.api.generator.debug.MainFromFile",
runtime_deps = [
"//src/main/java/com/google/api/generator",
"//src/main/java/com/google/api/generator/debug",
"//src/main/java/com/google/api/generator/gapic",
"@com_google_googleapis//google/api:api_java_proto",
"@com_google_googleapis//google/longrunning:longrunning_java_proto",
"@com_google_guava_guava",
"@com_google_protobuf//:protobuf_java",
],
)

# google-java-format
java_binary(
name = "google_java_format_binary",
Expand Down
67 changes: 54 additions & 13 deletions rules_java_gapic/java_gapic.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,18 @@ def _append_dep_without_duplicates(dest_deps, new_deps):
dest_deps.append(new_deps[i])
return dest_deps

def java_gapic_library(
def _java_gapic_srcjar(
name,
srcs,
grpc_service_config = None,
gapic_yaml = None,
service_yaml = None,
deps = [],
test_deps = [],
grpc_service_config,
gapic_yaml,
service_yaml,
# possible values are: "grpc", "rest", "grpc+rest"
transport = None,
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",
java_generator_name = "java_gapic",
output_suffix = ".srcjar",
**kwargs):
file_args_dict = {}

Expand Down Expand Up @@ -159,8 +158,6 @@ def java_gapic_library(
else:
fail("Service.yaml is no longer supported in the Java microgenerator")

srcjar_name = name + "_srcjar"
raw_srcjar_name = srcjar_name + "_raw"
output_suffix = ".srcjar"
opt_args = []

Expand All @@ -172,18 +169,43 @@ def java_gapic_library(
plugin_args = ["metadata"]

proto_custom_library(
name = raw_srcjar_name,
name = name,
deps = srcs,
plugin = Label("@gapic_generator_java//:protoc-gen-%s" % _java_generator_name),
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_type = java_generator_name,
output_suffix = output_suffix,
opt_args = opt_args,
**kwargs
)

def java_gapic_library(
name,
srcs,
grpc_service_config = None,
gapic_yaml = None,
service_yaml = None,
deps = [],
test_deps = [],
# possible values are: "grpc", "rest", "grpc+rest"
transport = None,
**kwargs):
srcjar_name = name + "_srcjar"
raw_srcjar_name = srcjar_name + "_raw"

_java_gapic_srcjar(
name = raw_srcjar_name,
srcs = srcs,
grpc_service_config = grpc_service_config,
gapic_yaml = gapic_yaml,
service_yaml = service_yaml,
transport = transport,
java_generator_name = "java_gapic",
**kwargs
)

_java_gapic_postprocess_srcjar(
name = srcjar_name,
gapic_srcjar = "%s.srcjar" % raw_srcjar_name,
Expand Down Expand Up @@ -282,3 +304,22 @@ def java_gapic_test(name, runtime_deps, test_classes, **kwargs):
tests = test_classes,
**kwargs
)

def java_generator_request_dump(
name,
srcs,
grpc_service_config = None,
gapic_yaml = None,
service_yaml = None,
transport = None,
**kwargs):
_java_gapic_srcjar(
name = name,
srcs = srcs,
grpc_service_config = grpc_service_config,
gapic_yaml = gapic_yaml,
service_yaml = service_yaml,
transport = transport,
java_generator_name = "code_generator_request_dumper",
**kwargs
)
6 changes: 2 additions & 4 deletions src/main/java/com/google/api/generator/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@
import com.google.api.ResourceProto;
import com.google.api.generator.gapic.Generator;
import com.google.longrunning.OperationsProto;
import com.google.protobuf.Descriptors.DescriptorValidationException;
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, InterruptedException, DescriptorValidationException {
public static void main(String[] args) throws IOException {
ExtensionRegistry registry = ExtensionRegistry.newInstance();
registerAllExtensions(registry);
CodeGeneratorRequest request = CodeGeneratorRequest.parseFrom(System.in, registry);
Expand All @@ -37,7 +35,7 @@ public static void main(String[] args)
}

/** Register all extensions needed to process API protofiles. */
private static void registerAllExtensions(ExtensionRegistry extensionRegistry) {
public static void registerAllExtensions(ExtensionRegistry extensionRegistry) {
OperationsProto.registerAllExtensions(extensionRegistry);
AnnotationsProto.registerAllExtensions(extensionRegistry);
ClientProto.registerAllExtensions(extensionRegistry);
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/com/google/api/generator/debug/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
load("@rules_java//java:defs.bzl", "java_library", "java_plugin")

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

filegroup(
name = "debug_files",
srcs = glob(["*.java"]),
)

java_library(
name = "debug",
srcs = [
":debug_files",
],
deps = [
"//src/main/java/com/google/api/generator",
"//src/main/java/com/google/api/generator/engine",
"//src/main/java/com/google/api/generator/engine/ast",
"//src/main/java/com/google/api/generator/gapic",
"//src/main/java/com/google/api/generator/gapic/model",
"//src/main/java/com/google/api/generator/util",
"@com_google_googleapis//google/api:api_java_proto",
"@com_google_googleapis//google/longrunning:longrunning_java_proto",
"@com_google_guava_guava//jar",
"@com_google_protobuf//:protobuf_java",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2021 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.debug;

import com.google.api.generator.Main;
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 CodeGeneratorRequestDumper {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@miraleung (from #767): If we're using Main in the class names, could we append it here for consistency? Alternatively, remove Main from the other debugger's main class's name.

Copy link
Contributor Author

@vam-google vam-google Jun 14, 2021

Choose a reason for hiding this comment

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

Those are different things basically. The MainFromFile is literally another variation of the Main class. This, on the other hand, is a very different class, doing different thing. That is why they are named differently. And strictly speaking, this file is really very-very non-main =)

I.e. this class is not really related to Main, while the other one is.

Either way, if you have preferrence how to name these classes, please let me know and I'll rename them. For now, those were the best names I could come up with.

Copy link
Contributor

Choose a reason for hiding this comment

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

MainStandalone could be something like CodeGeneratorRequestFileToGapicMain?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renamed

public static void main(String[] args) throws IOException {
ExtensionRegistry registry = ExtensionRegistry.newInstance();
Main.registerAllExtensions(registry);
CodeGeneratorRequest request = CodeGeneratorRequest.parseFrom(System.in, registry);

CodeGeneratorResponse.Builder response = CodeGeneratorResponse.newBuilder();
response
.setSupportedFeatures(CodeGeneratorResponse.Feature.FEATURE_PROTO3_OPTIONAL_VALUE)
.addFileBuilder()
.setName("desc-dump.bin")
.setContentBytes(request.toByteString());
response.build().writeTo(System.out);
}
}
43 changes: 43 additions & 0 deletions src/main/java/com/google/api/generator/debug/MainFromFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2021 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.debug;

import com.google.api.generator.Main;
import com.google.api.generator.gapic.Generator;
import com.google.protobuf.ExtensionRegistry;
import com.google.protobuf.compiler.PluginProtos.CodeGeneratorRequest;
import com.google.protobuf.compiler.PluginProtos.CodeGeneratorResponse;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class MainFromFile {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@miraleung (from #767): Could we give this a more descriptive name, e.g. GapicToFileMain

Copy link
Contributor Author

@vam-google vam-google Jun 14, 2021

Choose a reason for hiding this comment

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

Ok, I'll rename. WDYT about naming the Main class to GapicMain also?

About the GapicToFileMain class, I think that would misrepresent what this class does. Its main difference is that it reads/writes from files directly, instead of relying on protoc. I renamed it to MainStandalone (I can add Gapic in name, but in that case we would need to have that prefix in Main class as well, let me know if you want me to do that)

public static void main(String[] args) throws IOException {
ExtensionRegistry registry = ExtensionRegistry.newInstance();
Main.registerAllExtensions(registry);

String inputFile = args[0];
String outputFile = args[1];

try (InputStream inputStream = new FileInputStream(inputFile);
OutputStream outputStream = new FileOutputStream(outputFile)) {
CodeGeneratorRequest request = CodeGeneratorRequest.parseFrom(inputStream, registry);
CodeGeneratorResponse response = Generator.generateGapic(request);
response.writeTo(outputStream);
}
}
}