|
1
|
|
|
<?php |
|
2
|
|
|
use Robo\Result; |
|
3
|
|
|
use Robo\ResultData; |
|
4
|
|
|
use Robo\Collection\CollectionBuilder; |
|
5
|
|
|
|
|
6
|
|
|
use Consolidation\OutputFormatters\StructuredData\RowsOfFields; |
|
7
|
|
|
use Consolidation\AnnotatedCommand\AnnotationData; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Example RoboFile. |
|
11
|
|
|
* |
|
12
|
|
|
* To test: |
|
13
|
|
|
* |
|
14
|
|
|
* $ cd ROBO_PROJECT/examples |
|
15
|
|
|
* $ ../robo try:success |
|
16
|
|
|
* |
|
17
|
|
|
* - or - |
|
18
|
|
|
* |
|
19
|
|
|
* $ cd ROBO_PROJECT |
|
20
|
|
|
* $ ./robo -f examples try:formatters |
|
21
|
|
|
*/ |
|
22
|
|
|
class RoboFile extends \Robo\Tasks |
|
|
|
|
|
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Watch a file. |
|
26
|
|
|
* |
|
27
|
|
|
* Demonstrates the 'watch' command. Runs 'composer update' any time |
|
28
|
|
|
* composer.json changes. |
|
29
|
|
|
*/ |
|
30
|
|
|
public function tryWatch() |
|
31
|
|
|
{ |
|
32
|
|
|
$this->taskWatch()->monitor(['composer.json', 'composer.lock'], function () { |
|
33
|
|
|
$this->taskComposerUpdate()->run(); |
|
34
|
|
|
})->run(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Demonstrates Robo input APIs. |
|
39
|
|
|
*/ |
|
40
|
|
|
public function tryInput() |
|
41
|
|
|
{ |
|
42
|
|
|
$answer = $this->ask('how are you?'); |
|
43
|
|
|
$this->say('You are '.$answer); |
|
44
|
|
|
$yes = $this->confirm('Do you want one more question?'); |
|
45
|
|
|
if (!$yes) { |
|
46
|
|
|
return Result::cancelled(); |
|
47
|
|
|
} |
|
48
|
|
|
$lang = $this->askDefault('what is your favorite scripting language?', 'PHP'); |
|
49
|
|
|
$this->say($lang); |
|
50
|
|
|
$pin = $this->askHidden('Ok, now tell your PIN code (it is hidden)'); |
|
51
|
|
|
$this->yell('Ha-ha, your pin code is: '.$pin); |
|
52
|
|
|
$this->say('Bye!'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Demonstrates parallel execution. |
|
57
|
|
|
* |
|
58
|
|
|
* @option $printed Print the output of each process. |
|
59
|
|
|
* @option $error Include an extra process that fails. |
|
60
|
|
|
*/ |
|
61
|
|
|
public function tryPara($options = ['printed' => false, 'error' => false]) |
|
62
|
|
|
{ |
|
63
|
|
|
$dir = __DIR__; |
|
64
|
|
|
$para = $this->taskParallelExec() |
|
65
|
|
|
->printed($options['printed']) |
|
66
|
|
|
->process("php $dir/tests/_data/parascript.php hey 4") |
|
67
|
|
|
->process("php $dir/tests/_data/parascript.php hoy 3") |
|
68
|
|
|
->process("php $dir/tests/_data/parascript.php gou 2") |
|
69
|
|
|
->process("php $dir/tests/_data/parascript.php die 1"); |
|
70
|
|
|
if ($options['error']) { |
|
71
|
|
|
$para->process("ls $dir/tests/_data/filenotfound"); |
|
72
|
|
|
} |
|
73
|
|
|
return $para->run(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Demonstrates Robo argument passing. |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $a The first parameter. Required. |
|
80
|
|
|
* @param string $b The second parameter. Optional. |
|
81
|
|
|
*/ |
|
82
|
|
|
public function tryArgs($a, $b = 'default') |
|
83
|
|
|
{ |
|
84
|
|
|
$this->say("The parameter a is $a and b is $b"); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Demonstrate Robo variable argument passing. |
|
89
|
|
|
* |
|
90
|
|
|
* @param $a A list of commandline parameters. |
|
91
|
|
|
*/ |
|
92
|
|
|
public function tryArrayArgs(array $a) |
|
93
|
|
|
{ |
|
94
|
|
|
$this->say("The parameters passed are:\n" . var_export($a, true)); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Demonstrate Robo boolean options. |
|
99
|
|
|
* |
|
100
|
|
|
* @param $opts The options. |
|
101
|
|
|
* @option boolean $silent Supress output. |
|
102
|
|
|
*/ |
|
103
|
|
|
public function tryOptbool($opts = ['silent|s' => false]) |
|
104
|
|
|
{ |
|
105
|
|
|
if (!$opts['silent']) { |
|
106
|
|
|
$this->say("Hello, world"); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Demonstrate the use of the PHP built-in webserver. |
|
112
|
|
|
*/ |
|
113
|
|
|
public function tryServer() |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->taskServer(8000) |
|
116
|
|
|
->dir('site') |
|
117
|
|
|
->arg('site/index.php') |
|
118
|
|
|
->run(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Demonstrate the use of the Robo open-browser task. |
|
123
|
|
|
*/ |
|
124
|
|
|
public function tryOpenBrowser() |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->taskOpenBrowser([ |
|
127
|
|
|
'http://robo.li', |
|
128
|
|
|
'https://github.com/consolidation-org/Robo' |
|
129
|
|
|
])->run(); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Demonstrate Robo error output and command failure. |
|
134
|
|
|
*/ |
|
135
|
|
|
public function tryError() |
|
136
|
|
|
{ |
|
137
|
|
|
return $this->taskExec('ls xyzzy' . date('U'))->dir('/tmp')->run(); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Demonstrate Robo standard output and command success. |
|
142
|
|
|
*/ |
|
143
|
|
|
public function trySuccess() |
|
144
|
|
|
{ |
|
145
|
|
|
return $this->_exec('pwd'); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Demonstrate Robo formatters. Default format is 'table'. |
|
150
|
|
|
* |
|
151
|
|
|
* @field-labels |
|
152
|
|
|
* first: I |
|
153
|
|
|
* second: II |
|
154
|
|
|
* third: III |
|
155
|
|
|
* @default-string-field second |
|
156
|
|
|
* @usage try:formatters --format=yaml |
|
157
|
|
|
* @usage try:formatters --format=csv |
|
158
|
|
|
* @usage try:formatters --fields=first,third |
|
159
|
|
|
* @usage try:formatters --fields=III,II |
|
160
|
|
|
* @aliases tf |
|
161
|
|
|
* |
|
162
|
|
|
* @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields |
|
163
|
|
|
*/ |
|
164
|
|
|
public function tryFormatters($somthing = 'default', $options = ['format' => 'table', 'fields' => '']) |
|
|
|
|
|
|
165
|
|
|
{ |
|
166
|
|
|
$outputData = [ |
|
167
|
|
|
[ 'first' => 'One', 'second' => 'Two', 'third' => 'Three' ], |
|
168
|
|
|
[ 'first' => 'Eins', 'second' => 'Zwei', 'third' => 'Drei' ], |
|
169
|
|
|
[ 'first' => 'Ichi', 'second' => 'Ni', 'third' => 'San' ], |
|
170
|
|
|
[ 'first' => 'Uno', 'second' => 'Dos', 'third' => 'Tres' ], |
|
171
|
|
|
]; |
|
172
|
|
|
return new RowsOfFields($outputData); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Demonstrate an alter hook with an option |
|
177
|
|
|
* |
|
178
|
|
|
* @hook alter try:formatters |
|
179
|
|
|
* @option $french Add a row with French numbers. |
|
180
|
|
|
* @usage try:formatters --french |
|
181
|
|
|
*/ |
|
182
|
|
|
public function alterFormatters($result, array $args, AnnotationData $annotationData) |
|
|
|
|
|
|
183
|
|
|
{ |
|
184
|
|
|
if ($args['options']['french']) { |
|
185
|
|
|
$result[] = [ 'first' => 'Un', 'second' => 'Deux', 'third' => 'Trois' ]; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
return $result; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Demonstrate what happens when a command or a task |
|
193
|
|
|
* throws an exception. Note that typically, Robo commands |
|
194
|
|
|
* should return Result objects rather than throw exceptions. |
|
195
|
|
|
*/ |
|
196
|
|
|
public function tryException($options = ['task' => false]) |
|
197
|
|
|
{ |
|
198
|
|
|
if (!$options['task']) { |
|
199
|
|
|
throw new RuntimeException('Command failed with an exception.'); |
|
200
|
|
|
} |
|
201
|
|
|
return new ExceptionTask('Task failed with an exception.'); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Demonstrate deprecated task behavior. |
|
206
|
|
|
* |
|
207
|
|
|
* Demonstrate what happens when using a task that is created via |
|
208
|
|
|
* direct instantiation, which omits initialization done by the |
|
209
|
|
|
* container. Emits a warning message. |
|
210
|
|
|
*/ |
|
211
|
|
|
public function tryDeprecated() |
|
212
|
|
|
{ |
|
213
|
|
|
// Calling 'new' directly without manually setting |
|
214
|
|
|
// up dependencies will result in a deprecation warning. |
|
215
|
|
|
// @see RoboFile::trySuccess() |
|
|
|
|
|
|
216
|
|
|
return (new \Robo\Task\Base\Exec('pwd'))->run(); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Demonstrate the use of a collection builder to chain multiple tasks |
|
221
|
|
|
* together into a collection, which is executed once constructed. |
|
222
|
|
|
* |
|
223
|
|
|
* For demonstration purposes only; this could, of course, be done |
|
224
|
|
|
* with a single FilesystemStack. |
|
225
|
|
|
*/ |
|
226
|
|
|
public function tryBuilder() |
|
227
|
|
|
{ |
|
228
|
|
|
return $this->collectionBuilder() |
|
229
|
|
|
->taskFilesystemStack() |
|
230
|
|
|
->mkdir('a') |
|
231
|
|
|
->touch('a/a.txt') |
|
232
|
|
|
->taskFilesystemStack() |
|
233
|
|
|
->mkdir('a/b') |
|
234
|
|
|
->touch('a/b/b.txt') |
|
235
|
|
|
->taskFilesystemStack() |
|
236
|
|
|
->mkdir('a/b/c') |
|
237
|
|
|
->touch('a/b/c/c.txt') |
|
238
|
|
|
->run(); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
public function tryBuilderRollback() |
|
242
|
|
|
{ |
|
243
|
|
|
// This example will create two builders, and add |
|
244
|
|
|
// the first one as a child of the second in order |
|
245
|
|
|
// to demonstrate nested rollbacks. |
|
246
|
|
|
$collection = $this->collectionBuilder() |
|
247
|
|
|
->taskFilesystemStack() |
|
248
|
|
|
->mkdir('g') |
|
249
|
|
|
->touch('g/g.txt') |
|
250
|
|
|
->rollback( |
|
251
|
|
|
$this->taskDeleteDir('g') |
|
252
|
|
|
) |
|
253
|
|
|
->taskFilesystemStack() |
|
254
|
|
|
->mkdir('g/h') |
|
255
|
|
|
->touch('g/h/h.txt') |
|
256
|
|
|
->taskFilesystemStack() |
|
257
|
|
|
->mkdir('g/h/i/c') |
|
258
|
|
|
->touch('g/h/i/i.txt'); |
|
259
|
|
|
|
|
260
|
|
|
return $this->collectionBuilder() |
|
261
|
|
|
->progressMessage('Start recursive collection') |
|
262
|
|
|
->addTask($collection) |
|
263
|
|
|
->progressMessage('Done with recursive collection') |
|
264
|
|
|
->taskExec('ls xyzzy' . date('U')) |
|
265
|
|
|
->dir('/tmp') |
|
266
|
|
|
->run(); |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
public function tryWorkdir() |
|
270
|
|
|
{ |
|
271
|
|
|
// This example works like tryBuilderRollback, |
|
272
|
|
|
// but does equivalent operations using a working |
|
273
|
|
|
// directory. The working directory is deleted on rollback |
|
274
|
|
|
$collection = $this->collectionBuilder(); |
|
275
|
|
|
|
|
276
|
|
|
$workdir = $collection->workDir('w'); |
|
277
|
|
|
|
|
278
|
|
|
$collection |
|
279
|
|
|
->taskFilesystemStack() |
|
280
|
|
|
->touch("$workdir/g.txt") |
|
281
|
|
|
->taskFilesystemStack() |
|
282
|
|
|
->mkdir("$workdir/h") |
|
283
|
|
|
->touch("$workdir/h/h.txt") |
|
284
|
|
|
->taskFilesystemStack() |
|
285
|
|
|
->mkdir("$workdir/h/i/c") |
|
286
|
|
|
->touch("$workdir/h/i/i.txt"); |
|
287
|
|
|
|
|
288
|
|
|
return $this->collectionBuilder() |
|
289
|
|
|
->progressMessage('Start recursive collection') |
|
290
|
|
|
->addTask($collection) |
|
291
|
|
|
->progressMessage('Done with recursive collection') |
|
292
|
|
|
->taskExec('ls xyzzy' . date('U')) |
|
293
|
|
|
->dir('/tmp') |
|
294
|
|
|
->run(); |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
/** |
|
298
|
|
|
* Demonstrates Robo temporary directory usage. |
|
299
|
|
|
*/ |
|
300
|
|
|
public function tryTmpDir() |
|
301
|
|
|
{ |
|
302
|
|
|
// Set up a collection to add tasks to |
|
303
|
|
|
$collection = $this->collectionBuilder(); |
|
304
|
|
|
|
|
305
|
|
|
// Get a temporary directory to work in. Note that we get a path |
|
306
|
|
|
// back, but the directory is not created until the task runs. |
|
307
|
|
|
$tmpPath = $collection->tmpDir(); |
|
308
|
|
|
|
|
309
|
|
|
$result = $collection |
|
310
|
|
|
->taskWriteToFile("$tmpPath/file.txt") |
|
311
|
|
|
->line('Example file') |
|
312
|
|
|
->run(); |
|
313
|
|
|
|
|
314
|
|
|
if (is_dir($tmpPath)) { |
|
315
|
|
|
$this->say("The temporary directory at $tmpPath was not cleaned up after the collection completed."); |
|
316
|
|
|
} else { |
|
317
|
|
|
$this->say("The temporary directory at $tmpPath was automatically deleted."); |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
return $result; |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
/** |
|
324
|
|
|
* Description |
|
325
|
|
|
* @param $options |
|
326
|
|
|
* @option delay Miliseconds delay |
|
327
|
|
|
* @return type |
|
328
|
|
|
*/ |
|
329
|
|
|
public function tryProgress($options = ['delay' => 500]) |
|
330
|
|
|
{ |
|
331
|
|
|
$delay = $options['delay']; |
|
332
|
|
|
$delayUntilProgressStart = \Robo\Robo::config()->get(\Robo\Config::PROGRESS_BAR_AUTO_DISPLAY_INTERVAL); |
|
333
|
|
|
$this->say("Progress bar will display after $delayUntilProgressStart seconds of activity."); |
|
334
|
|
|
|
|
335
|
|
|
$processList = range(1, 10); |
|
336
|
|
|
return $this->collectionBuilder() |
|
337
|
|
|
->taskForEach($processList) |
|
338
|
|
|
->iterationMessage('Processing {value}') |
|
339
|
|
|
->call( |
|
340
|
|
|
function ($value) use($delay) { |
|
|
|
|
|
|
341
|
|
|
// TaskForEach::call should only be used to do |
|
342
|
|
|
// non-Robo operations. To use Robo tasks in an |
|
343
|
|
|
// iterator, @see TaskForEach::withBuilder. |
|
344
|
|
|
usleep($delay * 1000); // delay units: msec, usleep units: usec |
|
345
|
|
|
} |
|
346
|
|
|
) |
|
347
|
|
|
->run(); |
|
348
|
|
|
} |
|
349
|
|
|
|
|
350
|
|
|
public function tryIter() |
|
351
|
|
|
{ |
|
352
|
|
|
$workdir = 'build/iter-example'; |
|
353
|
|
|
$this->say("Creating sample direcories in $workdir."); |
|
354
|
|
|
|
|
355
|
|
|
$processList = ['cats', 'dogs', 'sheep', 'fish', 'horses', 'cows']; |
|
356
|
|
|
return $this->collectionBuilder() |
|
357
|
|
|
->taskFilesystemStack() |
|
358
|
|
|
->mkdir($workdir) |
|
359
|
|
|
->taskCleanDir($workdir) |
|
360
|
|
|
->taskForEach($processList) |
|
361
|
|
|
->withBuilder( |
|
362
|
|
|
function ($builder, $key, $value) use ($workdir) { |
|
363
|
|
|
return $builder |
|
364
|
|
|
->taskFilesystemStack() |
|
365
|
|
|
->mkdir("$workdir/$value"); |
|
366
|
|
|
} |
|
367
|
|
|
) |
|
368
|
|
|
->run(); |
|
369
|
|
|
} |
|
370
|
|
|
} |
|
371
|
|
|
|
|
372
|
|
|
class ExceptionTask extends \Robo\Task\BaseTask |
|
|
|
|
|
|
373
|
|
|
{ |
|
374
|
|
|
protected $message; |
|
375
|
|
|
|
|
376
|
|
|
public function __construct($message) |
|
377
|
|
|
{ |
|
378
|
|
|
$this->message = $message; |
|
379
|
|
|
} |
|
380
|
|
|
|
|
381
|
|
|
public function run() |
|
382
|
|
|
{ |
|
383
|
|
|
throw new RuntimeException($this->message); |
|
384
|
|
|
} |
|
385
|
|
|
} |
|
386
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.