Skip to content

Commit

Permalink
Merge pull request #5: Add software command
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk authored Jul 19, 2024
2 parents 6dc0cb1 + 6fdbc35 commit 82b3261
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 33 deletions.
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

Check failure on line 26 in src/Command/Get.php

View workflow job for this annotation

GitHub Actions / psalm (ubuntu-latest, 8.2, locked)

PropertyNotSetInConstructor

src/Command/Get.php:26:13: PropertyNotSetInConstructor: Property Internal\DLoad\Command\Get::$logger is not defined in constructor of Internal\DLoad\Command\Get or in any methods called in the constructor (see https://psalm.dev/074)

Check failure on line 26 in src/Command/Get.php

View workflow job for this annotation

GitHub Actions / psalm (ubuntu-latest, 8.2, locked)

PropertyNotSetInConstructor

src/Command/Get.php:26:13: PropertyNotSetInConstructor: Property Internal\DLoad\Command\Get::$container is not defined in constructor of Internal\DLoad\Command\Get or in any methods called in the constructor (see https://psalm.dev/074)
{
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

Check failure on line 21 in src/Command/ListSoftware.php

View workflow job for this annotation

GitHub Actions / psalm (ubuntu-latest, 8.2, locked)

PropertyNotSetInConstructor

src/Command/ListSoftware.php:21:13: PropertyNotSetInConstructor: Property Internal\DLoad\Command\ListSoftware::$logger is not defined in constructor of Internal\DLoad\Command\ListSoftware or in any methods called in the constructor (see https://psalm.dev/074)

Check failure on line 21 in src/Command/ListSoftware.php

View workflow job for this annotation

GitHub Actions / psalm (ubuntu-latest, 8.2, locked)

PropertyNotSetInConstructor

src/Command/ListSoftware.php:21:13: PropertyNotSetInConstructor: Property Internal\DLoad\Command\ListSoftware::$container is not defined in constructor of Internal\DLoad\Command\ListSoftware or in any methods called in the constructor (see https://psalm.dev/074)
{
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);
}
}

0 comments on commit 82b3261

Please sign in to comment.