Connecting Tech Pros Worldwide Help | Site Map

Version number and Build # generator for PHP

dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,094
#1: 2 Weeks Ago
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
dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,094
#2: 2 Weeks Ago

re: Version number and Build # generator for PHP


current test code I'm playing around with:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. <?php
  4.  
  5.  
  6.  
  7. /**
  8. * Gets entire file system recursively or non-recursivly in a given directory
  9. * @param mixed $directory
  10. * @param mixed $recursive
  11. * @return array
  12. */
  13. function getAllFiles($directory, $recursive = true) 
  14. {
  15.     $result = array();
  16.  
  17.     if(is_dir($directory))
  18.     {
  19.          $handle =  opendir($directory);
  20.          while ($datei = readdir($handle))
  21.          {
  22.               if (($datei != '.') && ($datei != '..')) {
  23.                    $file = $directory.$datei;
  24.                    if (is_dir($file)) {
  25.                         if ($recursive) {
  26.                              $result = array_merge($result, getAllFiles($file.'/'));
  27.                         }
  28.                    } 
  29.                    else {
  30.                         $result[] = $file;
  31.                    }
  32.               }
  33.          }
  34.          closedir($handle);
  35.     }
  36.  
  37.     return $result;
  38. }
  39.  
  40. /**
  41. * Gets the most recent file TS in a directory
  42. * @uses getAllFiles()
  43. * @param mixed $directory
  44. * @param mixed $recursive
  45. * @return integer
  46. */
  47. function getHighestFileTimestamp($directory, $recursive = true) 
  48. {
  49.      $allFiles = getAllFiles($directory, $recursive);
  50.  
  51.      $highestKnown = 0;
  52.      foreach ($allFiles as $val) 
  53.      {
  54.           $currentValue = filemtime($val);
  55.           if ($currentValue > $highestKnown) $highestKnown = $currentValue;
  56.      }
  57.  
  58.      return $highestKnown;
  59. }
  60.  
  61.  
  62. /**
  63. * Makes a build number based on newest file in the project and project version
  64. * @uses getHighestFileTimestamp()
  65. * @return integer
  66. */
  67. function makeBuildNumber($version,$dir)
  68. {
  69.     $newestFileTS = getHighestFileTimestamp($dir,true);
  70.  
  71.     // TS is too large, subtract and version number + padding in thousands to get a build number
  72.     $verMx = intval(preg_replace('/[^0-9]/','',$version)) + 1257455304 * 1000;
  73.  
  74.     return ($newestFileTS - $verMx); 
  75. }
  76.  
  77. echo makeBuildNumber("2.0.1","/path/to/project/");
  78.  
  79.  
see if you can get a better formula to work for line number 74 above.




Dan
dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,094
#3: 2 Weeks Ago

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:

Expand|Select|Wrap|Line Numbers
  1.  
  2. $version = "2.0.2"; 
  3. $multiplier = end(explode(".",$version)) * $othervalues; 
  4.  
  5.  
ya? no? got a better idea?



Dan
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#4: 2 Weeks Ago

re: Version number and Build # generator for PHP


Quote:

Originally Posted by dlite922 View Post

ya? no? got a better idea?

I’d use the subversion (or whatever you use) revision number as build number.
dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,094
#5: 2 Weeks Ago

re: Version number and Build # generator for PHP


Quote:

Originally Posted by Dormilich View Post

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
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#6: 2 Weeks Ago

re: Version number and Build # generator for PHP


the latest revision number should be somewhere in .svn.
Reply

Tags
build number, version