Created
July 31, 2024 16:58
-
-
Save Pamblam/44787d8a599abb430674a53f2ea4532d to your computer and use it in GitHub Desktop.
Bash script to generate iOS and Android app icons for a Flutter project.
This file contains hidden or 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
#!/bin/bash | |
# Given a square icon image file and a path to a flutter project, | |
# this will update the android and ios icon images for the project. | |
# Usage: generate_flutter_icons.sh -i /path/to/icon.png -p /path/to/project | |
# Note: -p flag can be omitted if the current working directory is | |
# the target flutter project. | |
while getopts i:p: flag | |
do | |
case "${flag}" in | |
i) icon=${OPTARG};; | |
p) project=${OPTARG};; | |
esac | |
done | |
if [ -z "${icon}" ]; then | |
echo "Error: Source icon image path must be set by passing the -i flag."; | |
exit 1; | |
fi | |
if [ ! -f "${icon}" ]; then | |
echo "Error: Source icon image file does not exist."; | |
exit 1; | |
fi | |
if [ -z "${project}" ]; then | |
project=$(pwd) | |
fi | |
androidpath="${project}/android/app/src/main/res" | |
if [ ! -d "${androidpath}" ]; then | |
echo "No Android project found in the project directory, skipping." | |
else | |
find "${androidpath}" -type f -print0 | while read -d $'\0' file; do | |
mime=$(file -b --mime-type "$file"); | |
if [[ "$mime" == "image"* ]]; then | |
width=$(identify -format "%w" "$file")> /dev/null | |
height=$(identify -format "%h" "$file")> /dev/null | |
convert "${icon}" -resize "$width"x"$height"\! "$file" | |
echo "Updated $file." | |
fi; | |
done | |
fi | |
iospath="${project}/ios/Runner/Assets.xcassets/AppIcon.appiconset" | |
if [ ! -d "${iospath}" ]; then | |
echo "No iOS project found in the project directory, skipping." | |
else | |
find "${iospath}" -type f -print0 | while read -d $'\0' file; do | |
mime=$(file -b --mime-type "$file"); | |
if [[ "$mime" == "image"* ]]; then | |
width=$(identify -format "%w" "$file")> /dev/null | |
height=$(identify -format "%h" "$file")> /dev/null | |
convert "${icon}" -resize "$width"x"$height"\! "$file" | |
echo "Updated $file." | |
fi; | |
done | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment