Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkgs.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"box": {
"repo": "hyperf/box",
"bin": "box",
"release_asset_keyword": "box",
"release_asset_match_rule": {
"Darwin.x86_64": "box_x86_64_macos",
"Darwin.arm64": "box_arm64_macos",
Expand Down
3 changes: 2 additions & 1 deletion src/app/Command/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
namespace App\Command;

use App\Config;
use Hyperf\Command\Command as HyperfCommand;
use RuntimeException;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Command\Command as HyperfCommand;

abstract class AbstractCommand extends HyperfCommand
{
Expand Down
6 changes: 6 additions & 0 deletions src/app/Command/GetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function configure()
$this->addArgument('pkg', InputArgument::REQUIRED, 'The package name');
$this->addOption('source', 's', InputOption::VALUE_OPTIONAL, 'The download source.');
$this->addOption('versions', '', InputOption::VALUE_NONE, 'Print the package versions only.');
$this->addOption('reinstall', 'r', InputOption::VALUE_NONE, 'Ignore the local file and reinstall.');
}

public function handle()
Expand All @@ -49,17 +50,22 @@ public function handle()
$pkg = $this->input->getArgument('pkg');
$source = $this->input->getOption('source');
$versions = $this->input->getOption('versions');
$reinstall = $this->input->getOption('reinstall');
[$pkg, $version] = $this->parsePkgVersion($pkg);
$options = [];
if ($source) {
$options['source'] = $source;
}
if ($reinstall) {
$options['reinstall'] = $reinstall;
}
if ($versions) {
$versions = $this->downloadManager->versions($pkg, $options);
$this->output->writeln($versions);
} else {
$this->downloadManager->get($pkg, $version, $options);
}
return self::SUCCESS;
}

protected function parsePkgVersion(string $pkg): array
Expand Down
14 changes: 14 additions & 0 deletions src/app/DownloadHandler/AbstractDownloadHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@

namespace App\DownloadHandler;

use App\Box;
use App\Config;
use App\Exception\BoxException;
use App\Exception\NotSupportVersionsException;
use App\GithubClient;
use App\PkgDefinition\Definition;
Expand Down Expand Up @@ -82,6 +84,7 @@ protected function fetchVersionsFromGithubRelease(string $fullRepo, ?string $ass
foreach ($release['assets'] as $asset) {
if (str_contains($asset['name'], $assetKeyword)) {
$versions[] = $release['tag_name'];
break;
}
}
} else {
Expand Down Expand Up @@ -201,4 +204,15 @@ protected function replaces(string $subject, array $replaces): string
}
return $subject;
}

protected function latestVersionCheck(string $currentVersion, Definition $definition, array $options = []): void
{
if (isset($options['reinstall']) && $options['reinstall']) {
return;
}
$latestVersion = $this->versions($definition->getPkgName())[0] ?? '';
if ($latestVersion && version_compare($currentVersion, $latestVersion, '>=')) {
throw new BoxException(sprintf('Your %s version %s is latest, no need to update, add `--reinstall` or `-r` option to force reinstall the package.', $definition->getPkgName(), $currentVersion));
}
}
}
29 changes: 11 additions & 18 deletions src/app/DownloadHandler/BoxHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,35 @@

namespace App\DownloadHandler;

use App\Box;
use Phar;
use SplFileInfo;

class BoxHandler extends AbstractDownloadHandler
{
protected string $fullRepo = 'hyperf/box';

protected string $binName = 'box';

public function __construct()
{
parent::__construct();
$this->binName = $this->getAssetName();
}

public function handle(string $pkgName, string $version, array $options = []): ?SplFileInfo
{
$url = $this->fetchDownloadUrlFromGithubRelease($this->getAssetName(), $this->fullRepo, $version);
$definition = $this->getDefinition($pkgName);
$this->latestVersionCheck(Box::VERSION, $definition, $options);
$assetName = $definition->getReleaseAssetMatchRule()[PHP_OS_FAMILY . '.' . php_uname('m')] ?? '';
if (! $assetName) {
throw new BoxException('Can not found any matched asset for your system.');
}
$url = $this->fetchDownloadUrlFromGithubRelease($assetName, $definition->getRepo(), $version);
$savePath = Phar::running(false) ?: $this->runtimePath . '/';

return $this->download($url, $savePath, 0755, $this->binName);
return $this->download($url, $savePath, 0755, $definition->getBin());
}

public function versions(string $pkgName, array $options = []): array
{
return $this->fetchVersionsFromGithubRelease($this->fullRepo, $this->getAssetName());
}

protected function getAssetName(): string
{
return match (PHP_OS) {
'Darwin' => 'box_x86_64_macos',
'Linux' => match (php_uname('m')) {
'x86_64' => 'box_x86_64_linux',
default => 'box_aarch64_linux',
}
};
$definition = $this->getDefinition($pkgName);
return $this->fetchVersionsFromGithubRelease($definition->getRepo(), $definition->getReleaseAssetKeyword());
}
}
1 change: 0 additions & 1 deletion src/app/DownloadHandler/ComposerHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

class ComposerHandler extends AbstractDownloadHandler
{
protected string $fullRepo = 'composer/composer';

protected string $githubBaseUrl = 'github.com';

Expand Down
2 changes: 0 additions & 2 deletions src/app/DownloadHandler/DefaultHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

use App\Exception\BoxException;
use App\Exception\NotSupportVersionsException;
use App\PkgDefinition\Definition;
use GuzzleHttp\Client;
use SplFileInfo;

class DefaultHandler extends AbstractDownloadHandler
Expand Down
5 changes: 0 additions & 5 deletions src/app/DownloadHandler/SwooleCliHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@
class SwooleCliHandler extends AbstractDownloadHandler
{

public function __construct()
{
parent::__construct();
}

public function handle(string $pkgName, string $version, array $options = []): ?SplFileInfo
{
$definition = $this->getDefinition($pkgName);
Expand Down
3 changes: 2 additions & 1 deletion src/app/DownloadManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use App\Exception\PkgDefinitionNotFoundException;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerInterface;
use SplFileInfo;

class DownloadManager
{
Expand Down Expand Up @@ -55,7 +56,7 @@ public function get(string $pkg, string $version, array $options = []): void
}
$handler = $this->container->get($this->handlers[$key]);
$file = $handler->handle($pkg, $version, $options);
if ($file->isWritable()) {
if ($file instanceof SplFileInfo && $file->isWritable()) {
chmod($file->getRealPath(), 0755);
}
}
Expand Down