Skip to content

Commit

Permalink
Add support for iterable XML blocks; preconfigure software registry
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Jul 18, 2024
1 parent ea19878 commit 9d50424
Show file tree
Hide file tree
Showing 13 changed files with 201 additions and 4 deletions.
18 changes: 18 additions & 0 deletions dload.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
<?xml version="1.0"?>
<dload>
<registry>
<software name="RoadRunner" description="High performant Application server">
<repository type="github" uri="roadrunner-server/roadrunner" pattern="/^roadrunner-.*/"/>
<file rename="rr" pattern="/^(roadrunner|rr)(?:\.exe)?/" />
</software>
</registry>
<registry>
<software name="Dolt" description="Dolt is a SQL database that you can fork, clone, branch, merge, push and pull just like a Git repository">
<repository type="github" uri="dolthub/dolt" pattern="/^dolt-.*/"/>
<file pattern="/^(dolt)(?:\.exe)?/" />
</software>
</registry>
<registry>
<software name="Temporal" description="Temporal command-line interface and development server">
<repository type="github" uri="temporalio/cli" pattern="/^temporal_cli_.*/"/>
<file pattern="/^(temporal)(?:\.exe)?/" />
</software>
</registry>
</dload>
1 change: 0 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
<directory name="src"/>
<ignoreFiles>
<directory name="vendor"/>
<directory name="src/Test"/>
<directory name="tests"/>
</ignoreFiles>
</projectFiles>
Expand Down
7 changes: 5 additions & 2 deletions src/Command/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function configure(): void
{
$this->addArgument('binary', InputArgument::REQUIRED, 'Binary name, e.g. "rr", "dolt", "temporal" etc.');
$this->addOption('path', null, InputOption::VALUE_OPTIONAL, 'Path to store the binary, e.g. "./bin"', ".");
$this->addOption('rename', null, InputOption::VALUE_OPTIONAL, 'Rename the binary, e.g. "rr"');
$this->addOption('arch', null, InputOption::VALUE_OPTIONAL, 'Architecture, e.g. "amd64", "arm64" etc.');
$this->addOption('os', null, InputOption::VALUE_OPTIONAL, 'Operating system, e.g. "linux", "darwin" etc.');
$this->addOption('stability', null, InputOption::VALUE_OPTIONAL, 'Stability, e.g. "stable", "beta" etc.');
Expand Down Expand Up @@ -76,10 +77,12 @@ protected function execute(
)->finish();

$output->writeln('Architecture: ' . $container->get(Architecture::class)->name);
$output->writeln('Operating system: ' . $container->get(OperatingSystem::class)->name);
$output->writeln('Stability: ' . $container->get(Stability::class)->name);
$output->writeln('Op. system: ' . $container->get(OperatingSystem::class)->name);
$output->writeln('Stability: ' . $container->get(Stability::class)->name);


tr($container->get(\Internal\DLoad\Module\Common\Config\SoftwareRegistry::class));

// $repo = 'roadrunner-server/roadrunner';
// trap(
// GitHubRepository::fromDsn($repo)->getReleases()->first()->getAssets()
Expand Down
16 changes: 15 additions & 1 deletion src/Module/Common/Config/BuildInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,34 @@

namespace Internal\DLoad\Module\Common\Config;

use Internal\DLoad\Module\Common\Architecture;
use Internal\DLoad\Module\Common\Internal\Attribute\InputOption;
use Internal\DLoad\Module\Common\OperatingSystem;
use Internal\DLoad\Module\Common\Stability;

/**
* @internal
* @psalm-internal Internal\DLoad\Module\Common
*/
final class BuildInput
{
/**
* Use {@see Architecture} to get final value.
*/
#[InputOption('arch')]
public ?string $arch = null;

/**
* Use {@see Stability} to get final value.
*/
#[InputOption('stability')]
public ?string $stability = null;

/**
* Use {@see OperatingSystem} to get final value.
*/
#[InputOption('os')]
public ?string $os = null;

#[InputOption('version')]
public ?string $version = null;
}
19 changes: 19 additions & 0 deletions src/Module/Common/Config/DestinationInput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Internal\DLoad\Module\Common\Config;

use Internal\DLoad\Module\Common\Internal\Attribute\InputOption;

/**
* @internal
*/
final class DestinationInput
{
#[InputOption('path')]
public ?string $path = null;

#[InputOption('rename')]
public ?string $rename = null;
}
19 changes: 19 additions & 0 deletions src/Module/Common/Config/Embed/File.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Internal\DLoad\Module\Common\Config\Embed;

use Internal\DLoad\Module\Common\Internal\Attribute\XPath;

final class File
{
/**
* @var non-empty-string|null In case of not null, found file will be renamed to this value with the same extension.
*/
#[XPath('@rename')]
public ?string $rename = null;

#[XPath('@pattern')]
public string $pattern = '/^.*$/';
}
17 changes: 17 additions & 0 deletions src/Module/Common/Config/Embed/Repository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Internal\DLoad\Module\Common\Config\Embed;

use Internal\DLoad\Module\Common\Internal\Attribute\XPath;

final class Repository
{
#[XPath('@type')]
public string $type = 'github';
#[XPath('@uri')]
public string $uri;
#[XPath('@pattern')]
public string $pattern = '/^.*$/';
}
29 changes: 29 additions & 0 deletions src/Module/Common/Config/Embed/Software.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Internal\DLoad\Module\Common\Config\Embed;

use Internal\DLoad\Module\Common\Internal\Attribute\XPath;
use Internal\DLoad\Module\Common\Internal\Attribute\XPathEmbedList;

final class Software
{
#[XPath('@name')]
public string $name;

/**
* If {@see null}, the name in lower case will be used.
*/
#[XPath('@alias')]
public ?string $alias = null;

#[XPath('@description')]
public string $description = '';

#[XPathEmbedList('repository', Repository::class)]
public array $repositories = [];

#[XPathEmbedList('file', File::class)]
public array $files = [];
}
16 changes: 16 additions & 0 deletions src/Module/Common/Config/GitHubConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Internal\DLoad\Module\Common\Config;

use Internal\DLoad\Module\Common\Internal\Attribute\Env;

/**
* @internal
*/
final class GitHubConfig
{
#[Env('GITHUB_TOKEN')]
public ?string $token = null;
}
13 changes: 13 additions & 0 deletions src/Module/Common/Config/SoftwareRegistry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Internal\DLoad\Module\Common\Config;

use Internal\DLoad\Module\Common\Internal\Attribute\XPathEmbedList;

final class SoftwareRegistry
{
#[XPathEmbedList('/dload/registry/software', Embed\Software::class)]
public array $software = [];
}
21 changes: 21 additions & 0 deletions src/Module/Common/Internal/Attribute/XPathEmbedList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Internal\DLoad\Module\Common\Internal\Attribute;

/**
* @internal
*/
#[\Attribute(\Attribute::TARGET_PROPERTY)]
final class XPathEmbedList implements ConfigAttribute
{
/**
* @param non-empty-string $path
* @param class-string $class
*/
public function __construct(
public string $path,
public string $class,
) {}
}
28 changes: 28 additions & 0 deletions src/Module/Common/Internal/Injection/ConfigLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Internal\DLoad\Module\Common\Internal\Attribute\InputOption;
use Internal\DLoad\Module\Common\Internal\Attribute\PhpIni;
use Internal\DLoad\Module\Common\Internal\Attribute\XPath;
use Internal\DLoad\Module\Common\Internal\Attribute\XPathEmbedList;
use Internal\DLoad\Service\Logger;

/**
Expand Down Expand Up @@ -66,6 +67,7 @@ private function injectValue(object $config, \ReflectionProperty $property, arra
/** @var mixed $value */
$value = match (true) {
$attribute instanceof XPath => $this->getXPath($attribute),
$attribute instanceof XPathEmbedList => $this->getXPathEmbeddedList($attribute),
$attribute instanceof Env => $this->env[$attribute->name] ?? null,
$attribute instanceof InputOption => $this->inputOptions[$attribute->name] ?? null,
$attribute instanceof InputArgument => $this->inputArguments[$attribute->name] ?? null,
Expand Down Expand Up @@ -121,4 +123,30 @@ private function getXPath(XPath $attribute): mixed
? $value[$attribute->key]
: null;
}

private function getXPathEmbeddedList(XPathEmbedList $attribute): array
{
$result = [];
$value = $this->xml?->xpath($attribute->path);
\is_array($value) or throw new \Exception(\sprintf('Invalid XPath `%s`', $attribute->path));

foreach ($value as $xml) {
\assert($xml instanceof \SimpleXMLElement);

// Instantiate
$item = new $attribute->class();

$this->withXml($xml)->hydrate($item);
$result[] = $item;
}

return $result;
}

private function withXml(\SimpleXMLElement $xml): self
{
$self = clone $this;
$self->xml = $xml;
return $self;
}
}
1 change: 1 addition & 0 deletions src/Service/Factoriable.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Class creates new instances of itself.
*
* @method static create
* Method creates new instance of the class. May contain any injectable parameters.
*
* @internal
*/
Expand Down

0 comments on commit 9d50424

Please sign in to comment.