taskman_version/version.inc.php

97 lines
2.4 KiB
PHP

<?php
namespace taskman;
use Exception;
task('version', function()
{
echo "Version: " . game_version() . "\n";
echo "Version code:" . game_version_code() . "\n";
});
task('inc_game_version', function()
{
global $GAME_ROOT;
$path_to_version = $GAME_ROOT . "/VERSION";
ensure_write($path_to_version, get_inc_game_version());
git_try_commit($path_to_version, "inc version");
});
function game_version_full_str()
{
global $GAME_ROOT;
return trim(file($GAME_ROOT . "/VERSION")[0]);
}
function game_version()
{
$full_str = game_version_full_str();
$has_patch_code = substr_count($full_str, '.') > 2;
if(!$has_patch_code)
return $full_str;
return substr($full_str, 0, strrpos($full_str, '.'));
}
function game_version_code($str = null)
{
$str = $str === null ? game_version() : $str;
$items = explode('.', $str);
if(sizeof($items) < 2)
throw new Exception("Bad version format: $str");
$maj = $items[0];
$min = $items[1];
$pat = isset($items[2]) ? $items[2] : 0;
$num = $pat + $min*100 + $maj*1000000;
return $num;
}
function game_patch_code()
{
$full_str = game_version_full_str();
$has_patch_code = substr_count($full_str, '.') > 2;
if(!$has_patch_code)
return 0;
$code = substr($full_str, strrpos($full_str, '.')+1);
if($code > 9)
throw new Exception("Patch code must be <= 9");
return $code;
}
function get_inc_game_version()
{
$string = game_version();
for($i = strlen($string) - 1; $i >= 0; $i--)
{
if(is_numeric($string[$i]))
{
$most_significant_number = $i;
if($string[$i] < 9)
{
$string[$i] = $string[$i] + 1;
break;
}
// The number was a 9, set it to zero and continue.
$string[$i] = 0;
}
}
// If the most significant number was set to a zero it has overflowed so we
// need to prefix it with a '1'.
if($string[$most_significant_number] === '0')
$string = substr_replace($string, '1', $most_significant_number, 0);
return $string;
}
function get_version_file_lifetime()
{
global $GAME_ROOT;
exec("git log --follow --format=%ad --date=format:'%Y-%m-%d %H:%M:%S' " . $GAME_ROOT . "/VERSION" , $creating_timestamp);
$minutes = (time() - strtotime($creating_timestamp[0])) / 60;
return ceil($minutes);
}