-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter-cargo
executable file
·51 lines (41 loc) · 1.68 KB
/
filter-cargo
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
#!/usr/bin/env python3
import json
import subprocess
def filterCargo(args, binaryArgs):
cmd = f"cargo {args.cargoCommand} --message-format=json-diagnostic-rendered-ansi"
if binaryArgs != "":
cmd += f" -- {binaryArgs}"
cmd += " | filter-rustc"
if args.debug: cmd += " --debug"
if args.disable: cmd += " --disable"
if args.useJq:
cmd += " | jq --raw-output 'select(.reason==\"compiler-message\") | .message.rendered'"
out = subprocess.check_output(cmd, shell=True).decode('utf-8').strip()
# Print the "rendered" strings that will show up in color in terminal.
if args.useJq:
print(out)
else:
for line in out.splitlines():
obj = json.loads(line)
if obj["reason"] != "compiler-message": continue
print(obj["message"]["rendered"])
if __name__ == "__main__":
import argparse
p = argparse.ArgumentParser(__doc__)
p.add_argument("cargoCommand", help="Eg 'run' to wrap 'cargo run'.")
p.add_argument("--useJq", action="store_true", help="Process the final output using `jq` as in the README.")
# These will be passed to filter-rustc, see its help.
p.add_argument("--debug", action="store_true")
p.add_argument("--disable", action="store_true")
args, argparseExtra = p.parse_known_args()
binaryArgs = ""
if args.cargoCommand == "run":
# If " -- " follows known arguments, then `argparseExtra` will not contain that token.
# If all the arguments are correct, you can even omit the " -- " when calling this script.
if "--" in argparseExtra:
raise Exception(f"Unknown argument {argparseExtra[0]}")
binaryArgsStarted = False
for s in argparseExtra:
if s != "": s += " "
binaryArgs += s
filterCargo(args, binaryArgs)