Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow merging deep scripts #268

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ Usage
"merge-extra": false,
"merge-extra-deep": false,
"merge-replace": true,
"merge-scripts": false
"merge-scripts": false,
"merge-scripts-deep": false
}
}
}
Expand Down Expand Up @@ -197,6 +198,14 @@ the master config wins over the version found in any imported config). If
and the last key found will win (e.g. the key in the master config is replaced
by the key in the imported config).

if `"merge-scripts-deep": true` (required `"merge-scripts": true`), the scripts will be merged, not replaced, with
the first option found, or the last one, depending on the settings, as
with only `"merge-scripts": true`.
For example, _root: script => 1, imported: script => 2, the output will be script => [1, 2]_,
but with the same values, at the moment, they will be repeated
_root: script => 1, imported: script => 1, output: script => [1, 1]_


Note: [custom commands][] added by merged configuration will work when invoked
as `composer run-script my-cool-command` but will not be available using the
normal `composer my-cool-command` shortcut.
Expand Down
13 changes: 9 additions & 4 deletions src/ExtraPackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -544,15 +544,20 @@ public function mergeScripts(RootPackageInterface $root, PluginState $state)
$rootScripts = $root->getScripts();
$unwrapped = self::unwrapIfNeeded($root, 'setScripts');

if ($state->shouldMergeScriptsDeep()) {
$unwrapped->setScripts(array_merge_recursive($rootScripts, $scripts));
return;
}

if ($state->replaceDuplicateLinks()) {
$unwrapped->setScripts(
array_merge($rootScripts, $scripts)
);
} else {
$unwrapped->setScripts(
array_merge($scripts, $rootScripts)
);
return;
}
$unwrapped->setScripts(
array_merge($scripts, $rootScripts)
);
}

/**
Expand Down
20 changes: 20 additions & 0 deletions src/PluginState.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ class PluginState
*/
protected $mergeScripts = false;

/**
* @var bool $mergeScriptsDeep
*/
protected $mergeScriptsDeep = false;


/**
* @var bool $firstInstall
*/
Expand Down Expand Up @@ -169,6 +175,7 @@ public function loadSettings()
'merge-extra-deep' => false,
'merge-replace' => true,
'merge-scripts' => false,
'merge-scripts-deep' => false,
],
$extra['merge-plugin'] ?? []
);
Expand All @@ -185,6 +192,7 @@ public function loadSettings()
$this->mergeExtraDeep = (bool)$config['merge-extra-deep'];
$this->mergeReplace = (bool)$config['merge-replace'];
$this->mergeScripts = (bool)$config['merge-scripts'];
$this->mergeScriptsDeep = (bool)$config['merge-scripts-deep'];
}

/**
Expand Down Expand Up @@ -418,5 +426,17 @@ public function shouldMergeScripts()
{
return $this->mergeScripts;
}

/**
* Should the scripts section be deep merged?
*
* By default, the scripts section is not merged.
*
* @return bool
*/
public function shouldMergeScriptsDeep()
{
return $this->mergeScriptsDeep;
}
}
// vim:sw=4:ts=4:sts=4:et:
35 changes: 35 additions & 0 deletions tests/phpunit/MergePluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1192,6 +1192,41 @@ function ($args) use ($that) {
$this->triggerPlugin($root->reveal(), $dir);
}

public function testMergeScriptsDeep()
{
$that = $this;
$dir = $this->fixtureDir(__FUNCTION__);

$root = $this->rootFromJson("{$dir}/composer.json");


$root->setScripts(Argument::type('array'))->will(
function ($args) use ($that) {
$scripts = $args[0];
var_dump($scripts);
$that->assertCount(4, $scripts);
$that->assertArrayHasKey('script2', $scripts);
$that->assertArrayHasKey('script3', $scripts);
$that->assertEquals(["echo 'script2-root'", "echo 'script2-1'"], $scripts['script2']);
$that->assertCount(3, $scripts['script3']);
$that->assertEquals(
["echo 'script3-root'", "echo 'script3-1'", "echo 'script3-2'"],
$scripts['script3']
);
$that->assertCount(2, $scripts['script4']);
$that->assertEquals(["echo 'script4'", "echo 'script4'"], $scripts['script4']);
}
)->shouldBeCalled();

$root->getRepositories()->shouldNotBeCalled();
$root->getConflicts()->shouldNotBeCalled();
$root->getReplaces()->shouldNotBeCalled();
$root->getProvides()->shouldNotBeCalled();
$root->getSuggests()->shouldNotBeCalled();

$this->triggerPlugin($root->reveal(), $dir);
}

/**
* @dataProvider provideOnPostPackageInstall
* @param string $package Package installed
Expand Down
18 changes: 18 additions & 0 deletions tests/phpunit/fixtures/testMergeScriptsDeep/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extra": {
"merge-plugin": {
"merge-scripts": true,
"merge-scripts-deep": true,
"include": [
"composer.local.json"
]
}
},
"scripts": {
"script2": "echo 'script2-root'",
"script3": [
"echo 'script3-root'"
],
"script4": "echo 'script4'"
}
}
14 changes: 14 additions & 0 deletions tests/phpunit/fixtures/testMergeScriptsDeep/composer.local.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"scripts": {

"script1": "echo 'script1-1'",
"script2": "echo 'script2-1'",
"script3": [
"echo 'script3-1'",
"echo 'script3-2'"
],
"script4": [
"echo 'script4'"
]
}
}