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)); } $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(); } else { passthru("exec ".$shell); } if ($title) { passthru('xtitle '.escapeshellarg("stopping: ".$title)); } if ($localenv) { unlink($envlocal); } $shutdown = $conf['shutdown']; if ($shutdown) { $script->evaluate($shutdown); } if ($title) { passthru('xtitle'); } } private function runDefaultShell() { $init = $this->env->getProjectDirectory() . "/.devmoderc~"; $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[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)); $cmdl = "exec bash --rcfile ".escapeshellarg($init); passthru($cmdl); unlink($init); } private function getConfig(): array { $defaults = [ 'title'=> null, 'triggers' => [], 'startup' => null, 'shutdown' => null, 'localenv' => 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']); } }