Categories
post

Git Flow Bump Version Script

I wrote a simple bump version PHP script that does quite a bit of lifting for me.

  • It maintains my version number, very basic
  • It takes an argument on whether I need my templates updated due to CSS/JS changes.
    • If necessary, creates a new assets directory based on version number to fix caching issues
    • Pulls latest css/js into that directory
    • Updates template to new assets directory
    • Adds and commits new template
  • Optional second argument overrides the version #
  • Starts a new release branch
  • Opens my RELEASE notes for editing
  • Adds and commits my release notes
  • Finishes my release branch

Below is specific to my situation. PATH_TO_TEMPLATE_FILE would need to be changed, as well as the preg_replace lines to suite your needs.


if (!in_array($argv[1], array('y', 'n'))) {
print "Usage: {$argv[0]} (y/n) [version]\n y=update css/js\n n=no update\n";
exit;
}

if ($argv[2] && !preg_match('#v\d\.\d*\.\d*#', $argv[2])) {
print "Invalid version (should be v\d.\d.\d)\nUsage: $0 (y/n) [version]\n y=update css/js\n n=no update\n";
exit;
}

chdir(dirname(__FILE__));

$fileName = '.Version';

$v = file_get_contents($fileName);
$a = explode('.', $v);
$minor = $a[2]+1;
$v = "{$a[0]}.{$a[1]}.$minor";
$fh = fopen($fileName, 'w');
fputs($fh, $v);
fclose($fh);
`git add $fileName`;
`git commit -m "Increased version to $v"`;
echo "Version updated to $v and added to git\n";

if ($argv[1] == 'y') {
$asset = "assets/v{$a[1]}.$minor";
mkdir($asset);
chdir($asset);
`sh ../../update_css_js.sh`;
`git add .`;
`git commit -m "Increased css/js version to v{$a[1]}.$minor"`;

chdir('../../');
$template = file_get_contents('PATH_TO_TEMPLATE_FILE');
$template = preg_replace('#data-style="sI" href=".*?" media#', 'data-style="sI" href="/assets/v'.$a[1].'.'.$minor.'/style.css" media', $template);
$template = preg_replace('#jQuery.getScript\(".*?" \+ pluginPack#', 'jQuery.getScript("/assets/v'.$a[1].'.'.$minor.'/" + pluginPack', $template);
$fh = fopen('PATH_TO_TEMPLATE_FILE', 'w');
fputs($fh, $template);
fclose($fh);

`git add PATH_TO_TEMPLATE_FILE`;
`git commit -m "Increased css/js version to v{$a[1]}.$minor"`;
}

`git flow release start $v`;
$date = date('Y-m-d');
$command = << /tmp/out && mv /tmp/out RELEASE
EOF;

system($command);
system("vim RELEASE > `tty`");

`git add RELEASE`
`git commit -m "Updated RELEASE notes"`

system("git flow release finish $v > `tty`");

 

Leave a Reply

Your email address will not be published. Required fields are marked *