2022-02-20 22:42:48 +00:00
|
|
|
|
<?php
|
|
|
|
|
|
2022-02-27 20:07:40 +00:00
|
|
|
|
namespace ba;
|
2022-02-20 22:42:48 +00:00
|
|
|
|
|
2022-05-10 22:22:13 +00:00
|
|
|
|
|
2022-02-20 22:42:48 +00:00
|
|
|
|
/**
|
|
|
|
|
* 版本类
|
|
|
|
|
*/
|
|
|
|
|
class Version
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 比较两个版本号
|
|
|
|
|
* @param $v1 string 要求的版本号
|
2023-06-22 22:02:45 +00:00
|
|
|
|
* @param $v2 bool | string 被比较版本号
|
2022-02-20 22:42:48 +00:00
|
|
|
|
* @return bool 是否达到要求的版本号
|
|
|
|
|
*/
|
2023-06-22 22:02:45 +00:00
|
|
|
|
public static function compare(string $v1, bool|string $v2): bool
|
2022-02-20 22:42:48 +00:00
|
|
|
|
{
|
|
|
|
|
if (!$v2) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 删除开头的 V
|
2022-05-10 22:22:13 +00:00
|
|
|
|
if (strtolower($v1[0]) == 'v') {
|
|
|
|
|
$v1 = substr($v1, 1);
|
|
|
|
|
}
|
2022-02-20 22:42:48 +00:00
|
|
|
|
if (strtolower($v2[0]) == 'v') {
|
|
|
|
|
$v2 = substr($v2, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($v1 == "*" || $v1 == $v2) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-16 17:14:15 +00:00
|
|
|
|
// 丢弃'-'后面的内容
|
2023-06-22 22:02:45 +00:00
|
|
|
|
if (str_contains($v1, '-')) $v1 = explode('-', $v1)[0];
|
|
|
|
|
if (str_contains($v2, '-')) $v2 = explode('-', $v2)[0];
|
2022-05-16 17:14:15 +00:00
|
|
|
|
|
2022-05-10 22:22:13 +00:00
|
|
|
|
$v1 = explode('.', $v1);
|
|
|
|
|
$v2 = explode('.', $v2);
|
|
|
|
|
|
|
|
|
|
// 将号码逐个进行比较
|
|
|
|
|
for ($i = 0; $i < count($v1); $i++) {
|
|
|
|
|
if (!isset($v2[$i])) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if ($v1[$i] == $v2[$i]) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if ($v1[$i] > $v2[$i]) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if ($v1[$i] < $v2[$i]) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (count($v1) != count($v2)) {
|
|
|
|
|
return !(count($v1) > count($v2));
|
2022-02-20 22:42:48 +00:00
|
|
|
|
}
|
2022-10-27 19:19:56 +00:00
|
|
|
|
return false;
|
2022-02-20 22:42:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-26 04:58:03 +00:00
|
|
|
|
/**
|
|
|
|
|
* 是否是一个数字版本号
|
2022-10-27 19:19:56 +00:00
|
|
|
|
* @param $version
|
|
|
|
|
* @return bool
|
2022-08-26 04:58:03 +00:00
|
|
|
|
*/
|
2022-10-27 19:19:56 +00:00
|
|
|
|
public static function checkDigitalVersion($version): bool
|
2022-02-20 22:42:48 +00:00
|
|
|
|
{
|
|
|
|
|
if (!$version) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (strtolower($version[0]) == 'v') {
|
|
|
|
|
$version = substr($version, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$rule1 = '/\.{2,10}/'; // 是否有两个的`.`
|
|
|
|
|
$rule2 = '/^\d+(\.\d+){0,10}$/';
|
|
|
|
|
if (!preg_match($rule1, (string)$version)) {
|
|
|
|
|
return !!preg_match($rule2, (string)$version);
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-27 19:19:56 +00:00
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public static function getCnpmVersion(): string
|
2022-02-20 22:42:48 +00:00
|
|
|
|
{
|
2022-08-26 04:58:03 +00:00
|
|
|
|
$execOut = Terminal::getOutputFromProc('version.cnpm');
|
2022-05-15 14:09:12 +00:00
|
|
|
|
if ($execOut) {
|
2022-08-26 04:58:03 +00:00
|
|
|
|
$preg = '/cnpm@(.+?) \(/is';
|
2022-05-15 14:09:12 +00:00
|
|
|
|
preg_match($preg, $execOut, $result);
|
2022-10-27 19:19:56 +00:00
|
|
|
|
return $result[1] ?? '';
|
2022-02-20 22:42:48 +00:00
|
|
|
|
} else {
|
2022-10-27 19:19:56 +00:00
|
|
|
|
return '';
|
2022-02-20 22:42:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-16 00:13:34 +00:00
|
|
|
|
/**
|
|
|
|
|
* 获取依赖版本号
|
|
|
|
|
* @param string $name 支持:npm、cnpm、yarn、pnpm、node
|
2022-10-27 19:19:56 +00:00
|
|
|
|
* @return string
|
2022-05-16 00:13:34 +00:00
|
|
|
|
*/
|
2022-10-27 19:19:56 +00:00
|
|
|
|
public static function getVersion(string $name): string
|
2022-02-20 22:42:48 +00:00
|
|
|
|
{
|
2022-05-16 00:13:34 +00:00
|
|
|
|
if ($name == 'cnpm') {
|
|
|
|
|
return self::getCnpmVersion();
|
|
|
|
|
} elseif (in_array($name, ['npm', 'yarn', 'pnpm', 'node'])) {
|
2022-08-26 04:58:03 +00:00
|
|
|
|
$execOut = Terminal::getOutputFromProc('version.' . $name);
|
2022-05-16 00:13:34 +00:00
|
|
|
|
if ($execOut) {
|
2022-11-20 17:55:19 +00:00
|
|
|
|
if (strripos($execOut, 'npm WARN') !== false) {
|
|
|
|
|
$preg = '/\d+(\.\d+){0,2}/';
|
|
|
|
|
preg_match($preg, $execOut, $matches);
|
|
|
|
|
if (isset($matches[0]) && self::checkDigitalVersion($matches[0])) {
|
|
|
|
|
return $matches[0];
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-08-26 04:58:03 +00:00
|
|
|
|
$execOut = preg_split('/\r\n|\r|\n/', $execOut);
|
2022-05-16 00:13:34 +00:00
|
|
|
|
// 检测两行,第一行可能会是个警告消息
|
|
|
|
|
for ($i = 0; $i < 2; $i++) {
|
|
|
|
|
if (isset($execOut[$i]) && self::checkDigitalVersion($execOut[$i])) {
|
|
|
|
|
return $execOut[$i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2022-10-27 19:19:56 +00:00
|
|
|
|
return '';
|
2022-05-16 00:13:34 +00:00
|
|
|
|
}
|
2022-02-20 22:42:48 +00:00
|
|
|
|
}
|
2022-10-27 19:19:56 +00:00
|
|
|
|
return '';
|
2022-02-20 22:42:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|