Improved devmode

* Added --init option to generate a new configuration file
* Improved prompt
* Added support for generating a dynamic .env.local file
This commit is contained in:
Chris 2021-12-30 22:56:37 +01:00
parent 206ef1e259
commit 63aae2a504
2 changed files with 67 additions and 5 deletions

View File

@ -8,6 +8,8 @@ use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Helper\QuestionHelper;
use Spark\DevMode\DevMode; use Spark\DevMode\DevMode;
#[AsCommand(name:'dev', description:'Enter development mode')] #[AsCommand(name:'dev', description:'Enter development mode')]
@ -15,15 +17,61 @@ class DevCommand extends Command
{ {
protected function configure() protected function configure()
{ {
$this->addOption("init", null, InputOption::VALUE_NONE, "Initialize a new devmode.json file");
} }
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)
{ {
$env = $this->getEnvironment(); $env = $this->getEnvironment();
if ($input->getOption("init")) {
$conffile = $env->getConfigDirectory()."/devmode.json";
if (file_exists($conffile)) {
$output->writeln("<error>You have already created a devmode.json file</>");
return Command::FAILURE;
}
$this->initConfig($conffile, $input, $output);
return Command::SUCCESS;
}
$devmode = new DevMode($env); $devmode = new DevMode($env);
$devmode->run(); $devmode->run();
return Command::SUCCESS; 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);
}
} }

View File

@ -32,6 +32,15 @@ class DevMode
passthru('xtitle '.escapeshellarg($title)); 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']; $shell = $conf['shell'];
if (!$shell) { if (!$shell) {
$this->runDefaultShell(); $this->runDefaultShell();
@ -43,6 +52,10 @@ class DevMode
passthru('xtitle '.escapeshellarg("stopping: ".$title)); passthru('xtitle '.escapeshellarg("stopping: ".$title));
} }
if ($localenv) {
unlink($envlocal);
}
$shutdown = $conf['shutdown']; $shutdown = $conf['shutdown'];
if ($shutdown) { if ($shutdown) {
$script->evaluate($shutdown); $script->evaluate($shutdown);
@ -56,16 +69,16 @@ class DevMode
private function runDefaultShell() private function runDefaultShell()
{ {
$init = $this->env->getProjectDirectory() . "/.devmoderc~"; $init = $this->env->getProjectDirectory() . "/.devmoderc~";
$initrc = [ $initrc = [];
//"source \$HOME/.bashrc",
"test -f .spark/devmode.rc && source .spark/devmode.rc",
];
foreach ($_ENV as $e=>$v) { foreach ($_ENV as $e=>$v) {
$initrc[] = sprintf("export %s=%s", $e, escapeshellarg($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()); $proj = basename($this->env->getProjectDirectory());
$initrc[] = "function _dev_prompt {"; $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[] = "}";
$initrc[] = "export PROMPT_COMMAND=_dev_prompt"; $initrc[] = "export PROMPT_COMMAND=_dev_prompt";
file_put_contents($init, join("\n", $initrc)); file_put_contents($init, join("\n", $initrc));
@ -82,6 +95,7 @@ class DevMode
'triggers' => [], 'triggers' => [],
'startup' => null, 'startup' => null,
'shutdown' => null, 'shutdown' => null,
'localenv' => null,
'shell' => null, 'shell' => null,
]; ];