-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
176 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the PMU project. | ||
* | ||
* (c) Antoine Bluchet <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Pmu\Command; | ||
|
||
use Composer\Command\BaseCommand; | ||
use Composer\Console\Input\InputArgument; | ||
use Composer\Console\Input\InputOption; | ||
use Composer\Json\JsonFile; | ||
use Composer\Package\PackageInterface; | ||
use Pmu\Config; | ||
use SplObjectStorage; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
final class BlendCommand extends BaseCommand | ||
{ | ||
protected function configure(): void | ||
{ | ||
$this->setName('blend') | ||
->setDefinition([ | ||
new InputOption('dev', 'D', InputOption::VALUE_NONE, 'Blend dev requirements.'), | ||
new InputArgument('projects', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, ''), | ||
]) | ||
->setDescription('Blend the mono-repository dependencies into each projects.'); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$composer = $this->requireComposer(); | ||
$config = Config::create($composer); | ||
$repo = $composer->getRepositoryManager(); | ||
$requireKey = true === $input->getOption('dev') ? 'require-dev' : 'require'; | ||
$packageAccessor = true === $input->getOption('dev') ? 'getDevRequires' : 'getRequires'; | ||
|
||
$projects = null; | ||
if (is_array($p = $input->getArgument('projects')) && $p) { | ||
$projects = array_map('strval', $p); | ||
} | ||
|
||
foreach ($config->projects as $p) { | ||
if ($projects && !in_array($p, $projects, true)) { | ||
continue; | ||
} | ||
|
||
$commandPackage = $repo->findPackage($p, '*'); | ||
if (!$commandPackage || !$commandPackage instanceof PackageInterface) { | ||
$output->writeln(sprintf('Package "%s" could not be found.', $p)); | ||
return 1; | ||
} | ||
|
||
/** | ||
* @var SplObjectStorage<PackageInterface, array<int|string, mixed>> | ||
*/ | ||
$packagesToUpdate = new SplObjectStorage(); | ||
foreach ($composer->getPackage()->{$packageAccessor}() as $g) { | ||
foreach ($commandPackage->{$packageAccessor}() as $r) { | ||
if ($g->getTarget() == $r->getTarget()) { | ||
if (!$packagesToUpdate->contains($commandPackage)) { | ||
$packagesToUpdate[$commandPackage] = []; | ||
} | ||
|
||
$a = $packagesToUpdate[$commandPackage]; | ||
$a[$g->getTarget()] = $g->getPrettyConstraint(); | ||
$packagesToUpdate[$commandPackage] = $a; | ||
} | ||
} | ||
} | ||
|
||
foreach ($packagesToUpdate as $package) { | ||
$dir = $package->getDistUrl(); | ||
if (!is_string($dir) || !is_dir($dir)) { | ||
$output->writeln(sprintf('Package "%s" could not be found at path "%s".', $p, $dir)); | ||
continue; | ||
} | ||
|
||
$json = new JsonFile($dir . '/composer.json'); | ||
/** @var array{require: array<string, string>, 'require-dev': array<string, string>} */ | ||
$composerDefinition = $json->read(); | ||
|
||
foreach ($packagesToUpdate[$package] as $target => $constraint) { | ||
$composerDefinition[$requireKey][$target] = $constraint; | ||
} | ||
|
||
$json->write($composerDefinition); | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the PMU project. | ||
* | ||
* (c) Antoine Bluchet <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Pmu\Tests\Functional; | ||
|
||
use Composer\Console\Application; | ||
use PHPUnit\Framework\TestCase; | ||
use Pmu\Command\BlendCommand; | ||
use RuntimeException; | ||
use Symfony\Component\Console\Input\StringInput; | ||
use Symfony\Component\Console\Output\BufferedOutput; | ||
|
||
final class BlendCommandTest extends TestCase { | ||
private Application $application; | ||
private string $backup; | ||
private string $file; | ||
public function setUp(): void { | ||
$this->application = new Application(); | ||
$this->application->add(new BlendCommand); | ||
$this->application->setAutoExit(false); | ||
chdir(__DIR__ . '/../monorepo'); | ||
} | ||
|
||
public function testGraph(): void { | ||
$this->file = __DIR__ . '/../monorepo/packages/A/composer.json'; | ||
$this->backup = file_get_contents($this->file) ?: throw new RuntimeException; | ||
$output = new BufferedOutput; | ||
$this->application->run(new StringInput('blend'), $output); | ||
$json = file_get_contents($this->file); | ||
|
||
/** @var array{require: array<string, string>, 'require-dev': array<string, string>} */ | ||
$new = json_decode($json, true); | ||
$this->assertEquals($new['require']['soyuka/noop'], '^3.0.0'); | ||
} | ||
|
||
public function testGraphDev(): void { | ||
$this->file = __DIR__ . '/../monorepo/packages/A/composer.json'; | ||
$this->backup = file_get_contents($this->file) ?: throw new RuntimeException; | ||
$output = new BufferedOutput; | ||
$this->application->run(new StringInput('blend --dev'), $output); | ||
$json = file_get_contents($this->file); | ||
|
||
/** @var array{require: array<string, string>, 'require-dev': array<string, string>} */ | ||
$new = json_decode($json, true); | ||
$this->assertEquals($new['require-dev']['soyuka/noop-dev'], '^3.0.0'); | ||
} | ||
|
||
protected function tearDown(): void { | ||
file_put_contents($this->file, $this->backup); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters