Skip to content

Commit

Permalink
chore: rewrite against picocli
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Gammon <[email protected]>
  • Loading branch information
sgammon committed Sep 3, 2023
1 parent 4969982 commit ebbad5b
Show file tree
Hide file tree
Showing 7 changed files with 258 additions and 115 deletions.
5 changes: 5 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ exports_files([
"pnpm-lock.yaml",
])

alias(
name = "wrapper",
actual = "//java",
)

# gazelle:prefix github.com/sgammon/rules_gradle

npm_link_all_packages(name = "node_modules")
Expand Down
13 changes: 13 additions & 0 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ http_archive(
],
)

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

http_archive(
name = "rules_kotlin",
sha256 = RULES_KOTLIN_SHA,
Expand Down Expand Up @@ -227,6 +236,10 @@ switched_rules_by_language(
name = "com_google_googleapis_imports",
)

load("@gazelle//:deps.bzl", "gazelle_dependencies")

gazelle_dependencies()

# - JavaScript

load("@aspect_rules_js//js:repositories.bzl", "rules_js_dependencies")
Expand Down
53 changes: 38 additions & 15 deletions internal/task.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@ GRADLE_WRAPPER = "@rules_gradle//gradle/wrapper"
GRADLE_TOOL_RUNNER = "@rules_gradle//java:runner"
JAVA_TOOLCHAIN_TYPE = "@bazel_tools//tools/jdk:runtime_toolchain_type"

_WrapperArgument = struct(
PROJECT = "gradle_project",
DISTRIBUTION = "gradle_distribution",
BUILDFILE = "gradle_build_file",
JAVA_HOME = "java_home",
LOGFILE = "logfile",
OUTPUT = "output",
)

def _arg(args, argument, value = None):
val = argument
if value != None:
val = "--%s=%s" % (argument, value)
args.add(val)

def _gradle_task_impl(ctx):
"""Run a Gradle task and capture a build output."""

Expand All @@ -19,7 +34,8 @@ def _gradle_task_impl(ctx):
transitive_inputs = []

# resolve java toolchains
transitive_inputs.append(ctx.toolchains[JAVA_TOOLCHAIN_TYPE].java_runtime.files)
java = ctx.toolchains[JAVA_TOOLCHAIN_TYPE].java_runtime
transitive_inputs.append(java.files)

# resolve project and wrapper inputs
project = ctx.attr.project.files.to_list()
Expand All @@ -40,41 +56,48 @@ def _gradle_task_impl(ctx):
env = {}
outputs = []
args = ctx.actions.args()
args.add(project_root)

# prepare required arguments first
_arg(args, _WrapperArgument.PROJECT, project_root)
_arg(args, _WrapperArgument.BUILDFILE, ctx.file.build_file.path)
_arg(args, _WrapperArgument.LOGFILE, ctx.outputs.output_log.path)
_arg(args, _WrapperArgument.DISTRIBUTION, ctx.attr.distribution.files.to_list()[0].path)
_arg(args, _WrapperArgument.JAVA_HOME, java.java_home)

# gradle outputs to the `build` directory within the project root
outputs.append(ctx.actions.declare_directory(
"build",
))
marker = ctx.actions.declare_file(
"bazel-gradle-build.args",
)
ctx.actions.write(
marker,
args,
is_executable = False,
)

args.add(ctx.file.build_file.path)
args.add(ctx.outputs.output_log.path)
args.add(ctx.attr.distribution.files.to_list()[0].path)

if ctx.attr.outputs != None and len(ctx.attr.outputs) > 0:
for out in ctx.attr.outputs:
if not out.startswith("build/"):
out = "build/%s" % out
outfile = ctx.actions.declare_file(out)
outputs.append(outfile)
args.add("--output=%s=%s" % (out, outfile.path))
_arg(args, _WrapperArgument.OUTPUT, "%s=%s" % (out, outfile.path))

# add each requisite task to the positional arguments list at the end
for i in tasklist:
args.add(i)

# write our arguments file, which we will pass to the wrapper
marker = ctx.actions.declare_file(
"bazel-gradle-build.args",
)
ctx.actions.write(
marker,
args,
is_executable = False,
)

# prepare list of inputs for gradle build execution
inputs = depset(
direct_inputs,
transitive = transitive_inputs,
)

# run gradle through the wrapper
ctx.actions.run(
inputs = inputs,
outputs = outputs + [ctx.outputs.output_log],
Expand Down
44 changes: 24 additions & 20 deletions java/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
load(
"@rules_kotlin//kotlin:jvm.bzl",
"kt_jvm_library",
java_binary = "kt_jvm_binary",
java_library = "kt_jvm_library",
)
load(
"@rules_java//java:defs.bzl",
"java_binary",
"java_library",
"java_plugin",
pure_java_library = "java_library",
)
load(
"@rules_graalvm//graalvm:defs.bzl",
Expand All @@ -28,51 +29,54 @@ DEPS = [
]

filegroup(
name = "srcs-java",
name = "srcs",
srcs = glob([
"build/bazel/**/*.java",
"build/bazel/**/*.kt",
]),
)

filegroup(
name = "srcs-kt",
srcs = glob([
"build/bazel/**/*.kt",
]),
java_plugin(
name = "picocli-processor",
generates_api = True,
processor_class = "picocli.codegen.aot.graalvm.processor.NativeImageConfigGeneratorProcessor",
deps = [maven("info.picocli:picocli-codegen")],
)

kt_jvm_library(
name = "lib-kt",
srcs = [":srcs-kt"],
deps = DEPS,
pure_java_library(
name = "picocli",
exported_plugins = [":picocli-processor"],
runtime_deps = [maven("info.picocli:picocli")],
)

java_library(
name = "lib-java",
srcs = [":srcs-java"],
deps = DEPS + [":lib-kt"],
name = "lib",
srcs = [":srcs"],
deps = DEPS + [
":picocli",
],
)

java_binary(
name = "runner",
main_class = "build.bazel.gradle.GradleRunner",
visibility = ["//visibility:public"],
runtime_deps = [":lib-java"],
runtime_deps = [":lib"],
)

java_binary(
name = "wrapper",
classpath_resources = [":logback.xml"],
main_class = "build.bazel.gradle.GradleW",
resources = [":logback.xml"],
visibility = ["//visibility:public"],
runtime_deps = [":lib-java"] + DEPS,
runtime_deps = [":lib"] + DEPS,
)

native_image(
name = "native",
main_class = "build.bazel.gradle.GradleRunner",
visibility = ["//visibility:public"],
deps = [":lib-java"],
deps = [":lib"],
)

alias(
Expand Down
Loading

0 comments on commit ebbad5b

Please sign in to comment.