Differential D17421 Diff 41896 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 synthetic test objects.')) | ->setSynopsis(pht('Generate synthetic test objects.')) | ||||
->setArguments( | ->setArguments( | ||||
array( | array( | ||||
array( | array( | ||||
'name' => 'force', | |||||
'short' => 'f', | |||||
'help' => pht( | |||||
'Generate objects without prompting for confirmation.'), | |||||
), | |||||
array( | |||||
'name' => 'quickly', | |||||
'help' => pht( | |||||
'Generate objects as quickly as possible.'), | |||||
), | |||||
array( | |||||
'name' => 'args', | 'name' => 'args', | ||||
'wildcard' => true, | 'wildcard' => true, | ||||
), | ), | ||||
)); | )); | ||||
} | } | ||||
public function execute(PhutilArgumentParser $args) { | public function execute(PhutilArgumentParser $args) { | ||||
$config_key = 'phabricator.developer-mode'; | $config_key = 'phabricator.developer-mode'; | ||||
if (!PhabricatorEnv::getEnvConfig($config_key)) { | if (!PhabricatorEnv::getEnvConfig($config_key)) { | ||||
throw new PhutilArgumentUsageException( | throw new PhutilArgumentUsageException( | ||||
pht( | pht( | ||||
'lipsum is a development and testing tool and may only be run '. | 'lipsum is a development and testing tool and may only be run '. | ||||
'on installs in developer mode. Enable "%s" in your configuration '. | 'on installs in developer mode. Enable "%s" in your configuration '. | ||||
'to enable lipsum.', | 'to enable lipsum.', | ||||
$config_key)); | $config_key)); | ||||
} | } | ||||
$all_generators = id(new PhutilClassMapQuery()) | $all_generators = id(new PhutilClassMapQuery()) | ||||
->setAncestorClass('PhabricatorTestDataGenerator') | ->setAncestorClass('PhabricatorTestDataGenerator') | ||||
->setUniqueMethod('getGeneratorKey') | ->setUniqueMethod('getGeneratorKey') | ||||
->execute(); | ->execute(); | ||||
$argv = $args->getArg('args'); | $argv = $args->getArg('args'); | ||||
$is_force = $args->getArg('force'); | |||||
$is_quickly = $args->getArg('quickly'); | |||||
$all = 'all'; | $all = 'all'; | ||||
if (isset($all_generators[$all])) { | if (isset($all_generators[$all])) { | ||||
throw new Exception( | throw new Exception( | ||||
pht( | pht( | ||||
'A lipsum generator is registered with key "%s". This key is '. | 'A lipsum generator is registered with key "%s". This key is '. | ||||
'reserved.', | 'reserved.', | ||||
$all)); | $all)); | ||||
▲ Show 20 Lines • Show All 75 Lines • ▼ Show 20 Lines | public function execute(PhutilArgumentParser $args) { | ||||
echo tsprintf( | echo tsprintf( | ||||
"**<bg:blue> %s </bg>** %s\n", | "**<bg:blue> %s </bg>** %s\n", | ||||
pht('GENERATORS'), | pht('GENERATORS'), | ||||
pht( | pht( | ||||
'Selected generators: %s.', | 'Selected generators: %s.', | ||||
implode(', ', mpull($generators, 'getGeneratorName')))); | implode(', ', mpull($generators, 'getGeneratorName')))); | ||||
if (!$is_force) { | |||||
echo tsprintf( | echo tsprintf( | ||||
"**<bg:yellow> %s </bg>** %s\n", | "**<bg:yellow> %s </bg>** %s\n", | ||||
pht('WARNING'), | pht('WARNING'), | ||||
pht( | pht( | ||||
'This command generates synthetic test data, including user '. | 'This command generates synthetic test data, including user '. | ||||
'accounts. It is intended for use in development environments '. | 'accounts. It is intended for use in development environments so '. | ||||
'so you can test features more easily. There is no easy way to '. | 'you can test features more easily. There is no easy way to delete '. | ||||
'delete this data or undo the effects of this command. If you run '. | 'this data or undo the effects of this command. If you run it in a '. | ||||
'it in a production environment, it will pollute your data with '. | 'production environment, it will pollute your data with large '. | ||||
'large amounts of meaningless garbage that you can not get rid of.')); | 'amounts of meaningless garbage that you can not get rid of.')); | ||||
$prompt = pht('Are you sure you want to generate piles of garbage?'); | $prompt = pht('Are you sure you want to generate piles of garbage?'); | ||||
if (!phutil_console_confirm($prompt, true)) { | if (!phutil_console_confirm($prompt, true)) { | ||||
return; | return; | ||||
} | } | ||||
} | |||||
echo tsprintf( | echo tsprintf( | ||||
"**<bg:green> %s </bg>** %s\n", | "**<bg:green> %s </bg>** %s\n", | ||||
pht('LIPSUM'), | pht('LIPSUM'), | ||||
pht( | pht( | ||||
'Generating synthetic test objects forever. '. | 'Generating synthetic test objects forever. '. | ||||
'Use ^C to stop when satisfied.')); | 'Use ^C to stop when satisfied.')); | ||||
$this->generate($generators); | $this->generate($generators, $is_quickly); | ||||
} | } | ||||
protected function generate(array $generators) { | protected function generate(array $generators, $is_quickly) { | ||||
$viewer = $this->getViewer(); | $viewer = $this->getViewer(); | ||||
foreach ($generators as $generator) { | foreach ($generators as $generator) { | ||||
$generator->setViewer($this->getViewer()); | $generator->setViewer($this->getViewer()); | ||||
} | } | ||||
while (true) { | while (true) { | ||||
$generator = $generators[array_rand($generators)]; | $generator = $generators[array_rand($generators)]; | ||||
Show All 10 Lines | while (true) { | ||||
echo tsprintf( | echo tsprintf( | ||||
"%B\n", | "%B\n", | ||||
$ex->getMessage()); | $ex->getMessage()); | ||||
continue; | continue; | ||||
} | } | ||||
if (is_string($object)) { | |||||
$object_phid = $object; | |||||
} else { | |||||
$object_phid = $object->getPHID(); | $object_phid = $object->getPHID(); | ||||
} | |||||
$handles = $viewer->loadHandles(array($object_phid)); | $handles = $viewer->loadHandles(array($object_phid)); | ||||
echo tsprintf( | echo tsprintf( | ||||
"%s\n", | "%s\n", | ||||
pht( | pht( | ||||
'Generated "%s": %s', | 'Generated "%s": %s', | ||||
$handles[$object_phid]->getTypeName(), | $handles[$object_phid]->getTypeName(), | ||||
$handles[$object_phid]->getFullName())); | $handles[$object_phid]->getFullName())); | ||||
if (!$is_quickly) { | |||||
sleep(1); | sleep(1); | ||||
} | } | ||||
} | } | ||||
} | |||||
} | } |