From 5bcef7cae1f4e623522224bcd50d4c8fdf7677b5 Mon Sep 17 00:00:00 2001 From: xiaomlove Date: Sat, 12 Feb 2022 19:46:04 +0800 Subject: [PATCH] swip update --- include/constants.php | 4 +- nexus/Install/Update.php | 83 +++++++++++++++++---------------- nexus/Install/update/update.php | 83 +++++++++++++++++++-------------- 3 files changed, 94 insertions(+), 76 deletions(-) diff --git a/include/constants.php b/include/constants.php index 4e98d93b..2aef9bca 100644 --- a/include/constants.php +++ b/include/constants.php @@ -1,6 +1,6 @@ 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; } } diff --git a/nexus/Install/update/update.php b/nexus/Install/update/update.php index d5182977..7bc3c83a 100644 --- a/nexus/Install/update/update.php +++ b/nexus/Install/update/update.php @@ -37,54 +37,69 @@ if ($currentStep == 2) { } catch (\Exception $exception) { $error = $exception->getMessage(); } - $versionHeader = [ - 'checkbox' => '选择', - 'tag_name' => '版本(标签)', - 'name' => '名称', - 'published_at' => '发布时间', - ]; - $tableRows[] = [ - 'checkbox' => sprintf(''), - '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('', $versionUrl, $checked), - 'tag_name' => $version['tag_name'], - 'name' => $version['name'], + 'checkbox' => sprintf(''), + 'tag_name' => '手动更新', + 'name' => '如若有改动不宜全量覆盖,请勾选此选项并确保已经手动更新了代码', + 'published_at' => '---', + ]; + $latestCommit = $update->getLatestCommit(); + $time = \Carbon\Carbon::parse($latestCommit['committer']['date']); + $time->tz = nexus_env('TIMEZONE'); + $tableRows[] = [ + 'checkbox' => sprintf(''), + '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('', $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());