Added devmode built-in

This commit is contained in:
Chris 2021-12-30 00:55:30 +01:00
parent 5a1268aed6
commit 206ef1e259
3 changed files with 128 additions and 0 deletions

View File

@ -0,0 +1,29 @@
<?php
namespace Spark\Commands;
use Symfony\Component\Console\Attribute\AsCommand;
use Spark\Commands\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Spark\DevMode\DevMode;
#[AsCommand(name:'dev', description:'Enter development mode')]
class DevCommand extends Command
{
protected function configure()
{
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$env = $this->getEnvironment();
$devmode = new DevMode($env);
$devmode->run();
return Command::SUCCESS;
}
}

98
src/DevMode/DevMode.php Normal file
View File

@ -0,0 +1,98 @@
<?php
namespace Spark\DevMode;
use Spark\Environment\Environment;
class DevMode
{
private Environment $env;
public function __construct(Environment $environment)
{
$this->env = $environment;
}
public function run()
{
$script = $this->env->getScriptRunner();
$conf = $this->getConfig();
$title = $conf['title'];
if ($title) {
passthru('xtitle '.escapeshellarg("starting: ".$title));
}
$startup = $conf['startup'];
if ($startup) {
$script->evaluate($startup);
}
if ($title) {
passthru('xtitle '.escapeshellarg($title));
}
$shell = $conf['shell'];
if (!$shell) {
$this->runDefaultShell();
} else {
passthru("exec ".$shell);
}
if ($title) {
passthru('xtitle '.escapeshellarg("stopping: ".$title));
}
$shutdown = $conf['shutdown'];
if ($shutdown) {
$script->evaluate($shutdown);
}
if ($title) {
passthru('xtitle');
}
}
private function runDefaultShell()
{
$init = $this->env->getProjectDirectory() . "/.devmoderc~";
$initrc = [
//"source \$HOME/.bashrc",
"test -f .spark/devmode.rc && source .spark/devmode.rc",
];
foreach ($_ENV as $e=>$v) {
$initrc[] = sprintf("export %s=%s", $e, escapeshellarg($v));
}
$proj = basename($this->env->getProjectDirectory());
$initrc[] = "function _dev_prompt {";
$initrc[] = " export PS1=\"\[\e[44;3m\] \[\e[1m\]dev\[\e[22;23m\] \[\e[36m\]{$proj}\[\e[39m\] \[\e[49m\] \w \$ \"";
$initrc[] = "}";
$initrc[] = "export PROMPT_COMMAND=_dev_prompt";
file_put_contents($init, join("\n", $initrc));
$cmdl = "exec bash --rcfile ".escapeshellarg($init);
passthru($cmdl);
unlink($init);
}
private function getConfig(): array
{
$defaults = [
'title'=> null,
'triggers' => [],
'startup' => null,
'shutdown' => null,
'shell' => null,
];
$conf = $this->env->readConfig("devmode.json");
if (!isset($conf['devmode'])) {
fprintf(STDERR, "Warning: Invalid devmode.json file\n");
return $defaults;
}
return array_merge($defaults, $conf['devmode']);
}
}

View File

@ -35,6 +35,7 @@ class SparkApplication extends Application
if ($this->environment) { if ($this->environment) {
$this->environment->loadEnvironment(); $this->environment->loadEnvironment();
$this->add(new Commands\DevCommand());
$this->add(new Commands\RunCommand()); $this->add(new Commands\RunCommand());
$this->add(new Commands\ResourcesCommand()); $this->add(new Commands\ResourcesCommand());
$this->add(new Commands\ReplCommand()); $this->add(new Commands\ReplCommand());