美文网首页
iOS打包脚本改进版

iOS打包脚本改进版

作者: sytuzhouyong | 来源:发表于2016-06-30 14:48 被阅读93次
    #!/bin/sh
    
    PlistBuddy=/usr/libexec/PlistBuddy
    
    function echo_tip() {
        echo "\033[36;49m$1\033[0m"
    }
    function echo_error() {
        echo "\033[31;49m$1\033[0m"
    }
    
    
    # if [[ "$1"x != "x" ]]; then
    #     project_root_path=$1
    # fi
    
    project_root_path=$(PWD -P)
    project_name=""
    project_type=""
    scheme=""
    info_plist_file_path=""
    ipa_file_name=""
    code_sign=""
    uuid=""
    
    ipa_file_path="$project_root_path/ipa"
    configuration="Release"
    provision_profile_path="$ipa_file_path/ZXIPTV_InHouse.mobileprovision"
    build_path="$project_root_path/build"
    
    # 获取工程名称 1: 单个工程, 2: 工作空间
    function get_project_info() {
        cd $1
        workspace=`ls . | grep "xcworkspace$"`
        if [[ x"$workspace" != "x" ]]; then
            project_name=`echo $workspace | cut -d "." -f1`
            project_type="2"
            return "0"
        else
            project=`ls . | grep "xcodeproj$"`
            if [[ x"$project" != "x" ]]; then
                project_name=`echo $project | cut -d "." -f1`
                project_type="1"
                return "0"
            fi
        fi
    
        echo_error "目录不是一个有效的工程目录"
        exit 1
    }
    
    # 校验scheme是否正确
    function validate_scheme() {
        info=`xcodebuild -list`
        schemes=${info#*Schemes:}
        for item in $schemes; do
            if [[ "$1"x == "$item"x ]]; then
                return "0"
            fi
        done
    
        echo "$1 is not in [$schemes]"
        exit 1
    }
    
    function validate_configuration() {
        info=`xcodebuild -list`
        configurations=${info#*Configurations:}
        configurations=${configurations%If*}
        for item in $configurations; do
            if [[ "$1"x == "$item"x ]]; then
                return "0"
            fi
        done
        exit 1
    }
    
    # 获取指定target对应的info.plist文件名称, 参数为scheme值
    function get_info_plist_file_path() {
        settings=`xcodebuild -showBuildSettings -scheme $1`
        str1=${settings#*PRODUCT_SETTINGS_PATH = }
        path=${str1%%.plist*}
        info_plist_file_path=$path".plist"
    }
    
    # 解析provision profile, 参数为描述文件路径
    function parse_provision_profile() {
        temp_plist_path="$ipa_file_path/temp.plist"
        if [[ -f "$temp_plist_path" ]]; then
                rm $temp_plist_path
        fi
    
        security cms -D -i $1 > $temp_plist_path
        code_sign_identity=`$PlistBuddy -c 'Print DeveloperCertificates:0' $temp_plist_path | \
                            openssl x509 -subject -inform der|head -n 1`
        code_sign=`echo "$code_sign_identity" | cut -d "/" -f3 | cut -d "=" -f2`
        uuid=`$PlistBuddy -c "Print UUID" $temp_plist_path`
    
        rm $temp_plist_path
    
        if [[ $code_sign == "" || $uuid == "" ]]; then
            echo_error "无法解析描述文件[$provision_profile_path]"
            exit 1
        fi
    }
    
    # 设置版本号, 参数1为version, 参数2为plist文件路径
    function update_version() {
        old_version=$($PlistBuddy -c "Print CFBundleVersion" $2)
        old_short_version=$($PlistBuddy -c "Print CFBundleShortVersionString" $2)
        echo_tip "before update, version = $old_version, short version = $old_short_version"
    
        short_version=$(echo $1 | cut -d "." -f 1-3)
        $PlistBuddy -c "Set:CFBundleVersion $1" "$2"
        $PlistBuddy -c "Set:CFBundleShortVersionString $short_version" "$2"
        new_version=$($PlistBuddy -c "Print CFBundleVersion" $2)
        new_short_version=$($PlistBuddy -c "Print CFBundleShortVersionString" $2)
        echo_tip "after update, version = $new_version, short version = $new_short_version"
        
        return "0"
    }
    
    #=====================================================================================
    
    get_project_info "$project_root_path"
    parse_provision_profile "$provision_profile_path"
    
    scheme=$project_name
    validate_scheme "$scheme"
    validate_configuration "$configuration"
    get_info_plist_file_path "$scheme"
    
    version=`$PlistBuddy -c "Print CFBundleVersion" "$info_plist_file_path"`
    if [[ "$1"x != "x" ]]; then
        version=$1
        update_version "$version" "$info_plist_file_path"
    fi
    
    ipa_file_name="$project_name-$version.ipa"
    
    echo_tip "project_root_path = $project_root_path"
    echo_tip "project_name = $project_name"
    echo_tip "scheme = $scheme"
    echo_tip "info_plist_file_path = $info_plist_file_path"
    echo_tip "version = $version"
    echo_tip "ipa_file_name = $ipa_file_name"
    echo_tip "ipa_file_path = $ipa_file_path"
    echo_tip "code_sign = $code_sign"
    echo_tip "uuid = $uuid"
    
    #=====================================================================================
    
    function prepare_build() {
        rm -rf $build_path
        mkdir -p $build_path
        mkdir -p $ipa_file_path
    
        for path in `find "$project_root_path" -name "*.a"`; do
            cp $path $build_path
        done
    }
    
    function build() {
        if [[ "$project_type" == "1" ]]; then
            /usr/bin/xcodebuild \
                -target $scheme  \
                -configuration $configuration -sdk iphoneos \
                OBJROOT=$build_path TARGET_BUILD_DIR=$build_path LIBRARY_SEARCH_PATHS=$build_path \
                CODE_SIGN_IDENTITY="$code_sign" PROVISIONING_PROFILE="$uuid"
        else
            /usr/bin/xcodebuild \
                -workspace $project_name.xcworkspace -scheme $scheme \
                -configuration $configuration -sdk iphoneos \
                OBJROOT=$build_path TARGET_BUILD_DIR=$build_path LIBRARY_SEARCH_PATHS=$build_path \
                CODE_SIGN_IDENTITY="$code_sign" PROVISIONING_PROFILE="$uuid"
        fi
    
        ReturnCode=$(echo $?)
        if ([[ $ReturnCode == "0" ]] && [[ -d "$build_path/$project_name.app" ]]); then
            echo_tip "编译成功!"
        else
            echo_error "编译失败,请修改后重试!"
            exit 1
        fi
    }
    
    function package() {
        # 删除旧的ipa文件
        if [[ -f "$ipa_file_path/$ipa_file_name" ]]; then
            rm -f "$ipa_file_path/$ipa_file_name"
        fi
    
        /usr/bin/xcrun -sdk iphoneos \
            PackageApplication \
                -v $build_path/$project_name.app \
                -o $ipa_file_path/$ipa_file_name \
                --sign "$code_sign" \
                --embed "$provision_profile_path"
    
        rm -rf $build_path
    
        if [[ -f "$ipa_file_path/$ipa_file_name" ]]; then
            echo_tip "打包成功!"
        else
            echo_error "打包失败,请修改后重试!"
            exit 1
        fi
    }
    
    prepare_build
    build
    package
    
    

    相关文章

      网友评论

          本文标题:iOS打包脚本改进版

          本文链接:https://www.haomeiwen.com/subject/vgdndttx.html