-
Notifications
You must be signed in to change notification settings - Fork 0
/
semver
executable file
·147 lines (127 loc) · 3.23 KB
/
semver
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
#!/usr/bin/env bash
# SPDX-License-Identifier: MIT
#
# Regex copied from semver-tool by François Saint-Jacques
# https://github.com/fsaintjacques/semver-tool
#
# Generate version numbers based on git tags
VERSION="1.0.0"
####### Below is copied from original
NAT='0|[1-9][0-9]*'
ALPHANUM='[0-9]*[A-Za-z-][0-9A-Za-z-]*'
IDENT="${NAT}|${ALPHANUM}"
FIELD='[0-9A-Za-z-]+'
SEMVER_REGEX="\
^[vV]?\
(${NAT})\\.(${NAT})\\.(${NAT})\
(\\-(${IDENT})(\\.(${IDENT}))*)?\
(\\+${FIELD}(\\.${FIELD})*)?$"
####### Above is copied from original
SHA='[0-9a-f]{4,40}'
GIT_DESCRIBE_REGEX="\
^[vV]?\
(${NAT})\\.(${NAT})\\.(${NAT})\
(\\-((${IDENT})(\\.(${IDENT}))*))?\
(\\+(${FIELD}(\\.${FIELD})*))?\
\\-(${NAT})\\-g(${SHA})(\\-(dirty|broken))?$"
semver() {
# Keep a copy of entry arguments
NUM_ARGS="$#"
ARGS=("$@")
# Stop on error
set -euo pipefail
parse
}
git_version() {
declare -n _ver="$1"
local git_describe=0
git_describe="$(git describe --long --broken)"
if [[ "${git_describe}" =~ ${GIT_DESCRIBE_REGEX} ]]; then
local major=${BASH_REMATCH[1]}
local minor=${BASH_REMATCH[2]}
local patch=${BASH_REMATCH[3]}
local prere=${BASH_REMATCH[4]}
local meta=${BASH_REMATCH[10]}
local num=${BASH_REMATCH[12]}
local sha=${BASH_REMATCH[13]}
local dirt=${BASH_REMATCH[15]}
_ver="${major}.${minor}.${patch}${prere}+"
if [[ -z "${meta}" ]]; then
_ver=${_ver}${num}.${sha}
else
_ver=${_ver}${meta}.${num}.${sha}
fi
if [[ -n "${dirt}" ]]; then
_ver=${_ver}.${dirt}
fi
# Let's just make a final validation it matches the official regex
if [[ "${_ver}" =~ ${SEMVER_REGEX} ]]; then
return 0
else
_ver=""
return 1
fi
else
_ver=""
return 1
fi
}
parse() {
if [[ ${NUM_ARGS} -eq 0 ]]; then
local ver
git_version ver
echo "${ver}"
else
for i in "${ARGS[@]}"; do
case ${i} in
-h | --help)
print_help
;;
-v | --version)
echo "${VERSION}"
;;
--file | --file=?*)
local file="version.properties"
if [[ "${i}" != '--file' ]]; then
file=${i##*=}
fi
local ver
git_version ver
echo "VERSION=${ver}" >"${file}"
;;
*)
print_help
echo "Unexpected arguments (${NUM_ARGS}): ${ARGS[*]}"
exit 1
;;
esac
done
fi
}
print_help() {
local script
script="semver"
echo "Generates a version number based on git tag information"
echo "This assumes you use Semantic Versioning 2.0.0 to tag your tree"
echo ""
echo "Usage: ${script} [--file[=<file>]]"
echo " ${script} [-h|--help|]"
echo " ${script} [-v|--version]"
echo ""
echo "Options:"
echo " --file[=<file>] Output to a file instead of the console. If no value is given,"
echo " a file named 'version.properties' will be created in the CWD"
echo " with a single key=value pair. Key is 'VERSION'"
echo " -h,--help Print this usage message"
echo " -v,--version Print the version of this tool"
echo ""
}
###########################
###### Startup logic ######
###########################
if [[ ${BASH_SOURCE[0]} != "${0}" ]]; then
export -f semver
else
semver "${@}"
exit $?
fi