Skip to content

Instantly share code, notes, and snippets.

@LeoValentim
Created August 12, 2019 19:06
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LeoValentim/23df4b483e4ae1df946168ef0bfa780f to your computer and use it in GitHub Desktop.
Save LeoValentim/23df4b483e4ae1df946168ef0bfa780f to your computer and use it in GitHub Desktop.
#USAGE: sh clean_unused_images.sh
project_path='/Users/../../Projects/iOS/APP_IOS/' # Change to your project's path
assets_path=$project_path"Assets.xcassets" # asset's path
assets_name="${assets_path/$project_path/$(echo '')}"
printf "\e[92mGetting first images:\e[0m\n"
images=""
# Get Images from assets root directory
first_images=$(ls $assets_path | awk '/ *.imageset/ {print $0}')
for first_image in $first_images; do
images+=" Assets.xcassets/$first_image"
done
# Get images from assets subdiretories
paths=$(ls -F $assets_path | grep / | awk '! / *.imageset/ {print $0}')
while [ "$paths" != "" ]; do
old_paths=$paths
paths=""
for path in $old_paths; do
new_image_path="$assets_path/$path"
new_images=$(ls $new_image_path | awk '/ *.imageset/ {print $0}')
if [ "$new_images" != "" ]; then
for new_image in $new_images; do
image_path="$new_image_path/$new_image"
images+=" ${image_path/$project_path/$(echo '')}"
done
fi
new_paths=$(ls -F $new_image_path | grep / | awk '! / *.imageset/ {print $0}')
for new_path in $new_paths; do
paths+=" $path/$new_path"
done
done
done
printf "\e[92mGetting unused images:\e[0m\n"
for image in $images; do
image_name=$(echo $image | perl -pe 's/([^\/]*+\/)++//g and s/\.imageset//g')
used_files=$(grep -R -l --exclude-dir=Assets.xcassets "$image_name" $project_path)
if [ "$used_files" == "" ]; then
printf "\e[33m$image_name on PATH: $image is not used\e[0m\n"
rm -rf $project_path/$image
printf "$image was \e[91mREMOVED\e[0m\n"
else
printf "\e[92m$image_name is used\e[0m\n"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment