These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 | * Demonstrate Robo configuration. |
||
67 | * |
||
68 | * Config values are loaded from the followig locations: |
||
69 | * |
||
70 | * - [Robo Project]/robo.yml |
||
71 | * - $HOME/.robo/robo.yml |
||
72 | * - $CWD/robo.yml |
||
73 | * - Environment variables ROBO_CONFIG_KEY (e.g. ROBO_OPTIONS_PROGRESS_DELAY) |
||
74 | * - Overridden on the commandline via -Doptions.progress-delay=value |
||
75 | * |
||
76 | * @param string $key Name of the option to read (e.g. options.progress-delay) |
||
77 | * @option opt An option whose value is printed. Can be overridden in |
||
78 | * configuration via the configuration key command.try.config.options.opt. |
||
79 | * @option show-all Also print out the value of all configuration options |
||
80 | */ |
||
81 | public function tryConfig($key = 'options.progress-delay', $options = ['opt' => '0', 'show-all' => false]) |
||
82 | { |
||
83 | $value = \Robo\Robo::config()->get($key); |
||
84 | |||
85 | $this->say("The value of $key is " . var_export($value, true)); |
||
86 | $this->say("The value of --opt (command.try.config.options.opt) is " . var_export($options['opt'], true)); |
||
87 | |||
88 | if ($options['show-all']) { |
||
89 | $this->say(var_export(\Robo\Robo::config()->export(), true) . "\n"); |
||
90 | } |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Demonstrates serial execution. |
||
95 | * |
||
96 | * @option $printed Print the output of each process. |
||
97 | * @option $error Include an extra process that fails. |
||
98 | */ |
||
99 | public function tryExec($options = ['printed' => true, 'error' => false]) |
||
100 | { |
||
101 | $dir = dirname(dirname(dirname(dirname(dirname(__DIR__))))); |
||
102 | $tasks = $this |
||
103 | ->taskExec('php') |
||
104 | ->args(["$dir/tests/_data/parascript.php", "hey", "4"]) |
||
105 | ->taskExec('php') |
||
106 | ->args(["$dir/tests/_data/parascript.php", "hoy", "3"]) |
||
107 | ->taskExec('php') |
||
108 | ->args(["$dir/tests/_data/parascript.php", "gou", "2"]) |
||
109 | ->taskExec('php') |
||
110 | ->args(["$dir/tests/_data/parascript.php", "die", "1"]); |
||
111 | if ($options['error']) { |
||
112 | $tasks->taskExec('ls')->arg("$dir/tests/_data/filenotfound"); |
||
113 | } |
||
114 | return $tasks->run(); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Demonstrates parallel execution. |
||
119 | * |
||
120 | * @option $printed Print the output of each process. |
||
121 | * @option $error Include an extra process that fails. |
||
122 | */ |
||
123 | public function tryPara($options = ['printed' => true, 'error' => false]) |
||
124 | { |
||
125 | $dir = dirname(dirname(dirname(dirname(dirname(__DIR__))))); |
||
126 | $para = $this->taskParallelExec() |
||
127 | ->printed($options['printed']) |
||
128 | ->process("php $dir/tests/_data/parascript.php hey 4") |
||
129 | ->process("php $dir/tests/_data/parascript.php hoy 3") |
||
130 | ->process("php $dir/tests/_data/parascript.php gou 2") |
||
131 | ->process("php $dir/tests/_data/parascript.php die 1"); |
||
132 | if ($options['error']) { |
||
133 | $para->process("ls $dir/tests/_data/filenotfound"); |
||
134 | } |
||
135 | return $para->run(); |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * try:opt-required |
||
140 | */ |
||
141 | public function tryOptRequired($options = ['foo' => InputOption::VALUE_REQUIRED]) |
||
142 | { |
||
143 | print "foo is " . $options['foo']; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Demonstrates Robo argument passing. |
||
148 | * |
||
149 | * @param string $a The first parameter. Required. |
||
150 | * @param string $b The second parameter. Optional. |
||
151 | */ |
||
152 | public function tryArgs($a, $b = 'default') |
||
153 | { |
||
154 | $this->say("The parameter a is $a and b is $b"); |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Demonstrate Robo variable argument passing. |
||
159 | * |
||
160 | * @param $a A list of commandline parameters. |
||
161 | */ |
||
162 | public function tryArrayArgs(array $a) |
||
163 | { |
||
164 | $this->say("The parameters passed are:\n" . var_export($a, true)); |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Demonstrate Robo boolean options. |
||
169 | * |
||
170 | * @param $opts The options. |
||
171 | * @option boolean $silent Supress output. |
||
172 | */ |
||
173 | public function tryOptbool($opts = ['silent|s' => false]) |
||
174 | { |
||
175 | if (!$opts['silent']) { |
||
176 | $this->say("Hello, world"); |
||
177 | } |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Demonstrate the use of the PHP built-in webserver. |
||
182 | */ |
||
183 | public function tryServer() |
||
184 | { |
||
185 | return $this->taskServer(8000) |
||
186 | ->dir('site') |
||
187 | ->arg('site/index.php') |
||
188 | ->run(); |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Demonstrate the use of the Robo open-browser task. |
||
193 | */ |
||
194 | public function tryOpenBrowser() |
||
195 | { |
||
196 | return $this->taskOpenBrowser([ |
||
197 | 'http://robo.li', |
||
198 | 'https://github.com/consolidation-org/Robo' |
||
199 | ])->run(); |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Demonstrate Robo error output and command failure. |
||
204 | */ |
||
205 | public function tryError() |
||
206 | { |
||
207 | return $this->taskExec('ls xyzzy' . date('U'))->dir('/tmp')->run(); |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * Demonstrate Robo standard output and command success. |
||
212 | */ |
||
213 | public function trySuccess() |
||
214 | { |
||
215 | return $this->_exec('pwd'); |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * @field-labels |
||
220 | * name: Name |
||
221 | * species: Species |
||
222 | * legs: Legs |
||
223 | * food: Favorite Food |
||
224 | * id: Id |
||
225 | * @return PropertyList |
||
226 | */ |
||
227 | public function tryInfo() |
||
228 | { |
||
229 | $outputData = [ |
||
230 | 'name' => 'fluffy', |
||
231 | 'species' => 'cat', |
||
232 | 'legs' => 4, |
||
233 | 'food' => 'salmon', |
||
234 | 'id' => 389245032, |
||
235 | ]; |
||
236 | |||
237 | $data = new PropertyList($outputData); |
||
238 | |||
239 | // Add a render function to transform cell data when the output |
||
240 | // format is a table, or similar. This allows us to add color |
||
241 | // information to the output without modifying the data cells when |
||
242 | // using yaml or json output formats. |
||
243 | $data->addRendererFunction( |
||
244 | // n.b. There is a fourth parameter $rowData that may be added here. |
||
245 | function ($key, $cellData, FormatterOptions $options) { |
||
0 ignored issues
–
show
|
|||
246 | if ($key == 'name') { |
||
247 | return "<info>$cellData</>"; |
||
248 | } |
||
249 | return $cellData; |
||
250 | } |
||
251 | ); |
||
252 | |||
253 | return $data; |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Demonstrate Robo formatters. Default format is 'table'. |
||
258 | * |
||
259 | * @field-labels |
||
260 | * first: I |
||
261 | * second: II |
||
262 | * third: III |
||
263 | * @default-string-field second |
||
264 | * @usage try:formatters --format=yaml |
||
265 | * @usage try:formatters --format=csv |
||
266 | * @usage try:formatters --fields=first,third |
||
267 | * @usage try:formatters --fields=III,II |
||
268 | * @aliases tf |
||
269 | * |
||
270 | * @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields |
||
271 | */ |
||
272 | public function tryFormatters($somthing = 'default', $options = ['format' => 'table', 'fields' => '']) |
||
0 ignored issues
–
show
|
|||
273 | { |
||
274 | $outputData = [ |
||
275 | 'en' => [ 'first' => 'One', 'second' => 'Two', 'third' => 'Three' ], |
||
276 | 'de' => [ 'first' => 'Eins', 'second' => 'Zwei', 'third' => 'Drei' ], |
||
277 | 'jp' => [ 'first' => 'Ichi', 'second' => 'Ni', 'third' => 'San' ], |
||
278 | 'es' => [ 'first' => 'Uno', 'second' => 'Dos', 'third' => 'Tres' ], |
||
279 | ]; |
||
280 | return new RowsOfFields($outputData); |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * Try word wrapping |
||
285 | * |
||
286 | * @field-labels |
||
287 | * first: First |
||
288 | * second: Second |
||
289 | * |
||
290 | * @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields |
||
291 | */ |
||
292 | public function tryWrap() |
||
293 | { |
||
294 | $data = [ |
||
295 | [ |
||
296 | 'first' => 'This is a really long cell that contains a lot of data. When it is rendered, it should be wrapped across multiple lines.', |
||
297 | '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.', |
||
298 | ] |
||
299 | ]; |
||
300 | return new RowsOfFields($data); |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * Demonstrate an alter hook with an option |
||
305 | * |
||
306 | * @hook alter try:formatters |
||
307 | * @option $french Add a row with French numbers. |
||
308 | * @usage try:formatters --french |
||
309 | */ |
||
310 | public function alterFormatters($result, CommandData $commandData) |
||
311 | { |
||
312 | if ($commandData->input()->getOption('french')) { |
||
313 | $result['fr'] = [ 'first' => 'Un', 'second' => 'Deux', 'third' => 'Trois' ]; |
||
314 | } |
||
315 | |||
316 | return $result; |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * Demonstrate what happens when a command or a task |
||
321 | * throws an exception. Note that typically, Robo commands |
||
322 | * should return Result objects rather than throw exceptions. |
||
323 | */ |
||
324 | public function tryException($options = ['task' => false]) |
||
325 | { |
||
326 | if (!$options['task']) { |
||
327 | throw new RuntimeException('Command failed with an exception.'); |
||
328 | } |
||
329 | return new ExceptionTask('Task failed with an exception.'); |
||
330 | } |
||
331 | |||
332 | /** |
||
333 | * Demonstrate deprecated task behavior. |
||
334 | * |
||
335 | * Demonstrate what happens when using a task that is created via |
||
336 | * direct instantiation, which omits initialization done by the |
||
337 | * container. Emits a warning message. |
||
338 | */ |
||
339 | public function tryDeprecated() |
||
340 | { |
||
341 | // Calling 'new' directly without manually setting |
||
342 | // up dependencies will result in a deprecation warning. |
||
343 | // @see RoboFile::trySuccess() |
||
344 | return (new \Robo\Task\Base\Exec('pwd'))->run(); |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | * Demonstrate the use of a collection builder to chain multiple tasks |
||
349 | * together into a collection, which is executed once constructed. |
||
350 | * |
||
351 | * For demonstration purposes only; this could, of course, be done |
||
352 | * with a single FilesystemStack. |
||
353 | */ |
||
354 | public function tryBuilder() |
||
355 | { |
||
356 | return $this->collectionBuilder() |
||
357 | ->taskFilesystemStack() |
||
358 | ->mkdir('a') |
||
359 | ->touch('a/a.txt') |
||
360 | ->taskFilesystemStack() |
||
361 | ->mkdir('a/b') |
||
362 | ->touch('a/b/b.txt') |
||
363 | ->taskFilesystemStack() |
||
364 | ->mkdir('a/b/c') |
||
365 | ->touch('a/b/c/c.txt') |
||
366 | ->run(); |
||
367 | } |
||
368 | |||
369 | public function tryState() |
||
370 | { |
||
371 | return $this->collectionBuilder() |
||
372 | ->taskExec('uname -n') |
||
373 | ->printOutput(false) |
||
374 | ->storeState('system-name') |
||
375 | ->taskFilesystemStack() |
||
376 | ->deferTaskConfiguration('mkdir', 'system-name') |
||
377 | ->run(); |
||
378 | } |
||
379 | |||
380 | public function tryBuilderRollback() |
||
381 | { |
||
382 | // This example will create two builders, and add |
||
383 | // the first one as a child of the second in order |
||
384 | // to demonstrate nested rollbacks. |
||
385 | $collection = $this->collectionBuilder() |
||
386 | ->taskFilesystemStack() |
||
387 | ->mkdir('g') |
||
388 | ->touch('g/g.txt') |
||
389 | ->rollback( |
||
390 | $this->taskDeleteDir('g') |
||
391 | ) |
||
392 | ->taskFilesystemStack() |
||
393 | ->mkdir('g/h') |
||
394 | ->touch('g/h/h.txt') |
||
395 | ->taskFilesystemStack() |
||
396 | ->mkdir('g/h/i/c') |
||
397 | ->touch('g/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 | public function tryWorkdir() |
||
409 | { |
||
410 | // This example works like tryBuilderRollback, |
||
411 | // but does equivalent operations using a working |
||
412 | // directory. The working directory is deleted on rollback |
||
413 | $collection = $this->collectionBuilder(); |
||
414 | |||
415 | $workdir = $collection->workDir('w'); |
||
416 | |||
417 | $collection |
||
418 | ->taskFilesystemStack() |
||
419 | ->touch("$workdir/g.txt") |
||
420 | ->taskFilesystemStack() |
||
421 | ->mkdir("$workdir/h") |
||
422 | ->touch("$workdir/h/h.txt") |
||
423 | ->taskFilesystemStack() |
||
424 | ->mkdir("$workdir/h/i/c") |
||
425 | ->touch("$workdir/h/i/i.txt"); |
||
426 | |||
427 | return $this->collectionBuilder() |
||
428 | ->progressMessage('Start recursive collection') |
||
429 | ->addTask($collection) |
||
430 | ->progressMessage('Done with recursive collection') |
||
431 | ->taskExec('ls xyzzy' . date('U')) |
||
432 | ->dir('/tmp') |
||
433 | ->run(); |
||
434 | } |
||
435 | |||
436 | /** |
||
437 | * Demonstrates Robo temporary directory usage. |
||
438 | */ |
||
439 | public function tryTmpDir() |
||
440 | { |
||
441 | // Set up a collection to add tasks to |
||
442 | $collection = $this->collectionBuilder(); |
||
443 | |||
444 | // Get a temporary directory to work in. Note that we get a path |
||
445 | // back, but the directory is not created until the task runs. |
||
446 | $tmpPath = $collection->tmpDir(); |
||
447 | |||
448 | $result = $collection |
||
449 | ->taskWriteToFile("$tmpPath/file.txt") |
||
450 | ->line('Example file') |
||
451 | ->run(); |
||
452 | |||
453 | if (is_dir($tmpPath)) { |
||
454 | $this->say("The temporary directory at $tmpPath was not cleaned up after the collection completed."); |
||
455 | } else { |
||
456 | $this->say("The temporary directory at $tmpPath was automatically deleted."); |
||
457 | } |
||
458 | |||
459 | return $result; |
||
460 | } |
||
461 | |||
462 | /** |
||
463 | * Description |
||
464 | * @param $options |
||
465 | * @option delay Miliseconds delay |
||
466 | * @return type |
||
467 | */ |
||
468 | public function tryProgress($options = ['delay' => 500]) |
||
469 | { |
||
470 | $delay = $options['delay']; |
||
471 | $delayUntilProgressStart = \Robo\Robo::config()->get(\Robo\Config::PROGRESS_BAR_AUTO_DISPLAY_INTERVAL); |
||
472 | $this->say("Progress bar will display after $delayUntilProgressStart seconds of activity."); |
||
473 | |||
474 | $processList = range(1, 10); |
||
475 | return $this->collectionBuilder() |
||
476 | ->taskForEach($processList) |
||
477 | ->iterationMessage('Processing {value}') |
||
478 | ->call( |
||
479 | function ($value) use($delay) { |
||
0 ignored issues
–
show
|
|||
480 | // TaskForEach::call should only be used to do |
||
481 | // non-Robo operations. To use Robo tasks in an |
||
482 | // iterator, @see TaskForEach::withBuilder. |
||
483 | usleep($delay * 1000); // delay units: msec, usleep units: usec |
||
484 | } |
||
485 | ) |
||
486 | ->run(); |
||
487 | } |
||
488 | |||
489 | public function tryIter() |
||
490 | { |
||
491 | $workdir = 'build/iter-example'; |
||
492 | $this->say("Creating sample direcories in $workdir."); |
||
493 | |||
494 | $processList = ['cats', 'dogs', 'sheep', 'fish', 'horses', 'cows']; |
||
495 | return $this->collectionBuilder() |
||
496 | ->taskFilesystemStack() |
||
497 | ->mkdir($workdir) |
||
498 | ->taskCleanDir($workdir) |
||
499 | ->taskForEach($processList) |
||
500 | ->withBuilder( |
||
501 | function ($builder, $key, $value) use ($workdir) { |
||
502 | return $builder |
||
503 | ->taskFilesystemStack() |
||
504 | ->mkdir("$workdir/$value"); |
||
505 | } |
||
506 | ) |
||
507 | ->run(); |
||
508 | } |
||
509 | } |
||
510 | |||
511 | class ExceptionTask extends \Robo\Task\BaseTask |
||
512 | { |
||
513 | protected $message; |
||
514 | |||
515 | public function __construct($message) |
||
516 | { |
||
517 | $this->message = $message; |
||
518 | } |
||
519 | |||
520 | public function run() |
||
521 | { |
||
522 | throw new RuntimeException($this->message); |
||
523 | } |
||
524 | } |
||
525 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.