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][codegen] feat: java_gapic_library rule impl #319

Merged
merged 20 commits into from
Sep 19, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 6 additions & 10 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ com_google_api_generator_properties(
file = "//:dependencies.properties",
)

load("@com_google_api_generator_properties//:dependencies.properties.bzl", "PROPERTIES")
load("//:repositories.bzl", "com_google_api_generator_repositories")

com_google_api_generator_repositories()
Expand All @@ -47,27 +48,22 @@ switched_rules_by_language(
java = True,
)

# TODO(miraleung): When the gax-java Bazel build PRs are submitted, do the following:
# - Rename the next rule.
# - Use these rules in build files:
# - "@com_google_api_gax_java//gax",
# - "@com_google_api_gax_java//gax-grpc:gax_grpc",
_gax_java_version = "1.58.2"
_gax_java_version = PROPERTIES["version.com_google_gax_java"]

http_archive(
name = "com_google_api_gax_java_temp",
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_temp//:repository_rules.bzl", "com_google_api_gax_java_properties")
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_temp//:dependencies.properties",
file = "@com_google_api_gax_java//:dependencies.properties",
)

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

com_google_api_gax_java_repositories()

Expand Down
8 changes: 5 additions & 3 deletions dependencies.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ version.com_google_protobuf=3.12.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.57.0
# TODO(miraleung): Update this.
version.com_google_gax_java=1.58.3
version.io_grpc_java=1.30.2

# Common deps.
Expand All @@ -21,7 +22,7 @@ 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.io_github_java_diff_utils=io.github.java-diff-utils:java-diff-utils:4.0
maven.javax_annotation_api=javax.annotation:javax.annotation-api:1.3.2
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

# Gapic YAML parsing for batching settings.
Expand All @@ -33,4 +34,5 @@ maven.org_threeten_threetenbp=org.threeten:threetenbp:1.3.3
# Testing.
maven.junit_junit=junit:junit:4.13
maven.org_mockito_mockito_core=org.mockito:mockito-core:2.21.0
maven.com_google_truth_truth=com.google.truth:truth:0.42
# Keep in sync with gax-java.
maven.com_google_truth_truth=com.google.truth:truth:1.0
18 changes: 0 additions & 18 deletions repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -99,24 +99,6 @@ def com_google_api_generator_repositories():
server_urls = ["https://repo.maven.apache.org/maven2/"],
)

# TODO(miraleung): Remove these gax imports when gax-java's Bazel build PRs have been submitted.
_gax_java_version = PROPERTIES["version.com_google_gax_java"]

# Use the Maven artifact because a full bazel-build requires pulling in many transitive deps.
_maybe(
jvm_maven_import_external,
name = "com_google_api_gax_java",
artifact = "com.google.api:gax:%s" % _gax_java_version,
server_urls = ["https://repo.maven.apache.org/maven2/"],
)

_maybe(
jvm_maven_import_external,
name = "com_google_api_gax_grpc",
artifact = "com.google.api:gax-grpc:%s" % _gax_java_version,
server_urls = ["https://repo.maven.apache.org/maven2/"],
)

# grpc-proto doesn't have releases, so we use hashes instead.
_io_grpc_proto_prefix = "0020624375a8ee4c7dd9b3e513e443b90bc28990" # Aug. 20, 2020.
_maybe(
Expand Down
109 changes: 106 additions & 3 deletions rules_java_gapic/java_gapic.bzl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2019 Google LLC
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -14,6 +14,59 @@

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

def _java_gapic_postprocess_srcjar_impl(ctx):
gapic_srcjar = ctx.file.gapic_srcjar
output_srcjar_name = ctx.label.name
output_main = ctx.outputs.main
output_test = ctx.outputs.test
formatter = ctx.executable.formatter

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

script = """
unzip -q {gapic_srcjar}
# Sync'd to the output file name in Writer.java.
unzip -q temp-codegen.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 ")
zip -r -j {output_srcjar_name}.srcjar {output_dir_path}/src/main/* {output_dir_path}/proto/src/main/*
zip -r -j {output_srcjar_name}-tests.srcjar {output_dir_path}/src/test/*
mv {output_srcjar_name}.srcjar {output_main}
mv {output_srcjar_name}-tests.srcjar {output_test}
""".format(
gapic_srcjar = gapic_srcjar.path,
output_srcjar_name = output_srcjar_name,
output_main = output_main.path,
formatter = formatter,
output_dir_name = output_dir_name,
output_dir_path = output_dir_path,
output_test = output_test.path,
)

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

_java_gapic_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 = {
"main": "%{name}.srcjar",
"test": "%{name}-test.srcjar",
},
implementation = _java_gapic_postprocess_srcjar_impl,
)

