diff --git a/pkgs.json b/pkgs.json index 432ee54..61d11b7 100644 --- a/pkgs.json +++ b/pkgs.json @@ -30,6 +30,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..34ea708 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,6 +75,7 @@ public function versions(string $pkg, array $options): array $key = $pkg; } $handler = $this->container->get($this->handlers[$key]); + return $handler->versions($pkg, $options); }