Skip to content

Instantly share code, notes, and snippets.

@vdugnist
Last active February 11, 2021 06:42
Show Gist options
  • Save vdugnist/edfb2cd32058a096f7978e040c55b009 to your computer and use it in GitHub Desktop.
Save vdugnist/edfb2cd32058a096f7978e040c55b009 to your computer and use it in GitHub Desktop.
Check if UIImage exists in assets
#!/bin/bash
CLASSES_FOLDERS=($PROJECT_NAME)
RESOURCE_FOLDER=$PROJECT_NAME
function show_code {
for ClassFolder in "${CLASSES_FOLDERS[@]}"; do
# try to find image name in [UIImage imageNamed:] pattern
ERROR_LOCATION=$(grep -Ron "\[UIImage imageNamed:@\"$1\"]" $ClassFolder)
if [[ -n $ERROR_LOCATION ]]; then break; fi;
# try to find image name in UIImage(named:) pattern
ERROR_LOCATION=$(grep -Ron "UIImage(named\:\"$1\")" $ClassFolder)
if [[ -n $ERROR_LOCATION ]]; then break; fi;
# try to find image name in xib pattern
ERROR_LOCATION=$(grep -Ron --include "*.xib" "\s*image=\"$1\"" $ClassFolder)
if [[ -n $ERROR_LOCATION ]]; then break; fi;
# try to find image name in storyboard pattern
ERROR_LOCATION=$(grep -Ron --include "*.storyboard" "\s*image=\"$1\"" $ClassFolder)
if [[ -n $ERROR_LOCATION ]]; then break; fi;
done
ERROR_LOCATION=$(echo $ERROR_LOCATION | cut -d ':' -f 1,2)
echo "$ERROR_LOCATION: error: Missing imageset with name $1"
}
function show_img {
local IMG_LOC=$(find "$RESOURCE_FOLDER" -name "$1.imageset" | sed 's/.xcassets\//.xcassets:.\//')
echo "$IMG_LOC/:: error: No more refs to imageset $1"
}
USED_NAMES=()
for ClassFolder in "${CLASSES_FOLDERS[@]}"; do
# find obj-c [UIImage imageNamed:@""]
USED_NAMES+=($(grep -Ron '\[UIImage imageNamed:\s*@"[^"]*"\s*\]' $ClassFolder | cut -d '"' -f 2))
# find obj-c [UIApplicationShotrcutIcon iconWithTemplateImageName:@""]
USED_NAMES+=($(grep -Ron '\[UIApplicationShortcutIcon iconWithTemplateImageName:\s*@"[^"]*"\s*\]' $ClassFolder | cut -d '"' -f 2))
# find swift UIImage(named "")
USED_NAMES+=($(grep -Ron 'UIImage(named\:\s*"[^"]\{1,\}"\s*)' $ClassFolder | cut -d '"' -f 2))
# find xib usages
USED_NAMES+=($(grep -Ron --include "*.xib" '\s*image="[^"]\{1,\}"\s*' $ClassFolder | cut -d '"' -f 2))
# find storyboard usages
USED_NAMES+=($(grep -Ron --include "*.storyboard" '\s*image="[^"]\{1,\}"\s*' $ClassFolder | cut -d '"' -f 2))
done
# find xib and storybord usages in resource folder
USED_NAMES+=($(grep -Ron --include "*.xib" '\s*image="[^"]\{1,\}"\s*' $RESOURCE_FOLDER | cut -d '"' -f 2))
USED_NAMES+=($(grep -Ron --include "*.storyboard" '\s*image="[^"]\{1,\}"\s*' $RESOURCE_FOLDER | cut -d '"' -f 2))
# find images names in assets and sort
PRESENTED_IMAGES=($(find $RESOURCE_FOLDER -name *.imageset | grep -v Pods | /usr/bin/sed -e 's/.*\///' -e 's/\.imageset$//'))
# sort result
SORTED_USED_NAMES=$(printf '%s\n' "${USED_NAMES[@]}" | sort -u)
SORTED_PRESENTED_IMAGES=$(printf '%s\n' "${PRESENTED_IMAGES[@]}" | sort -u)
EXIT_CODE=0
# printf '%s\n' "$SORTED_USED_NAMES" > used_names.txt
# printf '%s\n' "$SORTED_PRESENTED_IMAGES" > presented_images.txt
echo "Missing imageset with name:"
for name in $(comm -23 <(printf '%s\n' "$SORTED_USED_NAMES") <(printf '%s\n' "$SORTED_PRESENTED_IMAGES")); do
show_code $name
EXIT_CODE=1
done
echo "No more refs to imageset:"
for name in $(comm -13 <(printf '%s\n' "$SORTED_USED_NAMES") <(printf '%s\n' "$SORTED_PRESENTED_IMAGES")); do
show_img $name
EXIT_CODE=1
done
echo
exit $EXIT_CODE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment