检查iOS工程imageset图片缺失(shell版)

接到需求的时候就用shell写了 , 结果4000个文件要检测七八分钟

然后用python实现了 ,只用了不到2秒........这差距

需求背景: 要检查imageset里面2x图和3x图的缺失问题

检查指标:
1.imageset里面的Contents.json 里, 是否2x和3x都有对应的filename
2.有filename的情况下,imageset里是否有对应的图片

毕竟不是专门写shell的 , 如果有更优化的写法, 欢迎各位大佬指教

shell解析json比较麻烦 , 下面有2种 1.利用python 2.利用jq库
brew install jq

直接上代码:
#!/bin/sh


# find . -name '*.fb' -type f -print -exec rm -rf {} \;

#############################  分割线    [ 全局变量 ] ###################################

CHECK_PATH=$1 #要检查的路径
WHITELIST=$2 #白名单

echo 检查路径:${CHECK_PATH}
echo 白名单:${WHITELIST}

#############################  分割线    [ 自定义方法 ] ###################################

function check2x3x()
{
    contents_path=$1
    # echo $contents_path



    # 1.查 @2x 和 @3x的 filename是否存在
    # content=`cat $contents_path`
    # lens=`echo $content| python3 -c "import sys, json; data = json.load(sys.stdin); print(len(data['images']))"`
    # # echo $lens
    # for (( j = 0; j < $lens; j++ )); do
    #   filename=`echo $content| python3 -c "import sys, json; print(json.load(sys.stdin)['images'][$j].get('filename'))"`
    #   # echo $filename
    #   if [[ $filename == None ]]; then
    #       # echo "空"
    #       #filename 为空
    #       scale=`echo $content| python3 -c "import sys, json; print(json.load(sys.stdin)['images'][$j]['scale'])"`    
    #       echo "路径: $contents_path  ,Content.json文件缺少 @${scale} 图片的 filename"  >> checkImg_result.txt
    #   else
    #       # echo "非空"
    #       #filename 不为空 就检查对应图片是否存在
    #       #拼图片路径
    #       imageset_dir=`dirname $contents_path`
    #       imgPath=${imageset_dir}/${filename}
    #       if [[ ! -f $imgPath ]]; then
    #           echo "路径: ${imageset_dir}  ,缺少 ${filename} 图片"  >> checkImg_result.txt              
    #       fi

    #   fi

    # done


# 1.查 @2x 和 @3x的 filename是否存在

    content=`cat $contents_path`
    lens=`echo $content | jq '.images' | jq length`
    # echo $lens
    # echo lens:$lens
    for (( j = 0; j < $lens; j++ )); do
        filename=`echo $content | jq ".images[${j}]" | jq '.filename'`
        # echo $filename
        if [[ $filename == None ]]; then
            # echo "空"
            #filename 为空
            scale=`echo $content | jq ".images[${j}]" | jq '.scale'`
            echo "路径: $contents_path  ,Content.json文件缺少 @${scale} 图片的 filename"  >> checkImg_result.txt
        else
            # echo "非空"
            #filename 不为空 就检查对应图片是否存在
            #拼图片路径
            imageset_dir=`dirname $contents_path`
            imgPath=${imageset_dir}/${filename}
            if [[ ! -f $imgPath ]]; then
                echo "路径: ${imageset_dir}  ,缺少 ${filename} 图片"  >> checkImg_result.txt              
            fi

        fi

    done


    # 2.查scale是否缺失  [不用做]

    # 3.查对应的图片是否存在



    # echo $contents

    # lens=`cat $contents_path| python3 -c "import sys, json; data = json.load(sys.stdin); print(len(data['images']))"`
    # echo 数组数量:$lens
    # sizes=(1x 2x 3x)

    # for (( j = 0; j < $lens; j++ )); do
    #   # echo $i
    #   size=`cat $contents_path| python3 -c "import sys, json; print(json.load(sys.stdin)['images'][$j]['scale'])"`
    #   # echo $size
    #   sizes=( ${sizes[*]/$size} )

    # done
    # echo "移除数组结束"

    # # echo ${sizes[*]}

    # for (( k = 0; k < ${#sizes[@]}; k++ )); do
    #   echo "路径: $contents_path  ,Content.json文件缺少 ${sizes[k]} 图片"  >> checkImg_result.txt
    # done;

    # echo 写入文件结束
     

}



#############################  分割线    [ 流程 ] ###################################

#判断要检查的路径是否正确
if [[ ! -d $CHECK_PATH ]]; then
    echo "\033[31m ERROR : 要检查的不是一个路径 , 程序退出 \033[0m"
    exit 0
fi


#加载白名单 ,拼接查找命令
#有参数,且存在
find_command_line="find $CHECK_PATH"
if [[ ! -z $WHITELIST ]] && [[ -f $WHITELIST ]]; then
    echo 白名单存在
    content=`cat whitelist.txt`
    content=(${content})
    for((i = 0; i < ${#content[@]}; i++)); do
        echo 白名单目录$i:${content[i]}
        find_command_line="$find_command_line -path ${content[i]} -prune -o"
    done;
    find_command_line="$find_command_line -name *.imageset -type d -print"
else
    find_command_line="find $CHECK_PATH -name *.imageset -type d"
fi


#删除之前的记录
if [ -f checkImg_result.txt ];then
    echo "文件不存在"
    rm checkImg_result.txt
fi

#获取所有的 xxx.imageset
imageset_paths=`$find_command_line`

#转换一下, 要不不是数组
imageset_paths=(${imageset_paths})





#遍历路径
contents_json_len=${#imageset_paths[@]}
echo 文件个数:${#imageset_paths[@]}
for((i = 0; i < ${#imageset_paths[@]}; i++)); do

    imageset_path=${imageset_paths[i]}
    #拼接 Content.json路径
    contents_json_path=${imageset_path}/Contents.json
    # echo Content路径:$contents_json_path

    #通过路径检查是否缺失@2x 或者 @3x 的图片
    check2x3x $contents_json_path
    echo 进度:$i/$contents_json_len
    # echo "\n"
done;


open checkImg_result.txt



上一篇: 检查iOS工程imageset图片缺失(python版)

你可能感兴趣的:(检查iOS工程imageset图片缺失(shell版))