Differential D14873 Diff 35957 src/applications/lipsum/management/PhabricatorLipsumGenerateWorkflow.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/lipsum/management/PhabricatorLipsumGenerateWorkflow.php
| <?php | <?php | ||||
| final class PhabricatorLipsumGenerateWorkflow | final class PhabricatorLipsumGenerateWorkflow | ||||
| extends PhabricatorLipsumManagementWorkflow { | extends PhabricatorLipsumManagementWorkflow { | ||||
| protected function didConstruct() { | protected function didConstruct() { | ||||
| $this | $this | ||||
| ->setName('generate') | ->setName('generate') | ||||
| ->setExamples('**generate**') | ->setExamples('**generate**') | ||||
| ->setSynopsis(pht('Generate some lipsum.')) | ->setSynopsis(pht('Generate synthetic test objects.')) | ||||
| ->setArguments( | ->setArguments( | ||||
| array( | array( | ||||
| array( | array( | ||||
| 'name' => 'args', | 'name' => 'args', | ||||
| 'wildcard' => true, | 'wildcard' => true, | ||||
| ), | ), | ||||
| )); | )); | ||||
| } | } | ||||
| public function execute(PhutilArgumentParser $args) { | public function execute(PhutilArgumentParser $args) { | ||||
| $console = PhutilConsole::getConsole(); | $config_key = 'phabricator.developer-mode'; | ||||
| if (!PhabricatorEnv::getEnvConfig($config_key)) { | |||||
| throw new PhutilArgumentUsageException( | |||||
| pht( | |||||
| 'lipsum is a development and testing tool and may only be run '. | |||||
| 'on installs in developer mode. Enable "%s" in your configuration '. | |||||
| 'to enable lipsum.', | |||||
| $config_key)); | |||||
| } | |||||
| $supported_types = id(new PhutilClassMapQuery()) | $all_generators = id(new PhutilClassMapQuery()) | ||||
| ->setAncestorClass('PhabricatorTestDataGenerator') | ->setAncestorClass('PhabricatorTestDataGenerator') | ||||
| ->execute(); | ->execute(); | ||||
| $console->writeOut( | $argv = $args->getArg('args'); | ||||
| "%s:\n\t%s\n", | $all = 'all'; | ||||
| pht('These are the types of data you can generate'), | |||||
| implode("\n\t", array_keys($supported_types))); | |||||
| $prompt = pht('Are you sure you want to generate lots of test data?'); | if (!$argv) { | ||||
| if (!phutil_console_confirm($prompt, true)) { | $names = mpull($all_generators, 'getGeneratorName'); | ||||
| return; | sort($names); | ||||
| } | |||||
| $list = id(new PhutilConsoleList()) | |||||
| ->setWrap(false) | |||||
| ->addItems($names); | |||||
| $argv = $args->getArg('args'); | id(new PhutilConsoleBlock()) | ||||
| if (count($argv) == 0 || (count($argv) == 1 && $argv[0] == 'all')) { | ->addParagraph( | ||||
| $this->infinitelyGenerate($supported_types); | |||||
| } else { | |||||
| $new_supported_types = array(); | |||||
| for ($i = 0; $i < count($argv); $i++) { | |||||
| $arg = $argv[$i]; | |||||
| if (array_key_exists($arg, $supported_types)) { | |||||
| $new_supported_types[$arg] = $supported_types[$arg]; | |||||
| } else { | |||||
| $console->writeErr( | |||||
| "%s\n", | |||||
| pht( | pht( | ||||
| 'The type %s is not supported by the lipsum generator.', | 'Choose which type or types of test data you want to generate, '. | ||||
| $arg)); | 'or select "%s".', | ||||
| $all)) | |||||
| ->addList($list) | |||||
| ->draw(); | |||||
| return 0; | |||||
| } | } | ||||
| $generators = array(); | |||||
| foreach ($argv as $arg_original) { | |||||
| $arg = phutil_utf8_strtolower($arg_original); | |||||
| $match = false; | |||||
| foreach ($all_generators as $generator) { | |||||
| $name = phutil_utf8_strtolower($generator->getGeneratorName()); | |||||
| if ($arg == $all) { | |||||
| $generators[] = $generator; | |||||
| $match = true; | |||||
| break; | |||||
| } | |||||
| if (strpos($name, $arg) !== false) { | |||||
| $generators[] = $generator; | |||||
| $match = true; | |||||
| break; | |||||
| } | } | ||||
| $this->infinitelyGenerate($new_supported_types); | |||||
| } | } | ||||
| $console->writeOut( | if (!$match) { | ||||
| "%s\n%s:\n%s\n", | throw new PhutilArgumentUsageException( | ||||
| pht('None of the input types were supported.'), | pht( | ||||
| pht('The supported types are'), | 'Argument "%s" does not match the name of any generators.', | ||||
| implode("\n", array_keys($supported_types))); | $arg_original)); | ||||
| } | } | ||||
| } | |||||
| echo tsprintf( | |||||
| "**<bg:blue> %s </bg>** %s\n", | |||||
| pht('GENERATORS'), | |||||
| pht( | |||||
| 'Selected generators: %s.', | |||||
| implode(', ', mpull($generators, 'getGeneratorName')))); | |||||
| protected function infinitelyGenerate(array $supported_types) { | echo tsprintf( | ||||
| $console = PhutilConsole::getConsole(); | "**<bg:yellow> %s </bg>** %s\n", | ||||
| pht('WARNING'), | |||||
| pht( | |||||
| 'This command generates synthetic test data, including user '. | |||||
| 'accounts. It is intended for use in development environments '. | |||||
| 'so you can test features more easily. There is no easy way to '. | |||||
| 'delete this data or undo the effects of this command. If you run '. | |||||
| 'it in a production environment, it will pollute your data with '. | |||||
| 'large amounts of meaningless garbage that you can not get rid of.')); | |||||
| if (count($supported_types) == 0) { | $prompt = pht('Are you sure you want to generate piles of garbage?'); | ||||
| if (!phutil_console_confirm($prompt, true)) { | |||||
| return; | return; | ||||
| } | } | ||||
| $console->writeOut( | |||||
| "%s: %s\n", | echo tsprintf( | ||||
| pht('GENERATING'), | "**<bg:green> %s </bg>** %s\n", | ||||
| implode(', ', array_keys($supported_types))); | pht('LIPSUM'), | ||||
| pht( | |||||
| 'Generating synthetic test objects forever. '. | |||||
| 'Use ^C to stop when satisfied.')); | |||||
| $this->generate($generators); | |||||
| } | |||||
| protected function generate(array $generators) { | |||||
| $viewer = $this->getViewer(); | |||||
| while (true) { | while (true) { | ||||
| $type = $supported_types[array_rand($supported_types)]; | $generator = $generators[array_rand($generators)]; | ||||
| $admin = $this->getViewer(); | |||||
| $object = $generator->generateObject(); | |||||
| $object_phid = $object->getPHID(); | |||||
| $taskgen = newv($type, array()); | $handles = $viewer->loadHandles(array($object_phid)); | ||||
| $object = $taskgen->generate(); | |||||
| $handle = id(new PhabricatorHandleQuery()) | echo tsprintf( | ||||
| ->setViewer($admin) | "%s\n", | ||||
| ->withPHIDs(array($object->getPHID())) | pht( | ||||
| ->executeOne(); | 'Generated "%s": %s', | ||||
| $handles[$object_phid]->getTypeName(), | |||||
| $console->writeOut( | $handles[$object_phid]->getFullName())); | ||||
| "%s: %s\n", | |||||
| pht('Generated %s', $handle->getTypeName()), | |||||
| $handle->getFullName()); | |||||
| usleep(200000); | sleep(1); | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||