From 3be7ccb48381c72e8ad048c5cf60e24de756c5b8 Mon Sep 17 00:00:00 2001 From: Deeka Wong Date: Wed, 5 Oct 2022 09:54:06 +0800 Subject: [PATCH 1/4] Adds print proxy --- pkgs.json | 6 ++ src/app/Command/PintProxyCommand.php | 24 +++++++ src/app/DownloadHandler/PintHandler.php | 94 +++++++++++++++++++++++++ src/app/DownloadManager.php | 6 +- 4 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 src/app/Command/PintProxyCommand.php create mode 100644 src/app/DownloadHandler/PintHandler.php diff --git a/pkgs.json b/pkgs.json index 33f588d..7d2bb66 100644 --- a/pkgs.json +++ b/pkgs.json @@ -26,6 +26,12 @@ "latest": "8.1", "versions": ["8.1", "8.0"] }, + "pint": { + "url": "https://github.com/laravel/pint/archive/refs/tags/${{version}}.zip", + "bin": "pint", + "composer_name": "laravel/pint", + "latest_fetch_type": "packagist" + }, "micro": { "repo": "dixyes/lwmbs", "jobs": { diff --git a/src/app/Command/PintProxyCommand.php b/src/app/Command/PintProxyCommand.php new file mode 100644 index 0000000..03c19a9 --- /dev/null +++ b/src/app/Command/PintProxyCommand.php @@ -0,0 +1,24 @@ +getDefinition($pkgName); + if (! $definition) { + throw new \RuntimeException('The package not found'); + } + if ($definition->getRepo()) { + $url = $this->fetchDownloadUrlFromGithubRelease($definition->getBin(), $definition->getRepo(), $version); + } elseif ($definition->getUrl()) { + if ($version === 'latest') { + if ($definition->getLatest() && $definition->getLatest() !== 'latest') { + $specifiedVersion = $definition->getLatest(); + } else { + $versions = $this->versions($pkgName); + $specifiedVersion = array_shift($versions); + } + } else { + $specifiedVersion = $version; + } + $url = str_replace('${{version}}', $specifiedVersion, $definition->getUrl()); + } else { + throw new \RuntimeException('The definition of package is invalid'); + } + + $savePath = Phar::running(false) ?: $this->runtimePath . '/'; + $file = $this->download($url, $savePath, 0755); + if (! file_exists($savePath)) { + throw new \RuntimeException('Download failed, cannot locate the file in local.'); + } + + // Is file name ends with .zip? + if (str_ends_with($file->getFilename(), '.zip')) { + $zip = new \ZipArchive(); + $zip->open($file->getRealPath()); + $this->logger->info('Unpacking zip file ' . $savePath); + for ($i = 0; $i < $zip->numFiles; ++$i) { + $filename = $zip->getNameIndex($i); + if (str_ends_with($filename, 'builds/pint')) { + copy('zip://' . $file->getRealPath() . '#' . $filename, $savePath . $definition->getBin()); + } + } + $zip->close(); + $this->logger->info('Unpacked zip file ' . $savePath); + unlink($file->getRealPath()); + } + + $licenseFile = $savePath . 'LICENSE'; + // If license file exists, delete it. + + if (file_exists($licenseFile)) { + unlink($licenseFile); + } + + return new SplFileInfo($savePath . $definition->getBin()); + } + + public function versions(string $pkgName, array $options = []): array + { + $definition = $this->getDefinition($pkgName); + if (! $definition) { + throw new \RuntimeException('The package not found'); + } + if (! $definition->getRepo() && ! $definition->getComposerName()) { + throw new NotSupportVersionsException($pkgName); + } + if ($definition->getLatestFetchType() === 'github') { + return $this->fetchVersionsFromGithubRelease($definition->getRepo(), $definition->getBin()); + } + if ($definition->getLatestFetchType() === 'packagist') { + return $this->fetchVersionsFromPackagist($definition->getPkgName(), $definition->getComposerName()); + } + throw new BoxException('The definition of package is invalid'); + } +} diff --git a/src/app/DownloadManager.php b/src/app/DownloadManager.php index 5ab4e3b..6997256 100644 --- a/src/app/DownloadManager.php +++ b/src/app/DownloadManager.php @@ -7,6 +7,7 @@ * @link https://www.hyperf.io * @document https://hyperf.wiki * @contact group@hyperf.io + * * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ @@ -17,6 +18,7 @@ use App\DownloadHandler\DefaultHandler; use App\DownloadHandler\MicroHandler; use App\DownloadHandler\PhpHandler; +use App\DownloadHandler\PintHandler; use App\DownloadHandler\SwooleCliHandler; use App\Exception\PkgDefinitionNotFoundException; use Hyperf\Di\Annotation\Inject; @@ -30,6 +32,7 @@ class DownloadManager 'composer' => ComposerHandler::class, 'micro' => MicroHandler::class, 'php' => PhpHandler::class, + 'pint' => PintHandler::class, 'swoole-cli' => SwooleCliHandler::class, 'default' => DefaultHandler::class, ]; @@ -72,12 +75,13 @@ public function versions(string $pkg, array $options): array $key = $pkg; } $handler = $this->container->get($this->handlers[$key]); + return $handler->versions($pkg, $options); } protected function createRuntimePath(): void { - $path = $this->config->getConfig('path.runtime', getenv('HOME') . '/.box'); + $path = $this->config->getConfig('path.runtime', getenv('HOME').'/.box'); if (! file_exists($path)) { mkdir($path, 0755); chmod($path, 0755); From e2bbe335e10128c01eec41258fc891f70bfcda99 Mon Sep 17 00:00:00 2001 From: Deeka Wong Date: Wed, 5 Oct 2022 10:09:35 +0800 Subject: [PATCH 2/4] cs-fix --- src/app/DownloadManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/DownloadManager.php b/src/app/DownloadManager.php index 6997256..34ea708 100644 --- a/src/app/DownloadManager.php +++ b/src/app/DownloadManager.php @@ -81,7 +81,7 @@ public function versions(string $pkg, array $options): array protected function createRuntimePath(): void { - $path = $this->config->getConfig('path.runtime', getenv('HOME').'/.box'); + $path = $this->config->getConfig('path.runtime', getenv('HOME') . '/.box'); if (! file_exists($path)) { mkdir($path, 0755); chmod($path, 0755); From 9e728661799cfda8099dcee7b9777f08b80f0424 Mon Sep 17 00:00:00 2001 From: Deeka Wong Date: Wed, 5 Oct 2022 14:01:16 +0800 Subject: [PATCH 3/4] Fix full command --- src/app/Command/AbstractPhpCallProxyCommand.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/app/Command/AbstractPhpCallProxyCommand.php b/src/app/Command/AbstractPhpCallProxyCommand.php index 2b5acad..8487b6c 100644 --- a/src/app/Command/AbstractPhpCallProxyCommand.php +++ b/src/app/Command/AbstractPhpCallProxyCommand.php @@ -31,7 +31,8 @@ public function handle() { $bin = $this->buildBinCommand(); $command = Str::replaceFirst($this->proxyCommand . ' ', '', (string) $this->input); - $fullCommand = sprintf('%s %s', $bin, $command); + // $fullCommand = sprintf('%s %s', $bin, $command); + $fullCommand = $bin; $this->liveCommand($fullCommand); } From dad1ae2a16807d5df2b897cf39f715c59ac1eb0e Mon Sep 17 00:00:00 2001 From: Deeka Wong Date: Wed, 5 Oct 2022 14:23:19 +0800 Subject: [PATCH 4/4] Fix $fullCommand --- src/app/Command/AbstractPhpCallProxyCommand.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/app/Command/AbstractPhpCallProxyCommand.php b/src/app/Command/AbstractPhpCallProxyCommand.php index 8487b6c..9d69607 100644 --- a/src/app/Command/AbstractPhpCallProxyCommand.php +++ b/src/app/Command/AbstractPhpCallProxyCommand.php @@ -30,9 +30,8 @@ public function configure() public function handle() { $bin = $this->buildBinCommand(); - $command = Str::replaceFirst($this->proxyCommand . ' ', '', (string) $this->input); - // $fullCommand = sprintf('%s %s', $bin, $command); - $fullCommand = $bin; + $command = Str::replaceFirst($this->proxyCommand, '', (string) $this->input); + $fullCommand = sprintf('%s %s', $bin, trim($command)); $this->liveCommand($fullCommand); }