Skip to content
Draft
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ See the [RoadRunner documentation](https://docs.roadrunner.dev) for the configur

### Common options

The binary commands accept `--filter` (`-f`, version constraint), `--stability` (`-s`), `--os` (`-o`), `--arch` (`-a`) and `--location` (`-l`, target directory) options. Releases are fetched from the GitHub API; set the `GITHUB_TOKEN` environment variable to avoid its rate limits, and `RR_GITHUB_API_URL` to use another API endpoint (GitHub Enterprise, a mirror; defaults to `https://api.github.com`).
The binary commands accept `--filter` (`-f`, version constraint), `--stability` (`-s`), `--os` (`-o`), `--arch` (`-a`) and `--location` (`-l`, target directory) options. Releases are fetched from the GitHub API; set the `GITHUB_TOKEN` environment variable to avoid its rate limits, and `RR_GITHUB_API_URL` to use another API endpoint (GitHub Enterprise, a mirror; defaults to `https://api.github.com`). `get-binary` and `download-protoc-binary` download the binaries with [DLoad](https://github.com/php-internal/dload); a project's `dload.xml` does not affect them. For these two commands `RR_GITHUB_API_URL` is either `https://api.github.com` or a GitHub Enterprise Server API URL ending with `/api/v3` (e.g. `https://ghe.example.com/api/v3`), and `GITHUB_TOKEN` is sent only to the public GitHub: the token for a GitHub Enterprise Server goes into `DLOAD_TOKEN_<HOST>` (e.g. `DLOAD_TOKEN_GHE_EXAMPLE_COM`).
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@
"ext-json": "*",
"composer-runtime-api": "^2.0",
"composer/semver": "^3.4",
"internal/dload": "dev-feat/repository-server as 1.19.0",
"roadrunner/worker": "^3.8",
"spiral/tokenizer": "^2.13 || ^3.15",
"symfony/console": "^5.3 || ^6.0 || ^7.0 || ^8.0",
"symfony/http-client": "^4.4.51 || ^5.4.49 || ^6.4.17 || ^7.2 || ^8.0",
"symfony/console": "^6.4 || ^7.0 || ^8.0",
"symfony/http-client": "^6.4.17 || ^7.2 || ^8.0",
"symfony/yaml": "^5.4.49 || ^6.4.17 || ^7.2 || ^8.0"
},
"require-dev": {
Expand Down
3 changes: 3 additions & 0 deletions resources/dload.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0"?>
<!-- Intentionally empty: see Spiral\RoadRunner\Console\Downloader\DLoadDownloader -->
<dload/>
181 changes: 21 additions & 160 deletions src/DownloadProtocBinaryCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,13 @@

namespace Spiral\RoadRunner\Console;

use Spiral\RoadRunner\Console\Archive\ArchiveInterface;
use Spiral\RoadRunner\Console\Archive\Factory;
use Spiral\RoadRunner\Console\Command\ArchitectureOption;
use Spiral\RoadRunner\Console\Command\InstallationLocationOption;
use Spiral\RoadRunner\Console\Command\OperatingSystemOption;
use Spiral\RoadRunner\Console\Command\StabilityOption;
use Spiral\RoadRunner\Console\Command\VersionFilterOption;
use Spiral\RoadRunner\Console\Repository\AssetInterface;
use Spiral\RoadRunner\Console\Repository\ReleaseInterface;
use Spiral\RoadRunner\Console\Repository\ReleasesCollection;
use Spiral\RoadRunner\Console\Repository\RepositoryInterface;
use Symfony\Component\Console\Helper\ProgressBar;
use Spiral\RoadRunner\Console\Downloader\DLoadDownloader;
use Spiral\RoadRunner\Console\Environment\OperatingSystem;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\StyleInterface;
Expand All @@ -25,16 +20,16 @@
*/
final class DownloadProtocBinaryCommand extends Command
{
private const ERROR_ENVIRONMENT = 'Could not find any available protoc-gen-php-grpc binary version which meets criterion (--%s=%s --%s=%s --%s=%s). Available: %s';

private OperatingSystemOption $os;
private ArchitectureOption $arch;
private VersionFilterOption $version;
private StabilityOption $stability;
private InstallationLocationOption $location;
private DLoadDownloader $downloader;

public function __construct(?string $name = null)
public function __construct(?string $name = null, ?DLoadDownloader $downloader = null)
{
$this->downloader = $downloader ?? new DLoadDownloader();
parent::__construct($name ?? 'download-protoc-binary');

$this->os = new OperatingSystemOption($this);
Expand All @@ -56,91 +51,39 @@ public function execute(InputInterface $input, OutputInterface $output): int
$io = $this->io($input, $output);

$target = $this->location->get($input, $io);
$repository = $this->getRepository();
$os = $this->os->get($input, $io);

$output->writeln('');
$output->writeln(' Environment:');
$output->writeln(\sprintf(' - Version: <info>%s</info>', $this->version->get($input, $io)));
$output->writeln(\sprintf(' - Stability: <info>%s</info>', $this->stability->get($input, $io)));
$output->writeln(\sprintf(' - Operating System: <info>%s</info>', $this->os->get($input, $io)));
$output->writeln(\sprintf(' - Operating System: <info>%s</info>', $os));
$output->writeln(\sprintf(' - Architecture: <info>%s</info>', $this->arch->get($input, $io)));
$output->writeln('');

// List of all available releases
$releases = $this->version->find($input, $io, $repository);

/**
* @var AssetInterface $asset
* @var ReleaseInterface $release
*/
[$asset, $release] = $this->findAsset($repository, $releases, $input, $io);

// Installation
$output->writeln(
\sprintf(" - <info>%s</info>", $release->getRepositoryName()) .
\sprintf(' (<comment>%s</comment>):', $release->getVersion()) .
' Downloading...',
);

if ($output->isVerbose()) {
$output->writeln(\sprintf(" -- <info>%s</info>", $asset->getName()));
}

// Install rr binary
$file = $this->installBinary($target, $release, $asset, $io, $output);
$binary = $target . ($os === OperatingSystem::OS_WINDOWS ? '/protoc-gen-php-grpc.exe' : '/protoc-gen-php-grpc');

// Success
if ($file === null) {
if (!$this->checkExisting($binary, $io)) {
$io->warning('protoc-gen-php-grpc has not been installed');

return 1;
}

return 0;
}

private function installBinary(
string $target,
ReleaseInterface $release,
AssetInterface $asset,
StyleInterface $io,
OutputInterface $out,
): ?\SplFileInfo {
$extractor = $this->assetToArchive($asset, $out)
->extract([
'protoc-gen-php-grpc.exe' => $target . '/protoc-gen-php-grpc.exe',
'protoc-gen-php-grpc' => $target . '/protoc-gen-php-grpc',
]);

$file = null;
while ($extractor->valid()) {
$file = $extractor->current();
\assert($file instanceof \SplFileInfo);

if (!$this->checkExisting($file, $io)) {
$extractor->send(false);
continue;
}

// Success
$path = $file->getRealPath() ?: $file->getPathname();
$message = 'protoc-gen-php-grpc (<comment>%s</comment>) has been installed into <info>%s</info>';
$message = \sprintf($message, $release->getVersion(), $path);
$out->writeln($message);

$extractor->next();

if (!$file->isExecutable()) {
@\chmod($file->getRealPath(), 0755);
}
}

return $file;
return $this->downloader->download(
software: 'protoc-gen-php-grpc',
constraint: $this->version->get($input, $io),
stability: $this->stability->get($input, $io),
os: $os,
arch: $this->arch->get($input, $io),
location: $target,
force: true,
output: $output,
);
}

private function checkExisting(\SplFileInfo $bin, StyleInterface $io): bool
private function checkExisting(string $binary, StyleInterface $io): bool
{
if (\is_file($bin->getPathname())) {
if (\is_file($binary)) {
$io->warning('protoc-gen-php-grpc binary file already exists!');

if (!$io->confirm('Do you want overwrite it?', false)) {
Expand All @@ -152,86 +95,4 @@ private function checkExisting(\SplFileInfo $bin, StyleInterface $io): bool

return true;
}

private function findAsset(
RepositoryInterface $repo,
ReleasesCollection $releases,
InputInterface $in,
StyleInterface $io,
): array {
$osOption = $this->os->get($in, $io);
$archOption = $this->arch->get($in, $io);
$stabilityOption = $this->stability->get($in, $io);

/** @var ReleaseInterface[] $filtered */
$filtered = $releases
->minimumStability($stabilityOption)
->withAssets();

foreach ($filtered as $release) {
$asset = $release->getAssets()
->filter(
static fn(AssetInterface $asset): bool =>
\str_starts_with($asset->getName(), 'protoc-gen-php-grpc'),
)
->whereArchitecture($archOption)
->whereOperatingSystem($osOption)
->first();

if ($asset === null) {
$io->warning(
\vsprintf('%s %s does not contain available assembly (further search in progress)', [
$repo->getName(),
$release->getVersion(),
]),
);

continue;
}

return [$asset, $release];
}

$message = \vsprintf(self::ERROR_ENVIRONMENT, [
$this->os->getName(),
$osOption,
$this->arch->getName(),
$archOption,
$this->stability->getName(),
$stabilityOption,
$this->version->choices($releases),
]);

throw new \UnexpectedValueException($message);
}

private function assetToArchive(AssetInterface $asset, OutputInterface $out, ?string $temp = null): ArchiveInterface
{
$factory = new Factory();

$progress = new ProgressBar($out);
$progress->setFormat(' [%bar%] %percent:3s%% (%size%Kb/%total%Kb)');
$progress->setMessage('0.00', 'size');
$progress->setMessage('?.??', 'total');
$progress->display();

try {
return $factory->fromAsset($asset, static function (int $size, int $total) use ($progress): void {
if ($progress->getMaxSteps() !== $total) {
$progress->setMaxSteps($total);
}

if ($progress->getStartTime() === 0) {
$progress->start();
}

$progress->setMessage(\number_format($size / 1000, 2), 'size');
$progress->setMessage(\number_format($total / 1000, 2), 'total');

$progress->setProgress($size);
}, $temp);
} finally {
$progress->clear();
}
}
}
Loading
Loading