taskman_version/version.inc.php

101 lines
2.5 KiB
PHP
Raw Permalink Normal View History

2022-05-18 10:16:31 +03:00
<?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()
{
2022-08-17 13:16:55 +03:00
$path_to_version = get_path_to_version_file();
2022-05-18 10:16:31 +03:00
ensure_write($path_to_version, get_inc_game_version());
git_try_commit($path_to_version, "inc version");
});
function game_version_full_str()
{
2022-08-17 13:16:55 +03:00
return trim(file(get_path_to_version_file())[0]);
2022-05-18 10:16:31 +03:00
}
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;
}
2022-08-17 13:16:55 +03:00
function get_path_to_version_file()
2022-08-17 12:32:01 +03:00
{
2022-08-17 13:16:55 +03:00
global $GAME_ROOT;
return $GAME_ROOT . '/' . getor('VERSION_FILE_NAME', "VERSION");
2022-08-17 12:32:01 +03:00
}
2022-05-18 10:16:31 +03:00
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()
{
exec("git log --follow --format=%ad --date=format:\"%Y-%m-%d %H:%M:%S\" " . get_path_to_version_file() , $out, $ret);
2022-05-19 16:02:57 +03:00
if($ret != 0)
2022-08-17 12:22:44 +03:00
throw new Exception("git error");
2022-05-19 16:02:57 +03:00
$minutes = (time() - strtotime($out[0])) / 60;
return ceil($minutes);
}