Skip to content

Commit

Permalink
Issue #6: Fix configuration building for commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
ademarco committed Jan 4, 2018
1 parent 8c15e77 commit e44e268
Show file tree
Hide file tree
Showing 21 changed files with 157 additions and 76 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@ phpunit.xml
composer.lock
behat.yml
RoboFile.php
/tests/sandbox/*
7 changes: 1 addition & 6 deletions bin/run
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,5 @@ if (file_exists(__DIR__.'/../vendor/autoload.php')) {
require_once __DIR__ . '/../../../autoload.php';
}

$runner = new TaskRunner([
getcwd().'/runner.yml.dist',
getcwd().'/runner.yml',
]);

$statusCode = $runner->run();
$statusCode = (new TaskRunner())->run();
exit($statusCode);
Empty file added config/commands/base.yml
Empty file.
31 changes: 26 additions & 5 deletions src/Commands/BaseCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
use Robo\Exception\TaskException;
use Robo\LoadAllTasks;
use Robo\Result;
use Robo\Robo;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;

Expand All @@ -30,14 +32,33 @@ class BaseCommands implements BuilderAwareInterface, IOAwareInterface, ConfigAwa
use IO;

/**
* @param InputInterface $input
* @param AnnotationData $annotationData
* Path to YAML configuration file containing command defaults.
*
* @hook init
* Command classes should implement this method.
*
* @return null|string
*/
public function init(InputInterface $input, AnnotationData $annotationData)
public function getConfigurationFile()
{
$this->setInput($input);
return __DIR__.'/../../config/commands/base.yml';
}

