Skip to content
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
Binary file modified bin/idephix.phar
Binary file not shown.
30 changes: 25 additions & 5 deletions docs/writing_tasks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ Writing output to the console
-----------------------------

Idephix is based on Symfony console component so you can send output to the user using the
``\Symfony\Component\Console\Output\OutputInterface``. You can get the full ``OutputInterface`` component
``\Symfony\Component\Console\Style\SymfonyStyle``. You can get the full ``SymfonyStyle`` component
through the ``\Idephix\TaskExecutor::output`` method or you can use the shortcut methods:
``\Idephix\TaskExecutor::write`` and ``\Idephix\TaskExecutor::writeln``.

Expand All @@ -343,11 +343,31 @@ Here is an example of you you can send some output to the console.
{
$context->writeln(strtoupper($what));
$context->write(strtoupper($what) . PHP_EOL);
$context->output()->write(strtoupper($what) . PHP_EOL);
$context->output()->writeln(strtoupper($what));

$output = $idx->output();

// common output elements
$output->title($what);
$output->section($what);
$output->text($what);
$output->comment($what);
$output->note($what);
$output->caution($what);
$output->listing([$what, $what, $what]);
$output->success($what);
$output->error($what);
$output->warning($what);

//table
$headers = ['Parameter', 'Value', 'Value 3'];
$rows = [
['Param1', 'Value1', 'Value 3'],
['Param2', 'Value2', 'Value 3']
];
$output->table($headers, $rows);
}

.. hint::

For more information about ``OutputInterface`` read the official
component `documentation <http://symfony.com/doc/2.8/components/console.html>`_
For more information about ``SymfonyStyle`` read the official
component `documentation <http://symfony.com/blog/new-in-symfony-2-8-console-style-guide>`_
1 change: 1 addition & 0 deletions src/Idephix/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Idephix;

use Idephix\Task\Task;
use Idephix\Extension\Extension;

interface Builder
{
Expand Down
24 changes: 20 additions & 4 deletions src/Idephix/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ private function __construct(Dictionary $dictionary)
$this->dictionary = $dictionary;
}

