-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublish.sh
More file actions
311 lines (271 loc) · 8.45 KB
/
publish.sh
File metadata and controls
311 lines (271 loc) · 8.45 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/bin/bash
# Default values
COMMAND="publish"
BRANCH=""
COMMIT=""
MESSAGE=""
VERSION="auto"
BUMP_TYPE="patch"
PREVIEW="auto"
ARCH="win-x64"
UNATTENDED="false"
# Allowed values
_command_values=("publish" "package" "release")
_bump_type_values=("major" "minor" "patch")
_preview_values=("auto" "true" "false")
_arch_values=("win-x64" "linux-x64")
_unattended_values=("true" "false")
# Colors
Color_Off='\033[0m'
Color_Green='\033[0;32m'
Color_Yellow='\033[0;33m'
function main() {
read_args "$@"
case "$COMMAND" in
"publish") publish ;;
"package") package ;;
"release") release ;;
esac
}
function publish() {
rm -rf src/bin/ src/obj/
dotnet publish src -c Release -r $ARCH --self-contained -p:AssemblyVersion="$(echo $VERSION | sed 's/-preview//')" -p:Version="$VERSION" -o ./publish/$ARCH
if [[ ! $? -eq 0 ]]; then exit 1; fi # exit if build failed or canceled
echo
echo -e "${Color_Green}Publish OK${Color_Off}"
}
function package() {
local publish_path="publish/$ARCH"
local assembly_name="2fa"
local assembly_ext=""
local archive_ext=".zip"
case "$ARCH" in
win*) assembly_ext=".exe" ;;
*) archive_ext=".tar.gz" ;;
esac
local release_path="release/$ARCH"
local archive_name="${assembly_name}_${VERSION}_${ARCH}${archive_ext}"
local archive_path="$release_path/../$archive_name"
local assembly_full="${assembly_name}${assembly_ext}"
mkdir -p $release_path
cp $publish_path/$assembly_name$assembly_ext $release_path
tar -C "$release_path/" -a -c -f "$archive_path" "$assembly_full"
local sha256=$(sha256sum$assembly_ext "$archive_path" | cut -d " " -f 1)
echo "$sha256 $archive_name" >> $release_path/../checksums.txt
echo -e "${Color_Green}Package OK${Color_Off}"
}
function release() {
if [ ! $UNATTENDED = "true" ]
then
read -p "Do you want to continue to git commit, tag, push...? (y/n) " yn
if [ ! $yn = "y" ]; then exit; fi
fi
git checkout release && git pull || git switch -c release origin/release
local url="https://github.com/jurakovic/2fa-cli/releases/tag/v$VERSION"
echo "VERSION: $VERSION" > version
echo "BUMP_TYPE: $BUMP_TYPE" >> version
echo "PREVIEW: $PREVIEW" >> version
echo "BRANCH: $BRANCH" >> version
echo "COMMIT: $COMMIT" >> version
echo "URL: $url" >> version
git add version
git commit -m "v$VERSION ($BRANCH)"
git push
git checkout "$BRANCH"
git tag "v$VERSION"
git push origin "v$VERSION"
echo -e "${Color_Yellow}Version $VERSION released${Color_Off}"
echo -e "${Color_Green}All done${Color_Off}"
}
function read_args() {
while [ "${1:-}" != "" ]; do
case "$1" in
"-h" | "--help")
help
exit 0
;;
"-v" | "--version")
shift
# todo: regex validate
VERSION="$1"
;;
"-b" | "--bump_type")
shift
if is_in_array "$1" "${_bump_type_values[@]}"; then
BUMP_TYPE="$1"
else
invalid_argument "$1" "${_bump_type_values[*]}"
exit 1
fi
;;
"-p" | "--preview")
shift
if is_in_array "$1" "${_preview_values[@]}"; then
PREVIEW="$1"
else
invalid_argument "$1" "${_preview_values[*]}"
exit 1
fi
;;
"-a" | "--arch")
shift
if is_in_array "$1" "${_arch_values[@]}"; then
ARCH="$1"
else
invalid_argument "$1" "${_arch_values[*]}"
exit 1
fi
;;
"-u" | "--unattended")
shift
if is_in_array "$1" "${_unattended_values[@]}"; then
UNATTENDED="$1"
else
invalid_argument "$1" "${_unattended_values[*]}"
exit 1
fi
;;
*)
if is_in_array "$1" "${_command_values[@]}"; then
COMMAND="$1"
else
invalid_argument "$1" "${_command_values[*]}"
exit 1
fi
;;
esac
shift
done
BRANCH=$(git branch --show-current)
COMMIT=$(git log -n 1 --format="%H")
MESSAGE=$(git log -n 1 --format="%B")
if [ "$PREVIEW" = "auto" ]; then
if [ "$BRANCH" != "master" ]; then
PREVIEW="true"
else
PREVIEW="false"
fi
fi
if [ "$VERSION" = "auto" ]; then
local previous=$(git show origin/release:version >/dev/null 2>&1 || echo '0.0.0' | head -n 1 | sed 's/VERSION: //')
echo "PREV_VER: $previous"
VERSION=$(bump_version "$previous" "$BUMP_TYPE" "$PREVIEW")
else
BUMP_TYPE=""
PREVIEW=""
fi
echo "COMMAND: $COMMAND"
echo "VERSION: $VERSION"
echo "BUMP_TYPE: $BUMP_TYPE"
echo "PREVIEW: $PREVIEW"
echo "ARCH: $ARCH"
echo "BRANCH: $BRANCH"
echo "COMMIT: $COMMIT"
echo "MESSAGE: $MESSAGE"
if [ $UNATTENDED = "true" ]
then
for i in {3..0..1}; do echo -en "\rContinue in $i" && if [ "$i" -gt "0" ]; then sleep 1; fi; done
echo -en "\r "
else
read -p "Press any key to continue..." -n1 -s; echo
fi
echo
}
function help() {
echo "Usage:"
echo " ./publish.sh <command> [options]"
echo ""
echo "Commands:"
echo " publish Step 1: run dotnet publish"
echo " package Step 2: archive executable to zip or tarball"
echo " release Step 3: execute git commit, tag, push..."
echo ""
echo "Options: Allowed values:"
echo " -v, --version <VERSION> auto*, x.y.z"
echo " -b, --bump_type <BUMP_TYPE> major, minor, patch*"
echo " -p, --preview <PREVIEW> auto*, true, false"
echo " -a, --arch <ARCH> win-x64*, linux-x64"
echo " -u, --unattended <UNATTENDED> true, false*"
echo " -h, --help"
echo " * Default value"
echo ""
echo "Examples:"
echo " ./publish.sh publish"
echo " ./publish.sh publish -v 1.2.3"
echo " ./publish.sh publish -b major -p true"
}
function is_in_array() {
local value="$1"
shift
for element in "$@"; do
if [[ "$element" == "$value" ]]; then
return 0
fi
done
return 1
}
function invalid_argument() {
echo "Error. Invalid argument value: $1"
echo "Allowed values: ${*:2}"
echo
help
}
function bump_version() {
local current_version="$1"
local bump_type="$2"
local preview="$3"
IFS='-' read -ra chunks <<< $(echo $current_version | sed 's/-preview./-/')
local current_main_version="${chunks[0]}"
local current_prev_version="${chunks[1]:-0}"
local next_version=""
if [[ "$preview" == "false" ]]; then
if [[ "$current_prev_version" -eq 0 ]]; then
# Regular bump
next_version=$(bump_version_main "$current_main_version" "$bump_type")
else
# Unpreview
next_version="$current_main_version"
fi
else
if [[ "$current_prev_version" -eq 0 ]]; then
# First preview
next_version=$(bump_version_main "$current_main_version" "$bump_type")
next_version+="-preview.1"
else
# Bumping existing preview
next_version="$current_main_version"
next_version+="-preview.$((current_prev_version + 1))"
fi
fi
echo "$next_version"
}
function bump_version_main() {
local current_version="$1"
local bump_type="$2"
# Split the version into major, minor, and patch
IFS='.' read -ra chunks <<< "$current_version"
local major="${chunks[0]}"
local minor="${chunks[1]:-0}"
local patch="${chunks[2]:-0}"
# Increment version based on bump type
case "$bump_type" in
major)
((major++))
minor=0
patch=0
;;
minor)
((minor++))
patch=0
;;
patch)
((patch++))
;;
*)
echo "Invalid bump type: $bump_type"
return 1
;;
esac
echo "${major}.${minor}.${patch}"
}
main "$@"