No Description

unused-image.sh 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/bin/sh
  2. PROGNAME=$(basename "$0")
  3. PROGDIR=$(dirname "$0")
  4. usage()
  5. {
  6. echo "Usage: $PROGNAME [option] -p path-of-project"
  7. echo ""
  8. echo "-p Specifyed the path of your project"
  9. echo "-r Remove unused image file"
  10. echo "-h Show this message"
  11. exit 1
  12. }
  13. PRJ_ROOT=$1
  14. REMOVE=false
  15. COUNT=0
  16. while getopts ":rp:" optname
  17. do
  18. case "$optname" in
  19. "p")
  20. PRJ_ROOT=$OPTARG # specifyed the project root
  21. ;;
  22. "r")
  23. REMOVE=true # remove unused image resource
  24. ;;
  25. "?")
  26. usage
  27. ;;
  28. ":")
  29. echo "No argument value for option $OPTARG"
  30. ;;
  31. *)
  32. # Should not occur
  33. echo "Unknown error while processing options"
  34. ;;
  35. esac
  36. #echo "OPTIND is now $OPTIND"
  37. done
  38. check_files=`find $PRJ_ROOT -name '*.xib' -o -name '*.storyboard' -o -name '*.[mh]' -o -name '*.pch' -o -name '*.java' -o -name '*.xml'`
  39. for png in `find $PRJ_ROOT -name '*.png'`
  40. do
  41. match_name=`basename $png`
  42. suffix1="@2x.png"
  43. suffix2=".9.png"
  44. suffix3=".png"
  45. suffix4="@3x.png"
  46. if [[ ${match_name/${suffix1}//} != $match_name ]]; then
  47. match_name=${match_name%$suffix1}
  48. elif [[ ${match_name/${suffix4}//} != $match_name ]]; then
  49. match_name=${match_name%$suffix4}
  50. elif [[ ${match_name/${suffix2}//} != $match_name ]]; then
  51. match_name=${match_name%$suffix2}
  52. else
  53. match_name=${match_name%$suffix3}
  54. fi
  55. dir_name=`dirname $png`
  56. if [[ $dir_name =~ .bundle$ ]] || [[ $dir_name =~ .appiconset$ ]] || [[ $dir_name =~ .launchimage$ ]]; then
  57. continue
  58. fi
  59. referenced=false
  60. for file in `echo $check_files | sed 's/\n/ /g'`
  61. do
  62. if [[ $png =~ ".imageset" ]]; then
  63. assetName=${png%.imageset*}
  64. assetName=${assetName##*/}
  65. if grep -sh "$assetName" "$file"; then
  66. referenced=true
  67. fi
  68. elif grep -sh "$match_name" "$file"; then
  69. referenced=true
  70. fi
  71. done
  72. if ! $referenced ; then
  73. echo "The '$png' was not referenced in any file"
  74. COUNT=`expr $COUNT + 1`
  75. if $REMOVE ; then
  76. echo "Do remove unused image file '$png'"
  77. rm -f $png
  78. fi
  79. fi
  80. done
  81. echo "============= Total $COUNT unused image files ============="