Created
April 14, 2023 14:58
-
-
Save mkaulfers/2200dcfa76bad800823eca0e975103f6 to your computer and use it in GitHub Desktop.
[Broken] A script to download and add Iconoir SVG's into an `Assets.xcassets` folder in an XCode Project. Add this script to your root project directory.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/opt/homebrew/bin/python3 | |
# Function to check if a command exists | |
command_exists() { | |
command -v "$1" &> /dev/null | |
} | |
# 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" | |
# Check if CairoSVG is installed | |
if ! command_exists cairosvg; then | |
echo "CairoSVG is not installed. Please install it using 'pip install cairosvg'." | |
exit 1 | |
fi | |
# Create directories if they don't exist | |
mkdir -p "${ASSETS_DIR}" | |
# Fetch icons from the GitHub repository and save them to the Assets.xcassets folder | |
icon_list=$(curl -sL "${GITHUB_REPO}" | grep -o 'blob/main/icons/[^"]*\.svg' | sed 's/blob\/main\//main\//') | |
echo "Downloading icons:" | |
total_icons=0 | |
current_icon=0 | |
for svg_url in ${icon_list}; do | |
icon_file=$(basename "${svg_url}") | |
local_icon_path="${ASSETS_DIR}/${icon_file}" | |
if [[ ! -f "${local_icon_path}" ]]; then | |
total_icons=$((total_icons + 1)) | |
fi | |
done | |
for svg_url in ${icon_list}; do | |
icon_file=$(basename "${svg_url}") | |
icon_name="${icon_file%.*}" | |
local_icon_path="${ASSETS_DIR}/${icon_name}.imageset" | |
if [[ ! -f "${local_icon_path}/${icon_name}.pdf" ]]; then | |
mkdir -p "${local_icon_path}" | |
# Download the SVG file | |
curl -sL -o "${local_icon_path}/${icon_file}" "https://raw.githubusercontent.com/iconoir-icons/iconoir/${svg_url}" | |
# Convert the SVG file to a PDF file | |
/usr/bin/python3 /usr/local/bin/cairosvg "${local_icon_path}/${icon_file}" -o "${local_icon_path}/${icon_name}.pdf" | |
# Remove the SVG file | |
rm "${local_icon_path}/${icon_file}" | |
current_icon=$((current_icon + 1)) | |
# Calculate progress | |
progress=$((current_icon * 100 / total_icons)) | |
filled=$((progress / 2)) | |
empty=$((50 - filled)) | |
# Print progress bar | |
echo -ne "\033[2K" | |
printf "\r[" | |
printf "%0.s▮" $(seq 1 $filled) | |
printf "%0.s " $(seq 1 $empty) | |
printf "] %02d%%" $progress | |
# Create JSON file for the icon in Assets.xcassets | |
json_file="${local_icon_path}/Contents.json" | |
cat <<-JSON > "${json_file}" | |
{ | |
"images" : [ | |
{ | |
"filename" : "${icon_name}.pdf", | |
"idiom" : "universal", | |
"scale" : "1x" | |
}, | |
{ | |
"idiom" : "universal", | |
"scale" : "2x" | |
}, | |
{ | |
"idiom" : "universal", | |
"scale" : "3x" | |
} | |
], | |
"info" : { | |
"author" : "xcode", | |
"version" : 1 | |
} | |
} | |
JSON | |
fi | |
done | |
echo | |
# Create Icon.swift enum file | |
echo "public enum Icon: 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) | |
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 "*.pdf") | |
# 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." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment