Skip to content

Instantly share code, notes, and snippets.

@Chofoteddy
Created December 22, 2017 05:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Chofoteddy/21403951ef33d3a2ed3552daa0845fb2 to your computer and use it in GitHub Desktop.
Save Chofoteddy/21403951ef33d3a2ed3552daa0845fb2 to your computer and use it in GitHub Desktop.
Bump version is a single bash script that increase the version within a php file, according to the tag "version" of the PHPDoc standard.
#!/bin/bash
## Copyright (C) 2017 - All Rights Reserved
## Permission to copy/modify is granted under the MIT license
##
## Script: bump-version.sh
## Description: Increase the version within a php file, according to the tag @version
## of the PHPDoc standard
## Style: Shell Style Guide - https://google.github.io/styleguide/shell.xml
## Author: Christopher CM (@Chofoteddy)
## Created: 2017-12-16
###########################################
# Display "help" information.
#
# Globals:
# None
# Arguments:
# None
# Returns:
# None
###########################################
display_help() {
echo "Usage: $0 [OPTION] [PATH]";
echo
echo 'Mandatory options:';
echo ' -m Upgrade major (X.0.0).';
echo ' -i Upgrade minor (0.X.0).';
echo ' -p Upgrade patch (0.0.X).';
echo
}
if [[ $# -eq 0 ]]; then
echo 'Arguments are missing.' >&2;
echo "Try: '$0 -h' for more information." >&2;
exit 2;
fi
files="$(find $2 -type f -name '*.php')";
for file in $files; do
version="$(cat $file | egrep -o '@version ([0-9]+\.?){3}')";
semver="$(egrep -o '([0-9]+\.?){3}' <<< $version)";
if [[ -z $semver ]]; then
continue;
fi
declare -a array_version=($(tr '.' '\n' <<< $semver));
major_version=${array_version[0]};
minor_version=${array_version[1]};
patch_version=${array_version[2]};
case $1 in
-m )
new_major="$(expr $major_version + 1)";
array_version[0]=$new_major;
array_version[1]=0;
array_version[2]=0;
;;
-i )
new_minor="$(expr $minor_version + 1)";
array_version[1]=$new_minor;
array_version[2]=0;
;;
-p )
new_patch="$(expr $patch_version + 1)";
array_version[2]=$new_patch;
;;
-h )
display_help;
break;
;;
esac
new_semver="$(tr ' ' '.' <<< ${array_version[*]})";
echo 'File: '$file;
echo -e '... \e[96mUpgrading version: \e[91m'$semver \
'\e[0mto \e[92m'$new_semver'\e[0m\n';
sed -i "s/\(@version\) $semver/\1 $new_semver/g" $file;
done
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment