Multiple projects support

This commit is contained in:
wrenge 2023-11-09 13:02:47 +03:00
parent c26d06472c
commit 39ecdd39f8
1 changed files with 39 additions and 23 deletions

View File

@ -1,6 +1,7 @@
<?php <?php
namespace taskman; namespace taskman;
use Exception; use Exception;
use stdClass;
task('bhl_clean', function() task('bhl_clean', function()
{ {
@ -21,18 +22,22 @@ function bhl_proj_file()
return get('BHL_PROJ_FILE'); return get('BHL_PROJ_FILE');
} }
function bhl_proj() function bhl_proj(?string $proj_file = null)
{ {
global $GAME_ROOT; global $GAME_ROOT;
static $proj; static $projs = [];
if(!$proj)
$proj_file ??= bhl_proj_file();
if(!isset($projs[$proj_file]))
{ {
$proj_file = bhl_proj_file();
$proj = json_decode(ensure_read($proj_file)); $proj = json_decode(ensure_read($proj_file));
if(!$proj) if(!$proj)
throw new Exception("Bad bhl project file: $proj_file"); throw new Exception("Bad bhl project file: $proj_file");
$proj->file_path = $proj_file; //NOTE: adding path to the file for convenience
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);
@ -40,15 +45,22 @@ function bhl_proj()
$proj->bindings_sources[$k] = _bhl_make_abs_path($proj_file, $v); $proj->bindings_sources[$k] = _bhl_make_abs_path($proj_file, $v);
$proj->bindings_dll= _bhl_make_abs_path($proj_file, $proj->bindings_dll); $proj->bindings_dll= _bhl_make_abs_path($proj_file, $proj->bindings_dll);
if(isset($proj->postproc_sources))
{
foreach($proj->postproc_sources as $k => $v) foreach($proj->postproc_sources as $k => $v)
$proj->postproc_sources[$k] = _bhl_make_abs_path($proj_file, $v); $proj->postproc_sources[$k] = _bhl_make_abs_path($proj_file, $v);
}
if(isset($proj->postproc_dll))
$proj->postproc_dll = _bhl_make_abs_path($proj_file, $proj->postproc_dll); $proj->postproc_dll = _bhl_make_abs_path($proj_file, $proj->postproc_dll);
$proj->result_file = _bhl_make_abs_path($proj_file, $proj->result_file); $proj->result_file = _bhl_make_abs_path($proj_file, $proj->result_file);
$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_make_abs_path($proj_file, $path) function _bhl_make_abs_path($proj_file, $path)
@ -59,10 +71,10 @@ function _bhl_make_abs_path($proj_file, $path)
return $path; return $path;
} }
function bhl_result_file() function bhl_result_file(stdClass $bhl_proj = null)
{ {
global $GAME_ROOT; $bhl_proj ??= bhl_proj();
return bhl_proj()->result_file; return $bhl_proj->result_file;
} }
function bhl_dir() function bhl_dir()
@ -92,20 +104,22 @@ 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() function bhl_scan_files(stdClass $bhl_proj = null)
{ {
return scan_files_rec(bhl_proj()->src_dirs, array('.bhl')); $bhl_proj ??= bhl_proj();
return scan_files_rec($bhl_proj->src_dirs, array('.bhl'));
} }
function bhl_run($debug = true, $force = false, $exit_on_err = true) function bhl_run($debug = true, $force = false, $exit_on_err = true, stdClass $bhl_proj = null)
{ {
global $GAME_ROOT; global $GAME_ROOT;
$result_file = bhl_proj()->result_file; $bhl_proj ??= bhl_proj();
$result_file = $bhl_proj->result_file;
if($force || need_to_regen($result_file, bhl_scan_files())) if($force || need_to_regen($result_file, bhl_scan_files()))
{ {
bhl_shell("compile -p " . bhl_proj_file() . " " . bhl_shell("compile -p " . $bhl_proj->file_path . " " .
($debug ? " -d" : "") . " " . ($debug ? " -d" : "") . " " .
($force || !getor("BHL_USE_CACHE", true) ? " -C" : ""), ($force || !getor("BHL_USE_CACHE", true) ? " -C" : ""),
$ret_var, $ret_out $ret_var, $ret_out
@ -113,7 +127,7 @@ function bhl_run($debug = true, $force = false, $exit_on_err = true)
if($ret_var != 0) if($ret_var != 0)
{ {
bhl_handle_error_result($ret_out, bhl_proj()->error_file, $exit_on_err); bhl_handle_error_result($ret_out, $bhl_proj->error_file, $exit_on_err);
if(!$exit_on_err) if(!$exit_on_err)
return false; return false;
} }
@ -159,15 +173,16 @@ function bhl_handle_error_result(array $ret_out, $err_file, $exit = true)
exit(1); exit(1);
} }
function bhl_clean() function bhl_clean(stdClass $bhl_proj = null)
{ {
bhl_clean_cache(); bhl_clean_cache($bhl_proj);
bhl_shell_ensure("clean"); bhl_shell_ensure("clean");
} }
function bhl_clean_cache() function bhl_clean_cache(stdClass $bhl_proj = null)
{ {
ensure_rm(bhl_proj()->tmp_dir); $bhl_proj ??= bhl_proj();
ensure_rm($bhl_proj->tmp_dir);
} }
function bhl_show_position($line, $row, array $lines) function bhl_show_position($line, $row, array $lines)
@ -203,9 +218,10 @@ function bhl_line_row_to_pos($file, $line, $row)
return $pos; return $pos;
} }
function bhl_map_module_to_file($module) function bhl_map_module_to_file($module, stdClass $bhl_proj = null)
{ {
foreach(bhl_proj()->src_dirs as $dir) $bhl_proj ??= bhl_proj();
foreach($bhl_proj->src_dirs as $dir)
{ {
$tmp = $dir.'/'.$module.'.bhl'; $tmp = $dir.'/'.$module.'.bhl';
if(file_exists($tmp)) if(file_exists($tmp))