Debian Codegraph @

release

raw
1 #!/usr/bin/env -S bash -e
2
3 token_file=token
4
5 cd "$(dirname "$0")" || exit
6
7 check_can_release() {
8 local version="$1"
9
10 type -P asciidoctor >/dev/null || usage "You need asciidoctor to make releases"
11
12 if [ -n "$(git status --porcelain)" ]
13 then
14 git status >&2
15 echo >&2
16 usage "Please commit your pending changes first"
17 fi
18
19 [[ -z "$version" ]] && usage "No version specified on command line"
20 echo "$version" | grep -E '^v[0-9]*\.[0-9]*\.[0-9]*$' >/dev/null || usage "Invalid format for version: $version"
21 [ -r "$token_file" ] || usage "'$token_file' file does not exist"
22
23 # shellcheck disable=1090
24 source "$token_file"
25
26 [[ -z "$user" ]] && usage "user not found in file '$token_file'"
27 [[ -z "$token" ]] && usage "token not found in file '$token_file'"
28 true #avoid error on last instruction of function (see bash -e)
29 }
30
31 prepare_release() {
32 set -x
33 local version="$1"
34 shift
35
36 update_version "$version"
37 update_man
38
39 git add bash_unit
40 git add docs/man/man1/bash_unit.1
41
42 if [ $# -eq 0 ]
43 then
44 git commit -m "prepare release $version"
45 else
46 git commit -m "$*"
47 fi
48
49 git tag "$version"
50 }
51
52 update_version() {
53 local version="$1"
54 sed -i -e "s:^VERSION=.*:VERSION=$version:" bash_unit
55 }
56
57 update_man() {
58 asciidoctor -D docs/man/man1 -d manpage -b manpage README.adoc
59 }
60
61 publish_release() {
62 local version="$1"
63
64 git push
65 git push --tags
66 curl -u "$user:$token" -XPOST https://api.github.com/repos/bash-unit/bash_unit/releases -d "
67 {
68 \"tag_name\": \"$version\"
69 }"
70 }
71
72 usage() {
73 local message=$1
74
75 cat >&2 <<EOF
76 $message
77
78 $0 <version> [release note]
79 Publishes a new release of bash_unit on github.
80 Version must respect [Semantic Versioning](http://semver.org/)
81 A token file must exist in the current working directory. This file must be of the form:
82 user=<user>
83 token=<token>
84
85 <user> is your github account.
86 <token> is your github api token. See https://github.com/settings/tokens
87 EOF
88
89 exit 1
90 }
91
92 [[ $# -ge 1 ]] || usage "You must specify version on command line"
93 version=$1
94 shift
95
96 check_can_release "$version"
97
98 prepare_release "$version" "$@"
99 publish_release "$version"