Skip to content
Open
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
1 change: 1 addition & 0 deletions scaffold.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -212,5 +212,6 @@ presets:
scala:
langs: ['Scala']
lint: true
proto: true
minimal:
langs: []
58 changes: 58 additions & 0 deletions user_stories/scala.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,61 @@ output="$(bazel run src:Hello)"
exit 1
}
~~~

## Using protobuf and gRPC

Let's introduce a small data schema.

~~~sh
>src/foo.proto cat <<EOF
syntax = "proto3";
option java_package = "build.aspect.examples";
option java_outer_classname = "FooOuterClass";
message Foo {
string message = 1;
}
EOF
~~~

Generate the BUILD rules for protobuf:

~~~sh
bazel run gazelle
~~~

There isn't a Gazelle generator for scala_proto_library yet, so add it manually:

~~~sh
buildozer 'new_load @rules_scala//scala_proto:scala_proto.bzl scala_proto_library' src:__pkg__
buildozer 'new scala_proto_library foo_scala_proto' src:__pkg__
buildozer 'add deps :foo_proto' src:foo_scala_proto
buildozer 'add deps :foo_scala_proto' src:Hello
~~~

Now overwrite `Hello.scala` to use the generated `Foo` message:

~~~sh
>src/Hello.scala cat <<EOF
import build.aspect.examples.FooOuterClass

object Hello {
def main(args: Array[String]): Unit = {
val foo = FooOuterClass.Foo.newBuilder()
.setMessage("Hello from Scala Protobuf!")
.build()
println(foo)
}
}
EOF
~~~

Run the application again:

~~~sh
output="$(bazel run src:Hello)"

[ "${output}" = "message: \"Hello from Scala Protobuf!\"" ] || {
echo >&2 "Wanted output 'Hello from Scala Protobuf' but got '${output}'"
exit 1
}
~~~
13 changes: 11 additions & 2 deletions {{ .ProjectSnake }}/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ use_repo(multitool, "multitool")
# Protobuf and gRPC
bazel_dep(name = "protobuf", version = "33.4")
bazel_dep(name = "protovalidate", version = "1.0.0")
{{- if .Computed.java }}
bazel_dep(name = "grpc-java", version = "1.75.0")
# Force later resolutions of some transitives to pick up Bazel 9 fixes
bazel_dep(name = "rules_foreign_cc", version = "0.15.1", repo_name = None)
{{- if .Computed.java }}
bazel_dep(name = "grpc-java", version = "1.75.0")
bazel_dep(name = "bazel_jar_jar", version = "0.1.12", repo_name = None)

{{- end }}
Expand Down Expand Up @@ -252,6 +252,15 @@ scala_deps = use_extension(
"scala_deps",
)
scala_deps.scala()
{{ if .Scaffold.proto -}}
scala_deps.scala_proto(
default_gen_opts = [
"grpc",
"flat_package",
"scala3_sources",
],
)
{{- end }}
{{- end }}

{{- if .Scaffold.oci }}
Expand Down
Loading