add windows cmd and doctor base check

This commit is contained in:
crazywhalecc 2023-12-23 02:12:15 +08:00
parent 353e02c08a
commit b8d73e41ac
5 changed files with 104 additions and 1 deletions

View File

@ -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']) : '';

View File

@ -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');
}

View File

@ -0,0 +1,90 @@
<?php
declare(strict_types=1);
namespace SPC\util;
use SPC\exception\RuntimeException;
use ZM\Logger\ConsoleColor;
class WindowsCmd
{
private ?string $cd = null;
private bool $debug;
private array $env = [];
public function __construct(?bool $debug = null)
{
if (PHP_OS_FAMILY !== 'Windows') {
throw new RuntimeException('Only windows can use WindowsCmd');
}
$this->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;
}
}

View File

@ -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',

View File

@ -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);
}