|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +increment_version() { |
| 4 | + local usage=" USAGE: $FUNCNAME [-l] [-t] <version> [<position>] [<leftmost>] |
| 5 | + -l : remove leading zeros |
| 6 | + -t : drop trailing zeros |
| 7 | + <version> : The version string. |
| 8 | + <position> : Optional. The position (starting with one) of the number |
| 9 | + within <version> to increment. If the position does not |
| 10 | + exist, it will be created. Defaults to last position. |
| 11 | + <leftmost> : The leftmost position that can be incremented. If does not |
| 12 | + exist, position will be created. This right-padding will |
| 13 | + occur even to right of <position>, unless passed the -t flag." |
| 14 | + |
| 15 | + # Get flags. |
| 16 | + local flag_remove_leading_zeros=0 |
| 17 | + local flag_drop_trailing_zeros=0 |
| 18 | + while [ "${1:0:1}" == "-" ]; do |
| 19 | + if [ "$1" == "--" ]; then shift; break |
| 20 | + elif [ "$1" == "-l" ]; then flag_remove_leading_zeros=1 |
| 21 | + elif [ "$1" == "-t" ]; then flag_drop_trailing_zeros=1 |
| 22 | + else echo -e "Invalid flag: ${1}\n$usage"; return 1; fi |
| 23 | + shift; done |
| 24 | + |
| 25 | + # Get arguments. |
| 26 | + if [ ${#@} -lt 1 ]; then echo "$usage"; return 1; fi |
| 27 | + local v="${1}" # version string |
| 28 | + local targetPos=${2-last} # target position |
| 29 | + local minPos=${3-${2-0}} # minimum position |
| 30 | + |
| 31 | + # Split version string into array using its periods. |
| 32 | + local IFSbak; IFSbak=IFS; IFS='.' # IFS restored at end of func to |
| 33 | + read -ra v <<< "$v" # avoid breaking other scripts. |
| 34 | + |
| 35 | + # Determine target position. |
| 36 | + if [ "${targetPos}" == "last" ]; then |
| 37 | + if [ "${minPos}" == "last" ]; then minPos=0; fi |
| 38 | + targetPos=$((${#v[@]}>${minPos}?${#v[@]}:$minPos)); fi |
| 39 | + if [[ ! ${targetPos} -gt 0 ]]; then |
| 40 | + echo -e "Invalid position: '$targetPos'\n$usage"; return 1; fi |
| 41 | + (( targetPos-- )) || true # offset to match array index |
| 42 | + |
| 43 | + # Make sure minPosition exists. |
| 44 | + while [ ${#v[@]} -lt ${minPos} ]; do v+=("0"); done; |
| 45 | + |
| 46 | + # Increment target position. |
| 47 | + v[$targetPos]=`printf %0${#v[$targetPos]}d $((10#${v[$targetPos]}+1))`; |
| 48 | + |
| 49 | + # Remove leading zeros, if -l flag passed. |
| 50 | + if [ $flag_remove_leading_zeros == 1 ]; then |
| 51 | + for (( pos=0; $pos<${#v[@]}; pos++ )); do |
| 52 | + v[$pos]=$((${v[$pos]}*1)); done; fi |
| 53 | + |
| 54 | + # If targetPosition was not at end of array, reset following positions to |
| 55 | + # zero (or remove them if -t flag was passed). |
| 56 | + if [[ ${flag_drop_trailing_zeros} -eq "1" ]]; then |
| 57 | + for (( p=$((${#v[@]}-1)); $p>$targetPos; p-- )); do unset v[$p]; done |
| 58 | + else for (( p=$((${#v[@]}-1)); $p>$targetPos; p-- )); do v[$p]=0; done; fi |
| 59 | + |
| 60 | + echo "${v[*]}" |
| 61 | + IFS=IFSbak |
| 62 | + return 0 |
| 63 | +} |
| 64 | + |
| 65 | +#Generate new version |
| 66 | + |
| 67 | +currentVersionString=`grep -E 's.version.*=' JJException.podspec` |
| 68 | + |
| 69 | +VersionNumber=`tr -d [a-z][=\"] <<<"$currentVersionString"` |
| 70 | + |
| 71 | +currentVersion=`echo ${VersionNumber:3}` |
| 72 | + |
| 73 | +newVersion=`increment_version ${currentVersion} 3` |
| 74 | + |
| 75 | +lineNumber=`grep -nE 's.version.*=' JJException.podspec | cut -d : -f1` |
| 76 | + |
| 77 | +sed -i "" "${lineNumber}s/${currentVersion}/${newVersion}/g" JJException.podspec |
| 78 | + |
| 79 | +echo "Current version: ${currentVersion}, New version: ${newVersion}" |
| 80 | + |
| 81 | +#Push JJException.podspec |
| 82 | + |
| 83 | +git add JJException.podspec |
| 84 | +git commit -m ${newVersion} |
| 85 | + |
| 86 | +git tag ${newVersion} |
| 87 | +git push origin master --tags |
| 88 | +pod trunk push ./JJException.podspec --verbose --allow-warnings |
| 89 | + |
| 90 | + |
0 commit comments