Skip to content

Commit

Permalink
Use gazelle to generate bzl_library targets
Browse files Browse the repository at this point in the history
Hooked into the build as `//:gazelle`
  • Loading branch information
shs96c committed Dec 17, 2021
1 parent 661d1ee commit 06c1f60
Show file tree
Hide file tree
Showing 13 changed files with 239 additions and 47 deletions.
42 changes: 32 additions & 10 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@bazel_gazelle//:def.bzl", "gazelle", "gazelle_binary")
load("//java/private:pmd_ruleset.bzl", "pmd_ruleset")

bzl_library(
name = "doc-files",
srcs = [
"//java:doc-files",
],
visibility = [
"//visibility:public",
],
)

pmd_ruleset(
name = "pmd-config",
rulesets = [
Expand All @@ -20,3 +11,34 @@ pmd_ruleset(
"//visibility:public",
],
)

gazelle_binary(
name = "gazelle_bin",
languages = ["@bazel_skylib//gazelle/bzl"],
)

gazelle(
name = "gazelle",
gazelle = ":gazelle_bin",
)

bzl_library(
name = "repositories",
srcs = ["repositories.bzl"],
visibility = ["//visibility:public"],
deps = [
"//java/private:zip_repository",
"@bazel_tools//tools/build_defs/repo:http.bzl",
"@bazel_tools//tools/build_defs/repo:utils.bzl",
],
)

bzl_library(
name = "setup",
srcs = ["setup.bzl"],
visibility = ["//visibility:public"],
deps = [
"@apple_rules_lint//lint:implementation",
"@contrib_rules_jvm_deps//:defs",
],
)
40 changes: 35 additions & 5 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ lint_deps()
load("@apple_rules_lint//lint:setup.bzl", "lint_setup")

lint_setup({
"java-checkstyle": "//java:checkstyle-default-config",
"java-pmd": "//:pmd-config",
"java-spotbugs": "//java:spotbugs-default-config",
"java-checkstyle": "//java:checkstyle-default-config",
"java-pmd": "//:pmd-config",
"java-spotbugs": "//java:spotbugs-default-config",
})

load("//:repositories.bzl", "contrib_rules_jvm_deps")
Expand Down Expand Up @@ -70,8 +70,8 @@ maven_install(
"org.slf4j:slf4j-api:1.7.32",
"org.slf4j:slf4j-jdk14:1.7.32",
],
fetch_sources = True,
fail_if_repin_required = True,
fetch_sources = True,
maven_install_json = "@contrib_rules_jvm//:frozen_deps_install.json",
repositories = [
"https://repo1.maven.org/maven2",
Expand All @@ -93,8 +93,8 @@ maven_install(
"org.junit.platform:junit-platform-reporting:1.8.2",
"org.junit.vintage:junit-vintage-engine:5.8.2",
],
fetch_sources = True,
fail_if_repin_required = True,
fetch_sources = True,
maven_install_json = "@//:maven_install.json",
repositories = [
"https://repo1.maven.org/maven2",
Expand All @@ -105,6 +105,25 @@ load("@maven//:defs.bzl", "pinned_maven_install")

pinned_maven_install()

# Dependencies for generating `bzl_library` targets using Gazelle
http_archive(
name = "io_bazel_rules_go",
sha256 = "2b1641428dff9018f9e85c0384f03ec6c10660d935b750e3fa1492a281a53b0f",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.29.0/rules_go-v0.29.0.zip",
"https://github.com/bazelbuild/rules_go/releases/download/v0.29.0/rules_go-v0.29.0.zip",
],
)

http_archive(
name = "bazel_gazelle",
sha256 = "de69a09dc70417580aabf20a28619bb3ef60d038470c7cf8442fafcf627c21cb",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.24.0/bazel-gazelle-v0.24.0.tar.gz",
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.24.0/bazel-gazelle-v0.24.0.tar.gz",
],
)

http_archive(
name = "io_bazel_stardoc",
sha256 = "c9794dcc8026a30ff67cf7cf91ebe245ca294b20b071845d12c192afe243ad72",
Expand All @@ -117,3 +136,14 @@ http_archive(
load("@io_bazel_stardoc//:setup.bzl", "stardoc_repositories")

stardoc_repositories()

############################################
# Gazelle, for generating bzl_library targets
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")

go_rules_dependencies()

go_register_toolchains(version = "1.17.2")

gazelle_dependencies()
26 changes: 24 additions & 2 deletions bin/freeze-deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
import re
import subprocess
import sys
import textwrap
import zipfile
from os import path

UNCHANGED_FILES = ["outdated.sh", "outdated.artifacts", "outdated.repositories"]

parser = argparse.ArgumentParser(
description="""Convert rules_jvm_external lock files to frozen zip files that can be
imported in your own builds.""",
Expand Down Expand Up @@ -41,8 +44,7 @@
# Generate a stable-ish zip file
output = zipfile.ZipFile(args.zip, "w", zipfile.ZIP_DEFLATED)

unchanged_files = ["BUILD", "outdated.sh", "outdated.artifacts", "outdated.repositories"]
for f in unchanged_files:
for f in UNCHANGED_FILES:
p = path.join(base, "external", args.repo, f)
zinfo = zipfile.ZipInfo(filename=f, date_time=(1980, 1, 1, 0, 0, 0))
with open(p) as input:
Expand All @@ -59,4 +61,24 @@
defs.append(line)
output.writestr(zinfo, "\n".join(defs))

build_file = path.join(base, "external", args.repo, "BUILD")
zinfo = zipfile.ZipInfo(filename='BUILD.bazel', date_time=(1980, 1, 1, 0, 0, 0))
with open(build_file) as f:
build_file_contents = textwrap.dedent(
"""\
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
{original_contents}
bzl_library(
name = "defs",
srcs = ["defs.bzl"],
deps = [
"@rules_jvm_external//:implementation",
],
)
""".format(original_contents = textwrap.indent(f.read(), " "))
)
output.writestr(zinfo, build_file_contents)

output.close()
14 changes: 12 additions & 2 deletions docs/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@io_bazel_stardoc//stardoc:stardoc.bzl", "stardoc")

stardoc(
name = "java-docs",
input = "stardoc-input.bzl",
out = "java-docs.md",
deps = ["//:doc-files"],
input = "stardoc-input.bzl",
deps = [
"//java:defs",
],
)

genrule(
Expand All @@ -17,3 +20,10 @@ genrule(
outs = ["README.md"],
cmd = """cat $(location preamble.md) $(location :java-docs) $(location postfix.md) >$@""",
)

bzl_library(
name = "stardoc-input",
srcs = ["stardoc-input.bzl"],
visibility = ["//visibility:public"],
deps = ["//java:defs"],
)
27 changes: 16 additions & 11 deletions java/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,6 @@ load("//java/private:spotbugs_config.bzl", "spotbugs_config")

package(default_visibility = ["//visibility:public"])

bzl_library(
name = "doc-files",
srcs = [
"defs.bzl",
],
deps = [
"//java/private:doc-files",
],
visibility = ["//:__pkg__"],
)

alias(
name = "junit5-runner",
actual = "//java/src/com/github/bazel_contrib/contrib_rules_jvm/junit5",
Expand Down Expand Up @@ -68,3 +57,19 @@ java_binary(
artifact("org.slf4j:slf4j-jdk14"),
],
)

bzl_library(
name = "defs",
srcs = ["defs.bzl"],
deps = [
"//java/private:checkstyle",
"//java/private:checkstyle_config",
"//java/private:java_test_suite",
"//java/private:junit5",
"//java/private:library",
"//java/private:pmd",
"//java/private:pmd_ruleset",
"//java/private:spotbugs",
"//java/private:spotbugs_config",
],
)
98 changes: 93 additions & 5 deletions java/private/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,13 +1,101 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")

bzl_library(
name = "doc-files",
srcs = glob(["*.bzl"]),
name = "artifact",
srcs = ["artifact.bzl"],
visibility = ["//java:__subpackages__"],
deps = ["@rules_jvm_external//:implementation"],
)

bzl_library(
name = "checkstyle",
srcs = ["checkstyle.bzl"],
visibility = ["//java:__subpackages__"],
deps = [":checkstyle_config"],
)

bzl_library(
name = "checkstyle_config",
srcs = ["checkstyle_config.bzl"],
visibility = ["//java:__subpackages__"],
)

bzl_library(
name = "create_jvm_test_suite",
srcs = ["create_jvm_test_suite.bzl"],
visibility = ["//java:__subpackages__"],
deps = [":package"],
)

bzl_library(
name = "java_test_suite",
srcs = ["java_test_suite.bzl"],
visibility = ["//java:__subpackages__"],
deps = [
"@apple_rules_lint//lint:implementation",
":create_jvm_test_suite",
":library",
],
)

bzl_library(
name = "junit5",
srcs = ["junit5.bzl"],
visibility = ["//java:__subpackages__"],
deps = [
":library",
":package",
"@rules_jvm_external//:implementation",
],
visibility = [
"//java:__pkg__",
)

bzl_library(
name = "library",
srcs = ["library.bzl"],
visibility = ["//java:__subpackages__"],
deps = [
":checkstyle",
":pmd",
":spotbugs",
"@apple_rules_lint//lint:implementation",
"@rules_jvm_external//:implementation",
],
)

bzl_library(
name = "package",
srcs = ["package.bzl"],
visibility = ["//java:__subpackages__"],
)

bzl_library(
name = "pmd",
srcs = ["pmd.bzl"],
visibility = ["//java:__subpackages__"],
deps = [":pmd_ruleset"],
)

bzl_library(
name = "pmd_ruleset",
srcs = ["pmd_ruleset.bzl"],
visibility = ["//java:__subpackages__"],
)

bzl_library(
name = "spotbugs",
srcs = ["spotbugs.bzl"],
visibility = ["//java:__subpackages__"],
deps = [":spotbugs_config"],
)

bzl_library(
name = "spotbugs_config",
srcs = ["spotbugs_config.bzl"],
visibility = ["//java:__subpackages__"],
)

bzl_library(
name = "zip_repository",
srcs = ["zip_repository.bzl"],
visibility = ["//:__subpackages__"],
deps = ["@bazel_tools//tools/build_defs/repo:utils.bzl"],
)
2 changes: 1 addition & 1 deletion java/private/checkstyle.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,5 @@ checkstyle_test = rule(
},
executable = True,
test = True,
doc = """Use checkstyle to lint the `srcs`."""
doc = """Use checkstyle to lint the `srcs`.""",
)
Binary file modified java/private/contrib_rules_jvm_deps.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion java/private/pmd.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,5 @@ pmd_test = rule(
},
executable = True,
test = True,
doc = """Use PMD to lint the `srcs`."""
doc = """Use PMD to lint the `srcs`.""",
)
2 changes: 1 addition & 1 deletion java/private/spotbugs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,5 @@ spotbugs_test = rule(
},
executable = True,
test = True,
doc = """Use spotbugs to lint the `srcs`."""
doc = """Use spotbugs to lint the `srcs`.""",
)
13 changes: 10 additions & 3 deletions java/private/zip_repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,17 @@ zip_repository = repository_rule(
doc = """Create a repository from a saved zip file.""",
attrs = {
"path": attr.label(
doc = "Path to the zip file to use.",
mandatory = True,
),
"strip_prefix": attr.string(),
"patches": attr.label_list(),
"patch_args": attr.string_list(),
"strip_prefix": attr.string(
doc = "Prefix to remove from zip.",
),
"patches": attr.label_list(
doc = "A list of patches to be applied to the unpacked zip contents.",
),
"patch_args": attr.string_list(
doc = "Arguments to pass to the `patch` command.",
),
},
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ load("//java:defs.bzl", "JUNIT5_VINTAGE_DEPS", "java_test_suite")
java_test_suite(
name = "small-tests",
size = "small",
runner = "junit5",
srcs = glob(["*.java"]),
runner = "junit5",
deps = [
"//java/src/com/github/bazel_contrib/contrib_rules_jvm/junit5",
artifact("org.junit.jupiter:junit-jupiter-api"),
Expand Down
Loading

0 comments on commit 06c1f60

Please sign in to comment.