Merge pull request #3549 from localheinz/feature/sort-dependencies
Enhancement: Add sort-packages option which allows sorting of packagespull/3450/head
commit
2264a80a7d
|
@ -47,6 +47,7 @@ class RequireCommand extends InitCommand
|
||||||
new InputOption('update-no-dev', null, InputOption::VALUE_NONE, 'Run the dependency update with the --no-dev option.'),
|
new InputOption('update-no-dev', null, InputOption::VALUE_NONE, 'Run the dependency update with the --no-dev option.'),
|
||||||
new InputOption('update-with-dependencies', null, InputOption::VALUE_NONE, 'Allows inherited dependencies to be updated with explicit dependencies.'),
|
new InputOption('update-with-dependencies', null, InputOption::VALUE_NONE, 'Allows inherited dependencies to be updated with explicit dependencies.'),
|
||||||
new InputOption('ignore-platform-reqs', null, InputOption::VALUE_NONE, 'Ignore platform requirements (php & ext- packages).'),
|
new InputOption('ignore-platform-reqs', null, InputOption::VALUE_NONE, 'Ignore platform requirements (php & ext- packages).'),
|
||||||
|
new InputOption('sort-packages', null, InputOption::VALUE_NONE, 'Sorts packages when adding/updating a new dependency'),
|
||||||
))
|
))
|
||||||
->setHelp(<<<EOT
|
->setHelp(<<<EOT
|
||||||
The require command adds required packages to your composer.json and installs them
|
The require command adds required packages to your composer.json and installs them
|
||||||
|
@ -104,7 +105,9 @@ EOT
|
||||||
$versionParser->parseConstraints($constraint);
|
$versionParser->parseConstraints($constraint);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$this->updateFileCleanly($json, $baseRequirements, $requirements, $requireKey, $removeKey)) {
|
$sortPackages = $input->getOption('sort-packages');
|
||||||
|
|
||||||
|
if (!$this->updateFileCleanly($json, $baseRequirements, $requirements, $requireKey, $removeKey, $sortPackages)) {
|
||||||
foreach ($requirements as $package => $version) {
|
foreach ($requirements as $package => $version) {
|
||||||
$baseRequirements[$package] = $version;
|
$baseRequirements[$package] = $version;
|
||||||
|
|
||||||
|
@ -160,14 +163,14 @@ EOT
|
||||||
return $status;
|
return $status;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function updateFileCleanly($json, array $base, array $new, $requireKey, $removeKey)
|
private function updateFileCleanly($json, array $base, array $new, $requireKey, $removeKey, $sortPackages)
|
||||||
{
|
{
|
||||||
$contents = file_get_contents($json->getPath());
|
$contents = file_get_contents($json->getPath());
|
||||||
|
|
||||||
$manipulator = new JsonManipulator($contents);
|
$manipulator = new JsonManipulator($contents);
|
||||||
|
|
||||||
foreach ($new as $package => $constraint) {
|
foreach ($new as $package => $constraint) {
|
||||||
if (!$manipulator->addLink($requireKey, $package, $constraint)) {
|
if (!$manipulator->addLink($requireKey, $package, $constraint, $sortPackages)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!$manipulator->removeSubNode($removeKey, $package)) {
|
if (!$manipulator->removeSubNode($removeKey, $package)) {
|
||||||
|
|
|
@ -52,7 +52,7 @@ class JsonManipulator
|
||||||
return $this->contents . $this->newline;
|
return $this->contents . $this->newline;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addLink($type, $package, $constraint)
|
public function addLink($type, $package, $constraint, $sortPackages = false)
|
||||||
{
|
{
|
||||||
$decoded = JsonFile::parseJson($this->contents);
|
$decoded = JsonFile::parseJson($this->contents);
|
||||||
|
|
||||||
|
@ -90,6 +90,13 @@ class JsonManipulator
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (true === $sortPackages) {
|
||||||
|
$requirements = json_decode($links, true);
|
||||||
|
|
||||||
|
ksort($requirements);
|
||||||
|
$links = $this->format($requirements);
|
||||||
|
}
|
||||||
|
|
||||||
$this->contents = $matches[1] . $matches[2] . $links . $matches[4];
|
$this->contents = $matches[1] . $matches[2] . $links . $matches[4];
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -281,6 +281,58 @@ class JsonManipulatorTest extends \PHPUnit_Framework_TestCase
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider providerAddLinkAndSortPackages
|
||||||
|
*/
|
||||||
|
public function testAddLinkAndSortPackages($json, $type, $package, $constraint, $sortPackages, $expected)
|
||||||
|
{
|
||||||
|
$manipulator = new JsonManipulator($json);
|
||||||
|
$this->assertTrue($manipulator->addLink($type, $package, $constraint, $sortPackages));
|
||||||
|
$this->assertEquals($expected, $manipulator->getContents());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function providerAddLinkAndSortPackages()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
array(
|
||||||
|
'{
|
||||||
|
"require": {
|
||||||
|
"vendor/baz": "qux"
|
||||||
|
}
|
||||||
|
}',
|
||||||
|
'require',
|
||||||
|
'foo',
|
||||||
|
'bar',
|
||||||
|
true,
|
||||||
|
'{
|
||||||
|
"require": {
|
||||||
|
"foo": "bar",
|
||||||
|
"vendor/baz": "qux"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
'{
|
||||||
|
"require": {
|
||||||
|
"vendor/baz": "qux"
|
||||||
|
}
|
||||||
|
}',
|
||||||
|
'require',
|
||||||
|
'foo',
|
||||||
|
'bar',
|
||||||
|
false,
|
||||||
|
'{
|
||||||
|
"require": {
|
||||||
|
"vendor/baz": "qux",
|
||||||
|
"foo": "bar"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dataProvider removeSubNodeProvider
|
* @dataProvider removeSubNodeProvider
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue