-
Notifications
You must be signed in to change notification settings - Fork 80
/
release.sh
executable file
·161 lines (136 loc) · 5.29 KB
/
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env bash
##
## Versioning info: [major].[minor].[patch][-RC[num]], example: 3.1.159, 3.1.159-RC1
##
## IMPORTANT: requires gh and git-extras commands installed
##
# TODO: verificar porque só pega versões superiores (3.1.200 ao invés de 3.1.200-RC9)
# VERSION=`git describe --tags --abbrev=0`
VERSION_PATTERN='([0-9]+)\.([0-9]+)\.([0-9]+)(-RC[0-9]+)?'
SED_AWKWARD_PATTERN="[0-9]+\.[0-9]+\.[0-9]+(-RC[0-9]+){0,1}"
# Define colors
green_color='\033[0;32m'
red_color='\033[0;31m'
reset_color='\033[0m'
LATEST_VERSION=$(git tag | egrep $VERSION_PATTERN | sort --version-sort -r | head -1)
MAJOR_VERSION=$(echo $LATEST_VERSION | cut -d"-" -f1)
MAJOR_TAG_CREATED=$(git tag | egrep $MAJOR_VERSION"$")
if [ -n "$MAJOR_TAG_CREATED" ]; then
LATEST_VERSION=$MAJOR_VERSION
fi
IS_RC=$(echo $LATEST_VERSION | egrep '(-RC)')
LAST_DIGIT=`echo $MAJOR_VERSION | cut -f 3 -d '.'`
MAIN_REV=`echo $MAJOR_VERSION | cut -f 1,2 -d '.'`
NEXT_NUMBER=$(($LAST_DIGIT + 1))
NEXT_VERSION=$MAIN_REV'.'$NEXT_NUMBER
FINAL_VERSION=
function change_files {
# TODO: figure out better way of getting latest version
OLD_VERSION=$(grep -E 'interlegis/sapl:'$VERSION_PATTERN docker/docker-compose.yaml | cut -d':' -f3)
echo "Updating from "$OLD_VERSION" to "$FINAL_VERSION""
if [[ "$OSTYPE" == "darwin"* ]]; then
# MacOS (BSD sed)
sed -E -i "" "s|$OLD_VERSION|$FINAL_VERSION|g" docker/docker-compose.yaml
sed -E -i "" "s|$OLD_VERSION|$FINAL_VERSION|g" setup.py
sed -E -i "" "s|$OLD_VERSION|$FINAL_VERSION|g" sapl/templates/base.html
sed -E -i "" "s|$OLD_VERSION|$FINAL_VERSION|g" sapl/settings.py
else
# Linux (GNU sed)
sed -i -E "s|$OLD_VERSION|$FINAL_VERSION|g" docker/docker-compose.yaml
sed -i -E "s|$OLD_VERSION|$FINAL_VERSION|g" setup.py
sed -i -E "s|$OLD_VERSION|$FINAL_VERSION|g" sapl/templates/base.html
sed -i -E "s|$OLD_VERSION|$FINAL_VERSION|g" sapl/settings.py
fi
}
function set_major_version {
if [ -z "$IS_RC" ] || [ -n "$MAJOR_TAG_CREATED" ]; then
FINAL_VERSION=$NEXT_VERSION
else
FINAL_VERSION=$MAJOR_VERSION
fi
}
function set_rc_version {
if [ -z "$IS_RC" ]; then
NEXT_RC_VERSION=$NEXT_VERSION"-RC0"
else
LAST_RC_DIGIT=$(echo $LATEST_VERSION | rev | cut -d"-" -f1 | rev | sed s/RC//)
NEXT_RC_NUMBER=$(($LAST_RC_DIGIT + 1))
NEXT_RC_VERSION=$(echo $LATEST_VERSION | cut -d"-" -f1)'-RC'$NEXT_RC_NUMBER
fi
FINAL_VERSION=$NEXT_RC_VERSION
## DEBUG
# echo "OLD_VERSION: $OLD_VERSION"
# echo "FINAL_VERSION: $FINAL_VERSION"
}
# Function to display Yes/No prompt with colored message
prompt_yes_no() {
while true; do
echo -e "${green_color}$1 (y/n): ${reset_color}\c"
read answer
case $answer in
[Yy]* ) return 0;;
[Nn]* ) return 1;;
* ) echo -e "${red_color}Please answer 'yes' or 'no'.${reset_color}";;
esac
done
}
function commit_and_push {
echo -e "${green_color}Committing new release $FINAL_VERSION...${color_reset}"
git add docker/docker-compose.yaml setup.py sapl/settings.py sapl/templates/base.html
git changelog --tag $FINAL_VERSION --prune-old -x > latest_changes.md
cat latest_changes.md CHANGES.md > CHANGES.tmp
mv CHANGES.tmp CHANGES.md
git add CHANGES.md
rm latest_changes.md
if prompt_yes_no "${green_color}Do you want to commit SAPL $FINAL_VERSION release locally?${reset_color}"; then
git commit -m "Release: $FINAL_VERSION"
git tag $FINAL_VERSION
echo -e "${green_color}Commit and tag created locally!${color_reset}"
else
git reset --hard HEAD
echo -e "${red_color}Aborting release creation!${color_reset}"
return
fi
echo -e "${red_color}### BEFORE PROCEEDING, MAKE SURE THE NEW VERSION NUMBER AND CHANGES ARE CORRECT!${color_reset}"
echo -e "${green_color}Release: $FINAL_VERSION${reset_color}"
if prompt_yes_no "${green_color}Do you want to publish SAPL $FINAL_VERSION release on Github?${reset_color}"; then
echo -e "${green_color}Publishing $FINAL_VERSION on Github...${reset_color}"
current_date=$(date +%Y-%m-%d)
git push origin 3.1.x
gh release create $FINAL_VERSION --repo interlegis/sapl --title "Release: $FINAL_VERSION" --notes "Release notes for $FINAL_VERSION in CHANGES.md file. Release date: $current_date"
echo -e "${green_color}Done.${reset_color}"
else
echo -e "${red_color}Publishing aborted.${reset_color}"
fi
echo "${green_color}Done.${green_color}"
echo -e "${green_color}================================================================================${color_reset}"
}
case "$1" in
--latest)
git fetch
echo -e "${green_color}$LATEST_VERSION${reset_color}"
exit 0
;;
--major)
git fetch
set_major_version
echo -e "${green_color}Creating MAJOR release: "$FINAL_VERSION"${reset_color}"
# git tag $FINAL_VERSION
change_files
commit_and_push
exit 0
;;
--rc)
git fetch
set_rc_version
echo -e "${green_color}Creating RELEASE CANDIDATE (RC): "$FINAL_VERSION"${reset_color}"
# git tag $FINAL_VERSION
change_files
commit_and_push
exit 0
;;
--top)
git tag | sort --version-sort -r | head "-$2"
exit 0
;;
esac