Skip to content

Commit

Permalink
Add DebugCommand to display task execution order (#1491)
Browse files Browse the repository at this point in the history
* Add DebugCommand

* Adjust display of debug to table layout

* Update changelog

* Update CHANGELOG.md

* Fix StyleCI error

* Revert "Adjust display of debug to table layout" and setup new tree structure

This reverts commit db97551.

* Render tree view (the basic)

* Tweak rendering of tree

* Add depth for groups and pipe symbol

* Fix rendering of last element

* Display postfix for groups

* Fix issue with display of after-task after last grouped task

* Fix CS

* Fix StyleCI issues

* Add doc-comments

* Update CHANGELOG.md

Fix issue order (by request of Scrutinizer)
  • Loading branch information
skarnl authored and antonmedv committed Apr 18, 2018
1 parent ca130ec commit 906cb5b
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[v6.0.5...master](https://github.com/deployphp/deployer/compare/v6.0.5...master)

### Added
- Added debug:task command to display the order of task execution [#1488]
- Added a description to the autocomplete command [#1472]
- Added logging of unhandled exceptions into logfile [#1481]

Expand Down Expand Up @@ -357,6 +358,7 @@
[#1554]: https://github.com/deployphp/deployer/pull/1554
[#1521]: https://github.com/deployphp/deployer/pull/1521
[#1513]: https://github.com/deployphp/deployer/pull/1513
[#1488]: https://github.com/deployphp/deployer/issues/1488
[#1481]: https://github.com/deployphp/deployer/issues/1481
[#1480]: https://github.com/deployphp/deployer/issues/1480
[#1476]: https://github.com/deployphp/deployer/pull/1476
Expand Down
204 changes: 204 additions & 0 deletions src/Console/DebugCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
<?php
/* (c) Anton Medvedev <[email protected]>
/* (c) Oskar van Velden <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Deployer\Console;

use Deployer\Deployer;
use Deployer\Task\GroupTask;
use Deployer\Task\TaskCollection;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface as Input;
use Symfony\Component\Console\Output\OutputInterface as Output;

class DebugCommand extends Command
{
/** @var Output */
protected $output;

/**
* @var TaskCollection
*/
private $tasks;

/**
* @var Deployer
*/
private $deployer;

/**
* @var array
*/
private $tree;

/**
* Depth of nesting (for rendering purposes)
* @var int
*/
private $depth = 0;

/**
* @var array
*/
private $openGroupDepths = [];

/**
* @param Deployer $deployer
*/
public function __construct(Deployer $deployer)
{
parent::__construct('debug:task');
$this->setDescription('Display the task-tree for a given task');
$this->deployer = $deployer;
$this->tree = [];
}

/**
* Configures the command
*/
protected function configure()
{
$this->addArgument(
'task',
InputArgument::REQUIRED,
'Task to display the tree for'
);
}

/**
* {@inheritdoc}
*/
protected function execute(Input $input, Output $output)
{
$this->output = $output;

$rootTaskName = $input->getArgument('task');

$this->buildTree($rootTaskName);
$this->outputTree($rootTaskName);
}

/**
* Build the tree based on the given taskName
* @param $taskName
*
* @return void
*/
private function buildTree($taskName)
{
$this->tasks = Deployer::get()->tasks;
$this->createTreeFromTaskName($taskName, '', true);
}

/**
* Create a tree from the given taskname
*
* @param string $taskName
* @param string $postfix
* @param bool $isLast
*
* @return void
*/
private function createTreeFromTaskName($taskName, $postfix = '', $isLast = false)
{
$task = $this->tasks->get($taskName);

if ($task->getBefore()) {
$beforePostfix = sprintf(' [before:%s]', $task->getName());

foreach ($task->getBefore() as $beforeTask) {
$this->createTreeFromTaskName($beforeTask, $beforePostfix);
}
}

if ($task instanceof GroupTask) {
$isLast = $isLast && empty($task->getAfter());

$this->addTaskToTree($task->getName() . $postfix, $isLast);

if (!$isLast) {
$this->openGroupDepths[] = $this->depth;
}

$this->depth++;

$taskGroup = $task->getGroup();
foreach ($taskGroup as $subtask) {
$isLastSubtask = $subtask === end($taskGroup);
$this->createTreeFromTaskName($subtask, '', $isLastSubtask);
}

if (!$isLast) {
array_pop($this->openGroupDepths);
}

$this->depth--;
} else {
$this->addTaskToTree($task->getName() . $postfix, $isLast);
}

if ($task->getAfter()) {
$afterPostfix = sprintf(' [after:%s]', $task->getName());

foreach ($task->getAfter() as $afterTask) {
$this->createTreeFromTaskName($afterTask, $afterPostfix);
}
}
}

/**
* Add the (formatted) taskName to the rendertree, with some additional information
*
* @param string $taskName formatted with prefixes if needed
* @param bool $isLast indication for what symbol to use for rendering
*/
private function addTaskToTree($taskName, $isLast = false)
{
$this->tree[] = [
'taskName' => $taskName,
'depth' => $this->depth,
'isLast' => $isLast,
'openDepths' => $this->openGroupDepths
];
}

/**
* Render the tree, after everything is build
*
* @param $taskName
*/
private function outputTree($taskName)
{
$this->output->writeln("The task-tree for <fg=cyan>$taskName</fg=cyan>:");

/**
* @var $REPEAT_COUNT number of spaces for each depth increase
*/
$REPEAT_COUNT = 4;

foreach ($this->tree as $treeItem) {
$depth = $treeItem['depth'];

$startSymbol = $treeItem['isLast'] || $treeItem === end($this->tree) ? '' : '';

$prefix = '';

for ($i = 0; $i < $depth; $i++) {
if (in_array($i, $treeItem['openDepths'])) {
$prefix .= '' . str_repeat(' ', $REPEAT_COUNT - 1);
} else {
$prefix .= str_repeat(' ', $REPEAT_COUNT);
}
}

$prefix .= $startSymbol . '──';

$this->output->writeln(sprintf('%s %s', $prefix, $treeItem['taskName']));
}
}
}
2 changes: 2 additions & 0 deletions src/Deployer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Deployer\Console\Application;
use Deployer\Console\AutocompleteCommand;
use Deployer\Console\CommandEvent;
use Deployer\Console\DebugCommand;
use Deployer\Console\InitCommand;
use Deployer\Console\Output\Informer;
use Deployer\Console\Output\OutputWatcher;
Expand Down Expand Up @@ -224,6 +225,7 @@ public function init()
$this->getConsole()->add($this['init_command']);
$this->getConsole()->add(new SshCommand($this));
$this->getConsole()->add(new RunCommand($this));
$this->getConsole()->add(new DebugCommand($this));
$this->getConsole()->add(new AutocompleteCommand());
$this->getConsole()->afterRun([$this, 'collectAnonymousStats']);
}
Expand Down

0 comments on commit 906cb5b

Please sign in to comment.