Skip to content

Commit

Permalink
[yaml:*] Relocate commands. (#121)
Browse files Browse the repository at this point in the history
* [yaml:*] Relocate commands.

* [yaml:*] Update service regsitration.
  • Loading branch information
jmolivas committed Dec 29, 2016
1 parent 6e8c0f8 commit b8fa24f
Show file tree
Hide file tree
Showing 10 changed files with 1,160 additions and 5 deletions.
2 changes: 1 addition & 1 deletion phpqa.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ application:
arguments:
- '-n'
phpcs:
enabled: true
enabled: false
exception: false
options:
standard: PSR2
Expand Down
34 changes: 34 additions & 0 deletions services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,40 @@ services:
arguments: ['@console.configuration_manager']
tags:
- { name: drupal.command }
console.yaml_diff:
class: Drupal\Console\Core\Command\Yaml\DiffCommand
arguments: ['@console.nested_array']
tags:
- { name: drupal.command }
console.yaml_get_value:
class: Drupal\Console\Core\Command\Yaml\GetValueCommand
arguments: ['@console.nested_array']
tags:
- { name: drupal.command }
console.yaml_merge:
class: Drupal\Console\Core\Command\Yaml\MergeCommand
tags:
- { name: drupal.command }
console.yaml_split:
class: Drupal\Console\Core\Command\Yaml\SplitCommand
arguments: ['@console.nested_array']
tags:
- { name: drupal.command }
console.yaml_update_key:
class: Drupal\Console\Core\Command\Yaml\UpdateKeyCommand
arguments: ['@console.nested_array']
tags:
- { name: drupal.command }
console.yaml_update_value:
class: Drupal\Console\Core\Command\Yaml\UpdateValueCommand
arguments: ['@console.nested_array']
tags:
- { name: drupal.command }
console.yaml_unset_key:
class: Drupal\Console\Core\Command\Yaml\UnsetKeyCommand
arguments: ['@console.nested_array']
tags:
- { name: drupal.command }
# DrupalConsoleCore Generators
console.init_generator:
class: Drupal\Console\Core\Generator\InitGenerator
Expand Down
8 changes: 4 additions & 4 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,8 @@ private function registerCommandsFromAutoWireConfiguration()
}
}

public function registerChainCommands() {
public function registerChainCommands()
{
/**
* @var ChainDiscovery $chainDiscovery
*/
Expand All @@ -352,9 +353,8 @@ public function registerChainCommands() {
$description = $chainCommand['description'];
$command = new ChainCustomCommand($name, $description, $file);
$this->add($command);
}
catch (\Exception $e) {
echo $e->getMessage();
} catch (\Exception $e) {
echo $e->getMessage() . PHP_EOL;
}
}
}
Expand Down
243 changes: 243 additions & 0 deletions src/Command/Yaml/DiffCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
<?php

/**
* @file
* Contains \Drupal\Console\Core\Command\Yaml\DiffCommand.
*/

namespace Drupal\Console\Core\Command\Yaml;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Console\Command\Command;
use Drupal\Console\Core\Command\Shared\CommandTrait;
use Drupal\Console\Core\Style\DrupalStyle;
use Drupal\Console\Core\Utils\NestedArray;

