#!/bin/bash
#read env
echo 玩命打包中....
echo '程序会自动npm prune,旧包将备份在build/backup文件夹下'
echo '在updage.json文件中可以定义模板,binary版本信息等'
echo '在update.json中,_androidPPK,_iOSPPK字符将会替换成打包后的文件名'
echo ''
backup(){
    if [ ! -d "build/backup" ];then
        mkdir build/backup
    fi
    zipFiles=`ls build/output/*.zip |wc -w`
    if [ "$zipFiles" -gt "0" ];then
        mv build/output/*.zip build/backup/
     fi
     otherFile=`ls build/output/* |wc -w`
     if [ "$otherFile" -gt "0" ];then
         rm build/output/*
     fi
}

bundle(){
    node package.js bundle $1
}



package(){
    currentPath=`pwd`;
    workPath=build/output
    content=`cat ./update.json`;
    cd $workPath;
    android=`ls android*.ppk`;
    ios=`ls ios*.ppk`;
    content=${content/'_iOSPPK'/$ios};
    content=${content/'_androidPPK'/$android};
    content=${content/'_buildBy'/`git config user.name`};
    content=${content/'_buildDate'/`date '+%Y-%m-%d-%H-%M-%S'`};
    cd $currentPath
    androidBinary=`cd android && ./gradlew echoVersionName -q`
    cd $workPath;
    content=${content/'_androidBinary'/$androidBinary};
    echo $content > update.json;
    zip -r `date '+%Y-%m-%d-%H时%M分%S秒'.zip` ./
    rm *.ppk *.json
    cd $currentPath;
    echo '打包完毕,将自动打开Finder...' 
}

backup && npm prune && bundle '--platform ios' && bundle '--platform android'
package && open build/output




