-
Notifications
You must be signed in to change notification settings - Fork 650
Expand file tree
/
Copy pathgenerate_icons.py
More file actions
110 lines (85 loc) · 3.09 KB
/
generate_icons.py
File metadata and controls
110 lines (85 loc) · 3.09 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
# /// script
# dependencies = [
# "requests",
# "Jinja2",
# ]
# ///
import json
import re
from pathlib import Path
import requests
from jinja2 import Environment, FileSystemLoader
# Regex for parsing icon definitions (handles multi-line IconData)
ICON_VAR_PATTERN = re.compile(
r"""^\s*static const IconData\s+(\w+)\s*=""", re.MULTILINE
)
file_loader = FileSystemLoader(Path(__file__).parent / "templates")
templates = Environment(loader=file_loader)
def download_dart_file(url: str) -> str:
print(f"Downloading Dart file from: {url}")
response = requests.get(url)
response.raise_for_status()
return response.text
def parse_dart_icons(dart_content: str, set_id: int):
# Extract and sort icon names alphabetically
icon_names = sorted(ICON_VAR_PATTERN.findall(dart_content))
icons = []
for i, icon_name in enumerate(icon_names):
packed_value = (set_id << 16) | i
icons.append((icon_name, packed_value))
print(f"🔍 Found {len(icons)} icons for set ID {set_id} (sorted).")
return icons
def generate_file(icons, template_name, output_file: str):
template = templates.get_template(template_name)
with open(
Path(__file__).parent.joinpath(output_file).resolve(), "w", encoding="utf-8"
) as f:
f.write(template.render(icons=icons))
print(f"✅ File written to {output_file}")
def generate_json(icons, output_file: str):
payload = {name.upper(): value for name, value in icons}
output_path = Path(__file__).parent.joinpath(output_file).resolve()
output_path.parent.mkdir(parents=True, exist_ok=True)
output_path.write_text(json.dumps(payload, separators=(",", ":")), encoding="utf-8")
print(f"✅ JSON written to {output_file}")
def main():
# material icons
url = "https://raw.githubusercontent.com/flutter/flutter/refs/heads/stable/packages/flutter/lib/src/material/icons.dart"
set_id = 1
dart_content = download_dart_file(url)
icons = parse_dart_icons(dart_content, set_id)
generate_file(
icons,
"material_icons.dart",
"../../packages/flet/lib/src/utils/material_icons.dart",
)
generate_file(
icons,
"material_icons.pyi",
"../../sdk/python/packages/flet/src/flet/controls/material/icons.pyi",
)
generate_json(
icons,
"../../sdk/python/packages/flet/src/flet/controls/material/icons.json",
)
# cupertino icons
url = "https://raw.githubusercontent.com/flutter/flutter/refs/heads/stable/packages/flutter/lib/src/cupertino/icons.dart"
set_id = 2
dart_content = download_dart_file(url)
icons = parse_dart_icons(dart_content, set_id)
generate_file(
icons,
"cupertino_icons.dart",
"../../packages/flet/lib/src/utils/cupertino_icons.dart",
)
generate_file(
icons,
"cupertino_icons.pyi",
"../../sdk/python/packages/flet/src/flet/controls/cupertino/cupertino_icons.pyi",
)
generate_json(
icons,
"../../sdk/python/packages/flet/src/flet/controls/cupertino/cupertino_icons.json",
)
if __name__ == "__main__":
main()