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

External commands should be registered before custom commands #51

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions bin/run
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

use Symfony\Component\Console\Input\ArgvInput;
use OpenEuropa\TaskRunner\TaskRunner;
use Symfony\Component\Console\Output\ConsoleOutput;

if (file_exists(__DIR__.'/../vendor/autoload.php')) {
$classLoader = require __DIR__.'/../vendor/autoload.php';
} elseif (file_exists(__DIR__.'/../../../autoload.php')) {
$classLoader = require __DIR__ . '/../../../autoload.php';
}

$runner = new TaskRunner();
$runner->registerExternalCommands($classLoader);
exit($runner->run());
$runner = new TaskRunner(new ArgvInput(), new ConsoleOutput(), $classLoader);
exit($runner->run());
17 changes: 11 additions & 6 deletions src/TaskRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class TaskRunner
*/
private $input;

/**
* @var \Composer\Autoload\ClassLoader
*/
private $classLoader;

/**
* @var Application
*/
Expand All @@ -65,11 +70,13 @@ class TaskRunner
*
* @param InputInterface $input
* @param OutputInterface|null $output
* @param ClassLoader $classLoader
*/
public function __construct(InputInterface $input = null, OutputInterface $output = null)
public function __construct(InputInterface $input = null, OutputInterface $output = null, ClassLoader $classLoader = null)
{
$this->input = is_null($input) ? new ArgvInput() : $input;
$this->output = is_null($output) ? new ConsoleOutput() : $output;
$this->classLoader = is_null($classLoader) ? new ClassLoader() : $classLoader;

$this->workingDir = $this->getWorkingDir($this->input);
chdir($this->workingDir);
Expand All @@ -83,6 +90,7 @@ public function __construct(InputInterface $input = null, OutputInterface $outpu
$this->runner->setContainer($this->container);
$this->runner->registerCommandClasses($this->application, $this->getCommandDiscovery()->discover(__DIR__, 'OpenEuropa\\TaskRunner'));

$this->registerExternalCommands();
// Register commands defined in runner.yml file.
$this->registerDynamicCommands($this->application);
}
Expand All @@ -108,15 +116,12 @@ public function getCommands($class)
return $this->getContainer()->get("{$class}Commands");
}

/**
* @param \Composer\Autoload\ClassLoader $classLoader
*/
public function registerExternalCommands(ClassLoader $classLoader)
private function registerExternalCommands()
{
$commands = [];
$discovery = $this->getCommandDiscovery();

foreach ($classLoader->getPrefixesPsr4() as $baseNamespace => $directoryList) {
foreach ($this->classLoader->getPrefixesPsr4() as $baseNamespace => $directoryList) {
$directoryList = array_filter($directoryList, function ($path) {
return is_dir($path.'/TaskRunner/Commands');
});
Expand Down
3 changes: 2 additions & 1 deletion tests/AbstractTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ abstract class AbstractTaskTest extends AbstractTest implements ContainerAwareIn
public function setup()
{
$this->output = new BufferedOutput();
$runner = new TaskRunner(new StringInput(''), $this->output);
$classLoader = require __DIR__.'/../vendor/autoload.php';
$runner = new TaskRunner(new StringInput(''), $this->output, $classLoader);
$this->setContainer($runner->getContainer());
}

Expand Down
20 changes: 13 additions & 7 deletions tests/CommandsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function testSimulation($command, array $config, $composer, array $expect

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

$text = $output->fetch();
Expand Down Expand Up @@ -65,7 +65,7 @@ public function testSetupCommands($command, $source, $destination, array $config

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

$actual = file_get_contents($destination);
Expand Down Expand Up @@ -96,9 +96,7 @@ public function testCustomCommands()
{
$input = new StringInput("list");
$output = new BufferedOutput();
$runner = new TaskRunner($input, $output);
$classLoader = require __DIR__.'/../vendor/autoload.php';
$runner->registerExternalCommands($classLoader);
$runner = new TaskRunner($input, $output, $this->getClassLoader());
$runner->run();

$expected = [
Expand Down Expand Up @@ -127,7 +125,7 @@ public function testDrushSetup(array $config, array $expected)
file_put_contents($configFile, Yaml::dump($config));

$input = new StringInput("drupal:drush-setup --working-dir=".$this->getSandboxRoot());
$runner = new TaskRunner($input, new BufferedOutput());
$runner = new TaskRunner($input, new BufferedOutput(), $this->getClassLoader());
$runner->run();

foreach ($expected as $row) {
Expand All @@ -149,7 +147,7 @@ public function testSettingsSetup(array $config, array $expected)
file_put_contents($configFile, Yaml::dump($config));

$input = new StringInput("drupal:settings-setup --working-dir=".$this->getSandboxRoot());
$runner = new TaskRunner($input, new BufferedOutput());
$runner = new TaskRunner($input, new BufferedOutput(), $this->getClassLoader());
$runner->run();


Expand Down Expand Up @@ -210,4 +208,12 @@ protected function assertContainsNotContains($content, array $expected)
$this->assertNotContains($row['not_contains'], $content);
}
}

/**
* @return \Composer\Autoload\ClassLoader
*/
protected function getClassLoader()
{
return require __DIR__.'/../vendor/autoload.php';
}
}