Last active
October 4, 2022 09:54
-
-
Save andkirby/0046df5cad44f86b670a102b7c8b7ba7 to your computer and use it in GitHub Desktop.
Semantic versions sorting
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
<?php | |
/** | |
* Sort versions list | |
* | |
* Add an alias in GIT | |
* $ git config --global alias.tags "!git tag | xargs -i -0 php "$(where version_sort)" {}" | |
* @link https://gist.github.com/andkirby/0046df5cad44f86b670a102b7c8b7ba7 | |
*/ | |
try { | |
if (php_sapi_name() != 'cli') { | |
throw new Exception('This file can be run in CLI only.'); | |
} | |
if (empty($_SERVER['argv'][1])) { | |
throw new Exception('Invalid argument. Please set version/s as the only argument.'); | |
} | |
array_shift($_SERVER['argv']); //remove file name | |
$versionsRaw = trim(implode("\n", $_SERVER['argv'])); | |
$versionsRaw = str_replace(' ', "\n", $versionsRaw); | |
$versions = preg_replace('/\+[^\n]+/', '', $versionsRaw); //Remove meta information after sign "+" | |
$versions = explode("\n", $versions); | |
array_walk($versions, 'trim'); | |
usort($versions, 'version_compare'); | |
echo implode(PHP_EOL, $versions); | |
} catch (Exception $e) { | |
echo 'error: ' . $e->getMessage(); | |
exit(1); | |
} |
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 | |
# Install version_sort | |
# curl -Ls https://gist.github.com/andkirby/0046df5cad44f86b670a102b7c8b7ba7/raw/version_sort_install.sh | bash | |
set -o errexit | |
set -o pipefail | |
set -o nounset | |
#set -o xtrace | |
# Script running with pipeline | |
__dir=/usr/local/bin | |
if [ ! -d ${__dir} ]; then | |
__dir=/usr/bin | |
fi | |
__file=${__dir}/version_sort | |
curl -Ls https://gist.github.com/andkirby/0046df5cad44f86b670a102b7c8b7ba7/raw/version_sort.php -o ${__file} && \ | |
chmod u+x ${__file} && \ | |
sed -i '1s:^:#!/usr/bin/env php\n:' ${__file} && \ | |
echo 'Version sort: '${__file} && \ | |
exit 0 | |
exit 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
curl -Ls https://gist.github.com/andkirby/0046df5cad44f86b670a102b7c8b7ba7/raw/version_sort_install.sh | bash