Functions.php
<?php
namespace Tlf\Scrawl;
class BashFunctionsExt extends \Tlf\Scrawl\ExtensionType\DefaultExt /*\Tlf\Scrawl\Extension*/ {
protected $regexes = [
'function' => '/\ *function\ *([a-zA-Z\_].+)\(\)/'
];
protected $funcList = [];
public function __construct($scrawl){
parent::__construct($scrawl);
}
public function onSourceFileFound($srcFile){
if ($srcFile->ext!='bash')return;
// $dir = substr($srcFile->relPath,0,strpos($srcFile->relPath,'/'));
// $core = 'code/core'
$dir = dirname($srcFile->relPath);
if ($dir=='code/core'){
$this->coreDirFile($srcFile);
}
}
public function coreDirFile($srcFile){
$content = $srcFile->content();
$lexer = new \Tlf\Lexer();
$lexer->addGrammar(new \Tlf\Lexer\BashGrammar());
$ast = $lexer->lex($srcFile->path);
unset($ast->source);
$tree = $ast->getTree();
$start = 'code/core/';
$len = strlen($start);
if (substr($srcFile->relPath, 0,$len)==$start){
if (!isset($tree['function'])){
$relPath = $srcFile->relPath;
echo "There was a parsing error & functions were not found for $relPath";
print_r($tree);
exit;
}
foreach ($tree['function'] as $func){
$this->scrawl->addOutput('bent_function', $func['name'], $func);
}
}
}
public function onSourceFilesDone(){
// print_r($this->scrawl->getOutputs('function'));
// exit;
$funcs = $this->scrawl->getOutputs('bent_function');
// print_r(array_keys($funcs));
// exit;
$byGroup = [];
foreach ($funcs as $f){
$parts = explode('_', $f['name']);
$group = $parts[0];
// if ($group!='log')continue;
if ($group==$f['name'])$byGroup[$group]['main']=$f;
else $byGroup[$group][] = $f;
}
// print_r(array_keys($byGroup));
// exit;
$mains = [];
/** Generate help menu for each command group
*/
foreach ($byGroup as $group => $list){
$help = [];
if (!isset($list['main'])){
// var_dump($group);
// var_dump($list);
// exit;
// continue;
$list['main'] = [
'type'=>'function',
'name'=>$group,
'docblock'=>[
'type'=>'docblock',
'src'=>'--',
'description'=>'--',
],
];
}
$f = $list['main'];
// var_dump($f);
// exit;
unset($list['main']);
array_unshift($list,$f);
$mains[$f['name']] = $f;
$descript = $f['docblock']['tip'] ?? $f['docblock']['description'] ?? '';
$help[] = "# [bent $group] \${cOff}- $descript";
// if ($group=='log'){
// print_r($f);exit;
// }
foreach ($list as $function){
// "run core check" "check" "Check the status of your project"
// if (($function['docblock']??null)==null){
// $function['docblock']['']
// continue;
// }
$parts = explode('_', $function['name']);
$help[] = '${help_mode} '.implode(" ", $parts);
$help[] = implode(" ", count($parts)>1 ? array_slice($parts,1) : $parts);
$help[] = $function['docblock']['tip'] ?? $function['docblock']['description'] ?? '';
}
$help = array_map(function($v){return "\"$v\"";}, $help);
$helpStr ='prompt_choose_function '.implode(" ", $help);
$this->scrawl->addOutput('file', '../code/help/'.$group.'.bash', $helpStr);
}
uasort($mains,
function($a, $b){
$orderA = (int)($a['docblock']['order'] ?? 9999);
$orderB = (int)($b['docblock']['order'] ?? 9999);
if ($orderA > $orderB)return 1;
else if ($orderA == $orderB)return 0;
else return -1;
},
);
// print_r($mains);
// exit;
/** Generate help menu for [bent help] */
$help = [];
unset ($mains['']);
// $helpFunc = $mains['help'];
// unset($mains['help']);
// $help[] = "# ".($helpFunc['docblock']['description'] ?? $helpFunc['docblock']['tip'] ?? '');
foreach ($mains as $name=>$function){
$parts = explode('_', $function['name']);
$help[] = '${help_mode} '.implode(" ", $parts);
$help[] = implode(" ", array_slice($parts, 0,1));
$help[] = $function['docblock']['tip'] ?? $function['docblock']['description'] ?? '';
}
$help = array_map(function($v){return "\"$v\"";}, $help);
$helpStr = 'prompt_choose_function '.implode(" ", $help);
$this->scrawl->addOutput('file', '../code/help/help_groups.bash', $helpStr);
}
}