Using PHP-CLI : Automatic Incrementing of .NET Assembly Build Numbers
This is a PHP script to automatically bump up the build number of a .NET assembly on each build, designed to run from the command line (PHP Cli). I was done in PHP because I needed to use Regular Expressions. The code can be extended to take command line options, OR implemented as a DLL in C# or something.
How to Use:
Add a line to your .NET IDE’s Project Options (I use SharpDevelop 2.0) to run this script as a pre-build command, e.g.
[”C:\wamp\php\php.exe” -f “D:\projects\RentDesk\bump-version.php”]
The MSBuild engine uses relative paths to the comilation directory, so you have to specify the full paths to php.exe and the script file.
The code is listed below (remove the space before the “?php”)
< ?php
//set this to false to disable version bump logging
define('LOG_VERSION_BUMPS', true);
//Look for a string of the patern [assembly: AssemblyVersion("0.1.1.1620")]
$pattern = '/AssemblyVersion[(]\"(\d+)[.](\d+)[.](\d+)[.](\d+)\"[)]/';
//the assembly file
$file = 'D:\projects\rentdesk\AssemblyInfo.cs';
if (LOG_VERSION_BUMPS)
{
//a logfile to record all version bumps
$bumpLogFile = 'D:\projects\rentdesk\bumpVersion.log';
}
$filecontent = file_get_contents($file);
echo "Bumping version...\r\n";
//Callback function used by preg_replace_callback:
//It returns the bumped version string, and also writes to a log file
function fix_version($matches)
{
//the variable '$matches' contains an array of the sub-expressions
//returned by the regular expression engine. Note that $match[0]
//is the entire matchstring, consult your PHP manual for details
$versionString = 'AssemblyVersion("'. $matches[1] . '.'
. $matches[2] . '.' .$matches[3] . '.'
. ($matches[4] + 1) . '")';
if (LOG_VERSION_BUMPS)
{
global $bumpLogFile;
$line = "Bumped to $matches[1].$matches[2].$matches[3]."
.($matches[4] + 1). " on "
. date('M-d-Y, H:i:s',time())
."\n";
echo "Making bump entry in logfile...\r\n";
$h = fopen($bumpLogFile, a);
@fwrite($h, $line);
@fclose($h);
}
return $versionString;
}
$filecontent = preg_replace_callback(
$pattern, //pattern to match
'fix_version', //the name of the callback
$filecontent //the assembly file!
);
echo "Saving changes...\r\n";
//writing the modified string back to file
$fp = fopen($file, wb);
fwrite($fp, $filecontent);
fclose($fp);
echo 'Done';
exit(0);
? >
Download the file here.
Post a comment