Skip to content

Commit b247f7d

Browse files
authored
allow non-patch prereleases and shellformat versioning script (#320)
1 parent 4133dfc commit b247f7d

3 files changed

Lines changed: 91 additions & 57 deletions

File tree

scripts/versioning/bump_version.sh

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ prerel=${2:-none}
1111
targetpath="../../scripts/versioning"
1212

1313
if [[ $bump == "prerel" ]]; then
14-
bump="patch"
1514
prerel="prerel"
1615
fi
1716

@@ -23,7 +22,7 @@ elif [[ $(git status --porcelain -b | grep -e "ahead" -e "behind") != "" ]]; the
2322
exit 1
2423
fi
2524

26-
dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
25+
dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
2726

2827
previous_version="$("$dir"/$targetpath/version.sh -s)"
2928

@@ -51,8 +50,7 @@ echo "Bumping version from v${previous_version} to ${new_version}"
5150

5251
read -p "Are you sure? " -n 1 -r
5352
echo
54-
if [[ $REPLY =~ ^[Yy]$ ]]
55-
then
53+
if [[ $REPLY =~ ^[Yy]$ ]]; then
5654
git tag -m "release ${new_version}" -a "$new_version" && git push "${ORIGIN}" tag "$new_version"
5755
echo "done"
5856
fi

scripts/versioning/semver

Lines changed: 88 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -87,41 +87,56 @@ function compare-version {
8787
for i in 0 1 2; do
8888
local diff=$((${V[$i]} - ${V_[$i]}))
8989
if [[ $diff -lt 0 ]]; then
90-
echo -1; return 0
90+
echo -1
91+
return 0
9192
elif [[ $diff -gt 0 ]]; then
92-
echo 1; return 0
93+
echo 1
94+
return 0
9395
fi
9496
done
9597

9698
# PREREL should compare with the ASCII order.
9799
if [[ -z "${V[3]}" ]] && [[ -n "${V_[3]}" ]]; then
98-
echo 1; return 0;
100+
echo 1
101+
return 0
99102
elif [[ -n "${V[3]}" ]] && [[ -z "${V_[3]}" ]]; then
100-
echo -1; return 0;
103+
echo -1
104+
return 0
101105
elif [[ -n "${V[3]}" ]] && [[ -n "${V_[3]}" ]]; then
102106
if [[ "${V[3]}" > "${V_[3]}" ]]; then
103-
echo 1; return 0;
107+
echo 1
108+
return 0
104109
elif [[ "${V[3]}" < "${V_[3]}" ]]; then
105-
echo -1; return 0;
110+
echo -1
111+
return 0
106112
fi
107113
fi
108114

109115
echo 0
110116
}
111117

112118
function command-bump {
113-
local new; local version; local sub_version; local command;
119+
local new
120+
local version
121+
local sub_version
122+
local command
114123

115124
case $# in
116-
2) case $1 in
117-
major|minor|patch|release) command=$1; version=$2;;
118-
*) usage-help;;
119-
esac ;;
120-
3) case $1 in
121-
prerel|build) command=$1; sub_version=$2 version=$3 ;;
122-
*) usage-help;;
123-
esac ;;
124-
*) usage-help;;
125+
2) case $1 in
126+
major | minor | patch | release)
127+
command=$1
128+
version=$2
129+
;;
130+
*) usage-help ;;
131+
esac ;;
132+
3) case $1 in
133+
prerel | build)
134+
command=$1
135+
sub_version=$2 version=$3
136+
;;
137+
*) usage-help ;;
138+
esac ;;
139+
*) usage-help ;;
125140
esac
126141

