-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautorelease.sh
executable file
·171 lines (149 loc) · 4.72 KB
/
autorelease.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
161
162
163
164
165
166
167
168
169
170
171
#!/bin/bash
green="\033[0;32m"
magenta="\033[0;35m"
cyan="\033[0;36m"
bold_cyan="\033[1;36m"
yellow="\033[0;33m"
red="\033[0;31m"
blue="\033[0;34m"
reset="\033[0m"
clearline="\033[1K\r"
source_note_color="\033[36;40m"
script_file=$(echo "$0" | grep -oE "[a-zA-Z0-9_]+.sh$")
start_time=$SECONDS
function wrapup-previous-timed() {
units="seconds "
elapsed=$(( SECONDS - start_time ))
char_width=${#elapsed}
padding=$(( 4 - char_width ))
control_char="s"
bar="$script_file %-$padding$control_char$elapsed $units"
echo -ne "$clearline"
printf "$source_note_color[$bar]$reset $SAVED_MESSAGE\n"
unset TIME_NEXT
}
function logstyle-printf() {
if [[ "$TIME_NEXT" == "1" ]];
then
wrapup-previous-timed
fi
units=" "
elapsed=""
char_width=${#elapsed}
padding=$(( 4 - char_width ))
control_char="s"
bar="$script_file %-$padding$control_char$elapsed $units"
if [[ "$2" == "timed-command" ]];
then
TIME_NEXT=1
start_time=$SECONDS
SAVED_MESSAGE="$1"
printf "$source_note_color[$bar]$reset $1"
else
printf "$source_note_color[$bar]$reset $1\n"
fi
}
current_branch=$(git branch | grep '^* ')
if [[ "$current_branch" != "* main" ]]; then
logstyle-printf "$red""Do autoreleasing from branch main.$reset\n"
exit
fi
release_to_branch="release"
FOUND_VERSION_CHANGE=0
FOUND_ANOTHER_CHANGE=0
status=$(git status -s |
{
while IFS= read -r line
do
is_modified_file=$(echo "$line" | grep -oE '^ ?M')
if [[ "$is_modified_file" == ' M' || "$is_modified_file" == 'M' ]]; then
if [[ "$line" == ' M coincidencetest/version.txt' ]]; then
FOUND_VERSION_CHANGE=1
else
FOUND_ANOTHER_CHANGE=1
fi
fi
is_added_file=$(echo "$line" | grep -oE '^A[M ] ')
if [[ "$is_added_file" != "" ]]; then
FOUND_ANOTHER_CHANGE=1
fi
done
if [[ ( "$FOUND_VERSION_CHANGE" == "1" ) && ( "$FOUND_ANOTHER_CHANGE" == "1" ) ]]; then
echo "11"
fi
if [[ ( "$FOUND_VERSION_CHANGE" == "0" ) ]]; then
echo "0x"
fi
})
if [[ "$status" == "11" ]];
then
logstyle-printf "$red""Version has changed, but found another change. Not ready to autorelease.$reset"
exit
fi
if [[ "$status" == "0x" ]];
then
logstyle-printf "$red""Version has not changed, so not ready to autorelease.$reset"
logstyle-printf "$yellow""Maybe you need one last commit?$reset"
exit
fi
logstyle-printf "$green""Ready to autorelease: Version is updated, and everything else under version control is unmodified.$reset"
logstyle-printf "$green""Building package.$reset" timed-command
if test -d 'dist'; then
for file in dist/*;
do
if test -f "$file";
then
rm $file
fi
done
fi
python3 -m build 1>/dev/null
logstyle-printf "$green""Built:$reset"
for f in dist/*;
do
logstyle-printf "$yellow"" $f$reset"
done
installed=$(pip3 freeze | grep coincidencetest | wc -l)
if [[ "$installed" == "1" ]]; then
pip3 uninstall -y coincidencetest 1>/dev/null 2>/dev/null
fi
function clean_exit() {
deactivate
rm -rf venv/
exit
}
logstyle-printf "$green""Installing wheel into virtual environment.$reset" timed-command
python3 -m venv venv
source venv/bin/activate
for wheel in dist/*.whl;
do
pip install $wheel 1>/dev/null 2>/dev/null
done
pip install pytest 1>/dev/null 2>/dev/null
logstyle-printf "$green""Running unit tests.$reset" timed-command
cd tests/
outcome=$(python -m pytest -q . | tail -n1 | grep "[0-9]\+ \(failed\|errors\)")
if [[ ! "$outcome" == "" ]]; then
logstyle-printf "$red""Something went wrong in unit tests.$reset"
clean_exit
fi
cd ..
deactivate
rm -rf venv/
version=$(cat coincidencetest/version.txt)
logstyle-printf "$green""Committing this version:$reset$bold_cyan v$version$reset" timed-command
git add coincidencetest/version.txt 1>/dev/null 2>/dev/null && \
git commit -m "Autoreleasing v$version" 1>/dev/null 2>/dev/null && \
git tag v$version 1>/dev/null 2>/dev/null && \
git push 1>/dev/null 2>/dev/null && \
git push origin v$version 1>/dev/null 2>/dev/null && \
logstyle-printf "$green""Pushed ""$bold_cyan""v$version$reset$green on remote branch$reset$yellow main$reset$green ." && \
logstyle-printf "$green""Migrating updates to $reset$yellow$release_to_branch$reset$green branch.$reset" && \
rm coincidencetest/version.txt && \
git checkout $release_to_branch 1>/dev/null 2>/dev/null && \
git merge main 1>/dev/null 2>/dev/null && \
git push 1>/dev/null 2>/dev/null && \
git checkout main 1>/dev/null 2>/dev/null && \
logstyle-printf "$green""Uploading to PyPI.$reset" timed-command && \
python3 -m twine upload --repository coincidencetest dist/* && \
logstyle-printf "$green""Done.$reset"