fix: tests
This commit is contained in:
@@ -3,6 +3,26 @@
|
||||
load("//internal:bun_command.bzl", "add_flag", "add_flag_value", "add_flag_values", "add_install_mode", "add_raw_flags")
|
||||
load("//internal:js_library.bzl", "collect_js_sources")
|
||||
|
||||
def infer_entry_point_root(entries):
|
||||
if not entries:
|
||||
return None
|
||||
|
||||
common_segments = entries[0].path.split("/")[:-1]
|
||||
for entry in entries[1:]:
|
||||
entry_segments = entry.path.split("/")[:-1]
|
||||
common_length = min(len(common_segments), len(entry_segments))
|
||||
idx = common_length
|
||||
for segment_idx in range(common_length):
|
||||
if common_segments[segment_idx] != entry_segments[segment_idx]:
|
||||
idx = segment_idx
|
||||
break
|
||||
common_segments = common_segments[:idx]
|
||||
|
||||
if not common_segments:
|
||||
return "."
|
||||
|
||||
return "/".join(common_segments)
|
||||
|
||||
def bun_build_transitive_inputs(ctx):
|
||||
transitive_inputs = []
|
||||
if getattr(ctx.attr, "node_modules", None):
|
||||
@@ -11,13 +31,17 @@ def bun_build_transitive_inputs(ctx):
|
||||
transitive_inputs.append(collect_js_sources(dep))
|
||||
return transitive_inputs
|
||||
|
||||
def add_bun_build_common_flags(args, attr, metafile = None, metafile_md = None):
|
||||
def add_bun_build_common_flags(args, attr, metafile = None, metafile_md = None, root = None):
|
||||
build_root = root
|
||||
if build_root == None:
|
||||
build_root = getattr(attr, "root", None)
|
||||
|
||||
add_install_mode(args, getattr(attr, "install_mode", "disable"))
|
||||
add_flag_value(args, "--target", getattr(attr, "target", None))
|
||||
add_flag_value(args, "--format", getattr(attr, "format", None))
|
||||
add_flag(args, "--production", getattr(attr, "production", False))
|
||||
add_flag(args, "--splitting", getattr(attr, "splitting", False))
|
||||
add_flag_value(args, "--root", getattr(attr, "root", None))
|
||||
add_flag_value(args, "--root", build_root)
|
||||
|
||||
sourcemap = getattr(attr, "sourcemap", None)
|
||||
if sourcemap == True:
|
||||
|
||||
@@ -1,18 +1,34 @@
|
||||
"""Rules for Bun build outputs and standalone executables."""
|
||||
|
||||
load("//internal:bun_build_support.bzl", "add_bun_build_common_flags", "add_bun_compile_flags", "bun_build_transitive_inputs")
|
||||
load("//internal:bun_build_support.bzl", "add_bun_build_common_flags", "add_bun_compile_flags", "bun_build_transitive_inputs", "infer_entry_point_root")
|
||||
|
||||
def _bun_build_impl(ctx):
|
||||
toolchain = ctx.toolchains["//bun:toolchain_type"]
|
||||
bun_bin = toolchain.bun.bun_bin
|
||||
output_dir = ctx.actions.declare_directory(ctx.label.name)
|
||||
metafile = ctx.actions.declare_file(ctx.label.name + ".meta.json") if ctx.attr.metafile else None
|
||||
metafile_md = ctx.actions.declare_file(ctx.label.name + ".meta.md") if ctx.attr.metafile_md else None
|
||||
metafile = None
|
||||
if ctx.attr.metafile:
|
||||
metafile = ctx.actions.declare_file(ctx.label.name + ".meta.json")
|
||||
metafile_md = None
|
||||
if ctx.attr.metafile_md:
|
||||
metafile_md = ctx.actions.declare_file(ctx.label.name + ".meta.md")
|
||||
build_root = ctx.attr.root
|
||||
if not build_root:
|
||||
build_root = infer_entry_point_root(ctx.files.entry_points)
|
||||
transitive_inputs = bun_build_transitive_inputs(ctx)
|
||||
build_inputs = depset(
|
||||
direct = ctx.files.entry_points + ctx.files.data,
|
||||
transitive = transitive_inputs,
|
||||
)
|
||||
input_manifest = ctx.actions.declare_file(ctx.label.name + ".inputs")
|
||||
runner = ctx.actions.declare_file(ctx.label.name + "_runner.sh")
|
||||
|
||||
args = ctx.actions.args()
|
||||
args.add(input_manifest.path)
|
||||
args.add(bun_bin.path)
|
||||
args.add("--bun")
|
||||
args.add("build")
|
||||
add_bun_build_common_flags(args, ctx.attr, metafile = metafile, metafile_md = metafile_md)
|
||||
add_bun_build_common_flags(args, ctx.attr, metafile = metafile, metafile_md = metafile_md, root = build_root)
|
||||
args.add("--outdir")
|
||||
args.add(output_dir.path)
|
||||
args.add_all(ctx.files.entry_points)
|
||||
@@ -23,12 +39,74 @@ def _bun_build_impl(ctx):
|
||||
if metafile_md:
|
||||
outputs.append(metafile_md)
|
||||
|
||||
ctx.actions.write(
|
||||
output = input_manifest,
|
||||
content = "".join([file.path + "\n" for file in build_inputs.to_list()]),
|
||||
)
|
||||
|
||||
ctx.actions.write(
|
||||
output = runner,
|
||||
is_executable = True,
|
||||
content = """#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
manifest="$1"
|
||||
execroot="$(pwd -P)"
|
||||
bun_bin="$2"
|
||||
if [[ "${bun_bin}" != /* ]]; then
|
||||
bun_bin="${execroot}/${bun_bin}"
|
||||
fi
|
||||
shift 2
|
||||
|
||||
stage_dir="$(mktemp -d "${TMPDIR:-/tmp}/rules_bun_build.XXXXXX")"
|
||||
cleanup() {
|
||||
rm -rf "${stage_dir}"
|
||||
}
|
||||
trap cleanup EXIT
|
||||
|
||||
while IFS= read -r relpath; do
|
||||
if [[ -z "${relpath}" ]]; then
|
||||
continue
|
||||
fi
|
||||
src="${execroot}/${relpath}"
|
||||
dest="${stage_dir}/${relpath}"
|
||||
mkdir -p "$(dirname "${dest}")"
|
||||
cp -L "${src}" "${dest}"
|
||||
done < "${manifest}"
|
||||
|
||||
forwarded_args=()
|
||||
while (($#)); do
|
||||
case "$1" in
|
||||
--outdir)
|
||||
forwarded_args+=("$1" "${execroot}/$2")
|
||||
shift 2
|
||||
;;
|
||||
--metafile=*)
|
||||
forwarded_args+=("--metafile=${execroot}/${1#--metafile=}")
|
||||
shift
|
||||
;;
|
||||
--metafile-md=*)
|
||||
forwarded_args+=("--metafile-md=${execroot}/${1#--metafile-md=}")
|
||||
shift
|
||||
;;
|
||||
*)
|
||||
forwarded_args+=("$1")
|
||||
shift
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
cd "${stage_dir}"
|
||||
exec "${bun_bin}" "${forwarded_args[@]}"
|
||||
""",
|
||||
)
|
||||
|
||||
ctx.actions.run(
|
||||
executable = bun_bin,
|
||||
executable = runner,
|
||||
arguments = [args],
|
||||
inputs = depset(
|
||||
direct = ctx.files.entry_points + ctx.files.data,
|
||||
transitive = bun_build_transitive_inputs(ctx),
|
||||
direct = [input_manifest, bun_bin],
|
||||
transitive = [build_inputs],
|
||||
),
|
||||
outputs = outputs,
|
||||
mnemonic = "BunBuild",
|
||||
|
||||
Reference in New Issue
Block a user