127142
validate-version "$version" parts
@@ -133,68 +148,89 @@ function command-bump {
133148
local build="${parts[4]}"
134149

135150
case "$command" in
136-
major) new="$((major + 1)).0.0";;
137-
minor) new="${major}.$((minor + 1)).0";;
138-
patch) new="${major}.${minor}.$((patch + 1))";;
139-
release) new="${major}.${minor}.${patch}";;
140-
prerel) new=$(validate-version "${major}.${minor}.${patch}-${sub_version}");;
141-
build) new=$(validate-version "${major}.${minor}.${patch}${prere}+${sub_version}");;
142-
*) usage-help ;;
151+
major) new="$((major + 1)).0.0" ;;
152+
minor) new="${major}.$((minor + 1)).0" ;;
153+
patch) new="${major}.${minor}.$((patch + 1))" ;;
154+
release) new="${major}.${minor}.${patch}" ;;
155+
prerel) new=$(validate-version "${major}.${minor}.${patch}-${sub_version}") ;;
156+
build) new=$(validate-version "${major}.${minor}.${patch}${prere}+${sub_version}") ;;
157+
*) usage-help ;;
143158
esac
144159

145160
echo "$new"
146161
exit 0
147162
}
148163

149164
function command-compare {
150-
local v; local v_;
165+
local v
166+
local v_
151167

152168
case $# in
153-
2) v=$(validate-version "$1"); v_=$(validate-version "$2") ;;
154-
*) usage-help ;;
169+
2)
170+
v=$(validate-version "$1")
171+
v_=$(validate-version "$2")
172+
;;
173+
*) usage-help ;;
155174
esac
156175

157176
compare-version "$v" "$v_"
158177
exit 0
159178
}
160179

161-
162180
# shellcheck disable=SC2034
163181
function command-get {
164-
local part version
182+
local part version
165183

166-
if [[ "$#" -ne "2" ]] || [[ -z "$1" ]] || [[ -z "$2" ]]; then
167-
usage-help
168-
exit 0
169-
fi
184+
if [[ "$#" -ne "2" ]] || [[ -z "$1" ]] || [[ -z "$2" ]]; then
185+
usage-help
186+
exit 0
187+
fi
170188

171-
part="$1"
172-
version="$2"
189+
part="$1"
190+
version="$2"
173191

174-
validate-version "$version" parts
175-
local major="${parts[0]}"
176-
local minor="${parts[1]}"
177-
local patch="${parts[2]}"
178-
local prerel="${parts[3]:1}"
179-
local build="${parts[4]:1}"
192+
validate-version "$version" parts
193+
local major="${parts[0]}"
194+
local minor="${parts[1]}"
195+
local patch="${parts[2]}"
196+
local prerel="${parts[3]:1}"
197+
local build="${parts[4]:1}"
180198

181-
case "$part" in
182-
major|minor|patch|release|prerel|build) echo "${!part}" ;;
183-
*) usage-help ;;
184-
esac
199+
case "$part" in
200+
major | minor | patch | release | prerel | build) echo "${!part}" ;;
201+
*) usage-help ;;
202+
esac
185203

186-
exit 0
204+
exit 0
187205
}
188206

189207
case $# in
190-
0) echo "Unknown command: $*"; usage-help;;
208+
0)
209+
echo "Unknown command: $*"
210+
usage-help
211+
;;
191212
esac
192213

193214
case $1 in
194-
--help|-h) echo -e "$USAGE"; exit 0;;
195-
--version|-v) usage-version ;;
196-
bump) shift; command-bump "$@";;
197-
get) shift; command-get "$@";;
198-
compare) shift; command-compare "$@";;
199-
*) echo "Unknown arguments: $*"; usage-help;;
215+
--help | -h)
216+
echo -e "$USAGE"
217+
exit 0
218+
;;
219+
--version | -v) usage-version ;;
220+
bump)
221+
shift
222+
command-bump "$@"
223+
;;
224+
get)
225+
shift
226+
command-get "$@"
227+
;;
228+
compare)
229+
shift
230+
command-compare "$@"
231+
;;
232+
*)
233+
echo "Unknown arguments: $*"
234+
usage-help
235+
;;
200236
esac

scripts/versioning/version.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
ORIGIN=${ORIGIN:-origin}
22

3-
version=$(git fetch --tags "${ORIGIN}" &>/dev/null | git -c "versionsort.prereleasesuffix=-pre" tag -l --sort=version:refname | tail -n1 | cut -c 2-)
3+
version=$(git fetch --tags "${ORIGIN}" &>/dev/null | git -c "versionsort.prereleasesuffix=-pre" tag -l --sort=version:refname | tail -n1 | cut -c 2-)
44

55
echo "$version"

0 commit comments

Comments
 (0)