|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +vercomp () { |
| 4 | + if [[ $1 == $2 ]] |
| 5 | + then |
| 6 | + return 0 |
| 7 | + fi |
| 8 | + local IFS=. |
| 9 | + local i ver1=($1) ver2=($2) |
| 10 | + # fill empty fields in ver1 with zeros |
| 11 | + for ((i=${#ver1[@]}; i<${#ver2[@]}; i++)) |
| 12 | + do |
| 13 | + ver1[i]=0 |
| 14 | + done |
| 15 | + for ((i=0; i<${#ver1[@]}; i++)) |
| 16 | + do |
| 17 | + if [[ -z ${ver2[i]} ]] |
| 18 | + then |
| 19 | + # fill empty fields in ver2 with zeros |
| 20 | + ver2[i]=0 |
| 21 | + fi |
| 22 | + if ((10#${ver1[i]} > 10#${ver2[i]})) |
| 23 | + then |
| 24 | + return 1 |
| 25 | + fi |
| 26 | + if ((10#${ver1[i]} < 10#${ver2[i]})) |
| 27 | + then |
| 28 | + return 2 |
| 29 | + fi |
| 30 | + done |
| 31 | + return 0 |
| 32 | +} |
| 33 | + |
| 34 | +# check if `gh` tool is installed. |
| 35 | +if ! command -v gh &> /dev/null |
| 36 | +then |
| 37 | + echo "Github CLI tool is required, but could not be found." |
| 38 | + echo "Install it via: $ brew install gh" |
| 39 | + exit 1 |
| 40 | +fi |
| 41 | + |
| 42 | +# check that we're on the `main` branch |
| 43 | +branch=$(git rev-parse --abbrev-ref HEAD) |
| 44 | +if [ branch != 'main' ] |
| 45 | +then |
| 46 | + echo "The 'main' branch must be the current branch out to make a release." |
| 47 | + echo "You are currently on: $branch" |
| 48 | + exit 1 |
| 49 | +fi |
| 50 | + |
| 51 | +versionFile="./sources/Segment/Version.swift" |
| 52 | + |
| 53 | +# get last line in version.swift |
| 54 | +versionLine=$(tail -n 1 $versionFile) |
| 55 | +# split at the = |
| 56 | +version=$(cut -d "=" -f2- <<< "$versionLine") |
| 57 | +# remove quotes and spaces |
| 58 | +version=$(sed "s/[' \"]//g" <<< "$version") |
| 59 | + |
| 60 | +echo "Analytics-Swift current version: $version" |
| 61 | + |
| 62 | +# no args, so give usage. |
| 63 | +if [ $# -eq 0 ] |
| 64 | +then |
| 65 | + echo "Release automation script" |
| 66 | + echo "" |
| 67 | + echo "Usage: $ ./release.sh <version>" |
| 68 | + echo " ex: $ ./release.sh \"1.0.2\"" |
| 69 | + exit 0 |
| 70 | +fi |
| 71 | + |
| 72 | +newVersion="${1%.*}.$((${1##*.}))" |
| 73 | +echo "$version" |
| 74 | +echo "$newVersion" |
| 75 | + |
| 76 | +vercomp $newVersion $version |
| 77 | +case $? in |
| 78 | + 0) op='=';; |
| 79 | + 1) op='>';; |
| 80 | + 2) op='<';; |
| 81 | +esac |
| 82 | + |
| 83 | +if [ $op != '>' ] |
| 84 | +then |
| 85 | + echo "New version must be greater than previous version ($version)." |
| 86 | + exit 1 |
| 87 | +fi |
| 88 | + |
| 89 | +# get the commits since the last release... |
| 90 | +# note: we do this here so the "Version x.x.x" commit doesn't show up in logs. |
| 91 | +changelog=git log --pretty=format:"- (%an) %s" $(git describe --tags --abbrev=0 @^)..@ |
| 92 | +tempFile=$(mktemp) |
| 93 | +#write changelog to temp file. |
| 94 | +echo $changelog >> $tempFile |
| 95 | + |
| 96 | +# update sources/Segment/Version.swift |
| 97 | +# - remove last line... |
| 98 | +sed -i '' -e '$ d' $versionFile |
| 99 | +# - add new line w/ new version |
| 100 | +echo "internal let __segment_version = \"$newVersion\"" >> $versionFile |
| 101 | + |
| 102 | +# commit the version change. |
| 103 | +git commit -am "Version $newVersion" && git push |
| 104 | +# gh release will make both the tag and the release itself. |
| 105 | +gh release create $newVersion -F $tempFile -t "Version $newVersion" |
| 106 | + |
| 107 | +# remove the tempfile. |
| 108 | +rm $tempFile |
| 109 | + |
| 110 | + |
0 commit comments