mirror of
https://we.phorge.it/source/phorge.git
synced 2024-11-26 08:42:41 +01:00
Make bin/lipsum generate
hanldle generator keys and arguments more clearly
Summary: Ref T12319. Currently, `bin/lipsum` uses substring matches against human-readable text to chose which objects to generate. Instead: - Use separate selector keys which are guaranteed to be unique. - When a match is exact, select only that generator. - When a match is ambiguous, fail and warn the user. Test Plan: Generated several types of objects, tried to generate ambiguous objects like "e". Reviewers: chad Reviewed By: chad Maniphest Tasks: T12319 Differential Revision: https://secure.phabricator.com/D17420
This commit is contained in:
parent
1b2c047ce0
commit
99bcf5f112
9 changed files with 70 additions and 18 deletions
|
@ -3,6 +3,8 @@
|
|||
final class PhabricatorDifferentialRevisionTestDataGenerator
|
||||
extends PhabricatorTestDataGenerator {
|
||||
|
||||
const GENERATORKEY = 'revisions';
|
||||
|
||||
public function getGeneratorName() {
|
||||
return pht('Differential Revisions');
|
||||
}
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
final class PhabricatorFileTestDataGenerator
|
||||
extends PhabricatorTestDataGenerator {
|
||||
|
||||
const GENERATORKEY = 'files';
|
||||
|
||||
public function getGeneratorName() {
|
||||
return pht('Files');
|
||||
}
|
||||
|
|
|
@ -16,6 +16,10 @@ abstract class PhabricatorTestDataGenerator extends Phobject {
|
|||
return $this->viewer;
|
||||
}
|
||||
|
||||
final public function getGeneratorKey() {
|
||||
return $this->getPhobjectClassConstant('GENERATORKEY', 64);
|
||||
}
|
||||
|
||||
protected function loadRandomPHID($table) {
|
||||
$conn_r = $table->establishConnection('r');
|
||||
|
||||
|
|
|
@ -30,14 +30,30 @@ final class PhabricatorLipsumGenerateWorkflow
|
|||
|
||||
$all_generators = id(new PhutilClassMapQuery())
|
||||
->setAncestorClass('PhabricatorTestDataGenerator')
|
||||
->setUniqueMethod('getGeneratorKey')
|
||||
->execute();
|
||||
|
||||
$argv = $args->getArg('args');
|
||||
$all = 'all';
|
||||
|
||||
if (isset($all_generators[$all])) {
|
||||
throw new Exception(
|
||||
pht(
|
||||
'A lipsum generator is registered with key "%s". This key is '.
|
||||
'reserved.',
|
||||
$all));
|
||||
}
|
||||
|
||||
if (!$argv) {
|
||||
$names = mpull($all_generators, 'getGeneratorName');
|
||||
sort($names);
|
||||
ksort($all_generators);
|
||||
|
||||
$names = array();
|
||||
foreach ($all_generators as $generator) {
|
||||
$names[] = tsprintf(
|
||||
'%s (%s)',
|
||||
$generator->getGeneratorKey(),
|
||||
$generator->getGeneratorName());
|
||||
}
|
||||
|
||||
$list = id(new PhutilConsoleList())
|
||||
->setWrap(false)
|
||||
|
@ -59,31 +75,49 @@ final class PhabricatorLipsumGenerateWorkflow
|
|||
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') {
|
||||
$matches = $all_generators;
|
||||
} else {
|
||||
$matches = array();
|
||||
foreach ($all_generators as $generator) {
|
||||
$name = phutil_utf8_strtolower($generator->getGeneratorKey());
|
||||
|
||||
if ($arg == $all) {
|
||||
$generators[] = $generator;
|
||||
$match = true;
|
||||
break;
|
||||
// If there's an exact match, select just that generator.
|
||||
if ($arg == $name) {
|
||||
$matches = array($generator);
|
||||
break;
|
||||
}
|
||||
|
||||
// If there's a partial match, match that generator but continue.
|
||||
if (strpos($name, $arg) !== false) {
|
||||
$matches[] = $generator;
|
||||
}
|
||||
}
|
||||
|
||||
if (strpos($name, $arg) !== false) {
|
||||
$generators[] = $generator;
|
||||
$match = true;
|
||||
break;
|
||||
if (!$matches) {
|
||||
throw new PhutilArgumentUsageException(
|
||||
pht(
|
||||
'Argument "%s" does not match the name of any generators.',
|
||||
$arg_original));
|
||||
}
|
||||
|
||||
if (count($matches) > 1) {
|
||||
throw new PhutilArgumentUsageException(
|
||||
pht(
|
||||
'Argument "%s" is ambiguous, and matches multiple '.
|
||||
'generators: %s.',
|
||||
$arg_original,
|
||||
implode(', ', mpull($matches, 'getGeneratorName'))));
|
||||
}
|
||||
}
|
||||
|
||||
if (!$match) {
|
||||
throw new PhutilArgumentUsageException(
|
||||
pht(
|
||||
'Argument "%s" does not match the name of any generators.',
|
||||
$arg_original));
|
||||
foreach ($matches as $match) {
|
||||
$generators[] = $match;
|
||||
}
|
||||
}
|
||||
|
||||
$generators = mpull($generators, null, 'getGeneratorKey');
|
||||
|
||||
echo tsprintf(
|
||||
"**<bg:blue> %s </bg>** %s\n",
|
||||
pht('GENERATORS'),
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
final class PhabricatorManiphestTaskTestDataGenerator
|
||||
extends PhabricatorTestDataGenerator {
|
||||
|
||||
const GENERATORKEY = 'tasks';
|
||||
|
||||
public function getGeneratorName() {
|
||||
return pht('Maniphest Tasks');
|
||||
}
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
final class PhabricatorPasteTestDataGenerator
|
||||
extends PhabricatorTestDataGenerator {
|
||||
|
||||
const GENERATORKEY = 'pastes';
|
||||
|
||||
public function getGeneratorName() {
|
||||
return pht('Pastes');
|
||||
}
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
final class PhabricatorPeopleTestDataGenerator
|
||||
extends PhabricatorTestDataGenerator {
|
||||
|
||||
const GENERATORKEY = 'users';
|
||||
|
||||
public function getGeneratorName() {
|
||||
return pht('User Accounts');
|
||||
}
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
final class PhabricatorPholioMockTestDataGenerator
|
||||
extends PhabricatorTestDataGenerator {
|
||||
|
||||
const GENERATORKEY = 'mocks';
|
||||
|
||||
public function getGeneratorName() {
|
||||
return pht('Pholio Mocks');
|
||||
}
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
final class PhabricatorProjectTestDataGenerator
|
||||
extends PhabricatorTestDataGenerator {
|
||||
|
||||
const GENERATORKEY = 'projects';
|
||||
|
||||
public function getGeneratorName() {
|
||||
return pht('Projects');
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue