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

Organize scalapb rules #818

Closed
Closed
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: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ scala_repositories((
}
))
```
If you're using any of the rules `twitter_scrooge`, `tut_repositories`, `scala_proto_repositories`
If you're using any of the rules `twitter_scrooge`, `tut_repositories`, `scalapb_repositories`
or `specs2_junit_repositories` you also need to specify `scala_version` for them. See `./test_version/WORKSPACE.template`
for an example workspace using another scala version.

Expand All @@ -109,13 +109,25 @@ for an example workspace using another scala version.
| bazel | rules_scala gitsha |
|--------|--------------------|
| 0.28.0 | HEAD |
| 0.23.x | ca655e5a330cbf1d66ce1d9baa63522752ec6011 | |
| 0.23.x | ca655e5a330cbf1d66ce1d9baa63522752ec6011 |
| 0.22.x | f3113fb6e9e35cb8f441d2305542026d98afc0a2 |
| 0.16.x | f3113fb6e9e35cb8f441d2305542026d98afc0a2 |
| 0.15.x | 3b9ab9be31ac217d3337c709cb6bfeb89c8dcbb1 |
| 0.14.x | 3b9ab9be31ac217d3337c709cb6bfeb89c8dcbb1 |
| 0.13.x | 3c987b6ae8a453886759b132f1572c0efca2eca2 |

## Upgrades & Breaking Changes

Breaking changes do occur and we try our best to deprecate
functionaltiy before removing it. To assist in making upgrades easier,
we suggest you incrementally upgrade rules_scala in your codebase by
stepping through specific commits where large changes occurred.

| commit | date | change |
|------------------------------------------|---------|---------|
| HEAD | current |
| e2f8e8b0331a926936f60bf21cb9e39e0c58bbb7 | 2019-08 | `scala_proto_` prefix deprecated in favor of `scalapb_` prefix |

## Usage with [bazel-deps](https://github.com/johnynek/bazel-deps)

Bazel-deps allows you to generate bazel dependencies transitively for maven artifacts. Generally we don't want bazel-deps to fetch
Expand Down
22 changes: 11 additions & 11 deletions docs/scalapb_proto_library.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ To use this rule, you'll first need to add the following to your `WORKSPACE` fil
which adds a few dependencies needed for ScalaPB:

```python
load("@io_bazel_rules_scala//scala_proto:scala_proto.bzl", "scala_proto_repositories")
scala_proto_repositories(scala_version = "2.12.8") # or whatever scala_version you're on
load("@io_bazel_rules_scala//scalapb:scalapb.bzl", "scalapb_repositories")
scalapb_repositories(scala_version = "2.12.8") # or whatever scala_version you're on
```

Then you can import `scalapb_proto_library` in any `BUILD` file like this:

```python
load("@io_bazel_rules_scala//scala_proto:scala_proto.bzl", "scalapb_proto_library")
load("@io_bazel_rules_scala//scalapb:scalapb.bzl", "scalapb_proto_library")
scalapb_proto_library(
name = "my_scala_proto_lib",
deps = [":my_target"],
Expand All @@ -28,15 +28,15 @@ generated by the [ScalaPB compiler](https://github.com/scalapb/ScalaPB).
| name | `Name, required` <br> A unique name for this target.
| deps | `List of labels, required` <br> A list of `proto_library` targets for which to generate Scala code.

## Configuration (via `scala_proto_toolchain`)
## Configuration (via `scalapb_toolchain`)

To configure ScalaPB options, configure a different `scala_proto_toolchain` and declare it in a `BUILD` file:
To configure ScalaPB options, configure a different `scalapb_toolchain` and declare it in a `BUILD` file:

```python
load("@io_bazel_rules_scala//scala_proto:scala_proto_toolchain.bzl", "scala_proto_toolchain")
load("@io_bazel_rules_scala//scalapb:scalapb_toolchain.bzl", "scalapb_toolchain")

scala_proto_toolchain(
name = "scala_proto_toolchain_configuration",
scalapb_toolchain(
name = "scalapb_toolchain_configuration",
with_grpc = False,
with_flat_package = False,
with_single_line_to_string = False,
Expand All @@ -45,8 +45,8 @@ scala_proto_toolchain(

toolchain(
name = "scalapb_toolchain",
toolchain = ":scala_proto_toolchain_configuration",
toolchain_type = "@io_bazel_rules_scala//scala_proto:toolchain_type",
toolchain = ":scalapb_toolchain_configuration",
toolchain_type = "@io_bazel_rules_scala//scalapb:toolchain_type",
visibility = ["//visibility:public"],
)
```
Expand All @@ -65,4 +65,4 @@ toolchain(
| extra_generator_dependencies | `List of labels, optional` <br>
| grpc_deps | `List of labels, optional (has default)` <br> gRPC dependencies. A sensible default is provided.
| implicit_compile_deps | `List of labels, optional (has default)` <br> ScalaPB dependencies. A sensible default is provided.
| scalac | `Label, optional (has default)` <br> Target for scalac. A sensible default is provided.
| scalac | `Label, optional (has default)` <br> Target for scalac. A sensible default is provided.
28 changes: 28 additions & 0 deletions private/console.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""
Prints out a deprecation warning indicating that a paricular usage
of rules_scala will not be supported in the future
"""
def console_print_deprecation(
old_item,
new_item
):
_console_print(
["warning", "deprecation"],
"%s is deprecated." % old_item,
"Please use %s instead." % new_item,
"deprecated: %s" % old_item,
"supported : %s" % new_item
)

"""
Helper method for consistently printing out structured messages from
rules_scala.
"""
def _console_print(
mnemonic_crumbs,
*lines
):
mnemonic = "::".join(mnemonic_crumbs)
print("\nrules_scala::{mnemonic}>>\n {body}\n<<rules_scala::{mnemonic}".format(
mnemonic = "::".join(mnemonic_crumbs),
body = "\n ".join(lines)))
54 changes: 3 additions & 51 deletions scala_proto/BUILD
Original file line number Diff line number Diff line change
@@ -1,53 +1,5 @@
load("//scala_proto:scala_proto_toolchain.bzl", "scala_proto_toolchain")
load("//scala_proto:default_dep_sets.bzl", "DEFAULT_SCALAPB_COMPILE_DEPS", "DEFAULT_SCALAPB_GRPC_DEPS")


toolchain_type(
alias(
name = "toolchain_type",
actual = "//scalapb:toolchain_type",
visibility = ["//visibility:public"],
)

scala_proto_toolchain(
name = "default_toolchain_impl",
with_grpc = True,
with_flat_package=False,
with_single_line_to_string=False,
visibility = ["//visibility:public"],
)

toolchain(
name = "default_toolchain",
toolchain = ":default_toolchain_impl",
toolchain_type = "@io_bazel_rules_scala//scala_proto:toolchain_type",
visibility = ["//visibility:public"],
)


scala_proto_toolchain(
name = "enable_all_options_toolchain_impl",
with_grpc = True,
with_flat_package=True,
# with_java=True,
with_single_line_to_string=True,
visibility = ["//visibility:public"],
)

toolchain(
name = "enable_all_options_toolchain",
toolchain = ":enable_all_options_toolchain_impl",
toolchain_type = "@io_bazel_rules_scala//scala_proto:toolchain_type",
visibility = ["//visibility:public"],
)


java_library(
name="default_scalapb_compile_dependencies",
exports = DEFAULT_SCALAPB_COMPILE_DEPS,
visibility = ["//visibility:public"],
)

java_library(
name="default_scalapb_grpc_dependencies",
exports = DEFAULT_SCALAPB_GRPC_DEPS,
visibility = ["//visibility:public"],
)
)
73 changes: 14 additions & 59 deletions scala_proto/scala_proto.bzl
Original file line number Diff line number Diff line change
@@ -1,59 +1,14 @@
load(
"//scala:scala_cross_version.bzl",
_default_scala_version = "default_scala_version",
)
load(
"//scala_proto/private:scala_proto_default_repositories.bzl",
"scala_proto_default_repositories",
)
load(
"//scala_proto/private:scalapb_aspect.bzl",
"ScalaPBAspectInfo",
"ScalaPBInfo",
"merge_scalapb_aspect_info",
"scalapb_aspect",
)

def register_default_proto_dependencies():
if native.existing_rule("io_bazel_rules_scala/dependency/proto/grpc_deps") == None:
native.bind(
name = "io_bazel_rules_scala/dependency/proto/grpc_deps",
actual = "@io_bazel_rules_scala//scala_proto:default_scalapb_grpc_dependencies",
)

if native.existing_rule("io_bazel_rules_scala/dependency/proto/implicit_compile_deps") == None:
native.bind(
name = "io_bazel_rules_scala/dependency/proto/implicit_compile_deps",
actual = "@io_bazel_rules_scala//scala_proto:default_scalapb_compile_dependencies",
)



def scala_proto_repositories(
scala_version = _default_scala_version(),
maven_servers = ["http://central.maven.org/maven2"]):
ret = scala_proto_default_repositories(scala_version, maven_servers)
register_default_proto_dependencies()
return ret



def _scalapb_proto_library_impl(ctx):
aspect_info = merge_scalapb_aspect_info(
[dep[ScalaPBAspectInfo] for dep in ctx.attr.deps],
)
all_java = aspect_info.java_info

return [
all_java,
ScalaPBInfo(aspect_info = aspect_info),
DefaultInfo(files = aspect_info.output_files),
]

scalapb_proto_library = rule(
implementation = _scalapb_proto_library_impl,
attrs = {
"deps": attr.label_list(aspects = [scalapb_aspect]),
},
provides = [DefaultInfo, ScalaPBInfo, JavaInfo],
)
load("//private:console.bzl", "console_print_deprecation")
load("//scalapb:scalapb.bzl", "scalapb_repositories", _scalapb_proto_library = "scalapb_proto_library")

def scala_proto_repositories(**kwargs):
console_print_deprecation(
"//scala_proto:scala_proto.bzl scala_proto_repositories",
"//scalapb:scalapb.bzl scalapb_repositories")
scalapb_repositories(**kwargs)

def scalapb_proto_library(**kwargs):
console_print_deprecation(
"//scala_proto:scala_proto.bzl scalapb_proto_library",
"//scalapb:scalapb.bzl scalapb_proto_library")
_scalapb_proto_library(**kwargs)
54 changes: 7 additions & 47 deletions scala_proto/scala_proto_toolchain.bzl
Original file line number Diff line number Diff line change
@@ -1,49 +1,9 @@
load("//scala_proto:default_dep_sets.bzl", "DEFAULT_SCALAPB_COMPILE_DEPS", "DEFAULT_SCALAPB_GRPC_DEPS")

def _scala_proto_toolchain_impl(ctx):
toolchain = platform_common.ToolchainInfo(
with_grpc = ctx.attr.with_grpc,
with_flat_package = ctx.attr.with_flat_package,
with_single_line_to_string = ctx.attr.with_single_line_to_string,
blacklisted_protos = ctx.attr.blacklisted_protos,
code_generator = ctx.attr.code_generator,
extra_generator_dependencies = ctx.attr.extra_generator_dependencies,
scalac=ctx.attr.scalac,
named_generators = ctx.attr.named_generators,
)
return [toolchain]


# Args:
# with_grpc: Enables generation of grpc service bindings for services
# with_flat_package: When true, ScalaPB will not append the protofile base name to the package name
# with_single_line_to_string: Enables generation of toString() methods that use the single line format
# blacklisted_protos: list of protobuf targets to exclude from recursive building
# code_generator: what code generator to use, usually you'll want the default
scala_proto_toolchain = rule(
_scala_proto_toolchain_impl,
attrs = {
"with_grpc": attr.bool(),
"with_flat_package": attr.bool(),
"with_single_line_to_string": attr.bool(),
"blacklisted_protos": attr.label_list(default=[]),
"code_generator": attr.label(
executable = True,
cfg = "host",
default = Label("@io_bazel_rules_scala//src/scala/scripts:scalapb_generator"),
allow_files=True
),
"named_generators": attr.string_dict(),
"extra_generator_dependencies": attr.label_list(
providers = [JavaInfo]
),
"scalac": attr.label(
default = Label(
"@io_bazel_rules_scala//src/java/io/bazel/rulesscala/scalac",
),
),
},
)

load("//private:console.bzl", "console_print_deprecation")

load("//scalapb:scalapb_toolchain.bzl", "scalapb_toolchain")

def scala_proto_toolchain(**kwargs):
console_print_deprecation(
"//scala_proto:scala_proto_toolchain.bzl scala_proto_toolchain",
"//scalapb:scalapb_toolchain.bzl scalapb_toolchain")
scalapb_toolchain(**kwargs)
7 changes: 0 additions & 7 deletions scala_proto/toolchains.bzl

This file was deleted.

53 changes: 53 additions & 0 deletions scalapb/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
load("//scalapb:scalapb_toolchain.bzl", "scalapb_toolchain")
load("//scalapb:default_dep_sets.bzl", "DEFAULT_SCALAPB_COMPILE_DEPS", "DEFAULT_SCALAPB_GRPC_DEPS")


toolchain_type(
name = "toolchain_type",
visibility = ["//visibility:public"],
)

scalapb_toolchain(
name = "default_toolchain_impl",
with_grpc = True,
with_flat_package=False,
with_single_line_to_string=False,
visibility = ["//visibility:public"],
)

toolchain(
name = "default_toolchain",
toolchain = ":default_toolchain_impl",
toolchain_type = "@io_bazel_rules_scala//scalapb:toolchain_type",
visibility = ["//visibility:public"],
)


scalapb_toolchain(
name = "enable_all_options_toolchain_impl",
with_grpc = True,
with_flat_package=True,
# with_java=True,
with_single_line_to_string=True,
visibility = ["//visibility:public"],
)

toolchain(
name = "enable_all_options_toolchain",
toolchain = ":enable_all_options_toolchain_impl",
toolchain_type = "@io_bazel_rules_scala//scalapb:toolchain_type",
visibility = ["//visibility:public"],
)


java_library(
name="default_scalapb_compile_dependencies",
exports = DEFAULT_SCALAPB_COMPILE_DEPS,
visibility = ["//visibility:public"],
)

java_library(
name="default_scalapb_grpc_dependencies",
exports = DEFAULT_SCALAPB_GRPC_DEPS,
visibility = ["//visibility:public"],
)
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ load(
"write_manifest_file",
)
load("//scala/private:rule_impls.bzl", "compile_scala")
load("//scala_proto/private:proto_to_scala_src.bzl", "proto_to_scala_src")
load("//scalapb/private:proto_to_scala_src.bzl", "proto_to_scala_src")

ScalaPBAspectInfo = provider(fields = [
"proto_info",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ load(
_scala_maven_import_external = "scala_maven_import_external",
)

def scala_proto_default_repositories(
def scalapb_default_repositories(
scala_version = _default_scala_version(),
maven_servers = ["http://central.maven.org/maven2"]):
major_version = _extract_major_version(scala_version)
Expand Down
Loading