-
Notifications
You must be signed in to change notification settings - Fork 0
/
Semver.3sh.mdoc
263 lines (251 loc) · 5.57 KB
/
Semver.3sh.mdoc
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
.Dd Jun 06, 2018
.Dt SEMVER 3
.Os
.Sh NAME
.Nm Semver
.Nd semantic versioning functions
.Sh SYNOPSIS
.Nm Semver::validate
.Ar version
.Op Ar variable
.Nm Semver::compare
.Ar version
.Ar version
.Nm Semver::is_prerelease
.Ar version
.Nm Semver::increment_major
.Ar version
.Nm Semver::increment_minor
.Ar version
.Nm Semver::increment_patch
.Ar version
.Nm Semver::set_pre
.Ar version
.Ar pre-release
.Nm Semver::set_build
.Ar version
.Ar build metadata
.Nm Semver::pretty
.Ar major
.Ar minor
.Ar patch
.Op Ar pre-release
.Op Ar build_metadata
.Sh DESCRIPTION
.Nm Semver::validate
validates a semantic version string and optionally returns the parsed output.
It prints its input
.Ar version ,
erroring with a description of what's wrong if it's invalid.
If
.Ar variable
is also provided, a snippet for array assignment of the parsed parts to
.Ar variable
is instead printed, allowing for an easy
.Nm eval
afterwards.
(You probably want to run
.Ql declare -a my_var
beforehand so static analysis tools like
.Xr shellcheck 1
can work properly.)
.Pp
.Nm Semver::compare
compares two semvers and returns the result.
The result is one of
.Ql -1 ,
meaning less than,
.Ql 0 ,
meaning equals, or
.Ql 1 ,
meaning greater than.
This allows for natural use with
.Nm [[
or
.Xr test 1 ,
such as
.Ql [[ $(Semver::compare a b) -ge 0 ]]
testing whether
.Ar a
is greater than or equal to
.Ar b .
.Pp
.Nm Semver::is_prerelease
checks whether a semver is a pre-release.
The result is either
.Ql yes
or
.Ql no .
.Pp
.Nm Semver::increment_major ,
.Nm Semver::increment_minor ,
and
.Nm Semver::increment_patch
return a new semver with the relevant part incremented.
This does not modify
.Ar version .
When incrementing, the pre-release and build metadata parts are dropped.
.Pp
.Nm Semver::set_pre
and
.Nm Semver::set_build
return a new semver with the relevant part replaced.
This does not modify
.Ar version .
Validation is performed on
.Ar part .
A
.Ar part
of the empty string removes the part.
Setting the pre-release does not drop the build metadata.
.Pp
.Nm Semver::pretty
takes separate parts and formats them into a semver.
Note that no validation is performed on the returned version.
.Sh VENDORING
If you wish to copy and paste these functions into your own script to vendor
them,
.Nm Semver::validate
&
.Nm Semver::pretty
have no dependencies.
All other functions depend on
.Nm Semver::validate .
Please remember to provide proper attribution and license information.
.Sh ERRORS
All errors start with the name of their associated function, like so:
.Dl Semver::validate: ...
.Pp
.Bl -tag
.It Nm Semver::validate
.Bl -bullet
.It
.Ql invalid major: $major
|
.Ql inavlid minor: $minor
|
.Ql invalid patch: $patch
.Pp
The part is not an integer.
.It
.Ql invalid pre-release: $pre_release
|
.Ql invalid build metadata: $build_metadata
.Pp
The part is not a series of non-empty identifiers
.Pq Ql [0-9A-Za-z-]
separated by
dots
.Pq Ql "." .
.El
.It Nm Semver::compare
.Bl -bullet
.It
All
.Nm Semver::validate
errors.
.El
.It Nm Semver::increment_major | Nm Semver::increment_minor | Nm \
Semver::increment_patch
.Bl -bullet
.It
All
.Nm Semver::validate
errors.
.El
.It Nm Semver::set_pre | Nm Semver::set_build
.Bl -bullet
.It
All
.Nm Semver::validate
errors.
.It
.Nm Semver::set_pre :
.Ql invalid pre-release: $pre_release
|
.Nm Semver::set_build :
.Ql invalid build metadata: $build_metadata
.Pp
The part is not a series of non-empty identifiers
.Pq Ql [0-9A-Za-z-]
separated by dots
.Pq Ql "." .
.El
.El
.Sh EXAMPLE
The following script is designed to be placed next to a copy of
.Nm Semver.sh .
.Pp
.Bd -literal
#!/bin/bash
# example.sh
. ./semver.sh
ver_a='1.2.11-alpha+001'
ver_b='1.4.0'
ver_invalid='1.5.'
Semver::validate "$ver_invalid" 2>/dev/null \\
&& echo "$ver_invalid should be an invalid semver" \\
|| echo "$ver_invalid is an invalid semver"
echo
declare -a v
eval "$(Semver::validate "$ver_a" v)"
echo "Parts of \\$ver_a:"
echo "major=${v[0]}, minor=${v[1]}, patch=${v[2]},"
echo "pre-release=${v[3]}, build metadata=${v[4]}"
echo
compare_vers() {
echo -n "Comparsion between $ver_a & $ver_b: "
Semver::compare $ver_a $ver_b
}
compare_vers
ver_a=$(Semver::increment_minor "$ver_a")
ver_a=$(Semver::increment_minor "$ver_a")
compare_vers
ver_a=$(Semver::increment_patch "$ver_a")
compare_vers
ver_b=$(Semver::increment_patch "$ver_b")
ver_a=$(Semver::set_pre "$ver_a" 'alpha')
ver_b=$(Semver::set_pre "$ver_b" 'alpha')
ver_a=$(Semver::set_build "$ver_a" 'musl')
ver_b=$(Semver::set_build "$ver_b" 'glibc')
compare_vers
ver_b=$(Semver::set_pre "$ver_b" 'beta')
ver_b=$(Semver::set_build "$ver_b" '')
compare_vers
ver_a=$(Semver::set_pre "$ver_a" '')
ver_a=$(Semver::set_build "$ver_a" '')
compare_vers
echo
echo -n "Is $ver_b a pre-release? "
Semver::is_prerelease "$ver_b"
echo -n "What about $ver_a? "
Semver::is_prerelease "$ver_a"
.Ed
.Pp
We can then run
.Ql bash example.sh
for the following output:
.Pp
.Bd -literal
1.5. is an invalid semver
Parts of $ver_a:
major=1, minor=2, patch=11,
pre-release=alpha, build metadata=001
Comparsion between 1.2.11-alpha+001 & 1.4.10: -1
Comparsion between 1.4.0 & 1.4.0: 0
Comparsion between 1.4.1 & 1.4.0: 1
Comparsion between 1.4.1-alpha+musl & 1.4.1-alpha+glibc: 0
Comparsion between 1.4.1-alpha+musl & 1.4.1-beta: -1
Comparsion between 1.4.1 & 1.4.1-beta: 1
Is 1.4.1-beta a pre-release? yes
What about 1.4.1? no
.Ed
.Sh BUGS
Hopefully not, but if you do find some, let me know on GitHub or email me.
.Sh SEE ALSO
.Lk https://semver.org/ "Semantic Versioning specification" ,
.Xr bash 1
.Sh AUTHORS
.An bb010g Aq Mt [email protected]
The latest sources, full contributor list, and more can be found at
.Lk https://github.com/bb010g/semver.sh .