Multiple fixes

* Implemented ScriptRunner with environment expansion and cleaner
  code.
* Added ApiClient plugin (com.noccy.apiclient)
* Renamed CHANGELOG.md to VERSIONS.md
* Shuffled buildtools
* Added first unittests
This commit is contained in:
2021-12-11 01:44:01 +01:00
parent 8c6f7c1e93
commit 8cc1eac7a4
33 changed files with 1976 additions and 891 deletions

View File

@ -8,6 +8,7 @@ use Spark\SparkApplication;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Dotenv\Dotenv;
class Environment
{
@ -36,12 +37,23 @@ class Environment
return array_keys($this->config['scripts']??[]);
}
public function getScriptRunner(): ScriptRunner
{
$runner = new ScriptRunner();
$runner->setDirectory($this->getProjectDirectory());
foreach ((array)$this->config['scripts'] as $name => $script) {
$runner->defineScript($name, $script);
}
return $runner;
}
/*
public function runScript(string $name, array $args, InputInterface $input, OutputInterface $output)
{
$script = $this->config['scripts'][$name]??$name;
if (is_string($script)) {
$this->execScript($script, $args);
$this->execScript($script, $args, $output);
} elseif (is_array($script)) {
foreach ($script as $row) {
$a = str_getcsv($row, ' ', "'");
@ -50,13 +62,13 @@ class Environment
$c = ($this->config['scripts'][substr($c,1)])??$c;
$this->runScript($c, $a, $input, $output);
} else {
$this->execScript($c, $a);
$this->execScript($c, $a, $output);
}
}
}
}
private function execScript(string $script, array $args)
private function execScript(string $script, array $args, OutputInterface $output)
{
// call script directly
if (str_ends_with($script, '.php')) {
@ -74,6 +86,7 @@ class Environment
passthru($script);
}
}
*/
public function loadEnvironment()
{
@ -84,9 +97,24 @@ class Environment
if (!array_key_exists('project_dir', $this->config)) {
return;
}
chdir($this->config['project_dir']);
$envfile = $this->config['project_dir']."/.env";
if (file_exists($envfile)) {
$dotenv = new Dotenv();
$dotenv->load($envfile);
}
$blacklistFile = $this->config['config_dir']."/blacklist";
if (file_exists($blacklistFile)) {
$blacklist = json_decode(file_get_contents($blacklistFile), true);
}
if (empty($this->config['preload'])) {
fprintf(STDERR, "Error: Missing or malformed spark.json file.\n");
exit(1);
}
// $this->logger->info("Loading environment...");
$preloads = [];
$root = $this->config['project_dir'];
@ -102,7 +130,7 @@ class Environment
if (!str_starts_with($item, "/")) {
$item = $this->getProjectDirectory() . "/" . $item;
}
if (is_file($item)) {
if (is_file($item) && fnmatch("*.php", $item)) {
// $this->logger->debug("Preloading file {$item}");
try {
include_once($item);
@ -119,8 +147,8 @@ class Environment
//$this->logger->error("Error preloading plugin {$item}: {$t->getMessage()} in {$t->getFile()} on line {$t->getLine()}");
}
} else {
fprintf(STDERR, "warning: Could not preload %s\n", $item);
//$this->logger->warning("Could not preload {$item}");
// fprintf(STDERR, "warning: Could not preload %s\n", $item);
// $this->logger->warning("Could not preload {$item}");
}
}
@ -163,6 +191,11 @@ class Environment
return $env;
}
public function expandString(string $input): string
{
return preg_replace_callback('/([\%\$]\{(.+?)\}/', function ($v) {
print_r($v);
}, $input);
}
}

View File

@ -0,0 +1,91 @@
<?php
namespace Spark\Environment;
class ScriptRunner
{
private array $scripts = [];
private ?string $directory = null;
public function setDirectory(?string $directory)
{
$this->directory = $directory;
}
public function defineScript(string $name, string|array $script)
{
$this->scripts[$name] = $script;
}
public function evaluateDefinedScript(string $name)
{
$script = $this->scripts[$name];
$this->evaluate($script);
}
public function evaluate(string|array $script)
{
if (is_array($script)) {
foreach ($script as $step) {
$this->evaluate($step);
}
return;
}
$script = $this->expandString($script);
// Determine what to do
if (str_starts_with($script, '@')) {
// starts with @, call on a defined script
$subname = substr($script, 1);
$subscript = $this->scripts[$subname];
$this->evaluate($subscript);
} else {
if (posix_isatty(STDOUT)) {
printf("\e[0;33m> \e[0;93m%s\e[0m\n", $script);
} else {
printf("> %s\n", $script);
}
if (str_contains($script, ' ')) {
[$script, $args] = explode(" ", $script, 2);
$args = str_getcsv($args, ' ', "'");
} else {
$args = [];
}
if (is_callable($script)) {
// call script
call_user_func($script, ...$args);
} elseif (file_exists((string)$script) && fnmatch("*.php", (string)$script)) {
include $script;
} else {
// call shell
$cmdl = trim(escapeshellcmd((string)$script) . " " . join(" ", array_map("escapeshellarg", $args)));
$proc = proc_open($cmdl, [ 0 => STDIN, 1 => STDOUT, 2 => STDERR ], $pipes, $this->directory);
while ($stat = proc_get_status($proc)) {
if ($stat['running'] === false) {
$ec = $stat['exitcode'];
if ($ec != 0) {
printf("\e[31mcommand exited with code %d.\e[0m\n", $stat['exitcode']);
throw new \RuntimeException("Command {$cmdl} exited with code {$ec}");
}
break;
}
usleep(100000);
}
}
}
}
public function expandString(string $input)
{
return preg_replace_callback('/(\$\{(.+?)\})/', function ($match) {
return ($_ENV[$match[2]]??getenv($match[2]))??null;
}, $input);
}
}