美文网首页
Shell常用小方法

Shell常用小方法

作者: 桃李不言的蹊 | 来源:发表于2022-12-01 14:18 被阅读0次
    #去除换行 和 空格
    function trimWhiteSpaceAndNewLine() {
        TEXT=$1
        #先替换空格
        result=`echo ${TEXT//' '/''}`
        #在替换换行
        echo ${result//'\n'/''}
    }
    
    
    # 大写转小写
    function toLowerString(){
        lower_str=$(echo ${1} | tr '[A-Z]' '[a-z]')
        echo ${lower_str}
    }
    
    #小写转大写
    function toUperString(){
        upper_str=$(echo ${1} | tr '[a-z]' '[A-Z]')
        echo ${upper_str}
    }
    
    # 是否包含字符串  忽略大小写
    function containStrIgoreLower(){
    #大写转小写
    lower_str=$(toLowerString $2)
    #判断是否包含adhoc
    grepResult=$(echo $1 | grep "${lower_str}")
    if [[ "$grepResult" != "" ]]
    then
        echo "1"
        # return 1
    else
        echo "0"
        # return 0
    fi
    }
    
    
    ##json解析函数
    function jsonParse() { # $1 $2  json lable
         JSON_CONTENT=$1
         KEY='"'$2'":'
         echo ${JSON_CONTENT} | awk -F  ${KEY}  '{print $2}' | awk -F '"' '{print $2}'
    }
    
    
    
    
    
    
    
    #下面是一个iOS脚本打包上传蒲公英的自动化脚本
    #使用方法
    if [ ! -d ./IPADir ];
    then
    mkdir -p IPADir;
    fi
    
    #工程绝对路径
    project_path=/Users/liubin/Desktop/svn/landlord
    echo "环境:"
    read envir_desc
    echo "更新内容:"
    read update_desc
    
    # 反引号 命令替换
    branch=`cat $project_path/.git/HEAD` 
         # 分支名
         sub_branch=${branch: 16}
         # 打印
         echo "$sub_branch "
    
    # filelist=`ls $dir_path`
    # for file in $filelist
    # do
    #     echo "$file"
    #  # 检查目录和git目录是否存在
    #  if [[ -d $file ]]; then
    #     echo "111  $file"
    #      branch=`cat $dir_path/.git/.git/HEAD`
    #      # 分支名
    #      sub_branch=${branch: 16}
    #      # 打印
    #      echo "$sub_branch    $file"
    #  fi
    # done
    
    #去除换行  和  空格
    function trimWhiteSpaceAndNewLine() {
        TEXT=$1
        #先替换空格
        result=`echo ${TEXT//' '/''}`
        #在替换换行
        echo ${result//'\n'/''}
    }
    
    
    # 大写转小写
    function toLowerString(){
        lower_str=$(echo ${1} | tr '[A-Z]' '[a-z]')
        echo ${lower_str}
    }
    
    #小写转大写
    function toUperString(){
        upper_str=$(echo ${1} | tr '[a-z]' '[A-Z]')
        echo ${upper_str}
    }
    
    # 是否包含字符串  忽略大小写
    function containStrIgoreLower(){
    #大写转小写
    lower_str=$(toLowerString $2)
    #判断是否包含adhoc
    grepResult=$(echo $1 | grep "${lower_str}")
    if [[ "$grepResult" != "" ]]
    then
        echo "1"
        # return 1
    else
        echo "0"
        # return 0
    fi
    
    }
    
    
    
    #工程名
    project_name=Baletoo_landlord
    #scheme名
    scheme_name=Baletoo_landlord
    product_name=Baletoo_landlord
    
    #打包模式 Debug/Release
    development_mode=Debug
    
    #蒲公英参数
    user_key=******
    api_key=******
    
    current_path=$(cd `dirname $0`; pwd)
    
    #build文件夹路径
    build_path=${current_path}/build
    
    #plist文件所在路径
    exportOptionsPlistPath=${current_path}/exportDebug.plist
    
    isAdHocPackage=$(containStrIgoreLower "adhoc" "${envir_desc}")
    isReleasePackage=$(containStrIgoreLower "release" "${envir_desc}")
    if [[ $isAdHocPackage == 1 ]]; then
        development_mode=Release
    #elif 后面也要跟then
    elif [[ isReleasePackage == 1 ]]; then
        exportOptionsPlistPath=Release
        development_mode=Release
    else
        exportOptionsPlistPath=Debug
    fi
    #导出.ipa文件所在路径
    exportIpaPath=${current_path}/IPADir/${development_mode}
    
    
    
    
    ##json解析函数
    function jsonParse() { # $1 $2  json lable
    
         JSON_CONTENT=$1
         KEY='"'$2'":'
    
         echo ${JSON_CONTENT} | awk -F  ${KEY}  '{print $2}' | awk -F '"' '{print $2}'
    }
    
    ##删除斜杠'\'
    function trimSlash() {
        TEXT=$1
        echo ${TEXT//'\'/''}
    }
    
    
    echo "第一步,进入项目工程文件"
    cd $project_path
    echo '正在清理工程'
    xcodebuild \
    clean -configuration ${development_mode} -quiet  || exit
    echo '清理完成'
    echo '路径:' ${project_path}/${project_name}.xcworkspace
    echo '正在编译工程:'${development_mode}
    
    
    xcodebuild \
    archive -workspace ${project_path}/${project_name}.xcworkspace \
    -scheme ${scheme_name} \
    -configuration ${development_mode} \
    -archivePath ${build_path}/${project_name}.xcarchive -sdk iphoneos  -quiet  || exit
    
    
    #找到打包签名参数的plist文件
    # echo "isAdHocPackage is ${isAdHocPackage}"
    # echo "isReleasePackage is ${isReleasePackage}"
    
    if [[ $isAdHocPackage == 1 ]]; then
        exportOptionsPlistPath=${current_path}/exportAdHoc.plist
    #elif 后面也要跟then
    elif [[ isReleasePackage == 1 ]]; then
        exportOptionsPlistPath=${current_path}/exportRelease.plist
    else
        exportOptionsPlistPath=${current_path}/exportDebug.plist
    fi
    echo "exportPlistPath is ${exportPlistPathTmp}"
    echo '编译完成'
    echo '开始ipa打包'
    echo "=============================="
    echo ${build_path}/${project_name}.xcarchive
    echo ${development_mode}
    echo ${exportIpaPath}
    echo ${exportOptionsPlistPath}
    echo "=============================="
    
    xcodebuild -exportArchive -archivePath ${build_path}/${project_name}.xcarchive \
    -configuration ${development_mode} \
    -exportPath ${exportIpaPath} \
    -exportOptionsPlist ${exportOptionsPlistPath} \
    -quiet || exit
    
    if [ -e $exportIpaPath/$product_name.ipa ]; then
    echo 'ipa包已导出'
    
        echo '发布ipa包到 =============蒲公英平台============='
        RESPONSE=$(curl -F "file=@$exportIpaPath/$product_name.ipa" -F "uKey=${user_key}" -F "_api_key=${api_key}" -F "updateDescription=${update_desc}" https://www.pgyer.com/apiv2/app/upload)
    
        if [ $? -eq 0 ];then
        echo "=============提交蒲公英成功 ============="
    
        appQRCodeURL=$(trimSlash $(jsonParse "${RESPONSE}" "buildQRCodeURL"))
        appVersion=$(jsonParse "${RESPONSE}" "buildVersion")
        appBuildVersion=$(jsonParse "${RESPONSE}" "buildBuildVersion")
    
        let gitBranchName = shell("git rev-parse --abbrev-ref HEAD")
        .trimmingCharacters(in: .newlines)
    
        #通知到钉钉群 将xxxxxxxx替换为真实access_token `title`需要包含关键词
        curl 'https://oapi.dingtalk.com/robot/send?access_token=***********' \
            -H 'Content-Type: application/json' \
            -d '
        {
            "msgtype": "markdown",
            "markdown": {
                "title":"打包好了@**",
                "text":"![screenshot]('"$appQRCodeURL"')  \n  **版本:** '"$appVersion"' (build '"$appBuildVersion"')  \n  **环境:** '"$envir_desc"'  \n  **说明:** '"$update_desc"' \n **分支:** '"$sub_branch"'"
            }
        }'
        else
        echo "=============提交蒲公英失败 ============="
        fi
    #open $exportIpaPath
    else
    echo 'ipa包导出失败 '
    fi
    echo '打包ipa完成  '
    exit 0
    
    
    

    相关文章

      网友评论

          本文标题:Shell常用小方法

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