def java_gapic_library(
name,
srcs,
Expand All @@ -36,8 +89,6 @@ def java_gapic_library(
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"
Expand All @@ -53,3 +104,55 @@ def java_gapic_library(
output_suffix = output_suffix,
**kwargs
)

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

# General additional deps.
actual_deps = deps + [
"@com_google_googleapis//google/rpc:rpc_java_proto",
"@com_google_googleapis//google/longrunning:longrunning_java_proto",
"@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",
"@com_google_auth_google_auth_library_credentials//jar",
"@com_google_auth_google_auth_library_oauth2_http//jar",
"@com_google_http_client_google_http_client//jar",
"@javax_annotation_javax_annotation_api//jar",
]

native.java_library(
name = name,
srcs = ["%s.srcjar" % srcjar_name],
deps = actual_deps,
**kwargs
)

# Test deps.
actual_test_deps = test_deps + actual_deps + [
"@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",
]

native.java_library(
name = "%s_test" % name,
srcs = ["%s-test.srcjar" % srcjar_name],
deps = [":%s" % name] + actual_test_deps,
**kwargs
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ java_library(
"//src/main/java/com/google/api/generator/gapic/model",
"//src/main/java/com/google/api/generator/gapic/utils",
"@com_google_api_api_common//jar",
"@com_google_api_gax_grpc//jar",
"@com_google_api_gax_java//jar",
"@com_google_api_gax_java//gax",
"@com_google_api_gax_java//gax-grpc:gax_grpc",
"@com_google_code_findbugs_jsr305//jar",
"@com_google_googleapis//google/api:api_java_proto",
"@com_google_googleapis//google/longrunning:longrunning_java_proto",
Expand All @@ -30,7 +30,7 @@ java_library(
"@io_grpc_grpc_java//api",
"@io_grpc_grpc_java//protobuf",
"@io_grpc_grpc_java//stub",
"@javax_annotation_api//jar",
"@javax_annotation_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 @@ -81,9 +81,12 @@ public static CodeGeneratorResponse writeCode(List<GapicClass> clazzes, String o
private static String getPath(String pakkage, String className) {
String path = pakkage.replaceAll("\\.", "/");
if (className.startsWith("Mock") || className.endsWith("Test")) {
path = "test/" + path;
path = "src/test/java/" + path;
} else {
path = "src/main/java/" + path;
}
// Resource name helpers go into the protobuf package.

// Resource name helpers go into the protobuf package. Denote this with "proto/src/main/*".
if (className.endsWith("Name")) {
path = "proto/" + path;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ java_proto_library(
"//src/main/java/com/google/api/generator/gapic/model",
"//src/main/java/com/google/api/generator/gapic/protoparser",
"//src/test/java/com/google/api/generator/gapic/testdata:showcase_java_proto",
"@com_google_api_gax_java//jar",
"@com_google_api_gax_java//gax",
"@com_google_googleapis//google/logging/v2:logging_java_proto",
"@com_google_googleapis//google/pubsub/v1:pubsub_java_proto",
"@com_google_googleapis//google/rpc:rpc_java_proto",
Expand Down