Version number and Build # generator for PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #1

    Version number and Build # generator for PHP

    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
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    current test code I'm playing around with:

    Code:
    
    <?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

    Comment

    • dlite922
      Recognized Expert Top Contributor
      • Dec 2007
      • 1586

      #3
      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:

      Code:
      $version = "2.0.2"; 
      $multiplier = end(explode(".",$version)) * $othervalues;
      ya? no? got a better idea?



      Dan

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        Originally posted by dlite922
        ya? no? got a better idea?
        I’d use the subversion (or whatever you use) revision number as build number.

        Comment

        • dlite922
          Recognized Expert Top Contributor
          • Dec 2007
          • 1586

          #5
          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

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            the latest revision number should be somewhere in .svn.

            Comment

            Working...