Last active
October 24, 2024 08:48
-
-
Save bitmvr/9ed42e1cc2aac799b123de9fdc59b016 to your computer and use it in GitHub Desktop.
A POSIX Compliant SemVer Parser in Pure Bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
VERSION="${1#[vV]}" | |
VERSION_MAJOR="${VERSION%%.*}" | |
VERSION_MINOR_PATCH="${VERSION#*.}" | |
VERSION_MINOR="${VERSION_MINOR_PATCH%%.*}" | |
VERSION_PATCH_PRE_RELEASE="${VERSION_MINOR_PATCH#*.}" | |
VERSION_PATCH="${VERSION_PATCH_PRE_RELEASE%%[-+]*}" | |
VERSION_PRE_RELEASE="" | |
case "$VERSION" in | |
*-*) | |
VERSION_PRE_RELEASE="${VERSION#*-}" | |
VERSION_PRE_RELEASE="${VERSION_PRE_RELEASE%%+*}" | |
;; | |
esac | |
echo "Version: ${VERSION}" | |
echo "Version [major]: ${VERSION_MAJOR}" | |
echo "Version [minor]: ${VERSION_MINOR}" | |
echo "Version [patch]: ${VERSION_PATCH}" | |
echo "Version [pre-release]: ${VERSION_PRE_RELEASE}" |
Okay, I've patched it. I will add comments later around what's happening where as parameter expansion in Bash can be confusing.
@bitmvr Cool thanks a lot, I think that as much as SemVer is popular, we actually (if I am not mistaken) miss generally well available installed everywhere typ of tool to actually parse semver, to use in GitHub actions, and build scripts, etc. so your script is actually very handy :-)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Well, actually there is still one bug in the logic. The last test I ran locally, the
rc
is still a part of the patch release. I need to account for that.