Instead of passing BhlProj to routines we rather make it possible to set the active one

This commit is contained in:
Pavel Shevaev 2023-11-24 13:43:39 +03:00
parent 9ffca32323
commit 6c67e9ab37
2 changed files with 84 additions and 61 deletions

View File

@ -16,14 +16,12 @@ task('bhl_make_upm_package', function() {
bhl_make_upm_package(); bhl_make_upm_package();
}); });
function bhl_proj_file()
{
return get('BHL_PROJ_FILE');
}
class BhlProj class BhlProj
{ {
public static ?BhlProj $active = null;
public string $file_path; public string $file_path;
public array $inc_dirs = array();
public array $src_dirs = array(); public array $src_dirs = array();
public array $bindings_sources; public array $bindings_sources;
public array $postproc_sources; public array $postproc_sources;
@ -34,17 +32,21 @@ class BhlProj
public string $tmp_dir; public string $tmp_dir;
public int $max_threads; public int $max_threads;
public bool $deterministic; public bool $deterministic;
function getIncPath() : array
{
if($this->inc_dirs)
return $this->inc_dirs;
return $this->src_dirs;
}
} }
function bhl_proj(?string $proj_file = null) : BhlProj function bhl_proj_file() : string
{ {
global $GAME_ROOT; return get('BHL_PROJ_FILE');
}
static $projs = []; function bhl_proj_load(string $proj_file) : BhlProj
$proj_file ??= bhl_proj_file();
if(!isset($projs[$proj_file]))
{ {
$arr = json_decode(ensure_read($proj_file), true); $arr = json_decode(ensure_read($proj_file), true);
if(!$arr) if(!$arr)
@ -54,7 +56,11 @@ function bhl_proj(?string $proj_file = null) : BhlProj
foreach($arr as $k => $v) foreach($arr as $k => $v)
$proj->{$k} = $v; $proj->{$k} = $v;
$proj->file_path = $proj_file; //NOTE: adding path to the file for convenience //NOTE: adding path to the file for further convenience
$proj->file_path = $proj_file;
foreach($proj->inc_dirs as $k => $v)
$proj->inc_dirs[$k] = _bhl_make_abs_path($proj_file, $v);
foreach($proj->src_dirs as $k => $v) foreach($proj->src_dirs as $k => $v)
$proj->src_dirs[$k] = _bhl_make_abs_path($proj_file, $v); $proj->src_dirs[$k] = _bhl_make_abs_path($proj_file, $v);
@ -76,9 +82,24 @@ function bhl_proj(?string $proj_file = null) : BhlProj
$proj->error_file = _bhl_make_abs_path($proj_file, $proj->error_file); $proj->error_file = _bhl_make_abs_path($proj_file, $proj->error_file);
$proj->tmp_dir = _bhl_make_abs_path($proj_file, $proj->tmp_dir); $proj->tmp_dir = _bhl_make_abs_path($proj_file, $proj->tmp_dir);
$projs[$proj_file] = $proj; return $proj;
} }
return $projs[$proj_file];
function bhl_proj_set_active(BhlProj $proj) : ?BhlProj
{
$prev = BhlProj::$active;
BhlProj::$active = $proj;
return $prev;
}
function bhl_proj() : BhlProj
{
if(BhlProj::$active == null)
{
$proj = bhl_proj_load(bhl_proj_file());
BhlProj::$active = $proj;
}
return BhlProj::$active;
} }
function _bhl_make_abs_path($proj_file, $path) function _bhl_make_abs_path($proj_file, $path)
@ -89,10 +110,9 @@ function _bhl_make_abs_path($proj_file, $path)
return $path; return $path;
} }
function bhl_result_file(BhlProj $bhl_proj = null) function bhl_result_file()
{ {
$bhl_proj ??= bhl_proj(); return bhl_proj()->result_file;
return $bhl_proj->result_file;
} }
function bhl_dir() function bhl_dir()
@ -122,20 +142,19 @@ function bhl_shell_ensure($cmd)
throw new Exception("Error executing shell cmd: $ret_var"); throw new Exception("Error executing shell cmd: $ret_var");
} }
function bhl_scan_files(BhlProj $bhl_proj = null) function bhl_scan_files()
{ {
$bhl_proj ??= bhl_proj(); return scan_files_rec(bhl_proj()->src_dirs, array('.bhl'));
return scan_files_rec($bhl_proj->src_dirs, array('.bhl'));
} }
function bhl_run($debug = true, $force = false, $exit_on_err = true, BhlProj $bhl_proj = null) function bhl_run($debug = true, $force = false, $exit_on_err = true)
{ {
global $GAME_ROOT; global $GAME_ROOT;
$bhl_proj ??= bhl_proj(); $bhl_proj = bhl_proj();
$result_file = $bhl_proj->result_file; $result_file = $bhl_proj->result_file;
if($force || need_to_regen($result_file, bhl_scan_files($bhl_proj))) if($force || need_to_regen($result_file, bhl_scan_files()))
{ {
bhl_shell("compile -p " . $bhl_proj->file_path . " " . bhl_shell("compile -p " . $bhl_proj->file_path . " " .
($debug ? " -d" : "") . " " . ($debug ? " -d" : "") . " " .
@ -191,16 +210,15 @@ function bhl_handle_error_result(array $ret_out, $err_file, $exit = true)
exit(1); exit(1);
} }
function bhl_clean(BhlProj $bhl_proj = null) function bhl_clean()
{ {
bhl_clean_cache($bhl_proj); bhl_clean_cache();
bhl_shell_ensure("clean"); bhl_shell_ensure("clean");
} }
function bhl_clean_cache(BhlProj $bhl_proj = null) function bhl_clean_cache()
{ {
$bhl_proj ??= bhl_proj(); ensure_rm(bhl_proj()->tmp_dir);
ensure_rm($bhl_proj->tmp_dir);
} }
function bhl_show_position($line, $row, array $lines) function bhl_show_position($line, $row, array $lines)
@ -236,10 +254,10 @@ function bhl_line_row_to_pos($file, $line, $row)
return $pos; return $pos;
} }
function bhl_map_module_to_file($module, BhlProj $bhl_proj = null) function bhl_map_module_to_file($module)
{ {
$bhl_proj ??= bhl_proj(); $bhl_proj = bhl_proj();
foreach($bhl_proj->src_dirs as $dir) foreach($bhl_proj->getIncPath() as $dir)
{ {
$tmp = $dir.'/'.$module.'.bhl'; $tmp = $dir.'/'.$module.'.bhl';
if(file_exists($tmp)) if(file_exists($tmp))

View File

@ -5,11 +5,16 @@
*/ */
function macro_BHL_REF($proc, $module, $func, $signature_json = '') function macro_BHL_REF($proc, $module, $func, $signature_json = '')
{ {
if($module[0] !== '/') //check if it's a relative path
if($module[0] == '.')
{ {
$abs_module = \taskman\normalize_path(dirname($proc->getRootFile()) . '/' . $module); $abs_module = \taskman\normalize_path(dirname($proc->getRootFile()) . '/' . $module);
$mapped = false; $mapped = false;
foreach(\taskman\bhl_proj()->src_dirs as $dir)
$bhl_proj = \taskman\bhl_proj();
$inc_path = isset($bhl_proj->inc_dirs) ? $bhl_proj->inc_dirs : $bhl_proj->src_dirs;
foreach($inc_path as $dir)
{ {
$rel_module = str_replace(\taskman\normalize_path($dir), '', $abs_module); $rel_module = str_replace(\taskman\normalize_path($dir), '', $abs_module);
if($rel_module != $abs_module) if($rel_module != $abs_module)