Skip to content

Instantly share code, notes, and snippets.

@andkirby
Last active October 4, 2022 09:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andkirby/0046df5cad44f86b670a102b7c8b7ba7 to your computer and use it in GitHub Desktop.
Save andkirby/0046df5cad44f86b670a102b7c8b7ba7 to your computer and use it in GitHub Desktop.
Semantic versions sorting
<?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);
}
#!/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
@andkirby
Copy link
Author

andkirby commented Nov 2, 2016

curl -Ls https://gist.github.com/andkirby/0046df5cad44f86b670a102b7c8b7ba7/raw/version_sort_install.sh | bash

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment