Improved runtime and script classes

This commit is contained in:
Chris 2021-12-08 04:05:55 +01:00
parent 5af63f771a
commit c3e97ea4b1
4 changed files with 38 additions and 9 deletions

View File

@ -35,6 +35,7 @@
"symfony/finder": "^6.0", "symfony/finder": "^6.0",
"symfony/process": "^6.0", "symfony/process": "^6.0",
"psr/log": "^3.0", "psr/log": "^3.0",
"symfony/var-dumper": "^6.0" "symfony/var-dumper": "^6.0",
"symfony/yaml": "^6.0"
} }
} }

View File

@ -11,11 +11,13 @@ abstract class SparkPlug
return SparkApplication::$instance->getPluginManager()->getPlugin($name); return SparkApplication::$instance->getPluginManager()->getPlugin($name);
} }
function get_resource(string $name) { function getResource(string $name)
{
return SparkApplication::$instance->getResourceManager()->getNamedResource($name); return SparkApplication::$instance->getResourceManager()->getNamedResource($name);
} }
function read_config($file=null) { function readConfig($file=null)
{
if (!$file) return; if (!$file) return;
$abs = get_environment()->getConfigDirectory() . "/" . $file; $abs = get_environment()->getConfigDirectory() . "/" . $file;
if (!file_exists($abs)) { if (!file_exists($abs)) {
@ -29,5 +31,6 @@ abstract class SparkPlug
{ {
return SparkApplication::$instance->getEnvironment()->getProjectDirectory(); return SparkApplication::$instance->getEnvironment()->getProjectDirectory();
} }
} }

View File

@ -22,14 +22,21 @@ class RunCommand extends Command
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)
{ {
$env = $this->getEnvironment(); $env = $this->getEnvironment();
$args = $input->getArgument('args');
if ($input->getOption("list")) { if ($input->getOption("list") || empty($input->getArgument("script"))) {
$output->writeln($env->getDefinedScripts()); $scripts = $env->getDefinedScripts();
if (count($scripts) > 0) {
$output->writeln("<options=bold>Defined scripts:</>");
$output->writeln("- ".join("\n- ",$scripts));
} else {
$output->writeln("No scripts defined.");
}
return Command::SUCCESS; return Command::SUCCESS;
} elseif ($script = $input->getArgument('script')) { } elseif ($script = $input->getArgument('script')) {
$env->runScript($script, $input->getArgument('args'), $input, $output); $env->runScript($script, $args, $input, $output);
} }
return Command::SUCCESS; return Command::SUCCESS;
} }
} }

View File

@ -38,9 +38,26 @@ class Environment
public function runScript(string $name, array $args, InputInterface $input, OutputInterface $output) public function runScript(string $name, array $args, InputInterface $input, OutputInterface $output)
{ {
$script = $this->config['scripts'][$name]; $script = $this->config['scripts'][$name]??$name;
if (is_string($script)) { if (is_string($script)) {
$this->execScript($script, $args);
} elseif (is_array($script)) {
foreach ($script as $row) {
$a = str_getcsv($row, ' ', "'");
$c = array_shift($a);
if (str_starts_with($c, '@')) {
$c = ($this->config['scripts'][substr($c,1)])??$c;
$this->runScript($c, $a, $input, $output);
} else {
$this->execScript($c, $a);
}
}
}
}
private function execScript(string $script, array $args)
{
// call script directly // call script directly
if (str_ends_with($script, '.php')) { if (str_ends_with($script, '.php')) {
$GLOBALS['args'] = $args; $GLOBALS['args'] = $args;
@ -50,11 +67,12 @@ class Environment
fprintf(STDERR, "error: Could not find script file %s\n", $base."/".$script); fprintf(STDERR, "error: Could not find script file %s\n", $base."/".$script);
return; return;
} }
//echo "# ".$base."/".$script."\n";
include $base . "/" . $script; include $base . "/" . $script;
} else { } else {
//echo "$ {$script}\n";
passthru($script); passthru($script);
} }
}
} }
public function loadEnvironment() public function loadEnvironment()