-
Notifications
You must be signed in to change notification settings - Fork 28
/
sem-semantic-release
169 lines (138 loc) · 4.58 KB
/
sem-semantic-release
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
#!/usr/bin/env bash
set -euo pipefail
semantic-release::print_usage() {
printf "Usage: sem-semantic-release [OPTION]...\n\n"
printf "Options:\n"
printf " --dry-run \t runs semantic-release without publishing version\n"
printf " --plugins \t npm plugins and extensions to be installed\n"
printf " --branches \t branches to run semantic release for\n"
printf " --version \t semantic-release version\n"
}
semantic-release::parse_args() {
local PARSING_PLUGINS=1
local PARSING_BRANCHES=1
local DRY_RUN=1
local SEMANTIC_RELEASE_PLUGIN_LIST=()
local SEMANTIC_RELEASE_BRANCH_LIST=()
set -f
while [[ $# -gt 0 ]]; do
case $1 in
--help)
semantic-release::print_usage
exit 0
;;
--version)
PARSING_PLUGINS=1
PARSING_BRANCHES=1
SEMANTIC_RELEASE_VERSION="$2"
shift # past argument
shift # past value
;;
--plugins)
PARSING_PLUGINS=0
PARSING_BRANCHES=1
shift
;;
--branches)
PARSING_PLUGINS=1
PARSING_BRANCHES=0
shift
;;
--dry-run)
DRY_RUN=0;
shift
;;
-*)
semantic-release::print_usage
exit 1
;;
*)
if [[ $PARSING_PLUGINS == 0 ]];
then
SEMANTIC_RELEASE_PLUGIN_LIST+=("$1");
fi
if [[ $PARSING_BRANCHES == 0 ]];
then
SEMANTIC_RELEASE_BRANCH_LIST+=("$1");
fi
shift
;;
esac
done
set +f
if [[ $DRY_RUN -eq 0 ]]
then
SEMANTIC_RELEASE_OPTIONS+="--dry-run ";
fi
if [[ ${#SEMANTIC_RELEASE_PLUGIN_LIST[@]} -ne 0 ]]
then
SEMANTIC_RELEASE_PLUGINS="${SEMANTIC_RELEASE_PLUGIN_LIST[*]} "
else
SEMANTIC_RELEASE_PLUGINS=""
fi
if [[ ${#SEMANTIC_RELEASE_BRANCH_LIST[@]} -ne 0 ]]
then
SEMANTIC_RELEASE_OPTIONS+="--branches ${SEMANTIC_RELEASE_BRANCH_LIST[*]} "
fi
set -f
if [[ -n $BATS_VERSION ]]; then
echo "semantic-release version: $SEMANTIC_RELEASE_VERSION"
echo "semantic-release plugins: $SEMANTIC_RELEASE_PLUGINS"
echo "semantic-release options: $SEMANTIC_RELEASE_OPTIONS"
fi
}
semantic-release::install() {
if [[ -n $SEMANTIC_RELEASE_VERSION ]]
then
SEMANTIC_RELEASE_PACKAGE="semantic-release@$SEMANTIC_RELEASE_VERSION"
else
SEMANTIC_RELEASE_PACKAGE="semantic-release"
fi
npm install "$SEMANTIC_RELEASE_PACKAGE" --silent ||
{ echo "sem-semantic-release: Unsupported semantic-release version: $SEMANTIC_RELEASE_VERSION"; exit 1; }
if [[ -n $SEMANTIC_RELEASE_PLUGINS ]]
then
# we cannot use arrays here (because of set -u) and
# we must not prevent word splitting in that case
# shellcheck disable=SC2086
npm install $SEMANTIC_RELEASE_PLUGINS --silent ||
{ echo "sem-semantic-release: Unable to install plugins: $SEMANTIC_RELEASE_PLUGINS"; exit 1; }
fi
}
semantic-release::scrape_version() {
local RELEASE_VERSION=""
local RELEASE_NUMBERS=()
RELEASE_VERSION=$(grep "The next release version" /tmp/semantic-release.log | grep -oE '([0-9]+\.[0-9]+\.[0-9]+)')
if [[ -n $RELEASE_VERSION ]]
then
IFS='.' read -ra RELEASE_NUMBERS <<< "$RELEASE_VERSION"
sem-context put ReleasePublished="true"
sem-context put ReleaseVersion="$RELEASE_VERSION"
sem-context put ReleaseMajorVersion="${RELEASE_NUMBERS[0]}"
sem-context put ReleaseMinorVersion="${RELEASE_NUMBERS[1]}"
sem-context put ReleasePatchVersion="${RELEASE_NUMBERS[2]}"
echo "sem-semantic-release: Release $RELEASE_VERSION has been generated."
else
sem-context put ReleasePublished="false"
echo "sem-semantic-release: New release hasn't been generated."
fi
}
semantic-release::main() {
semantic-release::parse_args "$@"
semantic-release::install
if [[ -n $SEMANTIC_RELEASE_OPTIONS ]]
then
npx semantic-release "$SEMANTIC_RELEASE_OPTIONS" | tee /tmp/semantic-release.log
else
npx semantic-release | tee /tmp/semantic-release.log
fi
if [[ ! $? ]]; then return $?; fi
semantic-release::scrape_version
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
SEMANTIC_RELEASE_PLUGINS=()
SEMANTIC_RELEASE_OPTIONS=""
SEMANTIC_RELEASE_VERSION=""
BATS_VERSION=""
semantic-release::main "$@"
fi