From 206ef1e2590b84d8ea0efa0007efaeafc257e815 Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Thu, 30 Dec 2021 00:55:30 +0100 Subject: [PATCH] Added devmode built-in --- src/Commands/DevCommand.php | 29 +++++++++++ src/DevMode/DevMode.php | 98 +++++++++++++++++++++++++++++++++++++ src/SparkApplication.php | 1 + 3 files changed, 128 insertions(+) create mode 100644 src/Commands/DevCommand.php create mode 100644 src/DevMode/DevMode.php diff --git a/src/Commands/DevCommand.php b/src/Commands/DevCommand.php new file mode 100644 index 0000000..fb0818a --- /dev/null +++ b/src/Commands/DevCommand.php @@ -0,0 +1,29 @@ +getEnvironment(); + + $devmode = new DevMode($env); + $devmode->run(); + + return Command::SUCCESS; + } +} diff --git a/src/DevMode/DevMode.php b/src/DevMode/DevMode.php new file mode 100644 index 0000000..d76294d --- /dev/null +++ b/src/DevMode/DevMode.php @@ -0,0 +1,98 @@ +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']); + } + + +} diff --git a/src/SparkApplication.php b/src/SparkApplication.php index d6ca5b9..f38f784 100644 --- a/src/SparkApplication.php +++ b/src/SparkApplication.php @@ -35,6 +35,7 @@ class SparkApplication extends Application if ($this->environment) { $this->environment->loadEnvironment(); + $this->add(new Commands\DevCommand()); $this->add(new Commands\RunCommand()); $this->add(new Commands\ResourcesCommand()); $this->add(new Commands\ReplCommand());