1
|
|
|
<?php |
2
|
|
|
namespace RoboExample\Robo\Plugin\Commands; |
3
|
|
|
|
4
|
|
|
use Robo\Result; |
5
|
|
|
|
6
|
|
|
use Consolidation\AnnotatedCommand\CommandData; |
7
|
|
|
use Consolidation\OutputFormatters\Options\FormatterOptions; |
8
|
|
|
use Consolidation\OutputFormatters\StructuredData\RowsOfFields; |
9
|
|
|
use Consolidation\OutputFormatters\StructuredData\PropertyList; |
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Example Robo Plugin Commands. |
14
|
|
|
* |
15
|
|
|
* To create a Robo Plugin, create a standard Composer project. The |
16
|
|
|
* namespace for your commands must end Robo\Plugin\Commands, and |
17
|
|
|
* this suffix must immediately follow some namespace in your composer.json |
18
|
|
|
* file's autoload section. |
19
|
|
|
* |
20
|
|
|
* For example: |
21
|
|
|
* |
22
|
|
|
* "autoload": { |
23
|
|
|
* "psr-4": { |
24
|
|
|
* "RoboExample\\": "src" |
25
|
|
|
* } |
26
|
|
|
* }, |
27
|
|
|
* |
28
|
|
|
* In this instance, the namespace for your plugin commands must be |
29
|
|
|
* RoboExample\Robo\Plugin\Commands. |
30
|
|
|
*/ |
31
|
|
|
class ExampleCommands extends \Robo\Tasks |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* Watch a file. |
35
|
|
|
* |
36
|
|
|
* Demonstrates the 'watch' command. Runs 'composer update' any time |
37
|
|
|
* composer.json changes. |
38
|
|
|
*/ |
39
|
|
|
public function tryWatch() |
40
|
|
|
{ |
41
|
|
|
$this->taskWatch()->monitor(['composer.json', 'composer.lock'], function () { |
42
|
|
|
$this->taskComposerUpdate()->run(); |
43
|
|
|
})->run(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Demonstrates Robo input APIs. |
48
|
|
|
*/ |
49
|
|
|
public function tryInput() |
50
|
|
|
{ |
51
|
|
|
$this->say('The <b>expression</b> <bogus>is</bogus> <info>a < b</> it even works'); |
52
|
|
|
$answer = $this->ask('how are you?'); |
53
|
|
|
$this->say('You are '.$answer); |
54
|
|
|
$yes = $this->confirm('Do you want one more question?'); |
55
|
|
|
if (!$yes) { |
56
|
|
|
return Result::cancelled(); |
57
|
|
|
} |
58
|
|
|
$lang = $this->askDefault('what is your favorite scripting language?', 'PHP'); |
59
|
|
|
$this->say($lang); |
60
|
|
|
$pin = $this->askHidden('Ok, now tell your PIN code (it is hidden)'); |
61
|
|
|
$this->yell('Ha-ha, your pin code is: '.$pin); |
62
|
|
|
$this->say('Bye!'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Demonstrates serial execution. |
67
|
|
|
* |
68
|
|
|
* @option $printed Print the output of each process. |
69
|
|
|
* @option $error Include an extra process that fails. |
70
|
|
|
*/ |
71
|
|
|
public function tryExec($options = ['printed' => true, 'error' => false]) |
72
|
|
|
{ |
73
|
|
|
$dir = dirname(dirname(dirname(dirname(dirname(__DIR__))))); |
74
|
|
|
$tasks = $this |
|
|
|
|
75
|
|
|
->taskExec('php') |
76
|
|
|
->args(["$dir/tests/_data/parascript.php", "hey", "4"]) |
77
|
|
|
->taskExec('php') |
78
|
|
|
->args(["$dir/tests/_data/parascript.php", "hoy", "3"]) |
79
|
|
|
->taskExec('php') |
80
|
|
|
->args(["$dir/tests/_data/parascript.php", "gou", "2"]) |
81
|
|
|
->taskExec('php') |
82
|
|
|
->args(["$dir/tests/_data/parascript.php", "die", "1"]); |
83
|
|
|
if ($options['error']) { |
84
|
|
|
$tasks->taskExec('ls')->arg("$dir/tests/_data/filenotfound"); |
85
|
|
|
} |
86
|
|
|
return $tasks->run(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Demonstrates parallel execution. |
91
|
|
|
* |
92
|
|
|
* @option $printed Print the output of each process. |
93
|
|
|
* @option $error Include an extra process that fails. |
94
|
|
|
*/ |
95
|
|
|
public function tryPara($options = ['printed' => true, 'error' => false]) |
96
|
|
|
{ |
97
|
|
|
$dir = dirname(dirname(dirname(dirname(dirname(__DIR__))))); |
98
|
|
|
$para = $this->taskParallelExec() |
99
|
|
|
->printed($options['printed']) |
100
|
|
|
->process("php $dir/tests/_data/parascript.php hey 4") |
101
|
|
|
->process("php $dir/tests/_data/parascript.php hoy 3") |
102
|
|
|
->process("php $dir/tests/_data/parascript.php gou 2") |
103
|
|
|
->process("php $dir/tests/_data/parascript.php die 1"); |
104
|
|
|
if ($options['error']) { |
105
|
|
|
$para->process("ls $dir/tests/_data/filenotfound"); |
106
|
|
|
} |
107
|
|
|
return $para->run(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* try:opt-required |
112
|
|
|
*/ |
113
|
|
|
public function tryOptRequired($options = ['foo' => InputOption::VALUE_REQUIRED]) |
114
|
|
|
{ |
115
|
|
|
print "foo is " . $options['foo']; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Demonstrates Robo argument passing. |
120
|
|
|
* |
121
|
|
|
* @param string $a The first parameter. Required. |
122
|
|
|
* @param string $b The second parameter. Optional. |
123
|
|
|
*/ |
124
|
|
|
public function tryArgs($a, $b = 'default') |
125
|
|
|
{ |
126
|
|
|
$this->say("The parameter a is $a and b is $b"); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Demonstrate Robo variable argument passing. |
131
|
|
|
* |
132
|
|
|
* @param $a A list of commandline parameters. |
133
|
|
|
*/ |
134
|
|
|
public function tryArrayArgs(array $a) |
135
|
|
|
{ |
136
|
|
|
$this->say("The parameters passed are:\n" . var_export($a, true)); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Demonstrate Robo boolean options. |
141
|
|
|
* |
142
|
|
|
* @param $opts The options. |
143
|
|
|
* @option boolean $silent Supress output. |
144
|
|
|
*/ |
145
|
|
|
public function tryOptbool($opts = ['silent|s' => false]) |
146
|
|
|
{ |
147
|
|
|
if (!$opts['silent']) { |
148
|
|
|
$this->say("Hello, world"); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Demonstrate the use of the PHP built-in webserver. |
154
|
|
|
*/ |
155
|
|
|
public function tryServer() |
156
|
|
|
{ |
157
|
|
|
return $this->taskServer(8000) |
158
|
|
|
->dir('site') |
159
|
|
|
->arg('site/index.php') |
160
|
|
|
->run(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Demonstrate the use of the Robo open-browser task. |
165
|
|
|
*/ |
166
|
|
|
public function tryOpenBrowser() |
167
|
|
|
{ |
168
|
|
|
return $this->taskOpenBrowser([ |
169
|
|
|
'http://robo.li', |
170
|
|
|
'https://github.com/consolidation-org/Robo' |
171
|
|
|
])->run(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Demonstrate Robo error output and command failure. |
176
|
|
|
*/ |
177
|
|
|
public function tryError() |
178
|
|
|
{ |
179
|
|
|
return $this->taskExec('ls xyzzy' . date('U'))->dir('/tmp')->run(); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Demonstrate Robo standard output and command success. |
184
|
|
|
*/ |
185
|
|
|
public function trySuccess() |
186
|
|
|
{ |
187
|
|
|
return $this->_exec('pwd'); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @field-labels |
192
|
|
|
* name: Name |
193
|
|
|
* species: Species |
194
|
|
|
* legs: Legs |
195
|
|
|
* food: Favorite Food |
196
|
|
|
* id: Id |
197
|
|
|
* @return PropertyList |
198
|
|
|
*/ |
199
|
|
|
public function tryInfo() |
200
|
|
|
{ |
201
|
|
|
$outputData = [ |
202
|
|
|
'name' => 'fluffy', |
203
|
|
|
'species' => 'cat', |
204
|
|
|
'legs' => 4, |
205
|
|
|
'food' => 'salmon', |
206
|
|
|
'id' => 389245032, |
207
|
|
|
]; |
208
|
|
|
|
209
|
|
|
$data = new PropertyList($outputData); |
210
|
|
|
|
211
|
|
|
// Add a render function to transform cell data when the output |
212
|
|
|
// format is a table, or similar. This allows us to add color |
213
|
|
|
// information to the output without modifying the data cells when |
214
|
|
|
// using yaml or json output formats. |
215
|
|
|
$data->addRendererFunction( |
216
|
|
|
// n.b. There is a fourth parameter $rowData that may be added here. |
217
|
|
|
function ($key, $cellData, FormatterOptions $options) { |
|
|
|
|
218
|
|
|
if ($key == 'name') { |
219
|
|
|
return "<info>$cellData</>"; |
220
|
|
|
} |
221
|
|
|
return $cellData; |
222
|
|
|
} |
223
|
|
|
); |
224
|
|
|
|
225
|
|
|
return $data; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Demonstrate Robo formatters. Default format is 'table'. |
230
|
|
|
* |
231
|
|
|
* @field-labels |
232
|
|
|
* first: I |
233
|
|
|
* second: II |
234
|
|
|
* third: III |
235
|
|
|
* @default-string-field second |
236
|
|
|
* @usage try:formatters --format=yaml |
237
|
|
|
* @usage try:formatters --format=csv |
238
|
|
|
* @usage try:formatters --fields=first,third |
239
|
|
|
* @usage try:formatters --fields=III,II |
240
|
|
|
* @aliases tf |
241
|
|
|
* |
242
|
|
|
* @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields |
243
|
|
|
*/ |
244
|
|
|
public function tryFormatters($somthing = 'default', $options = ['format' => 'table', 'fields' => '']) |
|
|
|
|
245
|
|
|
{ |
246
|
|
|
$outputData = [ |
247
|
|
|
'en' => [ 'first' => 'One', 'second' => 'Two', 'third' => 'Three' ], |
248
|
|
|
'de' => [ 'first' => 'Eins', 'second' => 'Zwei', 'third' => 'Drei' ], |
249
|
|
|
'jp' => [ 'first' => 'Ichi', 'second' => 'Ni', 'third' => 'San' ], |
250
|
|
|
'es' => [ 'first' => 'Uno', 'second' => 'Dos', 'third' => 'Tres' ], |
251
|
|
|
]; |
252
|
|
|
return new RowsOfFields($outputData); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Try word wrapping |
257
|
|
|
* |
258
|
|
|
* @field-labels |
259
|
|
|
* first: First |
260
|
|
|
* second: Second |
261
|
|
|
* |
262
|
|
|
* @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields |
263
|
|
|
*/ |
264
|
|
|
public function tryWrap() |
265
|
|
|
{ |
266
|
|
|
$data = [ |
267
|
|
|
[ |
268
|
|
|
'first' => 'This is a really long cell that contains a lot of data. When it is rendered, it should be wrapped across multiple lines.', |
269
|
|
|
'second' => 'This is the second column of the same table. It is also very long, and should be wrapped across multiple lines, just like the first column.', |
270
|
|
|
] |
271
|
|
|
]; |
272
|
|
|
return new RowsOfFields($data); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Demonstrate an alter hook with an option |
277
|
|
|
* |
278
|
|
|
* @hook alter try:formatters |
279
|
|
|
* @option $french Add a row with French numbers. |
280
|
|
|
* @usage try:formatters --french |
281
|
|
|
*/ |
282
|
|
|
public function alterFormatters($result, CommandData $commandData) |
283
|
|
|
{ |
284
|
|
|
if ($commandData->input()->getOption('french')) { |
285
|
|
|
$result['fr'] = [ 'first' => 'Un', 'second' => 'Deux', 'third' => 'Trois' ]; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
return $result; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Demonstrate what happens when a command or a task |
293
|
|
|
* throws an exception. Note that typically, Robo commands |
294
|
|
|
* should return Result objects rather than throw exceptions. |
295
|
|
|
*/ |
296
|
|
|
public function tryException($options = ['task' => false]) |
297
|
|
|
{ |
298
|
|
|
if (!$options['task']) { |
299
|
|
|
throw new RuntimeException('Command failed with an exception.'); |
300
|
|
|
} |
301
|
|
|
return new ExceptionTask('Task failed with an exception.'); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Demonstrate deprecated task behavior. |
306
|
|
|
* |
307
|
|
|
* Demonstrate what happens when using a task that is created via |
308
|
|
|
* direct instantiation, which omits initialization done by the |
309
|
|
|
* container. Emits a warning message. |
310
|
|
|
*/ |
311
|
|
|
public function tryDeprecated() |
312
|
|
|
{ |
313
|
|
|
// Calling 'new' directly without manually setting |
314
|
|
|
// up dependencies will result in a deprecation warning. |
315
|
|
|
// @see RoboFile::trySuccess() |
|
|
|
|
316
|
|
|
return (new \Robo\Task\Base\Exec('pwd'))->run(); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Demonstrate the use of a collection builder to chain multiple tasks |
321
|
|
|
* together into a collection, which is executed once constructed. |
322
|
|
|
* |
323
|
|
|
* For demonstration purposes only; this could, of course, be done |
324
|
|
|
* with a single FilesystemStack. |
325
|
|
|
*/ |
326
|
|
|
public function tryBuilder() |
327
|
|
|
{ |
328
|
|
|
return $this->collectionBuilder() |
|
|
|
|
329
|
|
|
->taskFilesystemStack() |
330
|
|
|
->mkdir('a') |
331
|
|
|
->touch('a/a.txt') |
332
|
|
|
->taskFilesystemStack() |
333
|
|
|
->mkdir('a/b') |
334
|
|
|
->touch('a/b/b.txt') |
335
|
|
|
->taskFilesystemStack() |
336
|
|
|
->mkdir('a/b/c') |
337
|
|
|
->touch('a/b/c/c.txt') |
338
|
|
|
->run(); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
public function tryState() |
342
|
|
|
{ |
343
|
|
|
return $this->collectionBuilder() |
|
|
|
|
344
|
|
|
->taskExec('uname -n') |
345
|
|
|
->printOutput(false) |
346
|
|
|
->storeState('system-name') |
347
|
|
|
->taskFilesystemStack() |
348
|
|
|
->deferTaskConfiguration('mkdir', 'system-name') |
349
|
|
|
->run(); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
public function tryBuilderRollback() |
353
|
|
|
{ |
354
|
|
|
// This example will create two builders, and add |
355
|
|
|
// the first one as a child of the second in order |
356
|
|
|
// to demonstrate nested rollbacks. |
357
|
|
|
$collection = $this->collectionBuilder() |
|
|
|
|
358
|
|
|
->taskFilesystemStack() |
359
|
|
|
->mkdir('g') |
360
|
|
|
->touch('g/g.txt') |
361
|
|
|
->rollback( |
362
|
|
|
$this->taskDeleteDir('g') |
363
|
|
|
) |
364
|
|
|
->taskFilesystemStack() |
365
|
|
|
->mkdir('g/h') |
366
|
|
|
->touch('g/h/h.txt') |
367
|
|
|
->taskFilesystemStack() |
368
|
|
|
->mkdir('g/h/i/c') |
369
|
|
|
->touch('g/h/i/i.txt'); |
370
|
|
|
|
371
|
|
|
return $this->collectionBuilder() |
|
|
|
|
372
|
|
|
->progressMessage('Start recursive collection') |
373
|
|
|
->addTask($collection) |
374
|
|
|
->progressMessage('Done with recursive collection') |
375
|
|
|
->taskExec('ls xyzzy' . date('U')) |
376
|
|
|
->dir('/tmp') |
377
|
|
|
->run(); |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
public function tryWorkdir() |
381
|
|
|
{ |
382
|
|
|
// This example works like tryBuilderRollback, |
383
|
|
|
// but does equivalent operations using a working |
384
|
|
|
// directory. The working directory is deleted on rollback |
385
|
|
|
$collection = $this->collectionBuilder(); |
386
|
|
|
|
387
|
|
|
$workdir = $collection->workDir('w'); |
388
|
|
|
|
389
|
|
|
$collection |
|
|
|
|
390
|
|
|
->taskFilesystemStack() |
391
|
|
|
->touch("$workdir/g.txt") |
392
|
|
|
->taskFilesystemStack() |
393
|
|
|
->mkdir("$workdir/h") |
394
|
|
|
->touch("$workdir/h/h.txt") |
395
|
|
|
->taskFilesystemStack() |
396
|
|
|
->mkdir("$workdir/h/i/c") |
397
|
|
|
->touch("$workdir/h/i/i.txt"); |
398
|
|
|
|
399
|
|
|
return $this->collectionBuilder() |
|
|
|
|
400
|
|
|
->progressMessage('Start recursive collection') |
401
|
|
|
->addTask($collection) |
402
|
|
|
->progressMessage('Done with recursive collection') |
403
|
|
|
->taskExec('ls xyzzy' . date('U')) |
404
|
|
|
->dir('/tmp') |
405
|
|
|
->run(); |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
/** |
409
|
|
|
* Demonstrates Robo temporary directory usage. |
410
|
|
|
*/ |
411
|
|
|
public function tryTmpDir() |
412
|
|
|
{ |
413
|
|
|
// Set up a collection to add tasks to |
414
|
|
|
$collection = $this->collectionBuilder(); |
415
|
|
|
|
416
|
|
|
// Get a temporary directory to work in. Note that we get a path |
417
|
|
|
// back, but the directory is not created until the task runs. |
418
|
|
|
$tmpPath = $collection->tmpDir(); |
419
|
|
|
|
420
|
|
|
$result = $collection |
|
|
|
|
421
|
|
|
->taskWriteToFile("$tmpPath/file.txt") |
422
|
|
|
->line('Example file') |
423
|
|
|
->run(); |
424
|
|
|
|
425
|
|
|
if (is_dir($tmpPath)) { |
426
|
|
|
$this->say("The temporary directory at $tmpPath was not cleaned up after the collection completed."); |
427
|
|
|
} else { |
428
|
|
|
$this->say("The temporary directory at $tmpPath was automatically deleted."); |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
return $result; |
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
/** |
435
|
|
|
* Description |
436
|
|
|
* @param $options |
437
|
|
|
* @option delay Miliseconds delay |
438
|
|
|
* @return type |
439
|
|
|
*/ |
440
|
|
|
public function tryProgress($options = ['delay' => 500]) |
441
|
|
|
{ |
442
|
|
|
$delay = $options['delay']; |
443
|
|
|
$delayUntilProgressStart = \Robo\Robo::config()->get(\Robo\Config::PROGRESS_BAR_AUTO_DISPLAY_INTERVAL); |
444
|
|
|
$this->say("Progress bar will display after $delayUntilProgressStart seconds of activity."); |
445
|
|
|
|
446
|
|
|
$processList = range(1, 10); |
447
|
|
|
return $this->collectionBuilder() |
|
|
|
|
448
|
|
|
->taskForEach($processList) |
449
|
|
|
->iterationMessage('Processing {value}') |
450
|
|
|
->call( |
451
|
|
|
function ($value) use($delay) { |
|
|
|
|
452
|
|
|
// TaskForEach::call should only be used to do |
453
|
|
|
// non-Robo operations. To use Robo tasks in an |
454
|
|
|
// iterator, @see TaskForEach::withBuilder. |
455
|
|
|
usleep($delay * 1000); // delay units: msec, usleep units: usec |
456
|
|
|
} |
457
|
|
|
) |
458
|
|
|
->run(); |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
public function tryIter() |
462
|
|
|
{ |
463
|
|
|
$workdir = 'build/iter-example'; |
464
|
|
|
$this->say("Creating sample direcories in $workdir."); |
465
|
|
|
|
466
|
|
|
$processList = ['cats', 'dogs', 'sheep', 'fish', 'horses', 'cows']; |
467
|
|
|
return $this->collectionBuilder() |
|
|
|
|
468
|
|
|
->taskFilesystemStack() |
469
|
|
|
->mkdir($workdir) |
470
|
|
|
->taskCleanDir($workdir) |
471
|
|
|
->taskForEach($processList) |
472
|
|
|
->withBuilder( |
473
|
|
|
function ($builder, $key, $value) use ($workdir) { |
474
|
|
|
return $builder |
475
|
|
|
->taskFilesystemStack() |
476
|
|
|
->mkdir("$workdir/$value"); |
477
|
|
|
} |
478
|
|
|
) |
479
|
|
|
->run(); |
480
|
|
|
} |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
class ExceptionTask extends \Robo\Task\BaseTask |
|
|
|
|
484
|
|
|
{ |
485
|
|
|
protected $message; |
486
|
|
|
|
487
|
|
|
public function __construct($message) |
488
|
|
|
{ |
489
|
|
|
$this->message = $message; |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
public function run() |
493
|
|
|
{ |
494
|
|
|
throw new RuntimeException($this->message); |
495
|
|
|
} |
496
|
|
|
} |
497
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.