add windows system util

This commit is contained in:
crazywhalecc 2023-12-23 02:11:44 +08:00
parent 5336a02bd2
commit 353e02c08a
3 changed files with 71 additions and 0 deletions

3
.gitignore vendored
View File

@ -26,5 +26,8 @@ docker/source/
!/bin/setup-runtime*
!/bin/spc-alpine-docker
# exclude windows build tools
/php-sdk-binary-tools/
# default test directory
/tests/var/

View File

@ -0,0 +1,24 @@
<?php
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;
}
}

View 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, see https://static-php.dev/en/guide/windows-setup.html');
}
return CheckResult::ok();
}
#[AsCheckItem('if php-sdk-binary-tools2 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;
}
}