Semantic Versioning utility.
The semver utility is a text filter for Semantic Version strings. It searches text from the standard input, selects any Semantic Versions that are present, and writes them to the standard output. It can optionally sort or tabulate the selected versions.
A version string will be selected within the text stream if the following criteria are met:
- version is a valid Semantic Version.
- version is a whole line. (This can be modified with the
-woption.)
brew tap chriskilding/semver
brew install semverDependencies:
make
make test # optional
make installsemver [-hqstw]Options:
-h --helpShow the help screen.-q --quietQuiet - suppress normal output.-s --sortSort the matched versions in precedence order (low-to-high).-t --tabulateTabulate the matched versions (separator: '\t').-w --word-matchSelect words that match the semver pattern. (Equivalent to the grep(1)--word-regexpoption.)
Most options can be combined. For example, semver -stw will word-match occurrences of semvers, sort them, and print them in tabulated form.
man semverSelect lines that are version strings:
semver < example.txtCalculate the next Git tag:
# ++major
git tag | semver -st | tail -n 1 | awk -F '\t' '{ print ++$1 "." 0 "." 0 }'
# ++minor
git tag | semver -st | tail -n 1 | awk -F '\t' '{ print $1 "." ++$2 "." 0 }'
# ++patch
git tag | semver -st | tail -n 1 | awk -F '\t' '{ print $1 "." $2 "." ++$3 }'Cut out the major, minor, and patch components of a version:
semver -t <<< '1.2.3-alpha+1' | cut -f 1-3Download all artifacts in a version range:
v='0.0.1'
while curl -fs "https://example.com/artifact/$v.tar.gz" > "$v.tar.gz"; do
v=$(semver -t <<< "$v" | awk -F '\t' '{ print $1 "." $2 "." ++$3 }')
doneFind the current Git tag:
git tag | semver -s | tail -n 1Format versions as CSV:
semver -tw < example.txt | tr '\t' ','Validate a candidate version string:
semver -q <<< '1.2.3' && echo 'ok'These Bash helper functions can make complex versioning operations easier.
#!/usr/bin/env bash
function ++major {
semver -t <<< "$1" | awk -F '\t' '{ print ++$1 "." 0 "." 0 }'
}
function ++minor {
semver -t <<< "$1" | awk -F '\t' '{ print $1 "." ++$2 "." 0 }'
}
function ++patch {
semver -t <<< "$1" | awk -F '\t' '{ print $1 "." $2 "." ++$3 }'
}Examples:
++major '1.2.3' #=> 2.0.0
++minor '1.2.3' #=> 1.3.0
++patch '1.2.3' #=> 1.2.4