Skip to content

Commit 0c3d3fe

Browse files
bsneedBrandon Sneed
andauthored
Add release automation script. (#33)
* Add release automation script. * fixed casing. Co-authored-by: Brandon Sneed <[email protected]>
1 parent d5143fc commit 0c3d3fe

File tree

3 files changed

+123
-6
lines changed

3 files changed

+123
-6
lines changed

RELEASING.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
Releasing
22
=========
33

4-
1. Update the version in Sources/Version.swift
5-
2. `git commit -am "Version X.Y.Z"`
6-
3. `git tag -a X.Y.Z -m "Version X.Y.Z"`
7-
4. `git push && git push --tags`
8-
5. Create a new github release at https://github.com/segmentio/analytics-swift/releases
9-
* Summarize change history since last release in the notes.
4+
Use `release.sh` to perform releases. This script will perform all the safety checks as well
5+
as update Version.swfit, commit the change, and create tag + release. History since the last
6+
released version will be used as the changelog for the release.
7+
8+
ex: $ ./release.sh 1.1.1
9+

Sources/Segment/Version.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,12 @@
77

88
// Referred to by Analytics.swift
99

10+
// DO NOT MODIFY THIS FILE BY HAND!!
11+
// DO NOT MODIFY THIS FILE BY HAND!!
12+
// DO NOT MODIFY THIS FILE BY HAND!!
13+
// DO NOT MODIFY THIS FILE BY HAND!!
14+
15+
// Use release.sh's automation.
16+
1017
// BREAKING.FEATURE.FIX
1118
internal let __segment_version = "1.0.1"

release.sh

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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

Comments
 (0)