forked from mozilla-mobile/firefox-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync_acorn_icons.py
149 lines (124 loc) · 5.62 KB
/
sync_acorn_icons.py
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import requests
import json
import os
import shutil
import subprocess
def fetch_latest_release_from_acorn() -> dict|None:
owner = "FirefoxUX"
repo = "acorn-icons"
url = f"https://api.github.com/repos/{owner}/{repo}/releases/latest"
try:
response = requests.get(url=url)
if response.status_code == 200:
return response.json()
except Exception as e:
print(f"It was not possible to retrieve the latest acorn release.\nerror: {e}")
exit()
def save_latest_release_if_needed(data: dict) -> bool:
'''
Saves the latest release object if needed.
:returns bool: True if a new release has been deteceted, otherwise False
'''
file_path = "latest_acorn_release.json"
if not os.path.exists(file_path):
with open(file_path, "w"):
pass
file = open(file_path, "r+")
should_fetch_new_icons = False
if not file.read():
json.dump(data, fp=file, indent=4)
should_fetch_new_icons = True
else:
file.seek(0)
latest_fetched_id = data["id"]
saved_id = json.load(file)["id"]
if latest_fetched_id > saved_id:
# new release has to be fetched
with open(file_path, "w") as file:
json.dump(data, fp=file, indent=4)
should_fetch_new_icons = True
file.close()
return should_fetch_new_icons
def download_icons_and_save_in_assets():
temp_dir_folder_name = "temp_dir"
os.makedirs(temp_dir_folder_name, exist_ok=True)
os.chdir(temp_dir_folder_name)
clone_response = subprocess.run(["git", "clone", "https://github.com/FirefoxUX/acorn-icons"])
if clone_response.returncode != 0:
print(f"Couldn't clone acorn icon repository")
exit()
target_size_to_copy = [16, 20, 24, 30]
asset_folder_path = "../firefox-ios/Client/Assets/Images.xcassets/"
asset_folder_list = os.listdir(asset_folder_path)
for size in target_size_to_copy:
icons_dir_path = f"acorn-icons/icons/mobile/{size}/pdf"
directory_tree = os.walk(icons_dir_path)
for dir_object in directory_tree:
for file in dir_object[2]:
icon_path = os.path.join(dir_object[0], file)
folder_name = f"{os.path.splitext(file)[0]}.imageset".replace("Dark", "").replace("Light", "")
asset_file_path = f"{asset_folder_path}{folder_name}/{file}"
# file has to be a pdf and we need the file already present in the images folder
# the file need to be already in the asset folder, no different file can be added
if file.endswith(".pdf") and folder_name in asset_folder_list and os.path.exists(asset_file_path):
destination_folder = os.path.join(asset_folder_path, folder_name)
os.makedirs(destination_folder, exist_ok=True)
destination_file = os.path.join(destination_folder, file)
shutil.copy(icon_path, destination_file)
os.chdir("..")
subprocess.run(["rm", "-rf", temp_dir_folder_name])
def sort_icons_by_size() -> dict:
icons_by_size: dict[str, list[tuple[str]]] = {
"Small": [],
"Medium": [],
# Extra Large should be before Large since next() will pick Large also for ExtraLarge case
"ExtraLarge": [],
"Large": []
}
asset_folder_path = "firefox-ios/Client/Assets/Images.xcassets/"
for folder in os.listdir(asset_folder_path):
if folder.endswith(".imageset"):
file_name = folder.split(".")[0]
size_key = next((key for key in icons_by_size if key in file_name), None)
if size_key:
icon_name = file_name.replace(size_key, "")
icons_by_size[size_key].append((icon_name, file_name))
return icons_by_size
def generate_standard_image_identifiers_swift(sorted_icons: dict):
swift_file_content = """
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/
import Foundation
/// This struct defines all the standard image identifiers of icons and images used in the app.
/// When adding new identifiers, please respect alphabetical order.
/// Sing the song if you must.
public struct StandardImageIdentifiers {
"""
size_struct_map = {
"Small": "16x16",
"Medium": "20x20",
"Large": "24x24",
"ExtraLarge": "30x30"
}
for size, struct_name in size_struct_map.items():
if sorted_icons[size]:
swift_file_content += f" // Icon size {struct_name}\n"
swift_file_content += f" public struct {size} {{\n"
# Sort icons alphabetically and add them to the struct
for icon_info in sorted(sorted_icons[size]):
swift_file_content += f" public static let {icon_info[0]} = \"{icon_info[1]}\"\n"
swift_file_content += " }\n"
swift_file_content += "}"
standard_image_file_path = "BrowserKit/Sources/Common/Constants/StandardImageIdentifiers.swift"
with open(standard_image_file_path, "w") as swift_file:
swift_file.write(swift_file_content)
def main():
latest_release = fetch_latest_release_from_acorn()
if latest_release:
should_download_icons = save_latest_release_if_needed(latest_release)
if should_download_icons:
download_icons_and_save_in_assets()
sorted_icons = sort_icons_by_size()
generate_standard_image_identifiers_swift(sorted_icons)
main()