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

Adding back composer munge for blt:create target. #1189

Merged
merged 3 commits into from
Mar 15, 2017
Merged
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
4 changes: 3 additions & 1 deletion bin/blt-console
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env php
<?php

use Acquia\Blt\Console\Command\ComposerMungeCommand;
use Acquia\Blt\Console\Command\SchemaVersionCommand;
use Acquia\Blt\Console\Command\YamlMungeCommand;
use Acquia\Blt\Console\Command\UpdateCommand;
Expand All @@ -23,8 +24,9 @@ else {
}

$application = new Application();
$application->add(new YamlMungeCommand());
$application->add(new ComposerMungeCommand());
$application->add(new SchemaVersionCommand());
$application->add(new UpdateCommand());
$application->add(new YamlMungeCommand());

$application->run();
11 changes: 10 additions & 1 deletion phing/tasks/blt.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,17 @@
<exec dir="${repo.root}" command="result=${PWD##*/}; printf '%s\n' &quot;${PWD##*/}&quot;" logoutput="false" checkreturn="true" level="${blt.exec_level}" passthru="false" outputProperty="dirname"/>
<exec dir="${repo.root}" command="${composer.bin}/yaml-cli update:value ${blt.config-files.project} project.machine_name '${dirname}'" logoutput="true" checkreturn="false" level="${blt.exec_level}" passthru="true"/>

<!-- Merge in the extras configuration. This pulls in wikimedia/composer-merge-plugin and composer/installers settings. -->
<exec dir="${repo.root}" command="${repo.root}/${bin.path}/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>Installing new Composer dependencies provided by BLT. This make take a while</echo>
<!--<exec dir="${repo.root}" command="composer update &#45;&#45;no-interaction" logoutput="true" checkreturn="true" level="${blt.exec_level}" passthru="true"/>-->
<exec dir="${repo.root}" command="rm -rf ${repo.root}/vendor ${repo.root}/composer.lock" logoutput="true" checkreturn="true" level="${blt.exec_level}" passthru="true"/>
<exec dir="${repo.root}" command="composer install --no-interaction --prefer-dist" logoutput="true" checkreturn="true" level="${blt.exec_level}" passthru="true"/>
<copy file="${repo.root}/docroot/profiles/contrib/lightning/lightning.extend.yml" tofile="${repo.root}/docroot/sites/default/lightning.extend.yml"/>
Expand Down
107 changes: 107 additions & 0 deletions src/Console/Command/ComposerMungeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

namespace Acquia\Blt\Console\Command;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
*
*/
class ComposerMungeCommand extends BaseCommand {

/**
* ${inheritdoc}.
*/
protected function configure() {
$this
->setName('composer:munge')
->setDescription('Munge values in two composer.json files')
->addArgument(
'file1',
InputArgument::REQUIRED,
'The first composer.json. Any conflicts will prioritize the value in this file.'
)
->addArgument(
'file2',
InputArgument::REQUIRED,
'The second composer.json.'
);
}

/**
* ${inheritdoc}.
*/
protected function execute(InputInterface $input, OutputInterface $output) {
$file1 = $input->getArgument('file1');
$file2 = $input->getArgument('file2');

if (!file_exists($file1)) {
throw new \Exception("The file $file1 does not exist");
}
if (!file_exists($file2)) {
throw new \Exception("The file $file2 does not exist");
}

$munged_json = $this->munge($file1, $file2);

$output->writeln($munged_json);
}

/**
* Selectively merges parts of two composer.json files.
*
* @param string $file1
* The file path to the first composer.json file.
* @param string $file2
* The file path to the second composer.json file.
*
* @return string
* The new, merged composer.json contents.
*/
protected function munge($file1, $file2) {
$default_contents = [
'repositories' => [],
];
$file1_contents = (array) json_decode(file_get_contents($file1), TRUE) + $default_contents;
$file2_contents = (array) json_decode(file_get_contents($file2), TRUE) + $default_contents;

$output = $this->mergeKeyed($file1_contents, $file2_contents);
$output_json = json_encode($output, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);

return $output_json;
}

/**
* Merges specific keyed arrays and objects in composer.json files.
*
* @param $file1_contents
* @param $file2_contents
* @param array $exclude_keys
*
* @return mixed
*/
protected function mergeKeyed($file1_contents, $file2_contents, $exclude_keys = []) {
// Merge keyed arrays objects.
$merge_keys = [
'extra',
];
$output = $file1_contents;
foreach ($merge_keys as $key) {
// Set empty keys to empty placeholder arrays.
if (!array_key_exists($key, $file1_contents)) {
$file1_contents[$key] = [];
}
if (!array_key_exists($key, $file2_contents)) {
$file2_contents[$key] = [];
}

// Merge!
$output[$key] = $this->arrayMergeRecursiveDistinct($file1_contents[$key], $file2_contents[$key]);
}

return $output;
}

}