本地app文件打包为ipa并上传蒲公英发钉钉通知,shell的json解析

#!/bin/bash

### 方法简要说明:
### 1. 是先查找一个字符串:带双引号的key。如果没找到,则直接返回defaultValue。
### 2. 查找最近的冒号,找到后认为值的部分开始了,直到在层数上等于0时找到这3个字符:,}]。
### 3. 如果有多个同名key,则依次全部打印(不论层级,只按出现顺序)
### @author lux feary
###
### 3 params: json, key, defaultValue
function getJsonValuesByAwk() {
    awk -v json="$1" -v key="$2" -v defaultValue="$3" 'BEGIN{
        foundKeyCount = 0
        while (length(json) > 0) {
            # pos = index(json, "\""key"\""); ## 这行更快一些,但是如果有value是字符串,且刚好与要查找的key相同,会被误认为是key而导致值获取错误
            pos = match(json, "\""key"\"[ \\t]*?:[ \\t]*");
            if (pos == 0) {if (foundKeyCount == 0) {print defaultValue;} exit 0;}

            ++foundKeyCount;
            start = 0; stop = 0; layer = 0;
            for (i = pos + length(key) + 1; i <= length(json); ++i) {
                lastChar = substr(json, i - 1, 1)
                currChar = substr(json, i, 1)

                if (start <= 0) {
                    if (lastChar == ":") {
                        start = currChar == " " ? i + 1: i;
                        if (currChar == "{" || currChar == "[") {
                            layer = 1;
                        }
                    }
                } else {
                    if (currChar == "{" || currChar == "[") {
                        ++layer;
                    }
                    if (currChar == "}" || currChar == "]") {
                        --layer;
                    }
                    if ((currChar == "," || currChar == "}" || currChar == "]") && layer <= 0) {
                        stop = currChar == "," ? i : i + 1 + layer;
                        break;
                    }
                }
            }

            if (start <= 0 || stop <= 0 || start > length(json) || stop > length(json) || start >= stop) {
                if (foundKeyCount == 0) {print defaultValue;} exit 0;
            } else {
                print substr(json, start, stop - start);
            }

            json = substr(json, stop + 1, length(json) - stop)
        }
    }'
}

app_path=$1
cd $app_path

if [ ! -d "XXX.app" ];then
    echo "XXX.app 不存在"
    exit
fi
rm -rf Payload/
rm -f Payload.ipa
mkdir Payload

mv -f XXX.app Payload/

zip -qr Payload.zip Payload/*
mv Payload.zip Payload.ipa

upload_result=$(curl -X POST -H 'Content-Type: multipart/form-data' -F '[email protected]' -F '_api_key=XXX' https://www.pgyer.com/apiv2/app/upload)

qrcode=$(getJsonValuesByAwk "$upload_result" "buildQRCodeURL" "defaultValue")
qrcode=$(sed -e 's/^"//' -e 's/"$//' <<<"$qrcode")

screenshot="![screenshot](${qrcode})"
curl 'https://oapi.dingtalk.com/robot/send?access_token=XXX' \
   -H 'Content-Type: application/json' \
   -d '{
     "msgtype": "markdown",
     "markdown": {
         "title":"构建成功,本地ipa文件上传成功",
         "text": "### 本地ipa文件上传成功\n'${screenshot}'"
     }
 }'

你可能感兴趣的:(本地app文件打包为ipa并上传蒲公英发钉钉通知,shell的json解析)