-
Notifications
You must be signed in to change notification settings - Fork 1
/
pre_release.sh
executable file
·104 lines (83 loc) · 2.65 KB
/
pre_release.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/bash
set -e
declare XTOOLS_TAG
help() {
printf "\n"
printf "Usage: %s [-t tag]\n" "$0"
printf "\t-t New XTools unreleased tag. Updates all submodule go.mod files with this tag.\n"
exit 1 # Exit script after printing help
}
while getopts "t:" opt
do
case "$opt" in
t ) XTOOLS_TAG="$OPTARG" ;;
? ) help ;; # Print help
esac
done
declare -r SEMVER_REGEX="^v(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(\\-[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?(\\+[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?$"
validate_tag() {
local tag_=$1
if [[ "${tag_}" =~ ${SEMVER_REGEX} ]]; then
printf "%s is valid semver tag.\n" "${tag_}"
else
printf "%s is not a valid semver tag.\n" "${tag_}"
return 1
fi
}
# Print help in case parameter is empty
if [[ -z "$XTOOLS_TAG" ]]
then
printf "parameter '-t' must be specified.\n"
help
fi
## Validate tags first
validate_tag "${XTOOLS_TAG}" || exit $?
TAG_FOUND=$(git tag --list "${XTOOLS_TAG}")
if [[ ${TAG_FOUND} = "${XTOOLS_TAG}" ]] ; then
printf "Tag %s already exists in this repo\n" "${XTOOLS_TAG}"
exit 1
fi
# Get version for version.go
XTOOLS_VERSION=$(echo "${XTOOLS_TAG}" | egrep -o "${SEMVER_REGEX}")
# Strip leading v
XTOOLS_VERSION="${XTOOLS_VERSION#v}"
cd "$(dirname "$0")"
if ! git diff --quiet; then \
printf "Working tree is not clean, can't proceed\n"
git status
git diff
exit 1
fi
# Update version.go version definition
cp version.go version.go.bak
sed "s/\(return \"\)[0-9]*\.[0-9]*\.[0-9]*\"/\1${XTOOLS_VERSION}\"/" ./version.go.bak > ./version.go
rm -f ./version.go.bak
declare -r BRANCH_NAME=pre_release_${XTOOLS_TAG}
patch_gomods() {
local pkg_=$1
local tag_=$2
# now do the same for all the directories underneath
PACKAGE_DIRS=$(find . -mindepth 2 -type f -name 'go.mod' -exec dirname {} \; | egrep -v 'tools' | sed 's|^\.\/||' | sort)
# quote any '.' characters in the pkg name
local quoted_pkg_=${pkg_//./\\.}
for dir in $PACKAGE_DIRS; do
cp "${dir}/go.mod" "${dir}/go.mod.bak"
sed "s|${quoted_pkg_}\([^ ]*\) v[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*[^0-9]*.*$|${pkg_}\1 ${tag_}|" "${dir}/go.mod.bak" >"${dir}/go.mod"
rm -f "${dir}/go.mod.bak"
done
}
# branch off from existing branch main
git checkout -b "${BRANCH_NAME}" main
if [ -n "${XTOOLS_TAG}" ]; then
patch_gomods github.com/gojekfarm/xtools "${XTOOLS_TAG}"
fi
# Run lint to update go.sum
make lint
# Add changes and commit.
git add .
make ci
declare COMMIT_MSG=""
COMMIT_MSG+="Releasing ${XTOOLS_TAG}"
git commit -m "${COMMIT_MSG}"
printf "Now run following to verify the changes.\ngit diff main\n"
printf "\nThen push the changes to upstream\n"