-
Notifications
You must be signed in to change notification settings - Fork 11.5k
Expand file tree
/
Copy pathresolve_template.py
More file actions
63 lines (54 loc) 路 1.65 KB
/
Copy pathresolve_template.py
File metadata and controls
63 lines (54 loc) 路 1.65 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
#!/usr/bin/env python3
"""Resolve composed template content from the project template stack."""
from __future__ import annotations
import argparse
import json
import sys
from pathlib import Path
try:
from common import (
TemplateResolutionError,
get_repo_root,
resolve_template_content,
)
except ImportError: # pragma: no cover - direct execution from unusual cwd
sys.path.insert(0, str(Path(__file__).resolve().parent))
from common import (
TemplateResolutionError,
get_repo_root,
resolve_template_content,
)
def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser()
parser.add_argument("template_name")
parser.add_argument("--json", action="store_true")
args = parser.parse_args(argv)
repo_root = get_repo_root(Path(__file__))
try:
content = resolve_template_content(args.template_name, repo_root)
except TemplateResolutionError as exc:
print(f"ERROR: {exc}", file=sys.stderr)
return 1
if content is None:
print(
f"ERROR: Could not resolve required {args.template_name} from the "
f"template override stack for {repo_root}",
file=sys.stderr,
)
return 1
if args.json:
print(
json.dumps(
{
"TEMPLATE_NAME": args.template_name,
"TEMPLATE_CONTENT": content,
},
ensure_ascii=False,
separators=(",", ":"),
)
)
else:
sys.stdout.write(content)
return 0
if __name__ == "__main__":
raise SystemExit(main())