packages feed

cabal-buck2-0.6.0.0: buck2/prelude/java/jar_genrule.bzl

# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is dual-licensed under either the MIT license found in the
# LICENSE-MIT file in the root directory of this source tree or the Apache
# License, Version 2.0 found in the LICENSE-APACHE file in the root directory
# of this source tree. You may select, at your option, one of the
# above-listed licenses.

load("@prelude//:genrule.bzl", "process_genrule")
load("@prelude//java:java_toolchain.bzl", "JavaToolchainInfo")
load("@prelude//utils:expect.bzl", "expect")

def jar_genrule_impl(ctx: AnalysisContext) -> list[Provider]:
    output_name = "{}.jar".format(ctx.label.name)
    providers = process_genrule(ctx, output_name, None)
    expect(
        len(providers) == 1,
        "expected exactly one provider of type DefaultInfo from {} ({})".format(ctx.label.name, providers),
    )

    default_info = providers[0]  # DefaultInfo type
    outputs = default_info.default_outputs
    expect(
        len(outputs) == 1,
        "expected exactly one output from {} ({})".format(ctx.label.name, outputs),
    )
    output_jar = outputs[0]

    java_toolchain = ctx.attrs._java_toolchain[JavaToolchainInfo]
    java_cmd = cmd_args(java_toolchain.java[RunInfo])
    java_cmd.add("-jar", output_jar)

    providers.append(RunInfo(args = java_cmd))
    return providers