/**
* Command initialization.
*
* @param \Symfony\Component\Console\Event\ConsoleCommandEvent $event
*
* @hook pre-command-event *
*/
public function initializeRuntimeConfiguration(ConsoleCommandEvent $event)
{
$workingDir = $event->getInput()->getOption('working-dir');

Robo::loadConfiguration([
$this->getConfigurationFile(),
$workingDir.'/runner.yml.dist',
$workingDir.'/runner.yml',
], $this->getConfig());
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/Commands/ChangelogCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ class ChangelogCommands extends BaseCommands implements ComposerAwareInterface
{
use ComposerAwareTrait;

/**
* {@inheritdoc}
*/
public function getConfigurationFile()
{
return __DIR__.'/../../config/commands/changelog.yml';
}

/**
* Generate a changelog based on GitHub issues and pull requests.
*
Expand Down
20 changes: 19 additions & 1 deletion src/Commands/DrupalCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use EC\OpenEuropa\TaskRunner\Contract\ComposerAwareInterface;
use EC\OpenEuropa\TaskRunner\Traits\ComposerAwareTrait;
use Robo\Exception\TaskException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;

/**
Expand All @@ -17,6 +18,24 @@ class DrupalCommands extends BaseCommands implements ComposerAwareInterface
use ComposerAwareTrait;
use \NuvoleWeb\Robo\Task\Config\Php\loadTasks;

/**
* {@inheritdoc}
*/
public function getConfigurationFile()
{
return __DIR__.'/../../config/commands/drupal.yml';
}

/**
* @param \Symfony\Component\Console\Input\InputInterface $input
*
* @hook init
*/
public function init(InputInterface $input)
{
$this->getComposer()->setWorkingDir($input->getOption('working-dir'));
}

/**
* Install target site.
*
Expand Down Expand Up @@ -146,7 +165,6 @@ protected function getProjectName()
*/
protected function getProjectType()
{
return 'drupal-module';
return $this->getComposer()->getType();
}

Expand Down
8 changes: 8 additions & 0 deletions src/Commands/SetupCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ class SetupCommands extends BaseCommands
{
use ReplaceConfigTokens\loadTasks;

/**
* {@inheritdoc}
*/
public function getConfigurationFile()
{
return __DIR__.'/../../config/commands/setup.yml';
}

/**
* Setup Behat.
*
Expand Down
12 changes: 12 additions & 0 deletions src/Services/Composer.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,16 @@ public function getPackage()
{
return json_decode(file_get_contents($this->workingDir.'/composer.json'));
}

/**
* @param string $workingDir
*
* @return Composer
*/
public function setWorkingDir($workingDir)
{
$this->workingDir = $workingDir;

return $this;
}
}
46 changes: 11 additions & 35 deletions src/TaskRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Robo\Config\Config;
use Robo\Robo;
use Robo\Runner as RoboRunner;
use Symfony\Component\Console\Exception\InvalidOptionException;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -55,24 +54,22 @@ class TaskRunner
/**
* TaskRunner constructor.
*
* TaskRunner constructor.
* @param array $configPaths
* @param InputInterface $input
* @param OutputInterface|null $output
*/
public function __construct(array $configPaths = [], InputInterface $input = null, OutputInterface $output = null)
public function __construct(InputInterface $input = null, OutputInterface $output = null)
{
$this->output = is_null($output) ? new ConsoleOutput() : $output;
$this->input = is_null($input) ? new ArgvInput() : $input;

// Create application.
$this->application = new Application(self::APPLICATION_NAME, null);
// $this->application
// ->getDefinition()
// ->addOption(new InputOption('--working-dir', null, InputOption::VALUE_OPTIONAL, 'Working directory, defaults to current working directory.', getcwd()));
$this->application
->getDefinition()
->addOption(new InputOption('--working-dir', null, InputOption::VALUE_REQUIRED, 'Working directory, defaults to current working directory.', getcwd()));

$this->output = is_null($output) ? new ConsoleOutput() : $output;
$this->input = is_null($input) ? new ArgvInput() : $input;

// Create configuration.
$config = $this->createConfiguration($configPaths);
$config = $this->createConfiguration();
$this->setConfig($config);

// Create container.
Expand Down Expand Up @@ -136,34 +133,13 @@ private function discoverCommandClasses()
}

/**
* @param array $configPaths
* @return Config
*/
private function createConfiguration(array $configPaths)
private function createConfiguration()
{
$configPaths = array_merge([
return Robo::createConfiguration([
__DIR__.'/../config/runner.yml',
__DIR__.'/../config/commands/drupal.yml',
__DIR__.'/../config/commands/setup.yml',
__DIR__.'/../config/commands/changelog.yml',
], $configPaths);

return Robo::createConfiguration($configPaths);
}

/**
* @return bool|string
*/
private function getWorkingDirectory()
{
$input = clone $this->input;
$input->bind($this->application->getDefinition());
$option = $input->getOption('working-dir');
if (realpath($option) === false) {
throw new InvalidOptionException("Working directory '{$option}' does not exists.");
}

return realpath($option);
]);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion tests/AbstractTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use EC\OpenEuropa\TaskRunner\TaskRunner;
use League\Container\ContainerAwareInterface;
use League\Container\ContainerAwareTrait;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Robo\TaskAccessor;
use Robo\Collection\CollectionBuilder;
Expand All @@ -30,7 +31,7 @@ abstract class AbstractTaskTest extends AbstractTest implements ContainerAwareIn
public function setup()
{
$this->output = new BufferedOutput();
$runner = new TaskRunner([], null, $this->output);
$runner = new TaskRunner(new StringInput(''), $this->output);
$this->setContainer($runner->getContainer());
}

Expand Down
41 changes: 30 additions & 11 deletions tests/AbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,24 @@
abstract class AbstractTest extends TestCase
{

/**
* Call protected/private method of a class.
*
* @param object &$object Instantiated object that we will run method on.
* @param string $methodName Method name to call
* @param array $parameters Array of parameters to pass into method.
*
* @return mixed Method return.
*/
/**
* @inheritDoc
*/
protected function setUp()
{
array_map('unlink', glob($this->getSandboxRoot()."/*"));
}


/**
* Call protected/private method of a class.
*
* @param object &$object Instantiated object that we will run method on.
* @param string $methodName Method name to call
* @param array $parameters Array of parameters to pass into method.
*
* @return mixed Method return.
*/
protected function invokeMethod(&$object, $methodName, array $parameters = array())
{
$reflection = new \ReflectionClass(get_class($object));
Expand All @@ -46,8 +55,18 @@ protected function getFixtureContent($filepath)
*
* @return string
*/
protected function getSandboxPath($name)
protected function getSandboxFilepath($name)
{
return $this->getSandboxRoot().'/'.$name;
}

/**
* @param $name
*
* @return string
*/
protected function getSandboxRoot()
{
return __DIR__."/sandbox/{$name}";
return __DIR__."/sandbox";
}
}
19 changes: 12 additions & 7 deletions tests/CommandsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,22 @@ class CommandsTest extends AbstractTest
/**
* @param string $command
* @param array $config
* @param string $composer
* @param array $expected
*
* @dataProvider simulationDataProvider
*/
public function testSimulation($command, array $config, array $expected)
public function testSimulation($command, array $config, $composer, array $expected)
{
$configFile = $this->getSandboxPath('runner.test.yml');
$configFile = $this->getSandboxFilepath('runner.yml');
$composerFile = $this->getSandboxFilepath('composer.json');

file_put_contents($configFile, Yaml::dump($config));
$input = new StringInput("{$command} --simulate");
file_put_contents($composerFile, $composer);

$input = new StringInput("{$command} --simulate --working-dir=".$this->getSandboxRoot());
$output = new BufferedOutput();
$runner = new TaskRunner([$configFile], $input, $output);
$runner = new TaskRunner($input, $output);
$runner->run();

$text = $output->fetch();
Expand All @@ -50,13 +55,13 @@ public function testSimulation($command, array $config, array $expected)
*/
public function testSetupCommands($command, $content, $expected)
{
$source = $this->getSandboxPath('source.yml');
$destination = $this->getSandboxPath('destination.yml');
$source = $this->getSandboxFilepath('source.yml');
$destination = $this->getSandboxFilepath('destination.yml');
file_put_contents($source, $content);

$input = new StringInput("{$command} --source={$source} --destination={$destination}");
$output = new BufferedOutput();
$runner = new TaskRunner([], $input, $output);
$runner = new TaskRunner($input, $output);
$runner->run();

$actual = file_get_contents($destination);
Expand Down
2 changes: 1 addition & 1 deletion tests/Services/ComposerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ComposerTest extends AbstractTest
*/
public function testComposerParsing($content, array $assertions)
{
$filepath = $this->getSandboxPath('composer.json');
$filepath = $this->getSandboxFilepath('composer.json');
file_put_contents($filepath, $content);

$service = new Composer(dirname($filepath));
Expand Down
4 changes: 2 additions & 2 deletions tests/Tasks/ReplaceConfigTokensTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class ReplaceConfigTokensTaskTest extends AbstractTaskTest
*/
public function testTask(array $data, array $expected)
{
$source = $this->getSandboxPath('source.yml');
$destination = $this->getSandboxPath('destination.yml');
$source = $this->getSandboxFilepath('source.yml');
$destination = $this->getSandboxFilepath('destination.yml');
file_put_contents($source, Yaml::dump($data));
$this->taskReplaceConfigTokens($source, $destination)->run();
$destinationData = Yaml::parse(file_get_contents($destination));
Expand Down
Loading

0 comments on commit e44e268

Please sign in to comment.