Version number and Build # generator for PHP  | Expert | | Join Date: Dec 2007 Location: Moon, Dark Side
Posts: 1,094
| | |
Hey guys,
I've built applications and I've always wanted my own version and build number that's more like bigger applications.
Requirements:
1. The version should be manual (besides obviously there is no way for the app itself to know if it got a new feature vs a fix, I'd like to be the one starting new versions or sub-versions)
2. Build number should be automatic based on the most recently modified file.
My Idea:
1. Get all the project files and find the most recent file update time stamp
2. Calculate the build number based on given version number to get a value that's from hundreds to thousands long (I don't want to reach tens of thousands preferably)
I'm having trouble with the math for 2. Primarily how I can make the build look like it was reset when I change version number, say when I change the version value from 2.0.1 to 2.0.2 or 2.1.0
Maybe I should just store the date the version was changed when change the version value itself and calculate the number of hours from that TS (time stamp) and the last modified file TS.
I'm open to any other ideas.
Peace out,
Dan
|  | Expert | | Join Date: Dec 2007 Location: Moon, Dark Side
Posts: 1,094
| | | re: Version number and Build # generator for PHP
current test code I'm playing around with: -
-
-
<?php
-
-
-
-
/**
-
* Gets entire file system recursively or non-recursivly in a given directory
-
*
-
* @param mixed $directory
-
* @param mixed $recursive
-
* @return array
-
*/
-
function getAllFiles($directory, $recursive = true)
-
{
-
$result = array();
-
-
if(is_dir($directory))
-
{
-
$handle = opendir($directory);
-
while ($datei = readdir($handle))
-
{
-
if (($datei != '.') && ($datei != '..')) {
-
$file = $directory.$datei;
-
if (is_dir($file)) {
-
if ($recursive) {
-
$result = array_merge($result, getAllFiles($file.'/'));
-
}
-
}
-
else {
-
$result[] = $file;
-
}
-
}
-
}
-
closedir($handle);
-
}
-
-
return $result;
-
}
-
-
/**
-
* Gets the most recent file TS in a directory
-
*
-
* @uses getAllFiles()
-
* @param mixed $directory
-
* @param mixed $recursive
-
* @return integer
-
*/
-
function getHighestFileTimestamp($directory, $recursive = true)
-
{
-
$allFiles = getAllFiles($directory, $recursive);
-
-
$highestKnown = 0;
-
foreach ($allFiles as $val)
-
{
-
$currentValue = filemtime($val);
-
if ($currentValue > $highestKnown) $highestKnown = $currentValue;
-
}
-
-
return $highestKnown;
-
}
-
-
-
/**
-
* Makes a build number based on newest file in the project and project version
-
* @uses getHighestFileTimestamp()
-
* @return integer
-
*/
-
function makeBuildNumber($version,$dir)
-
{
-
$newestFileTS = getHighestFileTimestamp($dir,true);
-
-
// TS is too large, subtract and version number + padding in thousands to get a build number
-
$verMx = intval(preg_replace('/[^0-9]/','',$version)) + 1257455304 * 1000;
-
-
return ($newestFileTS - $verMx);
-
}
-
-
echo makeBuildNumber("2.0.1","/path/to/project/");
-
-
see if you can get a better formula to work for line number 74 above.
Dan
|  | Expert | | Join Date: Dec 2007 Location: Moon, Dark Side
Posts: 1,094
| | | re: Version number and Build # generator for PHP
My best idea so far:
Build number is based on the 3rd version value. So when you change from 2.0.1 to 2.0.5 the build number grows as well. But will seem to have reset when you change to 2.0.1
psuedocode: -
-
$version = "2.0.2";
-
$multiplier = end(explode(".",$version)) * $othervalues;
-
-
ya? no? got a better idea?
Dan
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,629
| | | re: Version number and Build # generator for PHP Quote:
Originally Posted by dlite922 ya? no? got a better idea? I’d use the subversion (or whatever you use) revision number as build number.
|  | Expert | | Join Date: Dec 2007 Location: Moon, Dark Side
Posts: 1,094
| | | re: Version number and Build # generator for PHP Quote:
Originally Posted by Dormilich I’d use the subversion (or whatever you use) revision number as build number. How would I display that on the main page of the app though? Every time I check anything into SVN i'd have to change the version number by hand, which is daily at the beginning phases of the project.
It would be cool if SVN would update a text file everytime you checked something in and I could read the file content.
Dan
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,629
| | | re: Version number and Build # generator for PHP
the latest revision number should be somewhere in .svn.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,272 network members.
|