Finishing touches

This commit is contained in:
Reid 'arrdem' McKenzie 2024-02-06 20:55:24 -07:00
parent 77912e9b65
commit fec681d41b
2 changed files with 22 additions and 6 deletions

View file

@ -6,10 +6,18 @@
# Bazel on behalf of the user. Consequently it has some magical (partial) knowledge of Bazel's CLI # Bazel on behalf of the user. Consequently it has some magical (partial) knowledge of Bazel's CLI
# options since it's really a CLI shim. # options since it's really a CLI shim.
# Since this can't run under Bazel, we have to set up the sys.path ourselves
import os
import sys
from pathlib import Path
if (p := str(Path(sys.argv[0]).absolute().parent.parent)) not in sys.path:
sys.path.pop(0) # Remove '.' / ''
sys.path.insert(0, p) # Insert the bazelshim root
from dataclasses import dataclass from dataclasses import dataclass
from shlex import quote, split as shlex from shlex import quote, split as shlex
import sys import sys
import os
from pathlib import Path from pathlib import Path
from typing import List, Optional from typing import List, Optional
from itertools import chain from itertools import chain
@ -65,6 +73,10 @@ def normalize_opts(args: List[str]) -> List[str]:
# Convert --no<foo> args to --<foo>=no # Convert --no<foo> args to --<foo>=no
acc.append("--" + args.pop(0).lstrip("--no") + "=false") acc.append("--" + args.pop(0).lstrip("--no") + "=false")
elif args[0] == "--isatty=0":
acc.append("--isatty=false")
args.pop(0)
elif ( elif (
args[0].startswith("--") args[0].startswith("--")
and not args[1].startswith("--") and not args[1].startswith("--")
@ -142,9 +154,11 @@ class BazelCli:
elif self.command == "run": elif self.command == "run":
acc.append("--") acc.append("--")
acc.extend(self.subprocess_opts) acc.extend(self.subprocess_opts)
elif self.command and not self.subprocess_opts:
pass
else: else:
print( print(
f"Warning: {self.command} does not support -- args!", f"Warning: {self.command} does not support -- args! {self.subprocess_opts!r}",
file=sys.stderr, file=sys.stderr,
) )
@ -178,6 +192,7 @@ def middleware(cli):
if __name__ == "__main__": if __name__ == "__main__":
# This script has a magical flag to help with resolving bazel
exclude = [] exclude = []
while len(sys.argv) > 1 and sys.argv[1].startswith("--bazelshim_exclude"): while len(sys.argv) > 1 and sys.argv[1].startswith("--bazelshim_exclude"):
exclude.append(Path(sys.argv.pop(1).split("=")[1]).absolute()) exclude.append(Path(sys.argv.pop(1).split("=")[1]).absolute())
@ -188,8 +203,9 @@ if __name__ == "__main__":
cli = BazelCli.parse_cli(["bazel"] + sys.argv[1:]) cli = BazelCli.parse_cli(["bazel"] + sys.argv[1:])
cli = middleware(cli) cli = middleware(cli)
next = cli.executable(exclude=exclude) next = cli.executable(exclude=exclude)
print( if sys.stderr.isatty() and not "--isatty=false" in cli.command_opts:
"Info: Executing\n" + cli.render_text(next), print(
file=sys.stderr, "\u001b[33mInfo\u001b[0m: Executing\n" + cli.render_text(next),
) file=sys.stderr,
)
os.execv(next, cli.render_cli()) os.execv(next, cli.render_cli())