debug = $debug ?? defined('DEBUG_MODE'); } public function cd(string $dir): WindowsCmd { logger()->info('Entering dir: ' . $dir); $c = clone $this; $c->cd = $dir; return $c; } /** * @throws RuntimeException */ public function exec(string $cmd): WindowsCmd { /* @phpstan-ignore-next-line */ logger()->info(ConsoleColor::yellow('[EXEC] ') . ConsoleColor::green($cmd)); if ($this->cd !== null) { $cmd = 'cd /d ' . escapeshellarg($this->cd) . ' && ' . $cmd; } if (!$this->debug) { $cmd .= ' >nul 2>&1'; } echo $cmd . PHP_EOL; f_passthru($cmd); return $this; } public function execWithResult(string $cmd, bool $with_log = true): array { if ($with_log) { /* @phpstan-ignore-next-line */ logger()->info(ConsoleColor::blue('[EXEC] ') . ConsoleColor::green($cmd)); } else { logger()->debug('Running command with result: ' . $cmd); } exec($cmd, $out, $code); return [$code, $out]; } public function setEnv(array $env): WindowsCmd { $this->env = array_merge($this->env, $env); return $this; } /** * @throws RuntimeException */ public function execWithEnv(string $cmd): WindowsCmd { if ($this->getEnvString() !== '') { return $this->exec($this->getEnvString() . "call {$cmd}"); } return $this->exec($cmd); } private function getEnvString(): string { $str = ''; foreach ($this->env as $k => $v) { $str .= 'set ' . $k . '=' . $v . ' && '; } return $str; } }