Skip to content

Commit dcdd847

Browse files
committed
Create an example project to demonstrate bzlmod usage.
1 parent 2c56db0 commit dcdd847

File tree

13 files changed

+1758
-0
lines changed

13 files changed

+1758
-0
lines changed

.bazelignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bzl-examples/

bzl-examples/bzlmod/.bazelrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build --cxxopt=-std=c++17 --host_cxxopt=-std=c++17

bzl-examples/bzlmod/.bazelversion

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
7.6.1

bzl-examples/bzlmod/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bazel-*
2+
MODULE.bazel.lock

bzl-examples/bzlmod/BUILD.bazel

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
load("@aspect_rules_lint//format:defs.bzl", "format_multirun")
2+
load("@rules_java//java:defs.bzl", "java_binary")
3+
4+
java_binary(
5+
name = "ktfmt",
6+
main_class = "com.facebook.ktfmt.cli.Main",
7+
runtime_deps = ["@ktfmt//jar"],
8+
)
9+
10+
format_multirun(
11+
name = "format",
12+
kotlin = ":ktfmt",
13+
protocol_buffer = "@rules_buf_toolchains//:buf",
14+
starlark = "@buildifier_prebuilt//:buildifier",
15+
visibility = ["//visibility:public"],
16+
)

bzl-examples/bzlmod/MODULE.bazel

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
module(
2+
name = "bzlmod",
3+
version = "1.0",
4+
)
5+
6+
bazel_dep(name = "protobuf", version = "30.2")
7+
bazel_dep(name = "rules_kotlin", version = "2.1.3")
8+
bazel_dep(name = "com_github_grpc_grpc_kotlin", version = "0.0.0")
9+
bazel_dep(name = "rules_jvm_external", version = "6.7")
10+
bazel_dep(name = "aspect_rules_lint", version = "1.3.4")
11+
bazel_dep(name = "rules_java", version = "8.11.0")
12+
bazel_dep(name = "buildifier_prebuilt", version = "8.0.3")
13+
bazel_dep(name = "rules_buf", version = "0.3.0")
14+
15+
local_path_override(
16+
module_name = "com_github_grpc_grpc_kotlin",
17+
path = "../../",
18+
)
19+
20+
maven = use_extension("@rules_jvm_external//:extensions.bzl", "maven")
21+
maven.install(
22+
# Rerun whenever adding new artifacts.
23+
# $ REPIN=1 bazelisk run @maven//:pin
24+
artifacts = [
25+
"com.google.truth:truth:1.4.2",
26+
"junit:junit:4.13.2",
27+
"com.google.truth.extensions:truth-proto-extension:1.4.4",
28+
"io.grpc:grpc-testing:1.71.0",
29+
],
30+
fetch_sources = False,
31+
generate_compat_repositories = True,
32+
lock_file = "//:maven_install.json",
33+
strict_visibility = True,
34+
)
35+
use_repo(maven, "maven")
36+
37+
install_ktfmt = use_extension("//:extensions.bzl", "install_ktfmt")
38+
use_repo(install_ktfmt, "ktfmt")
39+
40+
buf = use_extension("@rules_buf//buf:extensions.bzl", "buf")
41+
use_repo(buf, "rules_buf_toolchains")