public static function fromArray($data)
public static function fromArray(array $data)
{
$resolver = new OptionsResolver();
$resolver->setDefaults(array(
Expand All @@ -47,7 +47,11 @@ function ($envData) {
throw new InvalidConfigurationException("Each env must be an array \"$envData\" given'");
}

$envData['hosts'] = empty($envData['hosts']) ? array(null) : $envData['hosts'];
if (empty($envData['hosts'])) {
$envData['hosts'] = new \ArrayIterator();
} else {
$envData['hosts'] = new \ArrayIterator($envData['hosts']);
}

$sshParamsResolver = new OptionsResolver();
$sshParamsResolver->setDefaults(
Expand All @@ -60,7 +64,7 @@ function ($envData) {
'ssh_port' => '22'
)
);

$envData['ssh_params'] = $sshParamsResolver->resolve(
empty($envData['ssh_params']) ? array() : $envData['ssh_params']
);
Expand All @@ -84,7 +88,7 @@ public static function parseFile($configFile)
if (is_null($configFile)) {
return static::dry();
}

try {
new \SplFileObject($configFile);
} catch (\RuntimeException $e) {
Expand Down Expand Up @@ -136,6 +140,18 @@ public function get($offset, $default = null)
return $this->dictionary->get($offset, $default);
}

/**
* Add trailing slash to the path if it is omitted
*
* @param string $name
* @param string $default
* @return string fixed path
*/
public function getAsPath($name, $default = '')
{
return rtrim($this->get($name, $default), '/').'/';
}

public function set($key, $value)
{
$this->dictionary->set($key, $value);
Expand Down
114 changes: 112 additions & 2 deletions src/Idephix/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

namespace Idephix\Console;

use Idephix\Context;
use Idephix\TaskExecutor;
use Idephix\Task\Task;
use Idephix\Task\TaskCollection;
use Idephix\Exception\FailedCommandException;
use Symfony\Component\Console\Application as BaseApplication;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Command\HelpCommand;
use Idephix\Console\Command\ListCommand;

class Application extends BaseApplication
class Application extends BaseApplication implements TaskExecutor
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

{
private $logo = <<<'EOD'

Expand All @@ -23,13 +28,25 @@ class Application extends BaseApplication

private $releaseDate;

private $output;

private $input;

private $tasks;

public function __construct(
$name = 'UNKNOWN',
$version = 'UNKNOWN',
$releaseDate = 'UNKNOWN')
$releaseDate = 'UNKNOWN',
$input,
$output)
{
parent::__construct($name, $version);

$this->input = $input;
$this->output = $output;
$this->tasks = TaskCollection::dry();

$this->releaseDate = $releaseDate;

$this->setAutoExit(false);
Expand Down Expand Up @@ -65,4 +82,97 @@ protected function getDefaultCommands()
{
return array(new HelpCommand(), new ListCommand());
}

public function addTask(Task $task, Context $ctx)
{
$this->tasks[] = $task;
$this->add(Command::fromTask($task, $ctx));

return $this;
}

public function hasTask($name)
{
return $this->tasks->has($name) && $this->has($name);
}

public function runContext(Context $ctx)
{
$this->selectEnvironment($ctx);

if (!$ctx->getEnv()) {
return $this->runNoEnv();
}

return $this->runEnv($ctx);
}

public function runTask($name, $arguments = array())
{
$inputFactory = new InputFactory();

$input = $inputFactory->buildFromUserArgsForTask(
$arguments,
$this->tasks->get($name)
);

return $this->get($name)->run($input, $this->output);
}

private function runNoEnv()
{
$returnValue = $this->run($this->input, $this->output);

$hasErrors = !(is_null($returnValue) || ($returnValue == 0));

if ($hasErrors) {
throw new FailedCommandException();
}
}

private function runEnv(Context $ctx)
{
$hosts = $ctx->getHosts();

$hasErrors = false;

while ($hosts->valid()) {
$ctx->openRemoteConnection($hosts->current());
$returnValue = $this->run($this->input, $this->output);
$hasErrors = $hasErrors || !(is_null($returnValue) || ($returnValue == 0));
$ctx->closeRemoteConnection();

$hosts->next();
}

if ($hasErrors) {
throw new FailedCommandException();
}
}

protected function selectEnvironment(Context $context)
{
$environments = $context->getConfig()->environments();

if (!$this->input->hasParameterOption('--env')) {
return;
}

$userDefinedEnv = $this->input->getParameterOption(array('--env'));

if (!isset($environments[$userDefinedEnv])) {
$msg = sprintf(
'Wrong environment "%s". Available [%s]',
$userDefinedEnv,
implode(', ', array_keys($environments))
);

$this->output
->writeln('<error>'.$msg.'</error>');

exit(1);
}

$context->setEnv($userDefinedEnv);
}
}
26 changes: 10 additions & 16 deletions src/Idephix/Console/Command.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
namespace Idephix\Console;

use Idephix\TaskExecutor;
use Idephix\Task\Parameter;
use Idephix\Task\Task;
use Idephix\Context;
use Symfony\Component\Console\Command\Command as SymfonyCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -12,22 +12,15 @@

class Command extends SymfonyCommand
{
private $idxTaskCode;
/** @var TaskExecutor */
private $idx;
/** @var Task */
private $task;

/**
* @param Task $task
* @param TaskExecutor $idx
* @return Command
*/
public static function fromTask(Task $task, TaskExecutor $idx)
private $ctx;

public static function fromTask(Task $task, Context $ctx)
{
$command = new static($task->name());
$command->task = $task;
$command->idx = $idx;
$command->ctx = $ctx;

$command->setDescription($task->description());

Expand All @@ -51,14 +44,15 @@ public static function fromTask(Task $task, TaskExecutor $idx)
);
}

$command->idxTaskCode = $task->code();

return $command;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
return call_user_func_array($this->idxTaskCode, $this->extractArgumentsFrom($input));
return call_user_func_array(
$this->task->code(),
$this->extractArgumentsFrom($input)
);
}

/**
Expand All @@ -78,7 +72,7 @@ protected function extractArgumentsFrom(InputInterface $input)
/** @var Parameter\UserDefined $parameter */
foreach ($this->task->parameters() as $parameter) {
if ($parameter instanceof Parameter\Context) {
$args[] = $this->idx->getContext();
$args[] = $this->ctx;
continue;
}

Expand Down
9 changes: 7 additions & 2 deletions src/Idephix/Console/InputFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class InputFactory
*/
public function buildFromUserArgsForTask($arguments, Task $task)
{
$defaultArguments = array('command' => null);
$defaultArguments = array();
$inputArguments = array();

foreach ($task->userDefinedParameters() as $parameter) {
if ($parameter->isFlagOption()) {
Expand All @@ -24,7 +25,11 @@ public function buildFromUserArgsForTask($arguments, Task $task)
}

$values = array_replace(array_values($defaultArguments), $arguments);
$inputArguments = array_combine(array_keys($defaultArguments), $values);

// @todo: remove in php 5.3
if (!empty($values)) {
$inputArguments = array_combine(array_keys($defaultArguments), $values);
}

$input = new ArrayInput($inputArguments);

Expand Down
Loading