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

Introduce support for usage with Bazel. #216

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Empty file added WORKSPACE
Empty file.
5 changes: 5 additions & 0 deletions scripts/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
exports_files(
["google-java-format-diff.py"],
visibility = ["//visibility:public"],
)

Empty file added tools/bazel/BUILD.bazel
Empty file.
16 changes: 16 additions & 0 deletions tools/bazel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

Install for usage in your repo:

http_archive(
name = "com_google_google_java_format_source",
urls = ["https://github.com/google/google-java-format/archive/google-java-format-1.6.zip"],
)

load("@com_google_google_java_format_source//tools/bazel:def.bzl", "java_format")

java_format(
name = "com_google_google_java_format",
jar_url = "https://github.com/google/google-java-format/releases/download/google-java-format-1.5/google-java-format-1.5-all-deps.jar",
jar_sha256 = "7b839bb7534a173f0ed0cd0e9a583181d20850fcec8cf6e3800e4420a1fad184",
diff = "@com_google_google_java_format_source//scripts:google-java-format-diff.py",
)
50 changes: 50 additions & 0 deletions tools/bazel/def.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

def _java_format_impl(repository_ctx):
repository_ctx.download(
url = repository_ctx.attr.jar_url,
output = "jar/google-java-format.jar",
sha256 = repository_ctx.attr.jar_sha256,
)

repository_ctx.file(
"jar/BUILD.bazel",
content = """
java_import(
name = "jar",
jars = [
"google-java-format.jar",
],
visibility = ["//visibility:public"],
)
""",
)

repository_ctx.file(
"BUILD.bazel",
content = """
java_binary(
name = "google-java-format",
main_class = "com.google.googlejavaformat.java.Main",
runtime_deps = [
"//jar",
],
)

py_binary(
name = "google-java-format-diff",
srcs = [
"{}",
],
)
""".format(repository_ctx.attr.diff),
)

java_format = repository_rule(
implementation = _java_format_impl,
attrs = {
"jar_url": attr.string(mandatory = True),
"jar_sha256": attr.string(),
"diff": attr.label(allow_single_file = [".py"], mandatory = True),
},
)