From 63aae2a5042e4e226a02ca96d742177ca8fae823 Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Thu, 30 Dec 2021 22:56:37 +0100 Subject: [PATCH] Improved devmode * Added --init option to generate a new configuration file * Improved prompt * Added support for generating a dynamic .env.local file --- src/Commands/DevCommand.php | 48 +++++++++++++++++++++++++++++++++++++ src/DevMode/DevMode.php | 24 +++++++++++++++---- 2 files changed, 67 insertions(+), 5 deletions(-) diff --git a/src/Commands/DevCommand.php b/src/Commands/DevCommand.php index fb0818a..a31e919 100644 --- a/src/Commands/DevCommand.php +++ b/src/Commands/DevCommand.php @@ -8,6 +8,8 @@ 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 Symfony\Component\Console\Question\Question; +use Symfony\Component\Console\Helper\QuestionHelper; use Spark\DevMode\DevMode; #[AsCommand(name:'dev', description:'Enter development mode')] @@ -15,15 +17,61 @@ class DevCommand extends Command { protected function configure() { + $this->addOption("init", null, InputOption::VALUE_NONE, "Initialize a new devmode.json file"); } protected function execute(InputInterface $input, OutputInterface $output) { $env = $this->getEnvironment(); + if ($input->getOption("init")) { + $conffile = $env->getConfigDirectory()."/devmode.json"; + if (file_exists($conffile)) { + $output->writeln("You have already created a devmode.json file"); + return Command::FAILURE; + } + $this->initConfig($conffile, $input, $output); + return Command::SUCCESS; + } + $devmode = new DevMode($env); $devmode->run(); return Command::SUCCESS; } + + private function initConfig(string $file, InputInterface $input, OutputInterface $output) + { + $qh = new QuestionHelper(); + + $titleQuestion = new Question("Window title []: ", null); + $title = $qh->ask($input, $output, $titleQuestion); + + $commandQuestion = new Question(" Command []: ", null); + + $startup = []; + $output->writeln("Startup commands. Enter an empty line to continue."); + while ($command = $qh->ask($input, $output, $commandQuestion)) { + $startup[] = $command; + } + + $shutdown = []; + $output->writeln("Shutdown commands. Enter an empty line to continue."); + while ($command = $qh->ask($input, $output, $commandQuestion)) { + $shutdown[] = $command; + } + + $json = [ + 'devmode' => [ + ] + ]; + if ($title) + $json['devmode']['title'] = $title; + $json['devmode']['startup'] = $startup; + $json['devmode']['shutdown'] = $shutdown; + + $out = json_encode($json, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES); + + file_put_contents($file, $out); + } } diff --git a/src/DevMode/DevMode.php b/src/DevMode/DevMode.php index d76294d..36ecbc3 100644 --- a/src/DevMode/DevMode.php +++ b/src/DevMode/DevMode.php @@ -32,6 +32,15 @@ class DevMode passthru('xtitle '.escapeshellarg($title)); } + $localenv = $conf['localenv']; + $envlocal = $this->env->getProjectDirectory()."/.env.local"; + if (is_array($localenv) && !file_exists($envlocal)) { + printf("* Creating temporary %s\n", $envlocal); + file_put_contents($envlocal, join("\n", $localenv)); + } else { + $localenv = null; + } + $shell = $conf['shell']; if (!$shell) { $this->runDefaultShell(); @@ -43,6 +52,10 @@ class DevMode passthru('xtitle '.escapeshellarg("stopping: ".$title)); } + if ($localenv) { + unlink($envlocal); + } + $shutdown = $conf['shutdown']; if ($shutdown) { $script->evaluate($shutdown); @@ -56,16 +69,16 @@ class DevMode private function runDefaultShell() { $init = $this->env->getProjectDirectory() . "/.devmoderc~"; - $initrc = [ - //"source \$HOME/.bashrc", - "test -f .spark/devmode.rc && source .spark/devmode.rc", - ]; + $initrc = []; foreach ($_ENV as $e=>$v) { $initrc[] = sprintf("export %s=%s", $e, escapeshellarg($v)); } + + //"source \$HOME/.bashrc", + $initrc[] = "test -f .spark/devmode.rc && source .spark/devmode.rc"; $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[] = " export PS1=\"\[\e[40;37m\] \u{26a1} \[\e[44;37m\] {$proj} \[\e[0m\] \w \$ \""; $initrc[] = "}"; $initrc[] = "export PROMPT_COMMAND=_dev_prompt"; file_put_contents($init, join("\n", $initrc)); @@ -82,6 +95,7 @@ class DevMode 'triggers' => [], 'startup' => null, 'shutdown' => null, + 'localenv' => null, 'shell' => null, ];