swip update

This commit is contained in:
xiaomlove
2022-02-12 19:46:04 +08:00
parent cf47dedbb3
commit 5bcef7cae1
3 changed files with 94 additions and 76 deletions

View File

@@ -1,6 +1,6 @@
<?php
defined('VERSION_NUMBER') || define('VERSION_NUMBER', '1.6.0-beta12');
defined('RELEASE_DATE') || define('RELEASE_DATE', '2021-06-23');
defined('VERSION_NUMBER') || define('VERSION_NUMBER', '1.6.0-beta13');
defined('RELEASE_DATE') || define('RELEASE_DATE', '2022-02-14');
defined('IN_TRACKER') || define('IN_TRACKER', true);
defined('PROJECTNAME') || define("PROJECTNAME","NexusPHP");
defined('NEXUSPHPURL') || define("NEXUSPHPURL","https://nexusphp.org");

View File

@@ -182,29 +182,49 @@ class Update extends Install
public function listVersions()
{
$client = new Client();
$url = "https://api.github.com/repos/xiaomlove/nexusphp/releases";
$response = $client->get($url, ['timeout' => 10,]);
if (($statusCode = $response->getStatusCode()) != 200) {
throw new \RuntimeException("获取版本列表失败,状态码:$statusCode");
}
if ($response->getBody()->getSize() <= 0) {
throw new \RuntimeException("获取版本列表失败,结果为空");
}
$bodyString = $response->getBody()->getContents();
$this->doLog("[LIST_VERSION_RESPONSE]: $bodyString");
$versions = json_decode($bodyString, true);
if (empty($versions) || !is_array($versions)) {
throw new \RuntimeException("获取版本列表结果异常");
}
$versions = $this->requestGithub($url);
return array_reverse($versions);
}
public function downAndExtractCode($url)
public function getLatestCommit()
{
$url = "https://api.github.com/repos/xiaomlove/nexusphp/commits/php8";
return $this->requestGithub($url);
}
public function requestGithub($url)
{
$client = new Client();
$logPrefix = "请求 github: $url";
$response = $client->get($url, ['timeout' => 10,]);
if (($statusCode = $response->getStatusCode()) != 200) {
throw new \RuntimeException("$logPrefix 失败,状态码:$statusCode");
}
if ($response->getBody()->getSize() <= 0) {
throw new \RuntimeException("$logPrefix 失败,结果为空");
}
$bodyString = $response->getBody()->getContents();
$this->doLog("[REQUEST_GITHUB_RESPONSE]: $bodyString");
$results = json_decode($bodyString, true);
if (empty($results) || !is_array($results)) {
throw new \RuntimeException("$logPrefix 结果异常");
}
return $results;
}
public function downAndExtractCode($url): bool
{
$arr = explode('/', $url);
$basename = last($arr);
$suffix = '.tar.gz';
$isZip = false;
if (Str::contains($basename,'.zip')) {
$isZip = true;
$basename = strstr($basename, '.zip', true);
$suffix = ".zip";
} else {
$suffix = '.tar.gz';
}
$filename = sprintf('%s/nexusphp-%s-%s%s', sys_get_temp_dir(), $basename, date('YmdHis'), $suffix);
$client = new Client();
$response = $client->request('GET', $url, ['sink' => $filename]);
@@ -223,36 +243,19 @@ class Update extends Install
$this->doLog('SUCCESS_DOWNLOAD');
$extractDir = str_replace($suffix, "", $filename);
$command = "mkdir -p $extractDir";
$result = exec($command, $output, $result_code);
$this->doLog(sprintf(
"command: %s, output: %s, result_code: %s, result: %s",
$command, json_encode($output), $result_code, $result
));
if ($result_code != 0) {
throw new \RuntimeException("创建解压目录:$extractDir 失败, result_code: $result_code");
}
$this->executeCommand($command);
$command = "tar -xf $filename -C $extractDir";
$result = exec($command, $output, $result_code);
$this->doLog(sprintf(
"command: %s, output: %s, result_code: %s, result: %s",
$command, json_encode($output), $result_code, $result
));
if ($result_code != 0) {
throw new \RuntimeException("解压文件:$filename 到:$extractDir 失败, result_code: $result_code");
if ($isZip) {
$command = "unzip $filename -d $extractDir";
} else {
$command = "tar -xf $filename -C $extractDir";
}
$this->executeCommand($command);
foreach (glob("$extractDir/*") as $path) {
if (is_dir($path)) {
$command = sprintf('cp -Rf %s/* %s', $path, ROOT_PATH);
$result = exec($command, $output, $result_code);
$this->doLog(sprintf(
"command: %s, output: %s, result_code: %s, result: %s",
$command, json_encode($output), $result_code, $result
));
if ($result_code != 0) {
throw new \RuntimeException("复制错误, result_code: $result_code");
}
$this->executeCommand($command);
break;
}
}

View File

@@ -37,54 +37,69 @@ if ($currentStep == 2) {
} catch (\Exception $exception) {
$error = $exception->getMessage();
}
$versionHeader = [
'checkbox' => '选择',
'tag_name' => '版本(标签)',
'name' => '名称',
'published_at' => '发布时间',
];
$tableRows[] = [
'checkbox' => sprintf('<input type="radio" name="version_url" value="manual"/>'),
'tag_name' => '手动更新',
'name' => '如若有改动不宜全量覆盖,请勾选此选项并确保已经手动更新了代码',
'published_at' => '---',
];
foreach ($versions as $version) {
if ($version['draft']) {
// continue;
}
$time = \Carbon\Carbon::parse($version['published_at']);
$time->tz = nexus_env('TIMEZONE');
$versionUrl = $version['tag_name'] . '|' . $version['tarball_url'];
$checked = !empty($_REQUEST['version_url']) && $_REQUEST['version_url'] == $versionUrl ? ' checked' : '';
if (!$isPost) {
$versionHeader = [
'checkbox' => '选择',
'tag_name' => '版本(标签)',
'name' => '名称',
'published_at' => '发布时间',
];
$tableRows[] = [
'checkbox' => sprintf('<input type="radio" name="version_url" value="%s"%s/>', $versionUrl, $checked),
'tag_name' => $version['tag_name'],
'name' => $version['name'],
'checkbox' => sprintf('<input type="radio" name="version_url" value="manual"/>'),
'tag_name' => '手动更新',
'name' => '如若有改动不宜全量覆盖,请勾选此选项并确保已经手动更新了代码',
'published_at' => '---',
];
$latestCommit = $update->getLatestCommit();
$time = \Carbon\Carbon::parse($latestCommit['committer']['date']);
$time->tz = nexus_env('TIMEZONE');
$tableRows[] = [
'checkbox' => sprintf('<input type="radio" name="version_url" value="development"/>'),
'tag_name' => '最新开发代码',
'name' => "仅限开发测试!最新提交:" . $latestCommit['commit']['message'],
'published_at' => $time->format('Y-m-d H:i:s'),
];
foreach ($versions as $version) {
if ($version['draft']) {
continue;
}
$time = \Carbon\Carbon::parse($version['published_at']);
$time->tz = nexus_env('TIMEZONE');
$versionUrl = $version['tag_name'] . '|' . $version['tarball_url'];
$checked = !empty($_REQUEST['version_url']) && $_REQUEST['version_url'] == $versionUrl ? ' checked' : '';
$tableRows[] = [
'checkbox' => sprintf('<input type="radio" name="version_url" value="%s"%s/>', $versionUrl, $checked),
'tag_name' => $version['tag_name'],
'name' => $version['name'],
'published_at' => $time->format('Y-m-d H:i:s'),
];
}
}
// dd($tableRows);
while ($isPost) {
try {
if (empty($_REQUEST['version_url'])) {
throw new \RuntimeException("没有选择版本");
}
$downloadUrl = '';
if ($_REQUEST['version_url'] == 'manual') {
$update->nextStep();
}
$versionUrlArr = explode('|', $_REQUEST['version_url']);
$version = strtolower($versionUrlArr[0]);
$downloadUrl = $versionUrlArr[1];
if (\Illuminate\Support\Str::startsWith($version, 'v')) {
$version = substr($version, 1);
}
$update->doLog("version: $version, downloadUrl: $downloadUrl, currentVersion: " . VERSION_NUMBER);
if (version_compare($version, VERSION_NUMBER, '<=')) {
throw new \RuntimeException("必须选择一个高于当前版本(" . VERSION_NUMBER . ")的");
} elseif ($_REQUEST['version_url'] == 'development') {
$downloadUrl = 'https://github.com/xiaomlove/nexusphp/archive/refs/heads/php8.zip';
} else {
$versionUrlArr = explode('|', $_REQUEST['version_url']);
$version = strtolower($versionUrlArr[0]);
$downloadUrl = $versionUrlArr[1];
if (\Illuminate\Support\Str::startsWith($version, 'v')) {
$version = substr($version, 1);
}
$update->doLog("version: $version, downloadUrl: $downloadUrl, currentVersion: " . VERSION_NUMBER);
if (version_compare($version, VERSION_NUMBER, '<=')) {
throw new \RuntimeException("必须选择一个高于当前版本(" . VERSION_NUMBER . ")的");
}
}
$update->downAndExtractCode($downloadUrl);
$update->nextStep();
} catch (\Exception $exception) {
$update->doLog($exception->getMessage() . $exception->getTraceAsString());