Skip to content

Commit 7880f14

Browse files
committed
feat(commands): add Soar binary download command
- Added a new `soar:download` console command to download the Soar binary. - Implemented functionality to specify the source (`github` or `gitee`) and path for the binary download. - Integrated `DownloadCommand` into `SoarServiceProvider` for console use. - Updated test cases to include comments on the new download command, though not fully enabled. - Added `guzzlehttp/guzzle` dependency to handle HTTP requests for binary download.
1 parent aadf633 commit 7880f14

7 files changed

+135
-3
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ vendor/
88
.phpunit.result.cache
99
clover.xml
1010
composer.lock
11-
laravel/
11+
/laravel
1212
phpunit.xml
1313
psalm.xml
1414
tests.*

composer-dependency-analyser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
->ignoreErrorsOnPackageAndPath(
5959
'spatie/ray',
6060
__DIR__.'/src/Outputs/RayOutput.php',
61-
[ErrorType::DEV_DEPENDENCY_IN_PROD]
61+
[ErrorType::SHADOW_DEPENDENCY]
6262
)
6363
->ignoreErrorsOnPackages(
6464
[

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"ergebnis/rector-rules": "^1.4",
6262
"guanguans/ai-commit": "dev-main",
6363
"guanguans/monorepo-builder-worker": "^2.0",
64+
"guzzlehttp/guzzle": "^7.9",
6465
"itsgoingd/clockwork": "^5.3",
6566
"laradumps/laradumps": "^2.2 || ^3.0 || ^4.0",
6667
"laravel/facade-documenter": "dev-main",

laravel

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/Commands/DownloadCommand.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Copyright (c) 2020-2025 guanguans<ityaozm@gmail.com>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*
11+
* @see https://github.com/guanguans/laravel-soar
12+
*/
13+
14+
namespace Guanguans\LaravelSoar\Commands;
15+
16+
use Guanguans\LaravelSoar\Soar;
17+
use Illuminate\Console\Command;
18+
use Illuminate\Support\Facades\File;
19+
use Illuminate\Support\Facades\Http;
20+
use Symfony\Component\Console\Helper\ProgressBar;
21+
22+
/**
23+
* @method defaultSoarBinary()
24+
*/
25+
class DownloadCommand extends Command
26+
{
27+
private const SOURCES = [
28+
'github' => 'https://raw.githubusercontent.com/guanguans/soar-php/master/bin/%s',
29+
'gitee' => 'https://gitee.com/guanguans/soar-php/raw/master/bin/%s',
30+
];
31+
32+
/** @noinspection ClassOverridesFieldOfSuperClassInspection */
33+
protected $signature = <<<'SIGNATURE'
34+
soar:download
35+
{--s|source=github : The source of the Soar binary.}
36+
{--p|path= : The saved path of the Soar binary.}
37+
SIGNATURE;
38+
39+
/** @noinspection ClassOverridesFieldOfSuperClassInspection */
40+
protected $description = 'Download the Soar binary';
41+
42+
public function handle(): void
43+
{
44+
$savedPath = $this->savedPath();
45+
46+
if (File::isFile($savedPath)) {
47+
$this->warn("⚠️ The Soar binary [$savedPath] already exists.");
48+
49+
return;
50+
}
51+
52+
$this->info('⏳ Downloading Soar binary...');
53+
54+
Http::withoutVerifying()
55+
->withOptions([
56+
'sink' => $savedPath,
57+
'progress' => function (int $totalDownload, int $downloaded) use (&$progressBar): void {
58+
if (0 < $totalDownload && 0 < $downloaded && !$progressBar instanceof ProgressBar) {
59+
$progressBar = new ProgressBar($this->output, $totalDownload);
60+
$progressBar->start();
61+
}
62+
63+
if ($totalDownload === $downloaded && $progressBar instanceof ProgressBar) {
64+
$progressBar->finish();
65+
}
66+
67+
$progressBar and $progressBar->setProgress($downloaded);
68+
},
69+
])
70+
->get($this->url());
71+
72+
$this->info("✅ The Soar binary [$savedPath] has been downloaded.");
73+
}
74+
75+
private function soarBinaryName(): string
76+
{
77+
return basename((fn () => $this->defaultSoarBinary())->call(resolve(Soar::class)));
78+
}
79+
80+
/**
81+
* @noinspection OffsetOperationsInspection
82+
*/
83+
private function url(): string
84+
{
85+
return \sprintf(self::SOURCES[$this->option('source')], $this->soarBinaryName());
86+
}
87+
88+
private function savedPath(): string
89+
{
90+
return $this->option('path') ?: base_path($this->soarBinaryName());
91+
}
92+
}

src/SoarServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use Composer\InstalledVersions;
1717
use Guanguans\LaravelSoar\Commands\ClearCommand;
18+
use Guanguans\LaravelSoar\Commands\DownloadCommand;
1819
use Guanguans\LaravelSoar\Commands\RunCommand;
1920
use Guanguans\LaravelSoar\Commands\ScoreCommand;
2021
use Guanguans\LaravelSoar\Mixins\QueryBuilderMixin;
@@ -127,6 +128,7 @@ private function registerCommands(): void
127128
if ($this->app->runningInConsole()) {
128129
$this->commands([
129130
ClearCommand::class,
131+
DownloadCommand::class,
130132
RunCommand::class,
131133
ScoreCommand::class,
132134
]);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/** @noinspection AnonymousFunctionStaticInspection */
4+
/** @noinspection NullPointerExceptionInspection */
5+
/** @noinspection PhpPossiblePolymorphicInvocationInspection */
6+
/** @noinspection PhpUndefinedClassInspection */
7+
/** @noinspection PhpUnhandledExceptionInspection */
8+
/** @noinspection SqlResolve */
9+
/** @noinspection StaticClosureCanBeUsedInspection */
10+
declare(strict_types=1);
11+
12+
/**
13+
* Copyright (c) 2020-2025 guanguans<ityaozm@gmail.com>
14+
*
15+
* For the full copyright and license information, please view
16+
* the LICENSE file that was distributed with this source code.
17+
*
18+
* @see https://github.com/guanguans/laravel-soar
19+
*/
20+
21+
use Guanguans\LaravelSoar\Commands\DownloadCommand;
22+
use Illuminate\Support\Facades\Artisan;
23+
use Illuminate\Support\Facades\Http;
24+
25+
it('can download Soar binary', function (): void {
26+
Http::preventStrayRequests();
27+
28+
Artisan::call(
29+
DownloadCommand::class,
30+
[
31+
// '--path' => null,
32+
'--source' => 'gitee',
33+
]
34+
);
35+
36+
// dump(Artisan::output());
37+
expect(true)->toBeTrue();
38+
})->group(__DIR__, __FILE__)->skip();

0 commit comments

Comments
 (0)