class DiffCommand extends Command
{
use CommandTrait;

/**
* @var NestedArray
*/
protected $nestedArray;

/**
* RebuildCommand constructor.
* @param NestedArray $nestedArray
*/
public function __construct(NestedArray $nestedArray)
{
$this->nestedArray = $nestedArray;
parent::__construct();
}

protected function configure()
{
$this
->setName('yaml:diff')
->setDescription($this->trans('commands.yaml.diff.description'))
->addArgument(
'yaml-left',
InputArgument::REQUIRED,
$this->trans('commands.yaml.diff.arguments.yaml-left')
)
->addArgument(
'yaml-right',
InputArgument::REQUIRED,
$this->trans('commands.yaml.diff.arguments.yaml-right')
)
->addOption(
'stats',
false,
InputOption::VALUE_NONE,
$this->trans('commands.yaml.diff.options.stats')
)
->addOption(
'negate',
false,
InputOption::VALUE_NONE,
$this->trans('commands.yaml.diff.options.negate')
)
->addOption(
'limit',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.yaml.diff.options.limit')
)
->addOption(
'offset',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.yaml.diff.options.offset')
);
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new DrupalStyle($input, $output);

$yaml = new Parser();

$yaml_left = $input->getArgument('yaml-left');
$yaml_right = $input->getArgument('yaml-right');

$stats = $input->getOption('stats');

$negate = $input->getOption('negate');

$limit = $input->getOption('limit');
$offset = $input->getOption('offset');

if ($negate == 1 || $negate == 'TRUE') {
$negate = true;
} else {
$negate = false;
}

try {
$yamlLeftParsed = $yaml->parse(file_get_contents($yaml_left));

if (empty($yamlLeftParsed)) {
$io->error(
sprintf(
$this->trans('commands.yaml.merge.messages.wrong-parse'),
$yaml_left
)
);
}

$yamlRightParsed = $yaml->parse(file_get_contents($yaml_right));

if (empty($yamlRightParsed)) {
$io->error(
sprintf(
$this->trans('commands.yaml.merge.messages.wrong-parse'),
$yaml_right
)
);
}
} catch (\Exception $e) {
$io->error($this->trans('commands.yaml.merge.messages.error-parsing').': '.$e->getMessage());

return;
}

$statistics = ['total' => 0, 'equal'=> 0 , 'diff' => 0];
/* print_r($yamlLeftParsed);
print_r($yamlRightParsed);*/
$diff = $this->nestedArray->arrayDiff($yamlLeftParsed, $yamlRightParsed, $negate, $statistics);
print_r($diff);

if ($stats) {
$io->info(
sprintf(
$this->trans('commands.yaml.diff.messages.total'),
$statistics['total']
)
);

$io->info(
sprintf(
$this->trans('commands.yaml.diff.messages.diff'),
$statistics['diff']
)
);

$io->info(
sprintf(
$this->trans('commands.yaml.diff.messages.equal'),
$statistics['equal']
)
);

return;
}
// FLAT YAML file to display full yaml to be used with command yaml:update:key or yaml:update:value
$diffFlatten = array();
$keyFlatten = '';
$this->nestedArray->yamlFlattenArray($diff, $diffFlatten, $keyFlatten);

if ($limit !== null) {
if (!$offset) {
$offset = 0;
}
$diffFlatten = array_slice($diffFlatten, $offset, $limit);
}

$tableHeader = [
$this->trans('commands.yaml.diff.messages.key'),
$this->trans('commands.yaml.diff.messages.value'),
];

$tableRows = [];
foreach ($diffFlatten as $yamlKey => $yamlValue) {
$tableRows[] = [
$yamlKey,
$yamlValue
];
print $yamlKey . "\n";
print $yamlValue . "\n";
}

$io->table($tableHeader, $tableRows, 'compact');
}

/**
* {@inheritdoc}
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
$io = new DrupalStyle($input, $output);

$validator_filename = function ($value) use ($io) {
if (!strlen(trim($value)) || !is_file($value)) {
$io->error($this->trans('commands.common.errors.invalid-file-path'));

return false;
}

return $value;
};

// --yaml-left option
$yaml_left = $input->getArgument('yaml-left');
if (!$yaml_left) {
while (true) {
$yaml_left = $output->ask(
$this->trans('commands.yaml.diff.questions.yaml-left'),
null,
$validator_filename
);

if ($yaml_left) {
break;
}
}

$input->setArgument('yaml-left', $yaml_left);
}

// --yaml-right option
$yaml_right = $input->getArgument('yaml-right');
if (!$yaml_right) {
while (true) {
$yaml_right = $output->ask(
$this->trans('commands.yaml.diff.questions.yaml-right'),
null,
$validator_filename
);

if ($yaml_right) {
break;
}
}

$input->setArgument('yaml-right', $yaml_right);
}
}
}
Loading

0 comments on commit b8fa24f

Please sign in to comment.