-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathupdate.sh
executable file
·127 lines (108 loc) · 4.26 KB
/
update.sh
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
#!/bin/bash
# Variables
GITHUB_REPO="https://github.com/iconoir-icons/iconoir/tree/main/icons"
ASSETS_DIR="Sources/Iconoir/Assets.xcassets"
ICON_ENUM_FILE="Sources/Iconoir/Icon.swift"
TEMP_DIR="temp_icons"
# Create directories if they don't exist
if [ -d "${ASSETS_DIR}" ]; then
echo "${ASSETS_DIR} exists. Clearing it..."
rm -rf "${ASSETS_DIR}"/*
fi
mkdir -p "${ASSETS_DIR}"
mkdir -p "${TEMP_DIR}"
# Download the entire contents of the icons folder
git clone --depth 1 --filter=blob:none --sparse "https://github.com/iconoir-icons/iconoir.git" "${TEMP_DIR}"
cd "${TEMP_DIR}" || exit
git sparse-checkout init --cone
git sparse-checkout set icons
git checkout main
cd .. || exit
# Function to download and process an icon
download_and_process_icon() {
svg_path=$1
dir_name=$(basename "$(dirname "${svg_path}")")
if [[ "${dir_name}" == "regular" ]]; then
dir_name=""
fi
icon_file=$(basename "${svg_path}")
icon_name="${icon_file%.*}"
local_icon_path="${ASSETS_DIR}/${icon_name}${dir_name:+-${dir_name}}.imageset"
if [[ ! -f "${local_icon_path}/${icon_name}${dir_name:+-${dir_name}}.pdf" ]]; then
mkdir -p "${local_icon_path}"
# Copy the SVG file
cp "${svg_path}" "${local_icon_path}/${icon_name}${dir_name:+-${dir_name}}.svg"
sed -i '' -e 's/stroke="currentColor"/stroke="#000000"/g' -e 's/stroke-width="[0-9.]*"/stroke-width="1.5"/g' "${local_icon_path}/${icon_name}${dir_name:+-${dir_name}}.svg"
# Create JSON file for the icon in Assets.xcassets
json_file="${local_icon_path}/Contents.json"
cat <<-JSON > "${json_file}"
{
"images" : [
{
"filename" : "${icon_name}${dir_name:+-${dir_name}}.svg",
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
JSON
echo "Processed ${icon_name}${dir_name:+-${dir_name}}"
fi
}
# Iterate over the downloaded icons
for svg_path in "${TEMP_DIR}/icons"/**/*.svg; do
download_and_process_icon "${svg_path}"
done
# Remove the temporary directory
rm -rf "${TEMP_DIR}"
# Create Icon.swift enum file
echo "/// This file was generated by the update.sh script located in the root of this package." > "${ICON_ENUM_FILE}"
echo "/// This enum provides a list of all the icons provided in the [iconoir website](https://www.iconoir.com)." >> "${ICON_ENUM_FILE}"
echo "/// There are some conditions which the image was named after a keyword in swift, and as such has been replaced" >> "${ICON_ENUM_FILE}"
echo "/// by a placeholder, either prefixed or suffixed with \`icon\`. For example \`import\` would become \`importIcon\`." >> "${ICON_ENUM_FILE}"
echo "/// Any image that begins with a digit would be prefixed with \`icon\` for example \`icon2x2cell\`" >> "${ICON_ENUM_FILE}"
echo "public enum Iconoir: String, CaseIterable {" >> "${ICON_ENUM_FILE}"
# Initialize an array to store the enum cases
enum_cases=()
# Iterate through the downloaded icons using process substitution
while read -r icon_path; do
icon_file=$(basename "${icon_path}")
icon_name="${icon_file%.*}"
# Replace special characters with spaces
icon_name_clean=$(echo "${icon_name}" | tr "[:punct:]" " ")
# Convert to camel case
icon_case=$(echo "${icon_name_clean}" | awk '{for(i=1;i<=NF;i++){if(i==1){printf("%s",$i)}else{printf("%s",toupper(substr($i,1,1)) substr($i,2))}}}')
# Handle reserved keywords and starting with numbers
case "${icon_case}" in
import|default|case|var|let|class|struct|func|enum|repeat)
icon_case="${icon_case}Icon"
;;
[0-9]*)
icon_case="icon${icon_case}"
;;
esac
# Store the case with its raw value in the array
enum_cases+=(" case ${icon_case} = \"${icon_name}\"")
done < <(find "${ASSETS_DIR}" -name "*.svg")
# Sort the enum cases alphabetically
IFS=$'\n' sorted_cases=($(sort <<<"${enum_cases[*]}"))
unset IFS
# Write the sorted cases to the Icon.swift enum file
for case_line in "${sorted_cases[@]}"; do
echo "${case_line}" >> "${ICON_ENUM_FILE}"
done
# Close the enum declaration
echo "}" >> "${ICON_ENUM_FILE}"
echo "Script completed successfully."