-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathScriptManager.php
126 lines (111 loc) · 3.23 KB
/
ScriptManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
declare(strict_types=1);
/* (c) Anton Medvedev <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Deployer\Task;
use Deployer\Exception\Exception;
use function Deployer\Support\array_flatten;
class ScriptManager
{
/**
* @var TaskCollection
*/
private $tasks;
/**
* @var bool
*/
private $hooksEnabled = true;
/**
* @var array
*/
private $visitedTasks = [];
public function __construct(TaskCollection $tasks)
{
$this->tasks = $tasks;
}
/**
* Return tasks to run.
*
* @return Task[]
*/
public function getTasks(string $name, ?string $startFrom = null, array &$skipped = []): array
{
$tasks = [];
$this->visitedTasks = [];
$allTasks = $this->doGetTasks($name);
if ($startFrom === null) {
$tasks = $allTasks;
} else {
$skip = true;
foreach ($allTasks as $task) {
if ($skip) {
if ($task->getName() === $startFrom) {
$skip = false;
} else {
$skipped[] = $task->getName();
continue;
}
}
$tasks[] = $task;
}
if (count($tasks) === 0) {
throw new Exception('All tasks skipped via --start-from option. Nothing to run.');
}
}
$enabledTasks = [];
foreach ($tasks as $task) {
if ($task->isEnabled()) {
$enabledTasks[] = $task;
}
}
return $enabledTasks;
}
/**
* @return Task[]
*/
public function doGetTasks(string $name): array
{
if (array_key_exists($name, $this->visitedTasks)) {
if ($this->visitedTasks[$name] >= 100) {
throw new Exception("Looks like a circular dependency with \"$name\" task.");
}
$this->visitedTasks[$name]++;
} else {
$this->visitedTasks[$name] = 1;
}
$tasks = [];
$task = $this->tasks->get($name);
if ($this->hooksEnabled) {
$tasks = array_merge(array_map([$this, 'doGetTasks'], $task->getBefore()), $tasks);
}
if ($task instanceof GroupTask) {
foreach ($task->getGroup() as $taskName) {
$subTasks = $this->doGetTasks($taskName);
foreach ($subTasks as $subTask) {
$subTask->addSelector($task->getSelector());
if ($task->isOnce()) {
$subTask->once();
}
$tasks[] = $subTask;
}
}
} else {
$tasks[] = $task;
}
if ($this->hooksEnabled) {
$tasks = array_merge($tasks, array_map([$this, 'doGetTasks'], $task->getAfter()));
}
return array_flatten($tasks);
}
public function getHooksEnabled(): bool
{
return $this->hooksEnabled;
}
public function setHooksEnabled(bool $hooksEnabled): void
{
$this->hooksEnabled = $hooksEnabled;
}
}