Skip to content

Commit 95bad61

Browse files
committed
Fixed composer update during cleanup.
1 parent be1080b commit 95bad61

File tree

4 files changed

+27
-11
lines changed

4 files changed

+27
-11
lines changed

CustomizeCommand.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,23 @@ protected function process(array $answers): void {
227227
protected function cleanup(): void {
228228
$json = $this->readComposerJson();
229229

230+
$is_dependency = (
231+
!empty($json['require'])
232+
&& is_array($json['require'])
233+
&& isset($json['require']['alexskrypnyk/customizer'])
234+
) || (
235+
!empty($json['require-dev'])
236+
&& is_array($json['require-dev'])
237+
&& isset($json['require-dev']['alexskrypnyk/customizer'])
238+
);
239+
230240
static::arrayUnsetDeep($json, ['autoload', 'classmap'], basename(__FILE__), FALSE);
231241
static::arrayUnsetDeep($json, ['scripts', 'customize']);
232242
static::arrayUnsetDeep($json, ['scripts', 'post-create-project-cmd'], '@customize');
233243

234244
static::arrayUnsetDeep($json, ['require-dev', 'alexskrypnyk/customizer']);
235245
static::arrayUnsetDeep($json, ['autoload-dev', 'psr-4', 'AlexSkrypnyk\\Customizer\\Tests\\']);
246+
static::arrayUnsetDeep($json, ['config', 'allow-plugins', 'alexskrypnyk/customizer']);
236247

237248
if (!empty($this->cleanupCallback)) {
238249
call_user_func_array($this->cleanupCallback, [&$json, $this]);
@@ -242,17 +253,16 @@ protected function cleanup(): void {
242253
if (strcmp(serialize($this->packageData), serialize($json)) !== 0) {
243254
$this->writeComposerJson($json);
244255

245-
if ($this->isComposerDependenciesInstalled) {
256+
// We can only update the composer.lock file if the Customizer was not run
257+
// after the Composer dependencies were installed and the Customizer
258+
// was not installed as a dependency because the files will be removed
259+
// and this process will no longer have required dependencies.
260+
// For a Customizer installed as a dependency, the user should run
261+
// `composer update` manually (or through a plugin) after the Customizer
262+
// is finished.
263+
if ($this->isComposerDependenciesInstalled && !$is_dependency) {
246264
$this->io->writeLn('Updating composer.lock file after customization.');
247265
static::passthru('composer update --quiet --no-interaction --no-progress');
248-
// Composer checks for plugins within installed packages, even if the
249-
// packages are no longer is `composer.json`. So we need to remove the
250-
// plugin from the `composer.json` and update the dependencies again.
251-
if (isset($json['config']['allow-plugins']['alexskrypnyk/customizer'])) {
252-
static::arrayUnsetDeep($json, ['config', 'allow-plugins', 'alexskrypnyk/customizer']);
253-
$this->writeComposerJson($json);
254-
passthru('composer update --quiet --no-interaction --no-progress');
255-
}
256266
}
257267
}
258268

Plugin.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ public function postRootPackageInstall(PackageEvent $event): void {
7373
$scripts = $event->getComposer()->getPackage()->getScripts();
7474
$scripts['customize'][] = CustomizeCommand::class;
7575
$scripts['post-create-project-cmd'][] = '@customize';
76+
// Since we are removing the plugin, we need to run `composer update`,
77+
// but in a separate process to avoid a dependency deadlock.
78+
$scripts['post-create-project-cmd'][] = '@composer update --quiet --no-interaction --no-progress --no-plugins';
7679
$package->setScripts($scripts);
7780
}
7881
}

tests/phpunit/Fixtures/plugin/.customizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public static function processLicense(string $title, string $answer, array $answ
181181
* A callback to process cleanup.
182182
*/
183183
public static function cleanup(array &$composerjson, CustomizeCommand $customizer): void {
184-
unset($composerjson['config']['sort-packages']);
184+
$customizer::arrayUnsetDeep($composerjson, ['config', 'sort-packages']);
185185
}
186186

187187
}

tests/phpunit/Traits/ComposerTrait.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ protected function composerCommandInit(): void {
3333
protected function assertComposerCommandSuccessOutputContains(string|array $strings): void {
3434
$strings = is_array($strings) ? $strings : [$strings];
3535

36-
$this->assertSame(0, $this->tester->getStatusCode());
36+
if ($this->tester->getStatusCode() !== 0) {
37+
$this->fail($this->tester->getDisplay());
38+
}
39+
$this->assertSame(0, $this->tester->getStatusCode(), sprintf("The Composer command should have completed successfully:\n%s", $this->tester->getInput()->__toString()));
3740

3841
$output = $this->tester->getDisplay(TRUE);
3942
foreach ($strings as $string) {

0 commit comments

Comments
 (0)