Skip to content

Instantly share code, notes, and snippets.

@shakyaabiral
Last active August 17, 2023 16:06
Show Gist options
  • Save shakyaabiral/1813d3d79de882c71403b35d2c286668 to your computer and use it in GitHub Desktop.
Save shakyaabiral/1813d3d79de882c71403b35d2c286668 to your computer and use it in GitHub Desktop.
Bumpversion for php application
<?php
/*
* Creates a VERSION file if not exists in current directory.
* Version should be in the format. {Major.Minor.Patch}
* Patch has a suffix -dev for non release version
* Usage:
* php bumpversion.php [options]
* options:
* --version [part] (required)
* part = part of the version to increase, e.g. minor, major, patch, release
* if part = release, then -dev is removed from the current version
* Note: prints current version number if part is not specified
* --dryrun
* outputs the outcome but doesn't update the version file
* --commit
* commits the updated version file
* -f|--force
* forcefully commit file
* --tag
* creates a tag on release
*
*/
class Quark_Bumpversion
{
protected $_args = array();
protected $_versionfile = 'VERSION';
protected $_current_dir;
/**
* Parse input arguments
*
* @return Mage_Shell_Abstract
*/
protected function _parseArgs()
{
$current = null;
foreach ($_SERVER['argv'] as $arg) {
$match = array();
if (preg_match('#^--([\w\d_-]{1,})$#', $arg, $match) || preg_match('#^-([\w\d_]{1,})$#', $arg, $match)) {
$current = $match[1];
$this->_args[$current] = true;
} else {
if ($current) {
$this->_args[$current] = $arg;
} else if (preg_match('#^([\w\d_]{1,})$#', $arg, $match)) {
$this->_args[$match[1]] = true;
}
}
}
return $this;
}
/**
* Retrieve argument value by name or false
*
* @param string $name the argument name
* @return mixed
*/
public function getArg($name)
{
if (isset($this->_args[$name])) {
return $this->_args[$name];
}
return false;
}
public function check_branch_dirty()
{
$res = shell_exec('git status -sb');
$lines = preg_split('/$\R?^/m', $res);
return count($lines) > 1;
}
public function commit_versionfile($current_version, $new_version)
{
if ($this->getArg('commit')) {
if ($this->check_branch_dirty() && !($this->getArg('f') or $this->getArg('force'))) {
print("You have uncommitted work ! Use -f|--force option to force commit\n");
} else {
shell_exec("cd $this->_current_dir && git add -v VERSION");
shell_exec("cd $this->_current_dir && git commit -m 'Bumpversion: $current_version -> $new_version'");
}
}
}
public function can_proceed()
{
if ($this->getArg('commit') && $this->check_branch_dirty() && !($this->getArg('f') or $this->getArg('force'))) {
return false;
}
return true;
}
public function run()
{
$this->_current_dir = dirname(__FILE__);
$version_filepath = dirname(__FILE__) . DIRECTORY_SEPARATOR . $this->_versionfile;
if (!file_exists($version_filepath)) {
file_put_contents($version_filepath, '0.1.0');
print "Created VERSION file.";
}
$current_version = trim(file_get_contents($version_filepath));
$current_version_array = explode('.', $current_version);
$this->_parseArgs();
$versionAction = $this->getArg('version');
if (!$versionAction) {
print("Version option is required.");
return;
}
if (!$this->can_proceed()) {
print("You have uncommitted work ! Use -f|--force option to force commit\n");
return;
}
if ($versionAction === true) {
print $current_version;
} elseif ($versionAction == 'patch') {
$patch_version = intval(explode('-', $current_version_array[2])[0]) + 1 . '-dev';
$new_version = "$current_version_array[0].$current_version_array[1].$patch_version";
if (!$this->getArg('dryrun')) {
file_put_contents($version_filepath, $new_version);
$this->commit_versionfile($current_version, $new_version);
print "Version File Updated:\nNew Version: $new_version\n";
} else {
print $new_version;
}
} elseif ($versionAction == 'minor') {
$patch_version = '0-dev';
$minor_version = $current_version_array[1] + 1;
$new_version = "$current_version_array[0].$minor_version.$patch_version";
if (!$this->getArg('dryrun')) {
file_put_contents($version_filepath, $new_version);
$this->commit_versionfile($current_version, $new_version);
print "Version File Updated:\nNew Version: $new_version\n";
} else {
print $new_version;
}
} elseif ($versionAction == 'major') {
$patch_version = '0-dev';
$minor_version = '0';
$major_version = $current_version_array[0] + 1;
$new_version = "$major_version.$minor_version.$patch_version";
if (!$this->getArg('dryrun')) {
file_put_contents($version_filepath, $new_version);
$this->commit_versionfile($current_version, $new_version);
print "Version File Updated:\nNew Version: $new_version\n";
} else {
print $new_version;
}
} elseif ($versionAction == 'release') {
$patch_version_array = explode('-', $current_version_array[2]);
if (count($patch_version_array) == 2) {
$new_patch_version = $patch_version_array[0];
$new_version_array = $current_version_array;
$new_version_array[2] = $new_patch_version;
$new_version = join(".", $new_version_array);
if (!$this->getArg('dryrun')) {
file_put_contents($version_filepath, $new_version);
$this->commit_versionfile($current_version, $new_version);
print "Version File Updated:\nNew Version: $new_version\n";
if ($this->getArg('tag')){
shell_exec("cd {$this->_current_dir} && git tag -a v$new_version -m 'v$new_version'");
print "Tag created: v$new_version";
}
} else {
print $new_version;
}
} elseif (count($patch_version_array) == 1) {
print("Cannot release a non stable version: $current_version\n");
} else {
print("Invalid Version\n");
}
}
}
}
$shell = new Quark_Bumpversion();
$shell->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment