From b8d73e41aca184c72651522d4fdf3a1fb52bba5f Mon Sep 17 00:00:00 2001 From: crazywhalecc Date: Sat, 23 Dec 2023 02:12:15 +0800 Subject: [PATCH] add windows cmd and doctor base check --- src/SPC/doctor/item/OSCheckList.php | 2 +- src/SPC/util/UnixShell.php | 3 + src/SPC/util/WindowsCmd.php | 90 +++++++++++++++++++++++++++++ src/globals/defines.php | 4 ++ src/globals/functions.php | 6 ++ 5 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 src/SPC/util/WindowsCmd.php diff --git a/src/SPC/doctor/item/OSCheckList.php b/src/SPC/doctor/item/OSCheckList.php index 60ce10c6..8c7aa762 100644 --- a/src/SPC/doctor/item/OSCheckList.php +++ b/src/SPC/doctor/item/OSCheckList.php @@ -16,7 +16,7 @@ class OSCheckList #[AsCheckItem('if current OS are supported', level: 999)] public function checkOS(): ?CheckResult { - if (!in_array(PHP_OS_FAMILY, ['Darwin', 'Linux', 'BSD'])) { + if (!in_array(PHP_OS_FAMILY, ['Darwin', 'Linux', 'BSD', 'Windows'])) { return CheckResult::fail('Current OS is not supported: ' . PHP_OS_FAMILY); } $distro = PHP_OS_FAMILY === 'Linux' ? (' ' . SystemUtil::getOSRelease()['dist']) : ''; diff --git a/src/SPC/util/UnixShell.php b/src/SPC/util/UnixShell.php index 5fd730f9..2c0ba5d1 100644 --- a/src/SPC/util/UnixShell.php +++ b/src/SPC/util/UnixShell.php @@ -17,6 +17,9 @@ class UnixShell public function __construct(?bool $debug = null) { + if (PHP_OS_FAMILY === 'Windows') { + throw new RuntimeException('Windows cannot use UnixShell'); + } $this->debug = $debug ?? defined('DEBUG_MODE'); } diff --git a/src/SPC/util/WindowsCmd.php b/src/SPC/util/WindowsCmd.php new file mode 100644 index 00000000..a34f6e39 --- /dev/null +++ b/src/SPC/util/WindowsCmd.php @@ -0,0 +1,90 @@ +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; + } +} diff --git a/src/globals/defines.php b/src/globals/defines.php index 3415f5c6..dc8281be 100644 --- a/src/globals/defines.php +++ b/src/globals/defines.php @@ -22,6 +22,10 @@ define('SEPARATED_PATH', [ BUILD_ROOT_PATH, ]); +if (PHP_OS_FAMILY === 'Windows') { + define('PHP_SDK_PATH', is_string($a = getenv('PHP_SDK_PATH')) ? $a : (WORKING_DIR . '/php-sdk-binary-tools')); +} + // dangerous command const DANGER_CMD = [ 'rm', diff --git a/src/globals/functions.php b/src/globals/functions.php index d63f3350..afadf3a4 100644 --- a/src/globals/functions.php +++ b/src/globals/functions.php @@ -5,6 +5,7 @@ declare(strict_types=1); use Psr\Log\LoggerInterface; use SPC\exception\WrongUsageException; use SPC\util\UnixShell; +use SPC\util\WindowsCmd; use ZM\Logger\ConsoleLogger; /** @@ -120,3 +121,8 @@ function shell(?bool $debug = null): UnixShell { return new UnixShell($debug); } + +function cmd(?bool $debug = null): WindowsCmd +{ + return new WindowsCmd($debug); +}