bzl-examples/bzlmod/extensions.bzl

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
load("@aspect_rules_lint//format:repositories.bzl", "fetch_ktfmt")
2+
3+
def _install_ktfmt_impl(module_ctx):
4+
fetch_ktfmt()
5+
6+
install_ktfmt = module_extension(_install_ktfmt_impl)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
load("@com_github_grpc_grpc_kotlin//:kt_jvm_grpc.bzl", "kt_jvm_grpc_library", "kt_jvm_proto_library")
2+
load("@protobuf//bazel:proto_library.bzl", "proto_library")
3+
load("@rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")
4+
5+
package(default_visibility = ["//javatests/io/grpc/examples/bzlmod:__subpackages__"])
6+
7+
proto_library(
8+
name = "fibonacci_proto",
9+
srcs = ["fibonacci.proto"],
10+
deps = [],
11+
)
12+
13+
kt_jvm_proto_library(
14+
name = "fibonacci_kt_proto",
15+
deps = [":fibonacci_proto"],
16+
)
17+
18+
kt_jvm_grpc_library(
19+
name = "fibonacci_kt_grpc",
20+
srcs = [":fibonacci_proto"],
21+
deps = [":fibonacci_kt_proto"],
22+
)
23+
24+
kt_jvm_library(
25+
name = "fibonacci",
26+
srcs = ["Fibonacci.kt"],
27+
deps = [
28+
":fibonacci_kt_grpc",
29+
":fibonacci_kt_proto",
30+
],
31+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.grpc.examples.bzlmod
2+
3+
/** A simple implementation of a Fibonacci service. */
4+
class Fibonacci : FibonacciServiceGrpcKt.FibonacciServiceCoroutineImplBase() {
5+
override suspend fun query(request: QueryRequest): QueryResponse {
6+
if (request.nth <= 1) {
7+
return queryResponse { nthFibonacci = request.nth }
8+
}
9+
var prv = 0L
10+
var cur = 1L
11+
var nxt = 1L
12+
for (i in 2..request.nth) {
13+
prv = cur
14+
cur = nxt
15+
nxt = (prv + cur) % request.mod
16+
}
17+
return queryResponse { nthFibonacci = cur }
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
syntax = "proto3";
2+
3+
package io.grpc.examples.bzlmod;
4+
5+
option java_multiple_files = true;
6+
option java_outer_classname = "FibonacciProtos";
7+
option java_package = "io.grpc.examples.bzlmod";
8+
9+
message QueryRequest {
10+
int64 nth = 1;
11+
int64 mod = 2;
12+
}
13+
14+
message QueryResponse {
15+
int64 nth_fibonacci = 1;
16+
}
17+
18+
service FibonacciService {
19+
rpc Query(QueryRequest) returns (QueryResponse);
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
load("@rules_jvm_external//:defs.bzl", "artifact")
2+
load("@rules_kotlin//kotlin:jvm.bzl", "kt_jvm_test")
3+
4+
kt_jvm_test(
5+
name = "FibonacciTest",
6+
srcs = ["FibonacciTest.kt"],
7+
deps = [
8+
"//java/io/grpc/examples/bzlmod:fibonacci",
9+
"//java/io/grpc/examples/bzlmod:fibonacci_kt_grpc",
10+
"//java/io/grpc/examples/bzlmod:fibonacci_kt_proto",
11+
artifact("com.google.truth:truth"),
12+
artifact("com.google.truth.extensions:truth-proto-extension"),
13+
artifact("junit:junit"),
14+
artifact("io.grpc:grpc-testing"),
15+
],
16+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package io.grpc.examples.bzlmod
2+
3+
import com.google.common.truth.Truth.assertThat
4+
import com.google.common.truth.extensions.proto.ProtoTruth.assertThat
5+
import io.grpc.inprocess.InProcessChannelBuilder
6+
import io.grpc.inprocess.InProcessServerBuilder
7+
import io.grpc.testing.GrpcCleanupRule
8+
import kotlinx.coroutines.runBlocking
9+
import org.junit.Before
10+
import org.junit.Rule
11+
import org.junit.Test
12+
import org.junit.runner.RunWith
13+
import org.junit.runners.JUnit4
14+
15+
@RunWith(JUnit4::class)
16+
class FibonacciTest {
17+
@get:Rule val grpcCleanup = GrpcCleanupRule()
18+
19+
private lateinit var stub: FibonacciServiceGrpcKt.FibonacciServiceCoroutineStub
20+
21+
@Before
22+
fun setUp() {
23+
val serverName = InProcessServerBuilder.generateName()
24+
25+
grpcCleanup.register(
26+
InProcessServerBuilder.forName(serverName)
27+
.directExecutor()
28+
.addService(Fibonacci())
29+
.build()
30+
.start())
31+
32+
stub =
33+
FibonacciServiceGrpcKt.FibonacciServiceCoroutineStub(
34+
grpcCleanup.register(
35+
InProcessChannelBuilder.forName(serverName).directExecutor().build()))
36+
}
37+
38+
@Test
39+
fun query_succeeds() {
40+
runBlocking {
41+
val response =
42+
stub.query(
43+
queryRequest {
44+
nth = 20
45+
mod = 1000000007
46+
})
47+
assertThat(response).isEqualTo(queryResponse { nthFibonacci = 6765 })
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)