Skip to content

Commit 212408d

Browse files
authored
Merge pull request #40 from huangdijia/adds-pint
Adds print package and `box print` proxy command
2 parents fa446ff + dad1ae2 commit 212408d

4 files changed

Lines changed: 128 additions & 0 deletions

File tree

pkgs.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@
3030
"latest": "8.1",
3131
"versions": ["8.1", "8.0"]
3232
},
33+
"pint": {
34+
"url": "https://github.com/laravel/pint/archive/refs/tags/${{version}}.zip",
35+
"bin": "pint",
36+
"composer_name": "laravel/pint",
37+
"latest_fetch_type": "packagist"
38+
},
3339
"micro": {
3440
"repo": "dixyes/lwmbs",
3541
"jobs": {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact group@hyperf.io
10+
*
11+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
12+
*/
13+
14+
namespace App\Command;
15+
16+
use Hyperf\Command\Annotation\Command;
17+
18+
#[Command]
19+
class PintProxyCommand extends AbstractPhpCallProxyCommand
20+
{
21+
protected string $proxyCommand = 'pint';
22+
23+
protected string $proxyBin = 'pint';
24+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact group@hyperf.io
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace App\DownloadHandler;
13+
14+
use App\Exception\BoxException;
15+
use App\Exception\NotSupportVersionsException;
16+
use Phar;
17+
use SplFileInfo;
18+
19+
class PintHandler extends AbstractDownloadHandler
20+
{
21+
public function handle(string $pkgName, string $version, array $options = []): ?SplFileInfo
22+
{
23+
$definition = $this->getDefinition($pkgName);
24+
if (! $definition) {
25+
throw new \RuntimeException('The package not found');
26+
}
27+
if ($definition->getRepo()) {
28+
$url = $this->fetchDownloadUrlFromGithubRelease($definition->getBin(), $definition->getRepo(), $version);
29+
} elseif ($definition->getUrl()) {
30+
if ($version === 'latest') {
31+
if ($definition->getLatest() && $definition->getLatest() !== 'latest') {
32+
$specifiedVersion = $definition->getLatest();
33+
} else {
34+
$versions = $this->versions($pkgName);
35+
$specifiedVersion = array_shift($versions);
36+
}
37+
} else {
38+
$specifiedVersion = $version;
39+
}
40+
$url = str_replace('${{version}}', $specifiedVersion, $definition->getUrl());
41+
} else {
42+
throw new \RuntimeException('The definition of package is invalid');
43+
}
44+
45+
$savePath = Phar::running(false) ?: $this->runtimePath . '/';
46+
$file = $this->download($url, $savePath, 0755);
47+
if (! file_exists($savePath)) {
48+
throw new \RuntimeException('Download failed, cannot locate the file in local.');
49+
}
50+
51+
// Is file name ends with .zip?
52+
if (str_ends_with($file->getFilename(), '.zip')) {
53+
$zip = new \ZipArchive();
54+
$zip->open($file->getRealPath());
55+
$this->logger->info('Unpacking zip file ' . $savePath);
56+
for ($i = 0; $i < $zip->numFiles; ++$i) {
57+
$filename = $zip->getNameIndex($i);
58+
if (str_ends_with($filename, 'builds/pint')) {
59+
copy('zip://' . $file->getRealPath() . '#' . $filename, $savePath . $definition->getBin());
60+
}
61+
}
62+
$zip->close();
63+
$this->logger->info('Unpacked zip file ' . $savePath);
64+
unlink($file->getRealPath());
65+
}
66+
67+
$licenseFile = $savePath . 'LICENSE';
68+
// If license file exists, delete it.
69+
70+
if (file_exists($licenseFile)) {
71+
unlink($licenseFile);
72+
}
73+
74+
return new SplFileInfo($savePath . $definition->getBin());
75+
}
76+
77+
public function versions(string $pkgName, array $options = []): array
78+
{
79+
$definition = $this->getDefinition($pkgName);
80+
if (! $definition) {
81+
throw new \RuntimeException('The package not found');
82+
}
83+
if (! $definition->getRepo() && ! $definition->getComposerName()) {
84+
throw new NotSupportVersionsException($pkgName);
85+
}
86+
if ($definition->getLatestFetchType() === 'github') {
87+
return $this->fetchVersionsFromGithubRelease($definition->getRepo(), $definition->getBin());
88+
}
89+
if ($definition->getLatestFetchType() === 'packagist') {
90+
return $this->fetchVersionsFromPackagist($definition->getPkgName(), $definition->getComposerName());
91+
}
92+
throw new BoxException('The definition of package is invalid');
93+
}
94+
}

src/app/DownloadManager.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* @link https://www.hyperf.io
88
* @document https://hyperf.wiki
99
* @contact group@hyperf.io
10+
*
1011
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
1112
*/
1213

@@ -17,6 +18,7 @@
1718
use App\DownloadHandler\DefaultHandler;
1819
use App\DownloadHandler\MicroHandler;
1920
use App\DownloadHandler\PhpHandler;
21+
use App\DownloadHandler\PintHandler;
2022
use App\DownloadHandler\SwooleCliHandler;
2123
use App\Exception\PkgDefinitionNotFoundException;
2224
use Hyperf\Di\Annotation\Inject;
@@ -30,6 +32,7 @@ class DownloadManager
3032
'composer' => ComposerHandler::class,
3133
'micro' => MicroHandler::class,
3234
'php' => PhpHandler::class,
35+
'pint' => PintHandler::class,
3336
'swoole-cli' => SwooleCliHandler::class,
3437
'default' => DefaultHandler::class,
3538
];
@@ -72,6 +75,7 @@ public function versions(string $pkg, array $options): array
7275
$key = $pkg;
7376
}
7477
$handler = $this->container->get($this->handlers[$key]);
78+
7579
return $handler->versions($pkg, $options);
7680
}
7781

0 commit comments

Comments
 (0)