Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add software command #5

Merged
merged 2 commits into from
Jul 19, 2024
Merged
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
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,31 @@ composer require internal/dload -W

## Usage

### Get predefined software list

```bash
./vendor/bin/dload list
```

### Download single software

```bash
./vendor/bin/dload get dolt
```

### Configure preset for the project (WIP)

Create `dload.xml` file in the root of the project with the following content:

```xml
<?xml version="1.0"?>
<dload>
<todo />
</dload>
```

Download all the software from the preset:

```bash
./vendor/bin/dload dolt
./vendor/bin/dload get
```
3 changes: 2 additions & 1 deletion bin/dload
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ if ('cli' !== PHP_SAPI) {
$application->setCommandLoader(
new FactoryCommandLoader([
Command\Get::getDefaultName() => static fn() => new Command\Get(),
Command\ListSoftware::getDefaultName() => static fn() => new Command\ListSoftware(),
]),
);
$application->setDefaultCommand(Command\Get::getDefaultName(), true);
$application->setDefaultCommand(Command\Get::getDefaultName(), false);
$application->setVersion(Info::version());
$application->run();
})();
43 changes: 43 additions & 0 deletions src/Command/Base.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Internal\DLoad\Command;

use Internal\DLoad\Bootstrap;
use Internal\DLoad\Service\Container;
use Internal\DLoad\Service\Logger;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\StyleInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

/**
* @internal
*/
abstract class Base extends Command
{
protected Logger $logger;

protected Container $container;

protected function execute(
InputInterface $input,
OutputInterface $output,
): int {
$this->logger = new Logger($output);
$this->container = $container = Bootstrap::init()->withConfig(
xml: \dirname(__DIR__, 2) . '/dload.xml',
inputOptions: $input->getOptions(),
inputArguments: $input->getArguments(),
environment: \getenv(),
)->finish();
$container->set($input, InputInterface::class);
$container->set($output, OutputInterface::class);
$container->set(new SymfonyStyle($input, $output), StyleInterface::class);
$container->set($this->logger);

return Command::SUCCESS;
}
}
36 changes: 7 additions & 29 deletions src/Command/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,17 @@

namespace Internal\DLoad\Command;

use Internal\DLoad\Bootstrap;
use Internal\DLoad\DLoad;
use Internal\DLoad\Module\Common\Architecture;
use Internal\DLoad\Module\Common\OperatingSystem;
use Internal\DLoad\Module\Common\Stability;
use Internal\DLoad\Service\Container;
use Internal\DLoad\Service\Logger;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Command\SignalableCommandInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\StyleInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

/**
* @internal
Expand All @@ -28,14 +23,8 @@
name: 'get',
description: 'Download a binary',
)]
final class Get extends Command implements SignalableCommandInterface
final class Get extends Base implements SignalableCommandInterface
{
private bool $cancelling = false;

private Logger $logger;

private Container $container;

public function configure(): void
{
$this->addArgument('binary', InputArgument::REQUIRED, 'Binary name, e.g. "rr", "dolt", "temporal" etc.');
Expand Down Expand Up @@ -69,25 +58,14 @@ public function getSubscribedSignals(): array
return $result;
}

protected function execute(
InputInterface $input,
OutputInterface $output,
): int {
$this->logger = new Logger($output);
$output->writeln('Binary to load: ' . $input->getArgument('binary'));
$output->writeln('Path to store the binary: ' . $input->getOption('path'));
protected function execute(InputInterface $input, OutputInterface $output): int
{
parent::execute($input, $output);

$this->container = $container = Bootstrap::init()->withConfig(
xml: \dirname(__DIR__, 2) . '/dload.xml',
inputOptions: $input->getOptions(),
inputArguments: $input->getArguments(),
environment: \getenv(),
)->finish();
$container->set($input, InputInterface::class);
$container->set($output, OutputInterface::class);
$container->set(new SymfonyStyle($input, $output), StyleInterface::class);
$container->set($this->logger);
$container = $this->container;

$output->writeln('Binary to load: ' . $input->getArgument('binary'));
$output->writeln('Path to store the binary: ' . $input->getOption('path'));
$output->writeln('Architecture: ' . $container->get(Architecture::class)->name);
$output->writeln(' Op. system: ' . $container->get(OperatingSystem::class)->name);
$output->writeln(' Stability: ' . $container->get(Stability::class)->name);
Expand Down
49 changes: 49 additions & 0 deletions src/Command/ListSoftware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Internal\DLoad\Command;

use Internal\DLoad\Module\Common\Config\Embed\Software;
use Internal\DLoad\Module\Downloader\SoftwareCollection;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* @internal
*/
#[AsCommand(
name: 'software',
description: 'List available software',
)]
final class ListSoftware extends Base
{
protected function execute(
InputInterface $input,
OutputInterface $output,
): int {
parent::execute($input, $output);

/** @var SoftwareCollection $registry */
$registry = $this->container->get(SoftwareCollection::class);

$output->writeln('There are <options=bold>' . $registry->count() . '</> software available:');
$output->writeln('');

/** @var Software $software */
foreach ($registry->getIterator() as $software) {
$output->writeln("<fg=green;options=bold>{$software->getId()}</> $software->name");

foreach ($software->repositories as $repo) {
$output->writeln("<fg=blue>{$repo->type}: {$repo->uri}</>");
}

$software->description and $output->writeln("<fg=gray>" . \wordwrap($software->description, 78) . "</>");
$output->writeln('');
}

return Command::SUCCESS;
}
}
12 changes: 10 additions & 2 deletions src/Module/Downloader/SoftwareCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
/**
* @implements IteratorAggregate<Software>
*/
final class SoftwareCollection implements \IteratorAggregate
final class SoftwareCollection implements \IteratorAggregate, \Countable
{
public function __construct(
private SoftwareRegistry $softwareRegistry,
private readonly SoftwareRegistry $softwareRegistry,
) {}

public function findSoftware(string $name): ?Software
Expand All @@ -35,4 +35,12 @@ public function getIterator(): \Traversable
{
yield from $this->softwareRegistry->software;
}

/**
* @return int<0, max>
*/
public function count(): int
{
return \count($this->softwareRegistry->software);
}
}