M0ar
This commit is contained in:
parent
f7bce623bf
commit
edec17510e
2 changed files with 25 additions and 42 deletions
|
@ -1,3 +1,4 @@
|
|||
py_project(
|
||||
name = "bazelshim",
|
||||
main = "src/bazelshim/__main__.py",
|
||||
)
|
||||
|
|
|
@ -1,32 +1,5 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
"""
|
||||
A Bazel wrapper
|
||||
===============
|
||||
|
||||
This script exists to allow for the setting of environment viariables and other context flags to
|
||||
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. This allows for user(s) to define their own Bazel CLI
|
||||
middleware machinery while using structured and testable parsing rather than BASH nonsense.
|
||||
|
||||
It's important that this script stands "alone" because it needs to be usable mostly in isolation
|
||||
with a non-hermetic interpreter before we can do Bazel builds.
|
||||
|
||||
For instance this script could be used as a basis to define custom Bazel tasks such as 'repl' (run
|
||||
with some magic) or 'watch' as an alias for punting to ibazel/bazel-watcher.
|
||||
|
||||
Such a middleware can also be used to provide configurations which are hard to autodetect from
|
||||
within Bazel in an appropriately structured way such as
|
||||
`--action_env=GLIBC_VERSION=$(ldd --version)`
|
||||
or
|
||||
`--action_env=PLATFORM_FEATURE_POSTGRESQL=$(which pg_ctl >/dev/null 2>&1 && echo 'true' || echo 'false')`
|
||||
|
||||
I suggest using a `bazel` and maybe `bazelisk` shim script on your $PATH like
|
||||
exec "${SOURCE}/projects/bazelshim/src/bazelshim/__main__.py" \
|
||||
--bazelshim_exclude="$(dirname $(realpath $0))" \
|
||||
"$@"
|
||||
"""
|
||||
|
||||
# Since this can't run under Bazel, we have to set up the sys.path ourselves
|
||||
import os
|
||||
import sys
|
||||
|
@ -40,6 +13,7 @@ if (p := str(Path(sys.argv[0]).absolute().parent.parent)) not in sys.path:
|
|||
from dataclasses import dataclass
|
||||
from shlex import quote, split as shlex
|
||||
import sys
|
||||
import tomllib
|
||||
from pathlib import Path
|
||||
from typing import List, Optional
|
||||
from itertools import chain
|
||||
|
@ -61,6 +35,7 @@ VERBS = [
|
|||
"run",
|
||||
"sync",
|
||||
"test",
|
||||
"watch",
|
||||
]
|
||||
|
||||
|
||||
|
@ -169,6 +144,9 @@ class BazelCli:
|
|||
*self.startup_opts,
|
||||
]
|
||||
if self.command:
|
||||
if self.command == "watch":
|
||||
acc.extend(self.command_opts)
|
||||
else:
|
||||
acc.append(self.command)
|
||||
acc.extend(self.command_opts)
|
||||
|
||||
|
@ -190,6 +168,12 @@ class BazelCli:
|
|||
def executable(self, exclude: List[Path]):
|
||||
"""Try to resolve as via which() an executable to delegate to."""
|
||||
|
||||
if self.command == "watch":
|
||||
for p in chain(which("ibazel")):
|
||||
if p.parent not in exclude:
|
||||
return str(p)
|
||||
|
||||
else:
|
||||
for p in chain(which("bazelisk"), which("bazel")):
|
||||
if p.parent not in exclude:
|
||||
return str(p)
|
||||
|
@ -197,16 +181,14 @@ class BazelCli:
|
|||
def render_text(self, next):
|
||||
lines = []
|
||||
lines.append(" " + next)
|
||||
for arg in self.startup_opts:
|
||||
lines.append(" " + arg)
|
||||
if self.command:
|
||||
lines.append(" " + self.command)
|
||||
for arg in self.command_opts:
|
||||
lines.append(" " + arg)
|
||||
if self.subprocess_opts:
|
||||
lines.append(" --")
|
||||
for arg in self.subprocess_opts:
|
||||
lines.append(" " + arg)
|
||||
base_prefix = " "
|
||||
for arg in self.render_cli()[1:]:
|
||||
prefix = base_prefix
|
||||
if arg in VERBS or arg == self.command:
|
||||
prefix = " "
|
||||
elif arg == "--":
|
||||
base_prefix += " "
|
||||
lines.append(prefix + arg)
|
||||
return "\\\n".join(lines)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue