|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Swoft\Cli\Command; |
| 4 | + |
| 5 | +use Swoft\Cli\SwoftCLI; |
| 6 | +use Swoft\Console\Helper\Show; |
| 7 | +use Swoft\Console\Annotation\Mapping\Command; |
| 8 | +use Swoft\Console\Annotation\Mapping\CommandArgument; |
| 9 | +use Swoft\Console\Annotation\Mapping\CommandMapping; |
| 10 | +use Swoft\Console\Annotation\Mapping\CommandOption; |
| 11 | +use Swoft\Console\Input\Input; |
| 12 | +use Swoft\Console\Output\Output; |
| 13 | +use Swoole\Coroutine\Http\Client; |
| 14 | +use Toolkit\Cli\Download; |
| 15 | +use function file_get_contents; |
| 16 | +use function json_decode; |
| 17 | +use function parse_url; |
| 18 | + |
| 19 | +/** |
| 20 | + * update the swoft-cli to latest version from github releases |
| 21 | + * |
| 22 | + * @Command("self-update", alias="selfupdate, upself, update-self, updateself", coroutine=true) |
| 23 | + */ |
| 24 | +class SelfUpdateCommand |
| 25 | +{ |
| 26 | + public const LATEST_RELEASE_URL = 'https://api.github.com/repos/swoft-cloud/swoft-cli/releases/latest'; |
| 27 | + |
| 28 | + /** |
| 29 | + * update the swoft-cli to latest version from github releases |
| 30 | + * |
| 31 | + * @CommandMapping(alias="selfupdate, upself, update-self, updateself") |
| 32 | + * @CommandOption( |
| 33 | + * "only-check", type="bool", |
| 34 | + * desc="only fetch latest release information, but dont download and update package", |
| 35 | + * ) |
| 36 | + * @param Input $input |
| 37 | + * @param Output $output |
| 38 | + */ |
| 39 | + public function up(Input $input, Output $output): void |
| 40 | + { |
| 41 | + $output->colored('Current Version: ' . SwoftCLI::VERSION); |
| 42 | + |
| 43 | + // @see https://developer.github.com/v3/repos/releases/ |
| 44 | + // curl https://api.github.com/repos/swoft-cloud/swoft-cli/releases/latest |
| 45 | + |
| 46 | + // $jsonText = file_get_contents(self::LATEST_RELEASE_URL); |
| 47 | + |
| 48 | + $output->colored('Fetch latest release information for Github ...', 'cyan'); |
| 49 | + |
| 50 | + $info = $this->parseUrl(self::LATEST_RELEASE_URL); |
| 51 | + $port = (int)$info['port']; |
| 52 | + $path = $info['path']; |
| 53 | + |
| 54 | + $client = new Client($info['host'], $port, $port === 443); |
| 55 | + $client->setHeaders([ |
| 56 | + 'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36', |
| 57 | + ]); |
| 58 | + $client->execute($path); |
| 59 | + |
| 60 | + // $status = $client->statusCode; |
| 61 | + $result = $client->body; |
| 62 | + |
| 63 | + // close connection |
| 64 | + $client->close(); |
| 65 | + |
| 66 | + $latest = json_decode($result, true); |
| 67 | + if (!$latest) { |
| 68 | + Show::error('Failed for update swoft-cli: fetch latest version info failed'); |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + $tagName = $latest['tag_name']; |
| 73 | + |
| 74 | + $metaInfo = [ |
| 75 | + 'local version' => 'v' . SwoftCLI::VERSION, |
| 76 | + 'latest version' => $tagName, |
| 77 | + 'created at' => $latest['created_at'], |
| 78 | + 'published at' => $latest['published_at'], |
| 79 | + ]; |
| 80 | + |
| 81 | + Show::aList($metaInfo, 'latest release information'); |
| 82 | + |
| 83 | + if ($input->getOpt('only-check')) { |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + // get phar download address. |
| 88 | + if (!isset($latest['assets'][0]['browser_download_url'])) { |
| 89 | + Show::error('Failed for update swoft-cli: not found latest phar package download url'); |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + $pharUrl = $latest['assets'][0]['browser_download_url']; |
| 94 | + |
| 95 | + Download::file($pharUrl); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @param string $url |
| 100 | + * |
| 101 | + * @return array |
| 102 | + */ |
| 103 | + public function parseUrl(string $url): array |
| 104 | + { |
| 105 | + $info = parse_url($url); |
| 106 | + |
| 107 | + if ($info['scheme'] === 'https') { |
| 108 | + $info['port'] = 443; |
| 109 | + } |
| 110 | + |
| 111 | + return $info; |
| 112 | + } |
| 113 | +} |
0 commit comments