mirror of
https://github.com/crazywhalecc/static-php-cli.git
synced 2026-07-05 15:55:39 +08:00
Add initial windows runtime setup (#292)
* add initial windows runtime setup * add cool console output * doctor support windows base * add `add-path` and `remove-path` for bin/setup-runtime * fix composer.ps1 path * add windows system util * add windows cmd and doctor base check * add windows fallback for laravel/prompts * cd fix [skip ci] * dir separator and typo fix [skip ci]
This commit is contained in:
26
src/SPC/builder/windows/SystemUtil.php
Normal file
26
src/SPC/builder/windows/SystemUtil.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace SPC\builder\windows;
|
||||
|
||||
class SystemUtil
|
||||
{
|
||||
/**
|
||||
* @param string $name 命令名称
|
||||
* @param array $paths 寻找的目标路径(如果不传入,则使用环境变量 PATH)
|
||||
* @return null|string 找到了返回命令路径,找不到返回 null
|
||||
*/
|
||||
public static function findCommand(string $name, array $paths = []): ?string
|
||||
{
|
||||
if (!$paths) {
|
||||
$paths = explode(PATH_SEPARATOR, getenv('Path'));
|
||||
}
|
||||
foreach ($paths as $path) {
|
||||
if (file_exists($path . DIRECTORY_SEPARATOR . $name)) {
|
||||
return $path . DIRECTORY_SEPARATOR . $name;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -4,13 +4,17 @@ declare(strict_types=1);
|
||||
|
||||
namespace SPC\command;
|
||||
|
||||
use Laravel\Prompts\ConfirmPrompt;
|
||||
use Laravel\Prompts\Prompt;
|
||||
use Psr\Log\LogLevel;
|
||||
use SPC\ConsoleApplication;
|
||||
use SPC\exception\ExceptionHandler;
|
||||
use SPC\exception\WrongUsageException;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Helper\QuestionHelper;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Question\ConfirmationQuestion;
|
||||
use ZM\Logger\ConsoleLogger;
|
||||
|
||||
abstract class BaseCommand extends Command
|
||||
@@ -78,6 +82,15 @@ abstract class BaseCommand extends Command
|
||||
{
|
||||
$this->input = $input;
|
||||
$this->output = $output;
|
||||
|
||||
// windows fallback
|
||||
Prompt::fallbackWhen(PHP_OS_FAMILY === 'Windows');
|
||||
ConfirmPrompt::fallbackUsing(function (ConfirmPrompt $prompt) use ($input, $output) {
|
||||
$helper = new QuestionHelper();
|
||||
$case = $prompt->default ? ' [Y/n] ' : ' [y/N] ';
|
||||
$question = new ConfirmationQuestion($prompt->label . $case, $prompt->default);
|
||||
return $helper->ask($input, $output, $question);
|
||||
});
|
||||
if ($this->shouldExecute()) {
|
||||
try {
|
||||
return $this->handle();
|
||||
|
||||
@@ -75,8 +75,9 @@ class DoctorCommand extends BaseCommand
|
||||
} catch (\Throwable $e) {
|
||||
$this->output->writeln('<error>' . $e->getMessage() . '</error>');
|
||||
|
||||
pcntl_signal(SIGINT, SIG_IGN);
|
||||
|
||||
if (extension_loaded('pcntl')) {
|
||||
pcntl_signal(SIGINT, SIG_IGN);
|
||||
}
|
||||
return static::FAILURE;
|
||||
}
|
||||
|
||||
|
||||
@@ -34,12 +34,23 @@ final class CheckListHandler
|
||||
*/
|
||||
public function emitFix(OutputInterface $output, CheckResult $result): void
|
||||
{
|
||||
pcntl_signal(SIGINT, function () use ($output) {
|
||||
$output->writeln('<error>You cancelled fix</error>');
|
||||
});
|
||||
if (PHP_OS_FAMILY === 'Windows') {
|
||||
sapi_windows_set_ctrl_handler(function () use ($output) {
|
||||
$output->writeln('<error>You cancelled fix</error>');
|
||||
});
|
||||
} elseif (extension_loaded('pcntl')) {
|
||||
pcntl_signal(SIGINT, function () use ($output) {
|
||||
$output->writeln('<error>You cancelled fix</error>');
|
||||
});
|
||||
}
|
||||
|
||||
$fix_result = call_user_func($this->fix_map[$result->getFixItem()], ...$result->getFixParams());
|
||||
pcntl_signal(SIGINT, SIG_IGN);
|
||||
|
||||
if (PHP_OS_FAMILY === 'Windows') {
|
||||
sapi_windows_set_ctrl_handler(null);
|
||||
} elseif (extension_loaded('pcntl')) {
|
||||
pcntl_signal(SIGINT, SIG_IGN);
|
||||
}
|
||||
|
||||
if ($fix_result) {
|
||||
$output->writeln('<info>Fix done</info>');
|
||||
|
||||
@@ -16,8 +16,8 @@ class OSCheckList
|
||||
#[AsCheckItem('if current OS are supported', level: 999)]
|
||||
public function checkOS(): ?CheckResult
|
||||
{
|
||||
if (!in_array(PHP_OS_FAMILY, ['Darwin', 'Linux', 'BSD'])) {
|
||||
return CheckResult::fail('Current OS is not supported');
|
||||
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']) : '';
|
||||
return CheckResult::ok(PHP_OS_FAMILY . ' ' . php_uname('m') . $distro . ', supported');
|
||||
|
||||
44
src/SPC/doctor/item/WindowsToolCheckList.php
Normal file
44
src/SPC/doctor/item/WindowsToolCheckList.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace SPC\doctor\item;
|
||||
|
||||
use SPC\builder\windows\SystemUtil;
|
||||
use SPC\doctor\AsCheckItem;
|
||||
use SPC\doctor\AsFixItem;
|
||||
use SPC\doctor\CheckResult;
|
||||
use SPC\exception\RuntimeException;
|
||||
|
||||
class WindowsToolCheckList
|
||||
{
|
||||
#[AsCheckItem('if git are installed', limit_os: 'Windows', level: 999)]
|
||||
public function checkGit(): ?CheckResult
|
||||
{
|
||||
if (SystemUtil::findCommand('git.exe') === null) {
|
||||
return CheckResult::fail('Git not installed, please install git for windows manually, see: https://git-scm.com/download/win');
|
||||
// return CheckResult::fail('Git not installed, see https://static-php.dev/en/guide/windows-setup.html');
|
||||
}
|
||||
return CheckResult::ok();
|
||||
}
|
||||
|
||||
#[AsCheckItem('if php-sdk-binary-tools are downloaded', limit_os: 'Windows', level: 998)]
|
||||
public function checkSDK(): ?CheckResult
|
||||
{
|
||||
if (!file_exists(PHP_SDK_PATH . DIRECTORY_SEPARATOR . 'phpsdk-starter.bat')) {
|
||||
return CheckResult::fail('php-sdk-binary-tools not downloaded', 'install-php-sdk');
|
||||
}
|
||||
return CheckResult::ok(PHP_SDK_PATH);
|
||||
}
|
||||
|
||||
#[AsFixItem('install-php-sdk')]
|
||||
public function installPhpSdk(): bool
|
||||
{
|
||||
try {
|
||||
cmd(true)->exec('git clone https://github.com/php/php-sdk-binary-tools.git ' . PHP_SDK_PATH);
|
||||
} catch (RuntimeException) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -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');
|
||||
}
|
||||
|
||||
|
||||
90
src/SPC/util/WindowsCmd.php
Normal file
90
src/SPC/util/WindowsCmd.php
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user