shell 删除 XCode 不用的模拟器文件

每次 xcode 升级都会自带最新的模拟器文件,但是老版本的模拟器依然存在,并没有删除。本着硬盘空间能省一点是一点的原则,写了个脚本来删除不用的模拟器。

#/bin/sh
simulatorPath=~/Library/Developer/CoreSimulator/Devices
simulatorPlistPath="${simulatorPath}/device_set.plist"

if [[ ! -f ${simulatorPlistPath} ]]; then
    #statements
    echo "simulator plist file does not exit, please make sure it's at ${simulatorPlistPath};"
    exit 1
fi

cd ${simulatorPath}
backupDevicePath="../backup"

# 解析 plist
OSPrefix="com.apple.CoreSimulator.SimRuntime."
modelPrefix="com.apple.CoreSimulator.SimDeviceType"

retainIOSs="11-2"
retainTVOSs="11-2"
retainWatchOSs="4-2"

# 可以从 retianOS 中截取,使用最新的。
# endIOSVersion=11
# endTVOSVersion=11
# endWatchOSVersion=4

# PlistBuddy Print 只是打印内容,并不能把读出来的内容转成 map or array,所以把全部的可能性都遍历出来
SYSTEM=("iOS" "tvOS" "watchOS")

function delete() {
    system=$1
    # declare models
    # declare startVersion
    # declare retainOS
    if [[ ${system}x == "iOS"x ]]; then
        retainOS=${retainIOSs}
        models=("iPhone-4"
                "iPhone-4s"
                "iPhone-5"
                "iPhone-5s"
                "iPhone-6"
                "iPhone-6-Plus"
                "iPhone-6s"
                "iPhone-6s-Plus"
                "iPhone-7"
                "iPhone-7-Plus"
                "iPhone-8"
                "iPhone-8-Plus"
                "iPhone-X"
                "iPhone-SE"
                "iPad--5th-generation-"
                "iPad-Air"
                "iPad-Air-2"
                "iPad-2"
                "iPad-Retina"
                "iPad-Pro"
                "iPad-Pro--9-7-inch-"
                "iPad-Pro--10-5-inch-"
                "iPad-Pro--12-9-inch---2nd-generation-"
                "Resizable-iPad"
                "Resizable-iPhone"
                )
        startVersion=8
    elif [[ ${system}x == "tvOS"x ]]; then
        retainOS=${retainTVOSs}
        models=("Apple-TV-1080p"
                "Apple-TV-4K-1080p"
                "Apple-TV-4K-4K"
                )
        startVersion=8
    elif [[ ${system}x == "watchOS"x ]]; then
        retainOS=${retainWatchOSs}
        models=("Apple-Watch-38mm"
                "Apple-Watch-Series-2-38mm"
                "Apple-Watch-Series-2-42mm"
                "Apple-Watch-42mm"
                "Apple-Watch-Series-3-38mm"
                "Apple-Watch-Series-3-42mm"
                )
        startVersion=3
    fi

    logPath="./log/${system}"
    simulatorPre="${OSPrefix}${system}"
    # clear txt 输出
    if [[ ! -d ${logPath} ]]; then
        mkdir -p ${logPath}
    fi
    rm ${logPath}/*.txt

    # start from ios 7 to 11
    endVersion=`echo ${retainOS} | cut -d \- -f 1`
    declare -a versions
    for (( i=${startVersion}; i <= ${endVersion}; i++ )); do
        for (( j=0; j < 10; j++ )); do
            v="${i}-${j}"
            versions=(${versions[@]} ${v})
        done
    done

    # log
    echo "system:${system}\nlogPath:${logPath}\nmodels:${models[@]}\nversions:${versions[@]}" > ${logPath}/info.txt

    return
    #按数据遍历
    for v in ${versions[@]}
    do
        vname=${simulatorPre}-$v
        # echo "name:${vname}"
        if [[ ${v}x == ${retainOS}x ]]; then
            echo "find retain iOS ${vname}" >> ${logPath}/retain.txt
        else
            pname=`/usr/libexec/PlistBuddy -c "Print :DefaultDevices:${vname}" ${simulatorPlistPath}`
            if [[ ! -z "${pname}" ]]; then
                # os 版本存在
                echo "find OS : ${vname}" >> ${logPath}/findOSs.txt
                for n in ${models[@]}; do
                    model="${modelPrefix}.${n}"
                    modelId=`/usr/libexec/PlistBuddy -c "Print :DefaultDevices:${vname}:${model}" ${simulatorPlistPath}`
                    if [[ ! -z "${modelId}" ]]; then
                        # model 存在
                        echo "OS:${vname}\nmodel:${model}\nid:${modelId}" >> ${logPath}/findModels.txt

                        if [[ -d ./${modelId} ]]; then
                            # source exits
                            targetPath=${backupDevicePath}/${v}/${n}

                            if [[ ! -d ${targetPath} ]]; then
                                mkdir -p ${targetPath}
                            fi
                            mv -f ./${modelId} ${targetPath}
                        fi
                    else
                        echo "OS:${vname}\nmodel:${model}\nid:${modelId}" >> ${logPath}/notExit.txt
                    fi
                done
            else
                    echo "pname:${vname} does not exit!!!!!" >> ${logPath}/notExit.txt
            fi
        fi
    done
}

for p in ${SYSTEM[@]}; do
    delete ${p}
done
exit 0

你可能感兴趣的:(shell 删除 XCode 不用的模拟器文件)