From 9e872322a049a7ffb5551ad1f763604df562187a Mon Sep 17 00:00:00 2001 From: Greg Anderson Date: Thu, 16 Nov 2017 11:53:22 -0800 Subject: [PATCH] Fixes #3164: Introduce artwork:show command, et. al. --- examples/Commands/ArtCommands.php | 178 ++++++++++++++++++ examples/Commands/SandwichCommands.php | 112 ----------- examples/Commands/XkcdCommands.php | 2 +- examples/Commands/art-topic.md | 6 + .../Commands/{ => art}/sandwich-nocolor.txt | 0 examples/Commands/{ => art}/sandwich.txt | 0 examples/Commands/sandwich-topic.md | 4 - 7 files changed, 185 insertions(+), 117 deletions(-) create mode 100644 examples/Commands/ArtCommands.php delete mode 100644 examples/Commands/SandwichCommands.php create mode 100644 examples/Commands/art-topic.md rename examples/Commands/{ => art}/sandwich-nocolor.txt (100%) rename examples/Commands/{ => art}/sandwich.txt (100%) delete mode 100644 examples/Commands/sandwich-topic.md diff --git a/examples/Commands/ArtCommands.php b/examples/Commands/ArtCommands.php new file mode 100644 index 0000000000..5731208f4a --- /dev/null +++ b/examples/Commands/ArtCommands.php @@ -0,0 +1,178 @@ +getArt(); + $name = $data[$art]['name']; + $description = $data[$art]['description']; + $path = $data[$art]['path']; + $msg = dt('Okay. Here is {art}: {description}', + ['art' => $name, 'description' => $description] + ); + $this->output()->writeln("\n" . $msg . "\n"); + $this->printFile($path); + } + + /** + * Show a table of information about available art. + * + * @command artwork:list + * @aliases artls + * @field-labels + * name: Name + * description: Description + * path: Path + * @default-fields name,description + * + * @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields + */ + public function listArt($options = ['format' => 'table']) + { + $data = $this->getArt(); + return new RowsOfFields($data); + } + + /** + * Commandfiles may also add topics. These will appear in + * the list of topics when `drush topic` is executed. + * To view the topic below, run `drush --include=/full/path/to/examples topic` + */ + + /** + * Ruminations on the true meaning and philosophy of sandwiches. + * + * @command artwork:explain + * @hidden + * @topic + */ + public function ruminate() + { + self::printFile(__DIR__ . '/art-topic.md'); + } + + /** + * Return the available built-in art. Any Drush commandfile may provide + * more art by implementing a 'drush-art' on-event hook. This on-event + * hook is defined in the 'findArt' method beolw. + * + * @hook on-event drush-art + */ + public function builtInArt() + { + return [ + 'sandwich' => [ + 'name' => 'Sandwich', + 'description' => 'A tasty meal with bread often consumed at lunchtime.', + 'path' => __DIR__ . '/art/sandwich-nocolor.txt', + ], + ]; + } + + /** + * @hook interact artwork:show + */ + public function interact(InputInterface $input, OutputInterface $output, AnnotationData $annotationData) + { + $io = new DrushStyle($input, $output); + + // If the user did not specify any artwork, then prompt for one. + $art = $input->getArgument('art'); + if (empty($art)) { + $data = $this->getArt(); + $selections = $this->convertArtListToKeyValue($data); + $selection = $io->choice('Select art to display', $selections); + $input->setArgument('art', $selection); + } + } + + /** + * @hook validate artwork:show + */ + public function artValidate(CommandData $commandData) + { + $art = $commandData->input()->getArgument('art'); + $data = $this->getArt(); + if (!isset($data[$art])) { + throw new \Exception(dt('I do not have any art called "{name}".', ['name' => $art])); + } + } + + /** + * Get a list of available artwork. Cache result for future fast access. + */ + protected function getArt() + { + if (!isset($this->arts)) { + $this->arts = $this->findArt(); + } + return $this->arts; + } + + /** + * Use custom defined on-event hook 'drush-art' to find available artwork. + */ + protected function findArt() + { + $arts = []; + $handlers = $this->getCustomEventHandlers('drush-art'); + foreach ($handlers as $handler) { + $handlerResult = $handler(); + $arts = array_merge($arts, $handlerResult); + } + return $arts; + } + + /** + * Given a list of artwork, converte to a 'key' => 'Name: Description' array. + * @param array $data + * @return array + */ + protected function convertArtListToKeyValue($data) + { + $result = []; + foreach ($data as $key => $item) { + $result[$key] = $item['name'] . ': ' . $item['description']; + } + return $result; + } +} diff --git a/examples/Commands/SandwichCommands.php b/examples/Commands/SandwichCommands.php deleted file mode 100644 index 3120934a7d..0000000000 --- a/examples/Commands/SandwichCommands.php +++ /dev/null @@ -1,112 +0,0 @@ - NULL]) { - if ($spreads = StringUtils::csvToArray('spreads')) { - $list = implode(' and ', $spreads); - $str_spreads = ' with just a dash of ' . $list; - } - $msg = dt('Okay. Enjoy this !filling sandwich!str_spreads.', - ['!filling' => $filling, '!str_spreads' => $str_spreads] - ); - $this->output()->writeln("\n" . $msg . "\n"); - $this->printFile(__DIR__ . '/sandwich-nocolor.txt'); - } - - /** - * Show a table of information about available spreads. - * - * @command mmas-spreads - * @aliases mspreads - * @field-labels - * name: Name - * description: Description - * available: Num - * taste: Taste - * - * @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields - */ - public function spread($options = ['format' => 'table']) { - $data = [ - 'ketchup' => [ - 'name' => 'Ketchup', - 'description' => 'Some say its a vegetable, but we know its a sweet spread.', - 'available' => '7', - 'taste' => 'sweet', - ], - 'mayonnaise' => [ - 'name' => 'Mayonnaise', - 'description' => 'A nice dairy-free spread.', - 'available' => '12', - 'taste' => 'creamy', - ], - 'mustard' => [ - 'name' => 'Mustard', - 'description' => 'Pardon me, but could you please pass that plastic yellow bottle?', - 'available' => '8', - 'taste' => 'tangy', - ], - 'pickles' => [ - 'name' => 'Pickles', - 'description' => 'A necessary part of any sandwich that does not taste terrible.', - 'available' => '63', - 'taste' => 'tasty', - ], - ]; - return new RowsOfFields($data); - } - - /** - * Commandfiles may also add topics. These will appear in - * the list of topics when `drush topic` is executed. - * To view the topic below, run `drush --include=/full/path/to/examples topic` - */ - - /** - * Ruminations on the true meaning and philosophy of sandwiches. - * - * @command sandwich-exposition - * @hidden - * @topic - */ - public function ruminate() { - self::printFile(__DIR__ . '/sandwich-topic.md'); - } - - /** - * @hook validate make-me-a-sandwich - */ - public function sandwichValidate(CommandData $commandData) { - $name = posix_getpwuid(posix_geteuid()); - if ($name['name'] !== 'root') { - throw new \Exception(dt('What? Make your own sandwich.')); - } - } -} diff --git a/examples/Commands/XkcdCommands.php b/examples/Commands/XkcdCommands.php index b00a79d775..a4729c77f5 100644 --- a/examples/Commands/XkcdCommands.php +++ b/examples/Commands/XkcdCommands.php @@ -26,7 +26,7 @@ class XkcdCommands extends DrushCommands { * Retrieve and display cartoon #123 in eog. * @usage drush xkcd random --image-viewer=firefox * Retrieve and display a random cartoon in Firefox. - * @aliases @xkcd + * @aliases xkcd */ public function fetch($search = NULL, $options = ['image-viewer' => 'open', 'google-custom-search-api-key' => 'AIzaSyDpE01VDNNT73s6CEeJRdSg5jukoG244ek']) { if (empty($search)) { diff --git a/examples/Commands/art-topic.md b/examples/Commands/art-topic.md new file mode 100644 index 0000000000..53e3b00f4e --- /dev/null +++ b/examples/Commands/art-topic.md @@ -0,0 +1,6 @@ +I have discovered a truly marvelous proof that it is impossible to +separate a piece of art into two cubes, or four pieces of art into two +fourth of a piece of art, or in general, any artwork larger than the +second into two like artistic expressions. + +This text file is too narrow to contain it. diff --git a/examples/Commands/sandwich-nocolor.txt b/examples/Commands/art/sandwich-nocolor.txt similarity index 100% rename from examples/Commands/sandwich-nocolor.txt rename to examples/Commands/art/sandwich-nocolor.txt diff --git a/examples/Commands/sandwich.txt b/examples/Commands/art/sandwich.txt similarity index 100% rename from examples/Commands/sandwich.txt rename to examples/Commands/art/sandwich.txt diff --git a/examples/Commands/sandwich-topic.md b/examples/Commands/sandwich-topic.md deleted file mode 100644 index f0e0fa72aa..0000000000 --- a/examples/Commands/sandwich-topic.md +++ /dev/null @@ -1,4 +0,0 @@ -I have discovered a truly marvelous proof that it is impossible to -separate a sandwich into two cubes, or four sandwiches into two -fourth of a sandwich, or in general, any sandwich larger than the -second into two like sandwiches. This text file is too narrow to contain it.