forked from LucasPickering/slumber
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathx.py
More file actions
executable file
·113 lines (93 loc) · 2.9 KB
/
x.py
File metadata and controls
executable file
·113 lines (93 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python3
"""
Run the TUI with watchexec for development. This exists to make it easier to set some common
environment variables and flags for development. This will not enable the `cli` feature, so it
will compile a bit faster than the full binary.
"""
import os
import sys
import subprocess
import argparse
import itertools
WATCH_PATHS = ["Cargo.toml", "Cargo.lock", "src/", "crates/"]
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--log", "-l", default="DEBUG", help="Log level")
subparsers = parser.add_subparsers(help="Subcommand to execute")
# `cli` subcommand
cli_parser = subparsers.add_parser("cli", help=cli.__doc__)
cli_parser.add_argument(
"args", nargs=argparse.REMAINDER, help="Additional arguments to pass to the CLI"
)
cli_parser.set_defaults(func=cli)
# `tui` subcommand
tui_parser = subparsers.add_parser("tui", help=tui.__doc__)
tui_parser.set_defaults(func=tui)
tui_parser.add_argument(
"--tracing",
action="store_true",
help="Enable tokio tracing. Requires setting the tokio_unstable compiler flag,"
" so this will trigger a full recompilation",
)
tui_parser.add_argument(
"args", nargs=argparse.REMAINDER, help="Additional arguments to pass to the TUI"
)
args = vars(parser.parse_args())
# Defer to subcommand function
func = args.pop("func")
func(**args)
def cli(log: str, args: list[str]) -> None:
"""Run a CLI command"""
cargo_command = [
"cargo",
"run",
"--no-default-features",
"--features",
"cli",
"--",
# Forward args from the user
*args,
]
result = subprocess.run(cargo_command, env=env(log))
sys.exit(result.returncode)
def tui(log: str, tracing: bool, args: list[str]) -> None:
"""Run the TUI with watchexec"""
cargo_command = [
"cargo",
"run",
"--no-default-features",
"--features",
"tui",
*(["--features", "tokio_tracing"] if tracing else []),
"--",
# Forward args from the user
*args,
]
watchexec_command: list[str] = [
"watchexec",
"--restart",
"--no-process-group",
*(itertools.chain.from_iterable(["--watch", path] for path in WATCH_PATHS)),
"--",
*cargo_command,
]
try:
result = subprocess.run(
watchexec_command,
env={
"RUSTFLAGS": "--cfg=tokio_unstable" if tracing else "",
**env(log),
},
)
sys.exit(result.returncode)
except KeyboardInterrupt:
sys.exit(0)
def env(log: str) -> dict[str, str]:
"""Get the environment that cargo should be run with"""
return {
"RUST_LOG": f"slumber={log}",
"RUST_BACKTRACE": "1",
**os.environ,
}
if __name__ == "__main__":
main()