Changes command module:register to resolve dependencies first.

This commit is contained in:
Basilius Sauter
2019-03-22 18:29:06 +01:00
parent d7100deb0f
commit 14b1db8b82
2 changed files with 54 additions and 18 deletions
+3 -6
View File
@@ -4,10 +4,7 @@ declare(strict_types=1);
namespace LotGD\Core;
use Composer\{
Composer,
Factory,
IO\NullIO,
Package\CompletePackageInterface
Composer, Factory, IO\NullIO, Package\CompletePackageInterface, Package\PackageInterface
};
use Monolog\Logger;
@@ -73,7 +70,7 @@ class ComposerManager
/**
* Return all the packages installed in the current setup.
* @return array<Composer\PackageInterface>
* @return PackageInterface[]
*/
public function getPackages(): array
{
@@ -85,7 +82,7 @@ class ComposerManager
/**
* Return a list of the configured packages which are LotGD modules (type = 'lotgd-module').
* @return array Array of \Composer\PackageInterface.
* @return PackageInterface[]
*/
public function getModulePackages(): array
{
+51 -12
View File
@@ -3,12 +3,15 @@ declare(strict_types=1);
namespace LotGD\Core\Console\Command;
use Composer\Repository\RepositoryInterface;
use LotGD\Core\ModuleManager;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use LotGD\Core\Exceptions\ClassNotFoundException;
use LotGD\Core\Exceptions\ModuleAlreadyExistsException;
use LotGD\Core\LibraryConfiguration;
use Symfony\Component\Console\Style\SymfonyStyle;
/**
* Danerys command to register and initiate any newly installed modules.
@@ -29,21 +32,57 @@ class ModuleRegisterCommand extends BaseCommand
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);
$modules = $this->game->getComposerManager()->getModulePackages();
$registered = [];
foreach ($modules as $p) {
$library = new LibraryConfiguration($this->game->getComposerManager(), $p, $this->game->getCWD());
$name = $library->getName();
try {
$this->game->getModuleManager()->register($library);
$output->writeln("<info>Registered new module {$name}</info>");
} catch (ModuleAlreadyExistsException $e) {
$output->writeln("Skipping already registered module {$name}");
} catch (ClassNotFoundException $e) {
$output->writeln("<error>Error installing module {$name}: " . $e->getMessage() . "</error>");
}
$this->registerModule($p->getName(), $io, $registered);
}
}
/**
* Register a given package as a module if it is of type lotdg-module. Resolves dependencies and skips already registered packages.
* @param string $packageName
* @param OutputInterface $output
* @param array $registered
* @throws \LotGD\Core\Exceptions\InvalidConfigurationException
* @throws \LotGD\Core\Exceptions\WrongTypeException
*/
protected function registerModule(
string $packageName,
SymfonyStyle $io,
array &$registered
) {
$composerRepository = $this->game->getComposerManager()->getComposer()
->getRepositoryManager()->getLocalRepository();
$package = $composerRepository->findPackage($packageName, "*");
if ($package->getType() !== "lotgd-module") {
return;
}
if (!empty($registered[$packageName])) {
return;
}
$io->text("Reading module {$packageName} {$package->getPrettyVersion()}");
$library = new LibraryConfiguration($this->game->getComposerManager(), $package, $this->game->getCWD());
$dependencies = $package->getRequires();
foreach ($dependencies as $dependency) {
$this->registerModule($dependency->getTarget(), $io, $registered);
}
try {
$this->game->getModuleManager()->register($library);
$io->success("\tRegistered new module {$packageName}");
} catch (ModuleAlreadyExistsException $e) {
$io->note("\tSkipping already registered module {$packageName}");
} catch (ClassNotFoundException $e) {
$io->error("\tError installing module {$packageName}: {$e->getMessage()}");
}
$registered[$packageName] = true;
}
}