Compare commits
30 Commits
Author | SHA1 | Date |
---|---|---|
|
fcfa71aa0c | |
|
22ec477ddf | |
|
82ad2216da | |
|
4a85fa9b4d | |
|
d74b15c7ce | |
|
9c4c4521d8 | |
|
63dee1f015 | |
|
5f52f0a4ad | |
|
20b3d26ece | |
|
7e3927cb72 | |
|
f5d1c418ed | |
|
3b466a105f | |
|
307d25e303 | |
|
7257fa920f | |
|
ae55e3ffb9 | |
|
e4ba8a762f | |
|
6c67e9ab37 | |
|
9ffca32323 | |
|
9cfff757c3 | |
|
3e064092c7 | |
|
39ecdd39f8 | |
|
c26d06472c | |
|
891db33a8f | |
|
773bc1b5d9 | |
|
df99a32846 | |
|
e3e377d20a | |
|
4263855239 | |
|
4c32ed0d08 | |
|
852b90dc7a | |
|
09c71a0469 |
|
@ -0,0 +1,29 @@
|
|||
|
||||
name: Publish PHP Package
|
||||
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- 'v*'
|
||||
|
||||
jobs:
|
||||
docker:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Get tag name
|
||||
run: echo "TAG=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
|
||||
|
||||
- name: zip and send
|
||||
run: |
|
||||
ls -la
|
||||
apt-get update -y
|
||||
apt-get install -y zip
|
||||
cd ../
|
||||
zip -r ${{ gitea.event.repository.name }}.zip ${{ gitea.event.repository.name }} -x '*.git*'
|
||||
curl -v \
|
||||
--user composer-pbl:${{ secrets.COMPOSER_PSWD }} \
|
||||
--upload-file ${{ gitea.event.repository.name }}.zip \
|
||||
https://git.bit5.ru/api/packages/bit/composer?version=${{ env.TAG }}
|
239
bhl.inc.php
239
bhl.inc.php
|
@ -12,42 +12,94 @@ task('bhl_clean_cache', function()
|
|||
bhl_clean_cache();
|
||||
});
|
||||
|
||||
function bhl_proj_file()
|
||||
class BhlProj
|
||||
{
|
||||
public static ?BhlProj $active = null;
|
||||
|
||||
public string $file_path;
|
||||
public array $inc_dirs = array();
|
||||
public array $src_dirs = array();
|
||||
public array $defines = array();
|
||||
public array $bindings_sources;
|
||||
public array $postproc_sources;
|
||||
public string $postproc_dll;
|
||||
public string $bindings_dll;
|
||||
public string $result_file;
|
||||
public string $error_file;
|
||||
public string $tmp_dir;
|
||||
public int $max_threads;
|
||||
public bool $deterministic;
|
||||
|
||||
function getIncPath() : array
|
||||
{
|
||||
if($this->inc_dirs)
|
||||
return $this->inc_dirs;
|
||||
return $this->src_dirs;
|
||||
}
|
||||
}
|
||||
|
||||
function bhl_proj_file() : string
|
||||
{
|
||||
return get('BHL_PROJ_FILE');
|
||||
}
|
||||
|
||||
function bhl_proj()
|
||||
function bhl_proj_load(string $proj_file) : BhlProj
|
||||
{
|
||||
global $GAME_ROOT;
|
||||
$arr = json_decode(ensure_read($proj_file), true);
|
||||
if(!$arr)
|
||||
throw new Exception("Bad bhl project file: $proj_file");
|
||||
|
||||
static $proj;
|
||||
if(!$proj)
|
||||
$proj = new BhlProj();
|
||||
foreach($arr as $k => $v)
|
||||
$proj->{$k} = $v;
|
||||
|
||||
//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)
|
||||
$proj->src_dirs[$k] = _bhl_make_abs_path($proj_file, $v);
|
||||
|
||||
foreach($proj->bindings_sources as $k => $v)
|
||||
$proj->bindings_sources[$k] = _bhl_make_abs_path($proj_file, $v);
|
||||
$proj->bindings_dll = _bhl_make_abs_path($proj_file, $proj->bindings_dll);
|
||||
|
||||
if(isset($proj->postproc_sources))
|
||||
{
|
||||
$proj_file = bhl_proj_file();
|
||||
$proj = json_decode(ensure_read($proj_file));
|
||||
if(!$proj)
|
||||
throw new Exception("Bad bhl project file: $proj_file");
|
||||
|
||||
foreach($proj->src_dirs as $k => $v)
|
||||
$proj->src_dirs[$k] = _bhl_make_abs_path($proj_file, $v);
|
||||
|
||||
foreach($proj->bindings_sources as $k => $v)
|
||||
$proj->bindings_sources[$k] = _bhl_make_abs_path($proj_file, $v);
|
||||
$proj->bindings_dll= _bhl_make_abs_path($proj_file, $proj->bindings_dll);
|
||||
|
||||
foreach($proj->postproc_sources as $k => $v)
|
||||
$proj->postproc_sources[$k] = _bhl_make_abs_path($proj_file, $v);
|
||||
$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->error_file = _bhl_make_abs_path($proj_file, $proj->error_file);
|
||||
$proj->tmp_dir = _bhl_make_abs_path($proj_file, $proj->tmp_dir);
|
||||
}
|
||||
|
||||
if(isset($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->error_file = _bhl_make_abs_path($proj_file, $proj->error_file);
|
||||
$proj->tmp_dir = _bhl_make_abs_path($proj_file, $proj->tmp_dir);
|
||||
|
||||
return $proj;
|
||||
}
|
||||
|
||||
function _bhl_make_abs_path($proj_file, $path)
|
||||
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(string $proj_file, string $path) : string
|
||||
{
|
||||
if($path && $path[0] == '.')
|
||||
return dirname($proj_file) . '/' . $path;
|
||||
|
@ -55,53 +107,44 @@ function _bhl_make_abs_path($proj_file, $path)
|
|||
return $path;
|
||||
}
|
||||
|
||||
function bhl_result_file()
|
||||
function bhl_result_file() : string
|
||||
{
|
||||
global $GAME_ROOT;
|
||||
return bhl_proj()->result_file;
|
||||
}
|
||||
|
||||
function bhl_dir()
|
||||
function bhl_dir() : string
|
||||
{
|
||||
global $GAME_ROOT;
|
||||
return "$GAME_ROOT/composer/vendor/bit/bhl";
|
||||
}
|
||||
|
||||
function bhl_shell($cmd, &$ret_var, &$ret_out)
|
||||
function bhl_shell(string $cmd, &$ret_var, &$ret_out)
|
||||
{
|
||||
$prev_path = mono_try_override_path();
|
||||
|
||||
try
|
||||
{
|
||||
shell_try(bhl_dir()."/bhl $cmd", $ret_var, $ret_out);
|
||||
}
|
||||
finally
|
||||
{
|
||||
mono_try_restore_path($prev_path);
|
||||
}
|
||||
shell_try(bhl_dir()."/bhl $cmd", $ret_var, $ret_out);
|
||||
}
|
||||
|
||||
function bhl_shell_ensure($cmd)
|
||||
function bhl_shell_ensure(string $cmd)
|
||||
{
|
||||
bhl_shell($cmd, $ret_var, $ret_out);
|
||||
if($ret_var !== 0)
|
||||
throw new Exception("Error executing shell cmd: $ret_var");
|
||||
}
|
||||
|
||||
function bhl_scan_files()
|
||||
function bhl_scan_files() : array
|
||||
{
|
||||
return scan_files_rec(bhl_proj()->src_dirs, array('.bhl'));
|
||||
}
|
||||
|
||||
function bhl_run($debug = true, $force = false, $exit_on_err = true)
|
||||
function bhl_run(bool $debug = true, bool $force = false, bool $exit_on_err = true)
|
||||
{
|
||||
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()))
|
||||
{
|
||||
bhl_shell("compile -p " . bhl_proj_file() . " " .
|
||||
bhl_shell("compile -p " . $bhl_proj->file_path . " " .
|
||||
($debug ? " -d" : "") . " " .
|
||||
($force || !getor("BHL_USE_CACHE", true) ? " -C" : ""),
|
||||
$ret_var, $ret_out
|
||||
|
@ -109,22 +152,20 @@ function bhl_run($debug = true, $force = false, $exit_on_err = true)
|
|||
|
||||
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)
|
||||
return false;
|
||||
}
|
||||
else
|
||||
echo "BHL BUNDLE: total " . kb_len(filesize($result_file)) . "\n";
|
||||
else if($debug)
|
||||
echo "BHL BUNDLE: total " . kb_len(filesize($result_file)) . ", CRC " . hexdec(hash_file('CRC32', $result_file, false)) . "\n";
|
||||
}
|
||||
|
||||
@touch($result_file);
|
||||
}
|
||||
|
||||
function bhl_handle_error_result(array $ret_out, $err_file, $exit = true)
|
||||
function bhl_handle_error_result(array $ret_out, string $err_file, bool $exit = true)
|
||||
{
|
||||
if(!is_file($err_file))
|
||||
{
|
||||
stderr("Something went wrong: " . (is_array($ret_out) ? implode("\n", $ret_out) : "????"));
|
||||
stderr("Something went wrong: " . implode("\n", $ret_out));
|
||||
if($exit)
|
||||
exit(1);
|
||||
}
|
||||
|
@ -158,7 +199,7 @@ function bhl_handle_error_result(array $ret_out, $err_file, $exit = true)
|
|||
function bhl_clean()
|
||||
{
|
||||
bhl_clean_cache();
|
||||
bhl_shell_ensure("clean");
|
||||
bhl_shell("clean", $ret, $out);
|
||||
}
|
||||
|
||||
function bhl_clean_cache()
|
||||
|
@ -166,7 +207,7 @@ function bhl_clean_cache()
|
|||
ensure_rm(bhl_proj()->tmp_dir);
|
||||
}
|
||||
|
||||
function bhl_show_position($line, $row, array $lines)
|
||||
function bhl_show_position(int $line, int $row, array $lines) : string
|
||||
{
|
||||
if($line > 0 && $line <= count($lines))
|
||||
{
|
||||
|
@ -186,7 +227,7 @@ function bhl_show_position($line, $row, array $lines)
|
|||
return "??? @($line:$row)";
|
||||
}
|
||||
|
||||
function bhl_line_row_to_pos($file, $line, $row)
|
||||
function bhl_line_row_to_pos(string $file, int $line, int $row) : ?int
|
||||
{
|
||||
$pos = 0;
|
||||
$lines = file($file);
|
||||
|
@ -199,9 +240,10 @@ function bhl_line_row_to_pos($file, $line, $row)
|
|||
return $pos;
|
||||
}
|
||||
|
||||
function bhl_map_module_to_file($module)
|
||||
function bhl_map_module_to_file(string $module) : ?string
|
||||
{
|
||||
foreach(bhl_proj()->src_dirs as $dir)
|
||||
$bhl_proj = bhl_proj();
|
||||
foreach($bhl_proj->getIncPath() as $dir)
|
||||
{
|
||||
$tmp = $dir.'/'.$module.'.bhl';
|
||||
if(file_exists($tmp))
|
||||
|
@ -210,17 +252,40 @@ function bhl_map_module_to_file($module)
|
|||
return null;
|
||||
}
|
||||
|
||||
function bhl_validate_func_ref($module, $func, array $signature)
|
||||
function bhl_validate_func_ref(string $module_file, string $func_full_name, array $signature)
|
||||
{
|
||||
if(sizeof($signature) == 0)
|
||||
throw new Exception("Signature is invalid");
|
||||
|
||||
$module_file = bhl_map_module_to_file($module);
|
||||
|
||||
if(!$module_file)
|
||||
throw new Exception("Module not found '{$module}'");
|
||||
$name_items = explode('.', $func_full_name);
|
||||
$func = array_pop($name_items);
|
||||
$namespace = implode('.', $name_items);
|
||||
|
||||
$module_src = file_get_contents($module_file);
|
||||
$module_src = _bhl_remove_comments($module_src);
|
||||
|
||||
if(!$module_src)
|
||||
throw new Exception("Bad module file '{$module_file}'");
|
||||
|
||||
$ns_chunks = _bhl_split_by_namespaces($module_src);
|
||||
|
||||
$module_chunks = array();
|
||||
if($namespace)
|
||||
{
|
||||
if(!isset($ns_chunks[$namespace]))
|
||||
throw new Exception("No namespace '$namespace' found in '$module_file'");
|
||||
|
||||
foreach($ns_chunks[$namespace] as $ns_src)
|
||||
$module_chunks[] = $ns_src;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(!isset($ns_chunks['']))
|
||||
throw new Exception("No global namespace found in '$module_file'");
|
||||
|
||||
foreach($ns_chunks[''] as $ns_src)
|
||||
$module_chunks[] = $ns_src;
|
||||
}
|
||||
|
||||
$signature_pattern = '';
|
||||
$signature_pattern .= '~func\s+';
|
||||
|
@ -243,6 +308,58 @@ function bhl_validate_func_ref($module, $func, array $signature)
|
|||
$signature_pattern .= '\s*\)';
|
||||
$signature_pattern .= '~';
|
||||
|
||||
if(!preg_match($signature_pattern, $module_src))
|
||||
throw new Exception("Func '$func' signature '".implode(',', $signature)."' not found in module '$module'");
|
||||
foreach($module_chunks as $module_chunk_src)
|
||||
{
|
||||
if(preg_match($signature_pattern, $module_chunk_src))
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception("Func '$func_full_name(".implode(',', $signature).")' not found in '$module_file'");
|
||||
}
|
||||
|
||||
function _bhl_remove_comments(string $txt) : string
|
||||
{
|
||||
//block comments
|
||||
if(strpos($txt, '/*') !== false)
|
||||
{
|
||||
$regex = '~/\*.*?\*/~s';
|
||||
$txt = preg_replace_callback(
|
||||
$regex,
|
||||
//preserve the new lines for better error reporting
|
||||
function($m) { return str_repeat("\n", substr_count($m[0], "\n")); },
|
||||
$txt);
|
||||
}
|
||||
//line comments
|
||||
$txt = preg_replace("~\s*(?<!:)//.*~", "\n", $txt);
|
||||
return $txt;
|
||||
}
|
||||
|
||||
function _bhl_split_by_namespaces(string $src) : array
|
||||
{
|
||||
$nss = array();
|
||||
|
||||
$chunks = preg_split('~^\s*(namespace\s+[^\{]+){~m', $src, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
|
||||
|
||||
for($i=0;$i<count($chunks);)
|
||||
{
|
||||
$chunk = $chunks[$i];
|
||||
if(strpos(ltrim($chunk), 'namespace ') === 0)
|
||||
{
|
||||
$ns = trim(substr(ltrim($chunk), 9));
|
||||
$body = $chunks[$i+1];
|
||||
if(!isset($nss[$ns]))
|
||||
$nss[$ns] = array();
|
||||
$nss[$ns][] = $body;
|
||||
$i+=2;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(!isset($nss['']))
|
||||
$nss[''] = array();
|
||||
$nss[''][] = $chunk;
|
||||
++$i;
|
||||
}
|
||||
}
|
||||
|
||||
return $nss;
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
namespace metagen_php;
|
||||
use Exception;
|
||||
|
||||
function flt_bhl_ref($val, $name, $struct, $args)
|
||||
function flt_bhl_ref($val, $name, $data, $str_args)
|
||||
{
|
||||
if(!$val)
|
||||
return $val;
|
||||
|
@ -10,14 +10,16 @@ function flt_bhl_ref($val, $name, $struct, $args)
|
|||
if(!isset($val['module']))
|
||||
throw new Exception("Missing 'module'");
|
||||
|
||||
$module_file = \taskman\bhl_map_module_to_file($val['module']);
|
||||
if(!$module_file)
|
||||
throw new Exception("Module not found '{$val['module']}");
|
||||
|
||||
if(!isset($val['fn']))
|
||||
throw new Exception("Missing 'fn'");
|
||||
|
||||
$signature = explode(",", $args);
|
||||
if(!$signature)
|
||||
throw new Exception("Invalid signature: $args");
|
||||
$args = explode(",", $str_args);
|
||||
|
||||
\taskman\bhl_validate_func_ref($val['module'], $val['fn'], $signature);
|
||||
\taskman\bhl_validate_func_ref($module_file, $val['fn'], $args);
|
||||
|
||||
return $val;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @global
|
||||
*/
|
||||
function macro_BHL_REF($proc, $module, $func, $signature_json = '')
|
||||
{
|
||||
//check if it's a relative path
|
||||
if($module[0] == '.')
|
||||
{
|
||||
$abs_module = \taskman\normalize_path(dirname($proc->getRootFile()) . '/' . $module);
|
||||
$mapped = false;
|
||||
|
||||
$bhl_proj = \taskman\bhl_proj();
|
||||
$inc_path = $bhl_proj->getIncPath();
|
||||
|
||||
foreach($inc_path as $dir)
|
||||
{
|
||||
$rel_module = str_replace(\taskman\normalize_path($dir), '', $abs_module);
|
||||
if($rel_module != $abs_module)
|
||||
{
|
||||
$mapped = true;
|
||||
$rel_module = str_replace('\\', '/', $rel_module);
|
||||
$module = "/" . $rel_module;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!$mapped)
|
||||
throw new Exception("Could not map relative module: $module");
|
||||
}
|
||||
|
||||
$module_file = \taskman\bhl_map_module_to_file($module);
|
||||
if(!$module_file)
|
||||
throw new Exception("Module not found '{$module}'");
|
||||
|
||||
//validate signature only if it's explicitely passed
|
||||
if($signature_json)
|
||||
{
|
||||
$signature = json_decode($signature_json, false);
|
||||
if(!is_array($signature))
|
||||
throw new Exception("Signature is invalid '$module'");
|
||||
\taskman\bhl_validate_func_ref($module_file, $func, $signature);
|
||||
}
|
||||
|
||||
return array('module' => ltrim($module, '/'), 'fn' => $func);
|
||||
}
|
||||
|
|
@ -4,8 +4,5 @@
|
|||
"homepage": "https://git.bit5.ru/composer/taskman_bhl",
|
||||
"require": {
|
||||
"php": ">=7.4"
|
||||
},
|
||||
"autoload": {
|
||||
"classmap": ["bhl.inc.php"]
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue