Skip to content
This repository has been archived by the owner on Dec 3, 2023. It is now read-only.

Commit

Permalink
[MB] Decouple ConflictingPackageVersionsReporter
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Sep 9, 2018
1 parent b08d8e4 commit dfd5d42
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 17 deletions.
31 changes: 15 additions & 16 deletions packages/MonorepoBuilder/src/Console/Command/ValidateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symplify\MonorepoBuilder\Console\Reporter\ConflictingPackageVersionsReporter;
use Symplify\MonorepoBuilder\FileSystem\ComposerJsonProvider;
use Symplify\MonorepoBuilder\VersionValidator;
use Symplify\PackageBuilder\Console\Command\CommandNaming;
Expand All @@ -27,16 +28,22 @@ final class ValidateCommand extends Command
*/
private $composerJsonProvider;

/**
* @var ConflictingPackageVersionsReporter
*/
private $conflictingPackageVersionsReporter;

public function __construct(
SymfonyStyle $symfonyStyle,
ComposerJsonProvider $composerJsonProvider,
VersionValidator $versionValidator
VersionValidator $versionValidator,
ConflictingPackageVersionsReporter $conflictingPackageVersionsReporter
) {
parent::__construct();
$this->symfonyStyle = $symfonyStyle;
$this->versionValidator = $versionValidator;
$this->composerJsonProvider = $composerJsonProvider;

parent::__construct();
$this->conflictingPackageVersionsReporter = $conflictingPackageVersionsReporter;
}

protected function configure(): void
Expand All @@ -55,25 +62,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$composerFileInfos[] = $this->composerJsonProvider->getRootComposerJsonFileInfo();

$conflictingPackage = $this->versionValidator->findConflictingPackageInFileInfos($composerFileInfos);
if ($conflictingPackage === []) {
$conflictingPackageVersions = $this->versionValidator->findConflictingPackageVersionsInFileInfos(
$composerFileInfos
);
if ($conflictingPackageVersions === []) {
$this->symfonyStyle->success('All packages "composer.json" files use same package versions.');

// success
return 0;
}

foreach ($conflictingPackage as $packageName => $filesToVersions) {
$tableData = [];
foreach ($filesToVersions as $file => $version) {
$tableData[] = [$file, $version];
}

$this->symfonyStyle->title(sprintf('Package "%s" has various version', $packageName));
$this->symfonyStyle->table(['File', 'Version'], $tableData);
}

$this->symfonyStyle->error('Found conflicting package versions, fix them first.');
$this->conflictingPackageVersionsReporter->report($conflictingPackageVersions);

// fail
return 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php declare(strict_types=1);

namespace Symplify\MonorepoBuilder\Console\Reporter;

use Symfony\Component\Console\Style\SymfonyStyle;

final class ConflictingVersionsReporter
{
/**
* @var SymfonyStyle
*/
private $symfonyStyle;

public function __construct(SymfonyStyle $symfonyStyle)
{
$this->symfonyStyle = $symfonyStyle;
}

/**
* @param mixed[] $conflictingPackages
*/
public function reportConflictingPackages(array $conflictingPackages): void
{
foreach ($conflictingPackages as $packageName => $filesToVersions) {
$tableData = [];
foreach ($filesToVersions as $file => $version) {
$tableData[] = [$file, $version];
}

$this->symfonyStyle->title(sprintf('Package "%s" has various version', $packageName));
$this->symfonyStyle->table(['File', 'Version'], $tableData);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public function test(): void
{
$fileInfos = iterator_to_array(Finder::create()->name('*.json')->in(__DIR__ . '/Source') ->getIterator());

$conflictingPackageVersionsPerFile = $this->versionValidator->findConflictingPackageInFileInfos($fileInfos);
$conflictingPackageVersionsPerFile = $this->versionValidator->findConflictingPackageVersionsInFileInfos(
$fileInfos
);

$this->assertArrayHasKey('some/package', $conflictingPackageVersionsPerFile);

Expand Down

0 comments on commit dfd5d42

Please sign in to comment.