1
|
|
|
<?php |
2
|
|
|
/* (c) Anton Medvedev <[email protected]> |
3
|
|
|
* |
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
5
|
|
|
* file that was distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Deployer\Console; |
9
|
|
|
|
10
|
|
|
use Deployer\Deployer; |
11
|
|
|
use Deployer\Task\GroupTask; |
12
|
|
|
use Deployer\Task\TaskCollection; |
13
|
|
|
use Symfony\Component\Console\Command\Command; |
14
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
15
|
|
|
use Symfony\Component\Console\Input\InputInterface as Input; |
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface as Output; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @copyright (c) Oskar van Velden <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class TreeCommand extends Command |
22
|
|
|
{ |
23
|
|
|
protected $output; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var TaskCollection |
27
|
|
|
*/ |
28
|
|
|
private $tasks; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Deployer |
32
|
|
|
*/ |
33
|
|
|
private $deployer; |
34
|
|
|
|
35
|
|
|
private $tree; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Depth of nesting (for rendering purposes) |
39
|
|
|
* @var int |
40
|
|
|
*/ |
41
|
|
|
private $depth = 0; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
private $openGroupDepths = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param Deployer $deployer |
50
|
|
|
*/ |
51
|
12 |
|
public function __construct(Deployer $deployer) |
52
|
|
|
{ |
53
|
12 |
|
parent::__construct('tree'); |
54
|
12 |
|
$this->setDescription('Display the task-tree for a given task'); |
55
|
12 |
|
$this->deployer = $deployer; |
56
|
12 |
|
$this->tree = []; |
57
|
12 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Configures the command |
61
|
|
|
*/ |
62
|
12 |
|
protected function configure() |
63
|
|
|
{ |
64
|
12 |
|
$this->addArgument( |
65
|
12 |
|
'task', |
66
|
12 |
|
InputArgument::REQUIRED, |
67
|
12 |
|
'Task to display the tree for' |
68
|
|
|
); |
69
|
12 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
|
|
protected function execute(Input $input, Output $output) |
75
|
|
|
{ |
76
|
|
|
$this->output = $output; |
77
|
|
|
|
78
|
|
|
$rootTaskName = $input->getArgument('task'); |
79
|
|
|
|
80
|
|
|
$this->buildTree($rootTaskName); |
81
|
|
|
$this->outputTree($rootTaskName); |
82
|
|
|
return 0; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Build the tree based on the given taskName |
87
|
|
|
* @param $taskName |
88
|
|
|
* |
89
|
|
|
* @return void |
90
|
|
|
*/ |
91
|
|
|
private function buildTree($taskName) |
92
|
|
|
{ |
93
|
|
|
$this->tasks = Deployer::get()->tasks; |
94
|
|
|
$this->createTreeFromTaskName($taskName, '', true); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Create a tree from the given taskname |
99
|
|
|
* |
100
|
|
|
* @param string $taskName |
101
|
|
|
* @param string $postfix |
102
|
|
|
* @param bool $isLast |
103
|
|
|
* |
104
|
|
|
* @return void |
105
|
|
|
*/ |
106
|
|
|
private function createTreeFromTaskName($taskName, $postfix = '', $isLast = false) |
107
|
|
|
{ |
108
|
|
|
$task = $this->tasks->get($taskName); |
109
|
|
|
|
110
|
|
|
if ($task->getBefore()) { |
111
|
|
|
$beforePostfix = sprintf(" // before %s", $task->getName()); |
112
|
|
|
|
113
|
|
|
foreach ($task->getBefore() as $beforeTask) { |
114
|
|
|
$this->createTreeFromTaskName($beforeTask, $beforePostfix); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if ($task instanceof GroupTask) { |
119
|
|
|
$isLast = $isLast && empty($task->getAfter()); |
120
|
|
|
|
121
|
|
|
$this->addTaskToTree($task->getName() . $postfix, $isLast); |
122
|
|
|
|
123
|
|
|
if (!$isLast) { |
124
|
|
|
$this->openGroupDepths[] = $this->depth; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$this->depth++; |
128
|
|
|
|
129
|
|
|
$taskGroup = $task->getGroup(); |
130
|
|
|
foreach ($taskGroup as $subtask) { |
131
|
|
|
$isLastSubtask = $subtask === end($taskGroup); |
132
|
|
|
$this->createTreeFromTaskName($subtask, '', $isLastSubtask); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if (!$isLast) { |
136
|
|
|
array_pop($this->openGroupDepths); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$this->depth--; |
140
|
|
|
} else { |
141
|
|
|
$this->addTaskToTree($task->getName() . $postfix, $isLast); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
if ($task->getAfter()) { |
145
|
|
|
$afterPostfix = sprintf(" // after %s", $task->getName()); |
146
|
|
|
|
147
|
|
|
foreach ($task->getAfter() as $afterTask) { |
148
|
|
|
$this->createTreeFromTaskName($afterTask, $afterPostfix); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Add the (formatted) taskName to the rendertree, with some additional information |
155
|
|
|
* |
156
|
|
|
* @param string $taskName formatted with prefixes if needed |
157
|
|
|
* @param bool $isLast indication for what symbol to use for rendering |
158
|
|
|
*/ |
159
|
|
|
private function addTaskToTree($taskName, $isLast = false) |
160
|
|
|
{ |
161
|
|
|
$this->tree[] = [ |
162
|
|
|
'taskName' => $taskName, |
163
|
|
|
'depth' => $this->depth, |
164
|
|
|
'isLast' => $isLast, |
165
|
|
|
'openDepths' => $this->openGroupDepths |
166
|
|
|
]; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Render the tree, after everything is build |
171
|
|
|
* |
172
|
|
|
* @param $taskName |
173
|
|
|
*/ |
174
|
|
|
private function outputTree($taskName) |
175
|
|
|
{ |
176
|
|
|
$this->output->writeln("The task-tree for <info>$taskName</info>:"); |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @var $REPEAT_COUNT number of spaces for each depth increase |
180
|
|
|
*/ |
181
|
|
|
$REPEAT_COUNT = 4; |
182
|
|
|
|
183
|
|
|
foreach ($this->tree as $treeItem) { |
184
|
|
|
$depth = $treeItem['depth']; |
185
|
|
|
|
186
|
|
|
$startSymbol = $treeItem['isLast'] || $treeItem === end($this->tree) ? '└' : '├'; |
187
|
|
|
|
188
|
|
|
$prefix = ''; |
189
|
|
|
|
190
|
|
|
for ($i = 0; $i < $depth; $i++) { |
191
|
|
|
if (in_array($i, $treeItem['openDepths'])) { |
192
|
|
|
$prefix .= '│' . str_repeat(' ', $REPEAT_COUNT - 1); |
193
|
|
|
} else { |
194
|
|
|
$prefix .= str_repeat(' ', $REPEAT_COUNT); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
$prefix .= $startSymbol . '──'; |
199
|
|
|
|
200
|
|
|
$this->output->writeln(sprintf('%s %s', $prefix, $treeItem['taskName'])); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|