Skip to content

Commit

Permalink
fix: read "replace" when available
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Oct 1, 2024
1 parent f8aecbd commit b182404
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ vendor
tests/monorepo/packages/A/composer.lock
tests/monorepo/packages/B/composer.lock
tests/monorepo/packages/C/composer.lock
tests/replace/composer.lock
tests/monorepo/composer.lock
4 changes: 2 additions & 2 deletions src/Command/LinkCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$monoRepositoryComposerFile = $this->getComposerFileAtPath($path);
$monoRepositoryComposer = $this->readJsonFile($monoRepositoryComposerFile);
$baseDir = dirname($monoRepositoryComposerFile);
$config = Config::createFromJson($monoRepositoryComposer, $baseDir);
$composerFile = $this->getComposerFileAtPath($wd);
$repositories = $this->buildRepositories($config->composerFiles);
$composer = static::$fileContents[$composerFile] = $this->readJsonFile($composerFile);
$config = Config::createFromJson($monoRepositoryComposer, $baseDir, $composer);
$repositories = $this->buildRepositories($config->composerFiles);

$filesToWrite = [];
$revert = [];
Expand Down
23 changes: 19 additions & 4 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ public function __construct(public readonly array $projects = [], public readonl
}

/**
* @param array{extra?: array{pmu?: array{projects?: string[], exclude?: string[]}}} $composer
* @param array{name?: string, replace?: array<string, string>, extra?: array{pmu?: array{projects?: string[], exclude?: string[]}}} $composer
* @param array{name?: string, require?: array<string, string>, 'require-dev'?: array<string, string>} $projectComposer
*/
public static function createFromJson($composer, string $baseDir = '.'): static
public static function createFromJson($composer, string $baseDir = '.', array $projectComposer = []): static
{
$extra = $composer['extra'] ?? [];
if (!isset($extra['pmu'])) {
Expand All @@ -48,12 +49,22 @@ public static function createFromJson($composer, string $baseDir = '.'): static
}
}
}

$composerFiles = [];

// If the project requires the monorepository package, we add a repository for it.
// useful to work with `replace`
$name = $composer['name'] ?? null;
$hasMonorepositoryAsDependency = false;
if ($name && (array_key_exists($name, $projectComposer['require'] ?? []) || array_key_exists($name, $projectComposer['require-dev'] ?? []))) {
$hasMonorepositoryAsDependency = true;
$composerFiles[$name] = implode(DIRECTORY_SEPARATOR, [$baseDir, 'composer.json']);
}

$files = self::toArrayString($extra['pmu']['projects'] ?? null);
$projects = [];
$composerFiles = [];
foreach ($files as $glob) {
$filenames = glob(join(DIRECTORY_SEPARATOR, [$baseDir, $glob]));
$filenames = glob(implode(DIRECTORY_SEPARATOR, [$baseDir, $glob]));
if (!$filenames) {
return new self();
}
Expand All @@ -68,6 +79,10 @@ public static function createFromJson($composer, string $baseDir = '.'): static
throw new \RuntimeException(sprintf('Malformed JSON at path %s.', $filename));
}

if ($hasMonorepositoryAsDependency && array_key_exists($json['name'], $composer['replace'])) {

Check failure on line 82 in src/Config.php

View workflow job for this annotation

GitHub Actions / PHPStan

Offset 'replace' does not exist on array{name?: string, replace?: array<string, string>, extra?: array{pmu?: array{projects?: array<string>, exclude?: array<string>}}}.
continue;
}

$projects[] = $json['name'];
$composerFiles[$json['name']] = $filename;
}
Expand Down
8 changes: 8 additions & 0 deletions tests/functional/LinkCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,12 @@ public function testLink(): void {
$this->assertTrue(is_link("./tests/monorepo/vendor/test/b"));
$this->assertTrue(is_link("./tests/monorepo/vendor/test/c"));
}

public function testLinkWithReplace(): void {
chdir(__DIR__ . '/../replace');
$nullOutput = new NullOutput;
$this->application->run(new StringInput('link ../monorepo'), $nullOutput);
$this->assertTrue(is_link("./tests/monorepo/vendor/monorepo"));
$this->assertTrue(is_link("./tests/monorepo/vendor/test/d"));
}
}
5 changes: 5 additions & 0 deletions tests/monorepo/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
"test/c": "^1.0.0 || @dev",
"test/d": "^1.0.0 || @dev"
},
"replace": {
"test/a": "self.version",
"test/b": "self.version",
"test/c": "self.version"
},
"minimum-stability": "dev",
"autoload": {
"psr-4": {
Expand Down
39 changes: 39 additions & 0 deletions tests/replace/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"type": "project",
"require": {
"test/a": "*",
"test/b": "*",
"test/c": "*",
"test/d": "*",
"test/monorepo": "*"
},
"minimum-stability": "dev",
"autoload": {
"psr-4": {
"Replace\\": "."
}
},
"require-dev": {
"soyuka/pmu": "*@dev"
},
"repositories": [
{
"type": "path",
"url": "../../",
"options": {
"symlink": true
}
}
],
"extra": {
"branch-alias": {
"dev-main": "3.3.x-dev",
"dev-3.4": "3.4.x-dev"
}
},
"config": {
"allow-plugins": {
"soyuka/pmu": true
}
}
}

0 comments on commit b182404

Please sign in to comment.