Skip to content

Commit

Permalink
Fixing composer excludes. (#576)
Browse files Browse the repository at this point in the history
* Fixing composer excludes.

* Preventing overwrite of composer.json whenmunge fails.
  • Loading branch information
grasmash authored Oct 17, 2016
1 parent 33cefac commit f9f45aa
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
12 changes: 8 additions & 4 deletions phing/tasks/blt.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,14 @@
<!--@todo Output different message if composer.json does not exist.-->
<echo>Merging BLT's composer.json template with your project's composer.json.</echo>
<echo>This MAY overwrite some existing values.</echo>
<!--Values in the project's existing composer.json file will be overwritten.-->
<exec dir="${repo.root}" command="${repo.root}/vendor/bin/blt-console composer:munge ${repo.root}/composer.json ${blt.root}/template/composer.json > ${repo.root}/composer.json.tmp" logoutput="true" checkreturn="true" level="${blt.exec_level}"/>
<!--@todo Find out why can't we just redirect output directly back to composer.json. -->
<exec dir="${repo.root}" command="mv ${repo.root}/composer.json.tmp ${repo.root}/composer.json" logoutput="true" checkreturn="true" level="${blt.exec_level}" passthru="true"/>
<exec dir="${repo.root}" command="${repo.root}/vendor/bin/blt-console composer:munge ${repo.root}/composer.json ${blt.root}/template/composer.json > ${repo.root}/composer.json.tmp" logoutput="true" checkreturn="true" level="${blt.exec_level}" returnProperty="composer.munge.return"/>
<if>
<equals arg1="${composer.munge.return}" arg2="0"/>
<then>
<!--@todo Find out why can't we just redirect output directly back to composer.json. -->
<exec dir="${repo.root}" command="mv ${repo.root}/composer.json.tmp ${repo.root}/composer.json" logoutput="true" checkreturn="true" level="${blt.exec_level}" passthru="true"/>
</then>
</if>
<echo>If your composer.json was modified, you need to run "composer update".</echo>
</target>

Expand Down
37 changes: 23 additions & 14 deletions src/Console/Command/ComposerMungeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,14 @@ protected function munge($file1, $file2) {
}
// Skip merging entirely if '*' is excluded.
if ($exclude_keys == '*') {
return $file1_contents;
$output = $file1_contents;
}
else {
$output = $this->mergeKeyed($file1_contents, $file2_contents, $exclude_keys);

$output = $this->mergeKeyed($file1_contents, $file2_contents, $exclude_keys);

if (empty($exclude_keys['repositories'])) {
$output['repositories'] = $this->mergeRepositories((array) $file1_contents['repositories'], (array) $file2_contents['repositories']);
if (empty($exclude_keys['repositories'])) {
$output['repositories'] = $this->mergeRepositories((array) $file1_contents['repositories'], (array) $file2_contents['repositories']);
}
}

$output_json = json_encode($output, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
Expand All @@ -75,10 +76,11 @@ protected function munge($file1, $file2) {
*
* @param $file1_contents
* @param $file2_contents
* @param array $exclude_keys
*
* @return mixed
*/
protected function mergeKeyed($file1_contents, $file2_contents, $exclude_keys) {
protected function mergeKeyed($file1_contents, $file2_contents, $exclude_keys = []) {
// Merge keyed arrays objects.
$merge_keys = [
'autoload-dev',
Expand All @@ -91,17 +93,24 @@ protected function mergeKeyed($file1_contents, $file2_contents, $exclude_keys) {
foreach ($merge_keys as $key) {

// Handle exclusions.
if (in_array($key, $exclude_keys)) {
$exclude_key = $exclude_keys[$key];
if (in_array($key, array_keys($exclude_keys))) {
$excludes_value = $exclude_keys[$key];

// Wildcard exclusion.
if (is_string($exclude_key) && $exclude_key == '*') {
continue;
if (is_string($excludes_value)) {
// "require": "*"
if ($excludes_value == '*') {
continue;
}
// "require": "drupal/core"
else {
unset($file2_contents[$key][$excludes_value]);
}
}
// This implementation only supports an array depth of 1.
elseif (is_array($exclude_key)) {
foreach ($exclude_key as $exclude_subkey) {
unset($file2_contents[$exclude_key][$exclude_subkey]);
// "require": [ "drupal/core" ]
elseif (is_array($excludes_value)) {
foreach ($excludes_value as $exclude_package) {
unset($file2_contents[$key][$exclude_package]);
}
}
}
Expand Down

0 comments on commit f9f45aa

Please sign in to comment.