-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.py
More file actions
76 lines (63 loc) · 2.12 KB
/
cli.py
File metadata and controls
76 lines (63 loc) · 2.12 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
from __future__ import annotations
import argparse
import shutil
import sys
from pathlib import Path
from .report import render_report
from .scanner import scan_project
def build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
prog="codescan",
description="Find probably unused image assets in modern Xcode projects.",
)
parser.add_argument("project", help="Path to an Xcode project directory.")
parser.add_argument(
"--format",
choices=("text", "json", "markdown"),
default="text",
help="Report format. Defaults to text.",
)
parser.add_argument("--output", "-o", help="Write report to a file instead of stdout.")
parser.add_argument(
"--ignore",
action="append",
default=[],
help="Additional ignore glob. Can be passed multiple times.",
)
parser.add_argument(
"--delete",
action="store_true",
help="Delete probably unused assets after scanning. Requires --confirm.",
)
parser.add_argument(
"--confirm",
action="store_true",
help="Confirm deletion when used with --delete.",
)
return parser
def main(argv: list[str] | None = None) -> int:
parser = build_parser()
args = parser.parse_args(argv)
if args.delete and not args.confirm:
parser.error("--delete requires --confirm")
try:
result = scan_project(args.project, ignore=args.ignore)
except (FileNotFoundError, NotADirectoryError) as error:
print(str(error), file=sys.stderr)
return 2
report = render_report(result, args.format)
if args.output:
Path(args.output).write_text(report + "\n", encoding="utf-8")
else:
print(report)
if args.delete:
delete_assets(result.unused_assets)
print(f"Deleted {len(result.unused_assets)} probably unused assets.", file=sys.stderr)
return 0
def delete_assets(assets: list[object]) -> None:
for asset in assets:
path = getattr(asset, "path")
if path.is_dir():
shutil.rmtree(path)
elif path.exists():
path.unlink()