Semantic Versioning utility
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:
-w
option.)
brew tap chriskilding/semver
brew install semver
Dependencies:
make
make test # optional
make install
semver [-hqstw]
Options:
-h --help
-q --quiet
-s --sort
-t --tabulate
-w --word-match
--word-regexp
option.)Most options can be combined. For example, semver -stw
will word-match occurrences of semvers, sort them, and print them in tabulated form.
man semver
Select lines that are version strings:
semver < example.txt
Calculate 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-3
Download 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 }')
done
Find the current Git tag:
git tag | semver -s | tail -n 1
Format 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