1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace PhpTaskman\Core\Robo\Task\CollectionFactory; |
6
|
|
|
|
7
|
|
|
use PhpTaskman\Core\Contract\ConfigurationTokensAwareInterface; |
8
|
|
|
use PhpTaskman\Core\Robo\Task\Filesystem\LoadFilesystemTasks; |
9
|
|
|
use PhpTaskman\Core\Robo\Task\ProcessConfigFile\LoadProcessConfigFileTasks; |
10
|
|
|
use PhpTaskman\Core\Traits\ConfigurationTokensTrait; |
11
|
|
|
use Robo\Contract\BuilderAwareInterface; |
12
|
|
|
use Robo\Contract\SimulatedInterface; |
13
|
|
|
use Robo\Exception\TaskException; |
14
|
|
|
use Robo\LoadAllTasks; |
15
|
|
|
use Robo\Robo; |
16
|
|
|
use Robo\Task\BaseTask; |
17
|
|
|
use Symfony\Component\Yaml\Yaml; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class CollectionFactory. |
21
|
|
|
* |
22
|
|
|
* Return a task collection given its array representation. |
23
|
|
|
*/ |
24
|
|
|
final class CollectionFactory extends BaseTask implements |
25
|
|
|
BuilderAwareInterface, |
26
|
|
|
SimulatedInterface, |
27
|
|
|
ConfigurationTokensAwareInterface |
28
|
|
|
{ |
29
|
|
|
use ConfigurationTokensTrait; |
30
|
|
|
use LoadAllTasks; |
31
|
|
|
use LoadFilesystemTasks; |
32
|
|
|
use LoadProcessConfigFileTasks; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
protected $options; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $tasks; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* CollectionFactory constructor. |
46
|
|
|
* |
47
|
|
|
* @param array $tasks |
48
|
|
|
* @param array $options |
49
|
|
|
*/ |
50
|
|
|
public function __construct(array $tasks = [], array $options = []) |
51
|
|
|
{ |
52
|
|
|
$this->tasks = $tasks; |
53
|
|
|
$this->options = $options; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public function getHelp() |
60
|
|
|
{ |
61
|
|
|
return $this->tasks['help'] ?? 'Yaml command defined in tasks.yml'; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
|
|
public function getTasks() |
68
|
|
|
{ |
69
|
|
|
return $this->tasks['tasks'] ?? $this->tasks; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function run() |
76
|
|
|
{ |
77
|
|
|
$collection = $this->collectionBuilder(); |
78
|
|
|
|
79
|
|
|
foreach ($this->getTasks() as $task) { |
80
|
|
|
if (\is_string($task)) { |
81
|
|
|
$collection->addTask($this->taskExec($task)); |
82
|
|
|
|
83
|
|
|
continue; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (!\is_array($task)) { |
87
|
|
|
// Todo: Error. |
88
|
|
|
continue; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if (!isset($task['task'])) { |
92
|
|
|
// Todo: Error. |
93
|
|
|
continue; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (!\is_string($task['task'])) { |
97
|
|
|
// Todo: Error. |
98
|
|
|
continue; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$collection->addTask($this->taskFactory($task)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $collection->run(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
*/ |
110
|
|
|
public function simulate($context) |
111
|
|
|
{ |
112
|
|
|
foreach ($this->getTasks() as $task) { |
113
|
|
|
if (\is_array($task)) { |
114
|
|
|
$task = Yaml::dump($task, 0); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$this->printTaskInfo($task, $context); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Secure option value. |
123
|
|
|
* |
124
|
|
|
* @param array $task |
125
|
|
|
* @param string $name |
126
|
|
|
* @param mixed $default |
127
|
|
|
*/ |
128
|
|
|
protected function secureOption(array &$task, $name, $default) |
129
|
|
|
{ |
130
|
|
|
$task[$name] = $task[$name] ?? $default; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param array|string $task |
135
|
|
|
* |
136
|
|
|
* @throws \Robo\Exception\TaskException |
137
|
|
|
* |
138
|
|
|
* @return \Robo\Contract\TaskInterface |
139
|
|
|
* |
140
|
|
|
* @SuppressWarnings(PHPMD) |
141
|
|
|
*/ |
142
|
|
|
protected function taskFactory($task) |
143
|
|
|
{ |
144
|
|
|
$this->secureOption($task, 'force', false); |
|
|
|
|
145
|
|
|
$this->secureOption($task, 'umask', 0000); |
146
|
|
|
$this->secureOption($task, 'recursive', false); |
147
|
|
|
$this->secureOption($task, 'time', \time()); |
148
|
|
|
$this->secureOption($task, 'atime', \time()); |
149
|
|
|
$this->secureOption($task, 'mode', 0777); |
150
|
|
|
|
151
|
|
|
$taskMap = [ |
152
|
|
|
'mkdir' => [ |
153
|
|
|
'factory' => 'taskFilesystemFactory', |
154
|
|
|
'command' => 'mkdir', |
155
|
|
|
'parameters' => [ |
156
|
|
|
'dir', |
157
|
|
|
'mode', |
158
|
|
|
], |
159
|
|
|
], |
160
|
|
|
'touch' => [ |
161
|
|
|
'factory' => 'taskFilesystemFactory', |
162
|
|
|
'command' => 'touch', |
163
|
|
|
'parameters' => [ |
164
|
|
|
'file', |
165
|
|
|
'time', |
166
|
|
|
'atime', |
167
|
|
|
], |
168
|
|
|
], |
169
|
|
|
'copy' => [ |
170
|
|
|
'factory' => 'taskFilesystemFactory', |
171
|
|
|
'command' => 'copy', |
172
|
|
|
'parameters' => [ |
173
|
|
|
'from', |
174
|
|
|
'to', |
175
|
|
|
'force', |
176
|
|
|
], |
177
|
|
|
], |
178
|
|
|
'chmod' => [ |
179
|
|
|
'factory' => 'taskFilesystemFactory', |
180
|
|
|
'command' => 'chmod', |
181
|
|
|
'parameters' => [ |
182
|
|
|
'file', |
183
|
|
|
'permissions', |
184
|
|
|
'umask', |
185
|
|
|
'recursive', |
186
|
|
|
], |
187
|
|
|
], |
188
|
|
|
'chgrp' => [ |
189
|
|
|
'factory' => 'taskFilesystemFactory', |
190
|
|
|
'command' => 'chgrp', |
191
|
|
|
'parameters' => [ |
192
|
|
|
'file', |
193
|
|
|
'group', |
194
|
|
|
'recursive', |
195
|
|
|
], |
196
|
|
|
], |
197
|
|
|
'chown' => [ |
198
|
|
|
'factory' => 'taskFilesystemFactory', |
199
|
|
|
'command' => 'chown', |
200
|
|
|
'parameters' => [ |
201
|
|
|
'file', |
202
|
|
|
'user', |
203
|
|
|
'recursive', |
204
|
|
|
], |
205
|
|
|
], |
206
|
|
|
'remove' => [ |
207
|
|
|
'factory' => 'taskFilesystemFactory', |
208
|
|
|
'command' => 'remove', |
209
|
|
|
'parameters' => [ |
210
|
|
|
'file', |
211
|
|
|
], |
212
|
|
|
], |
213
|
|
|
'rename' => [ |
214
|
|
|
'factory' => 'taskFilesystemFactory', |
215
|
|
|
'command' => 'rename', |
216
|
|
|
'parameters' => [ |
217
|
|
|
'from', |
218
|
|
|
'to', |
219
|
|
|
'force', |
220
|
|
|
], |
221
|
|
|
], |
222
|
|
|
'symlink' => [ |
223
|
|
|
'factory' => 'taskFilesystemFactory', |
224
|
|
|
'command' => 'symlink', |
225
|
|
|
'parameters' => [ |
226
|
|
|
'from', |
227
|
|
|
'to', |
228
|
|
|
], |
229
|
|
|
], |
230
|
|
|
'mirror' => [ |
231
|
|
|
'factory' => 'taskFilesystemFactory', |
232
|
|
|
'command' => 'mirror', |
233
|
|
|
'parameters' => [ |
234
|
|
|
'from', |
235
|
|
|
'to', |
236
|
|
|
], |
237
|
|
|
], |
238
|
|
|
'process' => [ |
239
|
|
|
'factory' => 'taskProcessConfigFile', |
240
|
|
|
'command' => 'process', |
241
|
|
|
'parameters' => [ |
242
|
|
|
'source', |
243
|
|
|
'destination', |
244
|
|
|
], |
245
|
|
|
], |
246
|
|
|
]; |
247
|
|
|
|
248
|
|
|
if (isset($taskMap[$task['task']])) { |
249
|
|
|
$parameters = []; |
250
|
|
|
|
251
|
|
|
foreach ($taskMap[$task['task']]['parameters'] as $key) { |
252
|
|
|
if (isset($task[$key])) { |
253
|
|
|
$parameters[$key] = $task[$key]; |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
return $this->{$taskMap[$task['task']]['factory']}($task['task'], $parameters); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
switch ($task['task']) { |
261
|
|
|
case 'append': |
262
|
|
|
return $this->collectionBuilder()->addTaskList([ |
263
|
|
|
$this->taskWriteToFile($task['file'])->append()->text($task['text']), |
264
|
|
|
$this->taskProcessConfigFile($task['file'], $task['file']), |
265
|
|
|
]); |
266
|
|
|
|
267
|
|
|
case 'run': |
268
|
|
|
$taskExec = $this->taskExec( |
269
|
|
|
$this->getConfig()->get('taskman.bin_dir') . '/taskman' |
270
|
|
|
)->arg($task['command']); |
271
|
|
|
|
272
|
|
|
$container = Robo::getContainer(); |
273
|
|
|
|
274
|
|
|
/** @var \Robo\Application $app */ |
275
|
|
|
$app = $container->get('application'); |
276
|
|
|
|
277
|
|
|
/** @var \Consolidation\AnnotatedCommand\AnnotatedCommand $command */ |
278
|
|
|
$command = $app->get($task['command']); |
279
|
|
|
$commandOptions = $command->getDefinition()->getOptions(); |
280
|
|
|
|
281
|
|
|
// Propagate any input option passed to the child command. |
282
|
|
|
foreach ($this->options as $name => $values) { |
283
|
|
|
if (!isset($commandOptions[$name])) { |
284
|
|
|
continue; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
// But only if the called command has this option. |
288
|
|
|
foreach ((array) $values as $value) { |
289
|
|
|
$taskExec->option($name, $value); |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
return $taskExec; |
294
|
|
|
default: |
295
|
|
|
throw new TaskException($this, "Task '{$task['task']}' not supported."); |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
|