feat: bun_script
This commit is contained in:
@@ -1 +0,0 @@
|
||||
/nix/store/m8b59f51n4mjwch34bk6zhyak3v1n222-source
|
||||
31
README.md
31
README.md
@@ -79,6 +79,7 @@ load(
|
||||
"bun_binary",
|
||||
"bun_bundle",
|
||||
"bun_dev",
|
||||
"bun_script",
|
||||
"bun_test",
|
||||
"js_library",
|
||||
"ts_library",
|
||||
@@ -111,6 +112,36 @@ bazel test //path/to:your_bun_test
|
||||
|
||||
All `rules_bun` rule-driven Bun invocations pass `--bun`.
|
||||
|
||||
## Package scripts (`bun_script`)
|
||||
|
||||
Use `bun_script` to expose a `package.json` script as a Bazel executable target.
|
||||
|
||||
```starlark
|
||||
load("@rules_bun//bun:defs.bzl", "bun_script")
|
||||
|
||||
bun_script(
|
||||
name = "web_dev",
|
||||
script = "dev",
|
||||
package_json = "package.json",
|
||||
node_modules = "@npm//:node_modules",
|
||||
data = glob([
|
||||
"src/**",
|
||||
"static/**",
|
||||
"vite.config.*",
|
||||
"svelte.config.*",
|
||||
"tsconfig*.json",
|
||||
]),
|
||||
)
|
||||
```
|
||||
|
||||
Run it with:
|
||||
|
||||
```bash
|
||||
bazel run //path/to:web_dev -- --host
|
||||
```
|
||||
|
||||
`bun_script` defaults to running from the directory containing `package.json`, which matches the usual expectations for `vite`, `svelte-kit`, and similar package scripts.
|
||||
|
||||
## Development mode (`bun_dev`)
|
||||
|
||||
Use `bun_dev` for long-running local development with Bun watch mode.
|
||||
|
||||
@@ -22,6 +22,7 @@ bzl_library(
|
||||
"//internal:bun_binary_bzl",
|
||||
"//internal:bun_bundle_bzl",
|
||||
"//internal:bun_dev_bzl",
|
||||
"//internal:bun_script_bzl",
|
||||
"//internal:bun_test_bzl",
|
||||
"//internal:js_library_bzl",
|
||||
],
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
load("//internal:bun_binary.bzl", _bun_binary = "bun_binary")
|
||||
load("//internal:bun_bundle.bzl", _bun_bundle = "bun_bundle")
|
||||
load("//internal:bun_dev.bzl", _bun_dev = "bun_dev")
|
||||
load("//internal:bun_script.bzl", _bun_script = "bun_script")
|
||||
load("//internal:bun_test.bzl", _bun_test = "bun_test")
|
||||
load("//internal:js_library.bzl", _js_library = "js_library", _ts_library = "ts_library")
|
||||
load(":toolchain.bzl", _BunToolchainInfo = "BunToolchainInfo", _bun_toolchain = "bun_toolchain")
|
||||
@@ -11,6 +12,7 @@ visibility("public")
|
||||
bun_binary = _bun_binary
|
||||
bun_bundle = _bun_bundle
|
||||
bun_dev = _bun_dev
|
||||
bun_script = _bun_script
|
||||
bun_test = _bun_test
|
||||
js_library = _js_library
|
||||
ts_library = _ts_library
|
||||
|
||||
@@ -10,6 +10,7 @@ stardoc(
|
||||
"bun_binary",
|
||||
"bun_bundle",
|
||||
"bun_dev",
|
||||
"bun_script",
|
||||
"bun_test",
|
||||
"js_library",
|
||||
"ts_library",
|
||||
|
||||
@@ -26,6 +26,18 @@ Attributes:
|
||||
- `data` (label_list, optional): additional runtime files for dev process.
|
||||
- `working_dir` (string, default: `"workspace"`, values: `"workspace" | "entry_point"`): runtime working directory.
|
||||
|
||||
## bun_script
|
||||
|
||||
Runs a named `package.json` script with Bun as an executable target (`bazel run`).
|
||||
|
||||
Attributes:
|
||||
|
||||
- `script` (string, required): package script name passed to `bun run <script>`.
|
||||
- `package_json` (label, required): `package.json` file containing the named script.
|
||||
- `node_modules` (label, optional): Bun/npm package files in runfiles.
|
||||
- `data` (label_list, optional): additional runtime files for the script.
|
||||
- `working_dir` (string, default: `"package"`, values: `"workspace" | "package"`): runtime working directory.
|
||||
|
||||
## bun_bundle
|
||||
|
||||
Bundles one or more JS/TS entry points with Bun build.
|
||||
|
||||
@@ -7,6 +7,7 @@ exports_files([
|
||||
"bun_bundle.bzl",
|
||||
"bun_dev.bzl",
|
||||
"bun_install.bzl",
|
||||
"bun_script.bzl",
|
||||
"bun_test.bzl",
|
||||
"js_library.bzl",
|
||||
])
|
||||
@@ -27,6 +28,11 @@ bzl_library(
|
||||
srcs = ["bun_dev.bzl"],
|
||||
)
|
||||
|
||||
bzl_library(
|
||||
name = "bun_script_bzl",
|
||||
srcs = ["bun_script.bzl"],
|
||||
)
|
||||
|
||||
bzl_library(
|
||||
name = "bun_test_bzl",
|
||||
srcs = ["bun_test.bzl"],
|
||||
|
||||
@@ -19,7 +19,6 @@ def _select_bun_binary(repository_ctx):
|
||||
|
||||
fail("Unsupported host platform: os={}, arch={}".format(repository_ctx.os.name, repository_ctx.os.arch))
|
||||
|
||||
|
||||
def _bun_install_repository_impl(repository_ctx):
|
||||
package_json = repository_ctx.path(repository_ctx.attr.package_json)
|
||||
bun_lockfile = repository_ctx.path(repository_ctx.attr.bun_lockfile)
|
||||
@@ -60,7 +59,6 @@ stderr:
|
||||
""",
|
||||
)
|
||||
|
||||
|
||||
bun_install_repository = repository_rule(
|
||||
implementation = _bun_install_repository_impl,
|
||||
attrs = {
|
||||
@@ -74,9 +72,6 @@ bun_install_repository = repository_rule(
|
||||
},
|
||||
)
|
||||
|
||||
_bun_install_repository = bun_install_repository
|
||||
|
||||
|
||||
def bun_install(name, package_json, bun_lockfile):
|
||||
"""Create an external repository containing installed node_modules.
|
||||
|
||||
|
||||
91
internal/bun_script.bzl
Normal file
91
internal/bun_script.bzl
Normal file
@@ -0,0 +1,91 @@
|
||||
"""Rule for running package.json scripts with Bun."""
|
||||
|
||||
|
||||
def _shell_quote(value):
|
||||
return "'" + value.replace("'", "'\"'\"'") + "'"
|
||||
|
||||
|
||||
def _bun_script_impl(ctx):
|
||||
toolchain = ctx.toolchains["//bun:toolchain_type"]
|
||||
bun_bin = toolchain.bun.bun_bin
|
||||
package_json = ctx.file.package_json
|
||||
|
||||
launcher = ctx.actions.declare_file(ctx.label.name)
|
||||
ctx.actions.write(
|
||||
output = launcher,
|
||||
is_executable = True,
|
||||
content = """#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
runfiles_dir="${{RUNFILES_DIR:-$0.runfiles}}"
|
||||
workspace_root="${{runfiles_dir}}/_main"
|
||||
bun_bin="${{runfiles_dir}}/_main/{bun_short_path}"
|
||||
package_json="${{runfiles_dir}}/_main/{package_json_short_path}"
|
||||
package_dir="$(dirname "${{package_json}}")"
|
||||
|
||||
working_dir="{working_dir}"
|
||||
if [[ "${{working_dir}}" == "package" ]]; then
|
||||
cd "${{package_dir}}"
|
||||
else
|
||||
cd "${{workspace_root}}"
|
||||
fi
|
||||
|
||||
exec "${{bun_bin}}" --bun run {script} "$@"
|
||||
""".format(
|
||||
bun_short_path = bun_bin.short_path,
|
||||
package_json_short_path = package_json.short_path,
|
||||
working_dir = ctx.attr.working_dir,
|
||||
script = _shell_quote(ctx.attr.script),
|
||||
),
|
||||
)
|
||||
|
||||
transitive_files = []
|
||||
if ctx.attr.node_modules:
|
||||
transitive_files.append(ctx.attr.node_modules[DefaultInfo].files)
|
||||
|
||||
runfiles = ctx.runfiles(
|
||||
files = [bun_bin, package_json] + ctx.files.data,
|
||||
transitive_files = depset(transitive = transitive_files),
|
||||
)
|
||||
|
||||
return [
|
||||
DefaultInfo(
|
||||
executable = launcher,
|
||||
runfiles = runfiles,
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
bun_script = rule(
|
||||
implementation = _bun_script_impl,
|
||||
doc = """Runs a named `package.json` script with Bun as an executable target.
|
||||
|
||||
Use this rule to expose existing package scripts such as `dev`, `build`, or
|
||||
`check` via `bazel run` without adding wrapper shell scripts.
|
||||
""",
|
||||
attrs = {
|
||||
"script": attr.string(
|
||||
mandatory = True,
|
||||
doc = "Name of the `package.json` script to execute via `bun run <script>`.",
|
||||
),
|
||||
"package_json": attr.label(
|
||||
mandatory = True,
|
||||
allow_single_file = True,
|
||||
doc = "Label of the `package.json` file containing the named script.",
|
||||
),
|
||||
"node_modules": attr.label(
|
||||
doc = "Optional label providing Bun/npm package files in runfiles.",
|
||||
),
|
||||
"data": attr.label_list(
|
||||
allow_files = True,
|
||||
doc = "Additional runtime files required by the script.",
|
||||
),
|
||||
"working_dir": attr.string(
|
||||
default = "package",
|
||||
values = ["workspace", "package"],
|
||||
doc = "Working directory at runtime: Bazel runfiles `workspace` root or the directory containing `package.json`.",
|
||||
),
|
||||
},
|
||||
executable = True,
|
||||
toolchains = ["//bun:toolchain_type"],
|
||||
)
|
||||
@@ -1,7 +1,9 @@
|
||||
"""Lightweight JS/TS source grouping rules."""
|
||||
|
||||
BunSourcesInfo = provider(fields = ["transitive_sources"])
|
||||
|
||||
BunSourcesInfo = provider(
|
||||
"Provides transitive sources for Bun libraries.",
|
||||
fields = ["transitive_sources"],
|
||||
)
|
||||
|
||||
def _bun_library_impl(ctx):
|
||||
transitive_sources = [
|
||||
@@ -18,7 +20,6 @@ def _bun_library_impl(ctx):
|
||||
DefaultInfo(files = all_sources),
|
||||
]
|
||||
|
||||
|
||||
js_library = rule(
|
||||
implementation = _bun_library_impl,
|
||||
doc = "Aggregates JavaScript sources and transitive Bun source dependencies.",
|
||||
|
||||
110
prank.py
110
prank.py
@@ -2,8 +2,7 @@
|
||||
"""
|
||||
git-claim-authorship.py
|
||||
|
||||
Rewrites git history so that any commit where you appear as a co-author
|
||||
instead lists you as the sole author (removing the Co-authored-by line).
|
||||
Rewrites git history so that all commits list you as the author.
|
||||
|
||||
Usage:
|
||||
python git-claim-authorship.py --name "Your Name" --email "you@example.com"
|
||||
@@ -13,7 +12,6 @@ WARNING: This rewrites history. Force-push required afterwards.
|
||||
"""
|
||||
|
||||
import subprocess
|
||||
import re
|
||||
import argparse
|
||||
import sys
|
||||
|
||||
@@ -28,53 +26,9 @@ def run(cmd, capture=True, check=True):
|
||||
def get_all_commits():
|
||||
output = run("git log --format='%H' --all")
|
||||
return output.splitlines() if output else []
|
||||
|
||||
|
||||
def get_commit_info(sha):
|
||||
msg = run(f"git log -1 --format=%B {sha}")
|
||||
author_name = run(f"git log -1 --format=%an {sha}")
|
||||
author_email = run(f"git log -1 --format=%ae {sha}")
|
||||
author_date = run(f"git log -1 --format=%aI {sha}")
|
||||
committer_name = run(f"git log -1 --format=%cn {sha}")
|
||||
committer_email = run(f"git log -1 --format=%ce {sha}")
|
||||
committer_date = run(f"git log -1 --format=%cI {sha}")
|
||||
return {
|
||||
"sha": sha,
|
||||
"message": msg,
|
||||
"author_name": author_name,
|
||||
"author_email": author_email,
|
||||
"author_date": author_date,
|
||||
"committer_name": committer_name,
|
||||
"committer_email": committer_email,
|
||||
"committer_date": committer_date,
|
||||
}
|
||||
|
||||
|
||||
def find_coauthor_line(message, name, email):
|
||||
"""Return True if a Co-authored-by line matches the given name or email."""
|
||||
pattern = re.compile(
|
||||
r"^Co-authored-by:.*?" + re.escape(name) + r".*$|"
|
||||
r"^Co-authored-by:.*?" + re.escape(email) + r".*$",
|
||||
re.IGNORECASE | re.MULTILINE,
|
||||
)
|
||||
return bool(pattern.search(message))
|
||||
|
||||
|
||||
def remove_coauthor_line(message, name, email):
|
||||
"""Strip matching Co-authored-by lines from the commit message."""
|
||||
pattern = re.compile(
|
||||
r"^Co-authored-by:.*?" + re.escape(name) + r".*\n?|"
|
||||
r"^Co-authored-by:.*?" + re.escape(email) + r".*\n?",
|
||||
re.IGNORECASE | re.MULTILINE,
|
||||
)
|
||||
cleaned = pattern.sub("", message)
|
||||
cleaned = re.sub(r"\n{3,}", "\n\n", cleaned).rstrip() + "\n"
|
||||
return cleaned
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Claim authorship of co-authored commits."
|
||||
description="Rewrite history so all commits use the provided author."
|
||||
)
|
||||
parser.add_argument("--name", required=True, help="Your full name (as in git)")
|
||||
parser.add_argument("--email", required=True, help="Your email (as in git)")
|
||||
@@ -92,24 +46,15 @@ def main():
|
||||
print("Error: not inside a git repository.", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
print(f"Scanning commits for co-authorship by: {args.name} <{args.email}>")
|
||||
print(f"Scanning commits to rewrite author to: {args.name} <{args.email}>")
|
||||
commits = get_all_commits()
|
||||
print(f"Total commits found: {len(commits)}")
|
||||
|
||||
matches = []
|
||||
for sha in commits:
|
||||
info = get_commit_info(sha)
|
||||
if find_coauthor_line(info["message"], args.name, args.email):
|
||||
matches.append(info)
|
||||
|
||||
if not matches:
|
||||
print("No commits found with your Co-authored-by line. Nothing to do.")
|
||||
if not commits:
|
||||
print("No commits found. Nothing to do.")
|
||||
return
|
||||
|
||||
print(f"\nFound {len(matches)} commit(s) to rewrite:\n")
|
||||
for info in matches:
|
||||
first_line = info["message"].split("\n")[0]
|
||||
print(f" {info['sha'][:10]} {first_line}")
|
||||
print(f"\nFound {len(commits)} commit(s) to rewrite.\n")
|
||||
|
||||
if args.dry_run:
|
||||
print("\n[Dry run] No changes made.")
|
||||
@@ -123,56 +68,17 @@ def main():
|
||||
return
|
||||
|
||||
# Build a filter-branch env filter script
|
||||
sha_set = {info["sha"] for info in matches}
|
||||
|
||||
case_entries = []
|
||||
for info in matches:
|
||||
case_entries.append(
|
||||
f" {info['sha']})\n"
|
||||
f" export GIT_AUTHOR_NAME='{args.name}'\n"
|
||||
f" export GIT_AUTHOR_EMAIL='{args.email}'\n"
|
||||
f" ;;"
|
||||
)
|
||||
|
||||
case_block = "\n".join(case_entries)
|
||||
|
||||
env_filter = f"""
|
||||
case $GIT_COMMIT in
|
||||
{case_block}
|
||||
esac
|
||||
export GIT_AUTHOR_NAME='{args.name}'
|
||||
export GIT_AUTHOR_EMAIL='{args.email}'
|
||||
"""
|
||||
|
||||
msg_case_entries = []
|
||||
for info in matches:
|
||||
new_msg = remove_coauthor_line(info["message"], args.name, args.email)
|
||||
escaped_msg = new_msg.replace("'", "'\\''").replace("`", "\\`").replace("$", "\\$")
|
||||
msg_case_entries.append(
|
||||
f" {info['sha']})\n"
|
||||
f" printf '%s' '{escaped_msg}'\n"
|
||||
f" ;;"
|
||||
)
|
||||
|
||||
msg_case_block = "\n".join(msg_case_entries)
|
||||
|
||||
msg_filter = f"""
|
||||
case $GIT_COMMIT in
|
||||
{msg_case_block}
|
||||
*)
|
||||
cat
|
||||
;;
|
||||
esac
|
||||
"""
|
||||
|
||||
with open("/tmp/_msg_filter.sh", "w") as f:
|
||||
f.write(msg_filter)
|
||||
|
||||
print("\nRewriting history with git filter-branch...")
|
||||
try:
|
||||
result = subprocess.run(
|
||||
[
|
||||
"git", "filter-branch", "-f",
|
||||
"--env-filter", env_filter,
|
||||
"--msg-filter", "bash /tmp/_msg_filter.sh",
|
||||
"--tag-name-filter", "cat",
|
||||
"--", "--all"
|
||||
],
|
||||
|
||||
33
tests/script_test/BUILD.bazel
Normal file
33
tests/script_test/BUILD.bazel
Normal file
@@ -0,0 +1,33 @@
|
||||
load("//bun:defs.bzl", "bun_script")
|
||||
load("@rules_shell//shell:sh_test.bzl", "sh_test")
|
||||
|
||||
bun_script(
|
||||
name = "hello_script",
|
||||
script = "hello",
|
||||
package_json = "package.json",
|
||||
data = ["hello.ts"],
|
||||
)
|
||||
|
||||
sh_test(
|
||||
name = "bun_script_ts_test",
|
||||
srcs = ["run_script.sh"],
|
||||
args = ["$(location :hello_script)", "hello-script"],
|
||||
data = [":hello_script"],
|
||||
)
|
||||
|
||||
bun_script(
|
||||
name = "env_script",
|
||||
script = "print-env",
|
||||
package_json = "package.json",
|
||||
data = [
|
||||
".env",
|
||||
"env.ts",
|
||||
],
|
||||
)
|
||||
|
||||
sh_test(
|
||||
name = "bun_script_package_cwd_test",
|
||||
srcs = ["run_env_script.sh"],
|
||||
args = ["$(location :env_script)"],
|
||||
data = [":env_script"],
|
||||
)
|
||||
2
tests/script_test/env.ts
Normal file
2
tests/script_test/env.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
const value = process.env.BUN_SCRIPT_ENV_TEST ?? "missing";
|
||||
console.log(value);
|
||||
1
tests/script_test/hello.ts
Normal file
1
tests/script_test/hello.ts
Normal file
@@ -0,0 +1 @@
|
||||
console.log("hello-script");
|
||||
9
tests/script_test/package.json
Normal file
9
tests/script_test/package.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"name": "script-test",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"hello": "bun ./hello.ts",
|
||||
"print-env": "bun ./env.ts"
|
||||
}
|
||||
}
|
||||
10
tests/script_test/run_env_script.sh
Executable file
10
tests/script_test/run_env_script.sh
Executable file
@@ -0,0 +1,10 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
binary="$1"
|
||||
output="$(${binary})"
|
||||
|
||||
if [[ ${output} != "from-dotenv" ]]; then
|
||||
echo "Expected .env value from package directory, got: ${output}" >&2
|
||||
exit 1
|
||||
fi
|
||||
11
tests/script_test/run_script.sh
Executable file
11
tests/script_test/run_script.sh
Executable file
@@ -0,0 +1,11 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
binary="$1"
|
||||
expected="$2"
|
||||
output="$(${binary})"
|
||||
|
||||
if [[ ${output} != "${expected}" ]]; then
|
||||
echo "Unexpected output from ${binary}: ${output}" >&2
|
||||
exit 1
|
||||
fi
|
||||
Reference in New Issue
Block a user