Skip to content

Commit

Permalink
feat: implement DIREGAPIC
Browse files Browse the repository at this point in the history
  • Loading branch information
vam-google committed Jun 2, 2021
1 parent 8f6f40e commit 8983e23
Show file tree
Hide file tree
Showing 63 changed files with 7,458 additions and 123 deletions.
26 changes: 26 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,32 @@ java_binary(
],
)

java_binary(
name = "protoc-gen-java_dumper",
main_class = "com.google.api.generator.MainDumper",
runtime_deps = [
"//src/main/java/com/google/api/generator",
"//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_gapicfromfile",
main_class = "com.google.api.generator.MainFromFile",
runtime_deps = [
"//src/main/java/com/google/api/generator",
"//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
56 changes: 35 additions & 21 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
workspace(name = "gapic_generator_java")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:jvm.bzl", "jvm_maven_import_external")

# DO NOT REMOVE.
# This is needed to clobber any transitively-pulled in versions of bazel_skylib so that packages
Expand All @@ -14,6 +15,32 @@ http_archive(
],
)

jvm_maven_import_external(
name = "google_java_format_all_deps",
artifact = "com.google.googlejavaformat:google-java-format:jar:all-deps:1.7",
server_urls = ["https://repo.maven.apache.org/maven2/", "http://repo1.maven.org/maven2/"],
licenses = ["notice", "reciprocal"]
)

_gax_java_version = "1.64.0"

http_archive(
name = "com_google_api_gax_java",
strip_prefix = "gax-java-%s" % _gax_java_version,
urls = ["https://github.com/googleapis/gax-java/archive/v%s.zip" % _gax_java_version],
)

load("@com_google_api_gax_java//:repository_rules.bzl", "com_google_api_gax_java_properties")

com_google_api_gax_java_properties(
name = "com_google_api_gax_java_properties",
file = "@com_google_api_gax_java//:dependencies.properties",
)

load("@com_google_api_gax_java//:repositories.bzl", "com_google_api_gax_java_repositories")

com_google_api_gax_java_repositories()

load("//:repository_rules.bzl", "gapic_generator_java_properties")

gapic_generator_java_properties(
Expand All @@ -35,8 +62,8 @@ protobuf_deps()
# Import the monolith so we can transitively use its gapic rules for googleapis.
http_archive(
name = "com_google_api_codegen",
strip_prefix = "gapic-generator-2.4.6",
urls = ["https://github.com/googleapis/gapic-generator/archive/v2.4.6.zip"],
strip_prefix = "gapic-generator-2.11.1",
urls = ["https://github.com/googleapis/gapic-generator/archive/v2.11.1.zip"],
)

load("@com_google_googleapis//:repository_rules.bzl", "switched_rules_by_language")
Expand All @@ -48,25 +75,12 @@ switched_rules_by_language(
java = True,
)

_gax_java_version = PROPERTIES["version.com_google_gax_java"]

http_archive(
name = "com_google_api_gax_java",
strip_prefix = "gax-java-%s" % _gax_java_version,
urls = ["https://github.com/googleapis/gax-java/archive/v%s.zip" % _gax_java_version],
)

load("@com_google_api_gax_java//:repository_rules.bzl", "com_google_api_gax_java_properties")

com_google_api_gax_java_properties(
name = "com_google_api_gax_java_properties",
file = "@com_google_api_gax_java//:dependencies.properties",
)

load("@com_google_api_gax_java//:repositories.bzl", "com_google_api_gax_java_repositories")

com_google_api_gax_java_repositories()

load("@io_grpc_grpc_java//:repositories.bzl", "grpc_java_repositories")

grpc_java_repositories()

http_archive(
name = "com_google_disco_to_proto3_converter",
strip_prefix = "disco-to-proto3-converter-1839f6aca5e968e59b7acc03e7018b0fda8c480b",
urls = ["https://github.com/googleapis/disco-to-proto3-converter/archive/1839f6aca5e968e59b7acc03e7018b0fda8c480b.zip"],
)
5 changes: 2 additions & 3 deletions dependencies.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
# Target workspace name: com_google_api_codegen

# Versions only, for dependencies which actual artifacts differ between Bazel and Gradle
version.com_google_protobuf=3.13.0
version.com_google_protobuf=3.15.2
# Version of google-java-format is downgraded from 1.8 to 1.7, because 1.8 supports java 11 minimum, while our JRE is java 8.
version.google_java_format=1.7
version.com_google_api_common_java=1.9.3
version.com_google_gax_java=1.62.0
version.io_grpc_java=1.30.2

# Common deps.
Expand All @@ -20,7 +19,7 @@ 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_code_gson=com.google.code.gson:gson:2.8.6
maven.com_google_protobuf_protobuf_java=com.google.protobuf:protobuf-java:3.12.2
maven.com_google_protobuf_protobuf_java=com.google.protobuf:protobuf-java:3.15.8
maven.io_github_java_diff_utils=io.github.java-diff-utils:java-diff-utils:4.0
maven.javax_annotation_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
Expand Down
10 changes: 8 additions & 2 deletions repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,18 @@ def gapic_generator_java_repositories():
_maybe(
http_archive,
name = "com_google_googleapis",
strip_prefix = "googleapis-2bea43cdc7a4443876380732980d83cd8d560582",
strip_prefix = "googleapis-0b26f05c9f469dc710e13a94f10d46dae4fa0b3c",
urls = [
"https://github.com/googleapis/googleapis/archive/2bea43cdc7a4443876380732980d83cd8d560582.zip",
"https://github.com/googleapis/googleapis/archive/0b26f05c9f469dc710e13a94f10d46dae4fa0b3c.zip",
],
)

_maybe(
native.local_repository,
name = "com_google_googleapis_discovery",
path = "/usr/local/google/home/vam/_/projects/_/googleapis-discovery"
)

_maybe(
native.bind,
name = "guava",
Expand Down
47 changes: 35 additions & 12 deletions rules_java_gapic/java_gapic.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,14 @@ def java_gapic_library(
service_yaml = None,
deps = [],
test_deps = [],
transport = None,
java_generator_name = "java_gapic",
**kwargs):
file_args_dict = {}

if grpc_service_config:
file_args_dict[grpc_service_config] = "grpc-service-config"
else:
elif transport != "rest":
for keyword in NO_GRPC_CONFIG_ALLOWLIST:
if keyword not in name:
fail("Missing a gRPC service config file")
Expand All @@ -157,21 +159,25 @@ def java_gapic_library(
srcjar_name = name + "_srcjar"
raw_srcjar_name = srcjar_name + "_raw"
output_suffix = ".srcjar"
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"]

_java_generator_name = "java_gapic"
proto_custom_library(
name = raw_srcjar_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
)

Expand Down Expand Up @@ -201,10 +207,7 @@ def java_gapic_library(
"@com_google_protobuf//:protobuf_java",
"@com_google_api_api_common//jar",
"@com_google_api_gax_java//gax:gax",
"@com_google_api_gax_java//gax-grpc:gax_grpc",
"@com_google_guava_guava//jar",
"@io_grpc_grpc_java//core:core",
"@io_grpc_grpc_java//protobuf:protobuf",
"@com_google_code_findbugs_jsr305//jar",
"@org_threeten_threetenbp//jar",
"@io_opencensus_opencensus_api//jar",
Expand All @@ -214,6 +217,17 @@ def java_gapic_library(
"@javax_annotation_javax_annotation_api//jar",
]

if transport == "rest":
actual_deps += [
"@com_google_api_gax_java//gax-httpjson:gax_httpjson",
]
else:
actual_deps += [
"@com_google_api_gax_java//gax-grpc:gax_grpc",
"@io_grpc_grpc_java//core:core",
"@io_grpc_grpc_java//protobuf:protobuf",
]

native.java_library(
name = name,
srcs = ["%s.srcjar" % srcjar_name],
Expand All @@ -224,15 +238,24 @@ def java_gapic_library(
# Test deps.
actual_test_deps = [
"@com_google_googleapis//google/type:type_java_proto", # Commonly used.
"@com_google_api_gax_java//gax-grpc:gax_grpc_testlib",
"@com_google_api_gax_java//gax:gax_testlib",
"@com_google_code_gson_gson//jar",
"@io_grpc_grpc_java//auth:auth",
"@io_grpc_grpc_netty_shaded//jar",
"@io_grpc_grpc_java//stub:stub",
"@io_opencensus_opencensus_contrib_grpc_metrics//jar",
"@junit_junit//jar",
]

if transport == "rest":
actual_test_deps += [
"@com_google_api_gax_java//gax-httpjson:gax_httpjson_testlib",
]
else:
actual_test_deps += [
"@com_google_api_gax_java//gax-grpc:gax_grpc_testlib",
"@io_grpc_grpc_java//auth:auth",
"@io_grpc_grpc_netty_shaded//jar",
"@io_grpc_grpc_java//stub:stub",
"@io_opencensus_opencensus_contrib_grpc_metrics//jar",
]

_append_dep_without_duplicates(actual_test_deps, test_deps)
_append_dep_without_duplicates(actual_test_deps, actual_deps)

Expand Down
8 changes: 7 additions & 1 deletion rules_java_gapic/java_gapic_pkg.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ def java_gapic_assembly_gradle_pkg(
name,
deps,
assembly_name = None,
transport = None,
**kwargs):
package_dir = name
if assembly_name:
Expand Down Expand Up @@ -350,9 +351,14 @@ def java_gapic_assembly_gradle_pkg(
grpc_target_dep = ["%s" % grpc_target]

if client_deps:
if transport == "rest":
template_label = Label("//rules_java_gapic:resources/gradle/client_rest.gradle.tmpl")
else:
template_label = Label("//rules_java_gapic:resources/gradle/client_grpc.gradle.tmpl")

_java_gapic_gradle_pkg(
name = client_target,
template_label = Label("//rules_java_gapic:resources/gradle/client.gradle.tmpl"),
template_label = template_label,
deps = proto_target_dep + client_deps,
test_deps = grpc_target_dep + client_test_deps,
**kwargs
Expand Down
60 changes: 60 additions & 0 deletions rules_java_gapic/resources/gradle/client_rest.gradle.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
buildscript {
repositories {
mavenCentral()
}
}

apply plugin: 'java'

description = 'GAPIC library for {{name}}'
group = 'com.google.cloud'
version = (findProperty('version') == 'unspecified') ? '0.0.0-SNAPSHOT' : version
sourceCompatibility = 1.7
targetCompatibility = 1.7

repositories {
mavenCentral()
mavenLocal()
}

compileJava.options.encoding = 'UTF-8'
javadoc.options.encoding = 'UTF-8'

dependencies {
compile 'com.google.api:gax:{{version.gax}}'
testCompile 'com.google.api:gax:{{version.gax}}:testlib'
compile 'com.google.api:gax-httpjson:{{version.gax_httpjson}}'
testCompile 'com.google.api:gax-httpjson:{{version.gax_httpjson}}:testlib'
testCompile '{{maven.junit_junit}}'
{{extra_deps}}
}

task smokeTest(type: Test) {
filter {
includeTestsMatching "*SmokeTest"
setFailOnNoMatchingTests false
}
}

test {
exclude "**/*SmokeTest*"
}

sourceSets {
main {
java {
srcDir 'src/main/java'
}
}
}

clean {
delete 'all-jars'
}

task allJars(type: Copy) {
dependsOn test, jar
into 'all-jars'
// Replace with `from configurations.testRuntime, jar` to include test dependencies
from configurations.runtime, jar
}
50 changes: 50 additions & 0 deletions src/main/java/com/google/api/generator/MainDumper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 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;

import com.google.api.AnnotationsProto;
import com.google.api.ClientProto;
import com.google.api.FieldBehaviorProto;
import com.google.api.ResourceProto;
import com.google.longrunning.OperationsProto;
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 MainDumper {
public static void main(String[] args) throws IOException {
ExtensionRegistry registry = ExtensionRegistry.newInstance();
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);
}

/** Register all extensions needed to process API protofiles. */
private static void registerAllExtensions(ExtensionRegistry extensionRegistry) {
OperationsProto.registerAllExtensions(extensionRegistry);
AnnotationsProto.registerAllExtensions(extensionRegistry);
ClientProto.registerAllExtensions(extensionRegistry);
ResourceProto.registerAllExtensions(extensionRegistry);
FieldBehaviorProto.registerAllExtensions(extensionRegistry);
}
}
Loading

0 comments on commit 8983e23

Please sign in to comment.