Skip to content

Commit ba37149

Browse files
author
epriestley
committed
Improve bin/lipsum UX
Summary: Ref T9156. This makes the UX a little more modern/standard/safe. Test Plan: ``` epriestley@orbital ~/dev/phabricator $ ./bin/lipsum generate Choose which type or types of test data you want to generate, or select "all". - Differential Revisions - Files - Maniphest Tasks - Pastes - Pholio Mocks - Projects - User Accounts ``` Reviewers: chad Reviewed By: chad Maniphest Tasks: T9156 Differential Revision: https://secure.phabricator.com/D14873
1 parent 1c572d1 commit ba37149

10 files changed

+141
-67
lines changed

scripts/lipsum/manage_lipsum.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
require_once $root.'/scripts/__init_script__.php';
66

77
$args = new PhutilArgumentParser($argv);
8-
$args->setTagline(pht('manage lipsum'));
8+
$args->setTagline(pht('synthetic data generator'));
99
$args->setSynopsis(<<<EOSYNOPSIS
1010
**lipsum** __command__ [__options__]
11-
Manage Phabricator Test Data Generator.
11+
Generate synthetic test data to make development easier.
1212
1313
EOSYNOPSIS
1414
);

src/applications/differential/lipsum/PhabricatorDifferentialRevisionTestDataGenerator.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
final class PhabricatorDifferentialRevisionTestDataGenerator
44
extends PhabricatorTestDataGenerator {
55

6-
public function generate() {
6+
public function getGeneratorName() {
7+
return pht('Differential Revisions');
8+
}
9+
10+
public function generateObject() {
711
$author = $this->loadPhabrictorUser();
812

913
$revision = DifferentialRevision::initializeNewRevision($author);

src/applications/files/lipsum/PhabricatorFileTestDataGenerator.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
final class PhabricatorFileTestDataGenerator
44
extends PhabricatorTestDataGenerator {
55

6-
public function generate() {
6+
public function getGeneratorName() {
7+
return pht('Files');
8+
}
9+
10+
public function generateObject() {
711
$author_phid = $this->loadPhabrictorUserPHID();
812
$dimension = 1 << rand(5, 12);
913
$image = id(new PhabricatorLipsumMondrianArtist())

src/applications/lipsum/generator/PhabricatorTestDataGenerator.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
abstract class PhabricatorTestDataGenerator extends Phobject {
44

5-
public function generate() {
6-
return;
7-
}
5+
abstract public function getGeneratorName();
6+
abstract public function generateObject();
87

98
public function loadOneRandom($classname) {
109
try {

src/applications/lipsum/management/PhabricatorLipsumGenerateWorkflow.php

Lines changed: 102 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ protected function didConstruct() {
77
$this
88
->setName('generate')
99
->setExamples('**generate**')
10-
->setSynopsis(pht('Generate some lipsum.'))
10+
->setSynopsis(pht('Generate synthetic test objects.'))
1111
->setArguments(
1212
array(
1313
array(
@@ -18,77 +18,124 @@ protected function didConstruct() {
1818
}
1919

2020
public function execute(PhutilArgumentParser $args) {
21-
$console = PhutilConsole::getConsole();
21+
$config_key = 'phabricator.developer-mode';
22+
if (!PhabricatorEnv::getEnvConfig($config_key)) {
23+
throw new PhutilArgumentUsageException(
24+
pht(
25+
'lipsum is a development and testing tool and may only be run '.
26+
'on installs in developer mode. Enable "%s" in your configuration '.
27+
'to enable lipsum.',
28+
$config_key));
29+
}
2230

23-
$supported_types = id(new PhutilClassMapQuery())
31+
$all_generators = id(new PhutilClassMapQuery())
2432
->setAncestorClass('PhabricatorTestDataGenerator')
2533
->execute();
2634

27-
$console->writeOut(
28-
"%s:\n\t%s\n",
29-
pht('These are the types of data you can generate'),
30-
implode("\n\t", array_keys($supported_types)));
35+
$argv = $args->getArg('args');
36+
$all = 'all';
3137

32-
$prompt = pht('Are you sure you want to generate lots of test data?');
33-
if (!phutil_console_confirm($prompt, true)) {
34-
return;
38+
if (!$argv) {
39+
$names = mpull($all_generators, 'getGeneratorName');
40+
sort($names);
41+
42+
$list = id(new PhutilConsoleList())
43+
->setWrap(false)
44+
->addItems($names);
45+
46+
id(new PhutilConsoleBlock())
47+
->addParagraph(
48+
pht(
49+
'Choose which type or types of test data you want to generate, '.
50+
'or select "%s".',
51+
$all))
52+
->addList($list)
53+
->draw();
54+
55+
return 0;
3556
}
3657

37-
$argv = $args->getArg('args');
38-
if (count($argv) == 0 || (count($argv) == 1 && $argv[0] == 'all')) {
39-
$this->infinitelyGenerate($supported_types);
40-
} else {
41-
$new_supported_types = array();
42-
for ($i = 0; $i < count($argv); $i++) {
43-
$arg = $argv[$i];
44-
if (array_key_exists($arg, $supported_types)) {
45-
$new_supported_types[$arg] = $supported_types[$arg];
46-
} else {
47-
$console->writeErr(
48-
"%s\n",
49-
pht(
50-
'The type %s is not supported by the lipsum generator.',
51-
$arg));
58+
$generators = array();
59+
foreach ($argv as $arg_original) {
60+
$arg = phutil_utf8_strtolower($arg_original);
61+
62+
$match = false;
63+
foreach ($all_generators as $generator) {
64+
$name = phutil_utf8_strtolower($generator->getGeneratorName());
65+
66+
if ($arg == $all) {
67+
$generators[] = $generator;
68+
$match = true;
69+
break;
70+
}
71+
72+
if (strpos($name, $arg) !== false) {
73+
$generators[] = $generator;
74+
$match = true;
75+
break;
5276
}
5377
}
54-
$this->infinitelyGenerate($new_supported_types);
78+
79+
if (!$match) {
80+
throw new PhutilArgumentUsageException(
81+
pht(
82+
'Argument "%s" does not match the name of any generators.',
83+
$arg_original));
84+
}
5585
}
5686

57-
$console->writeOut(
58-
"%s\n%s:\n%s\n",
59-
pht('None of the input types were supported.'),
60-
pht('The supported types are'),
61-
implode("\n", array_keys($supported_types)));
62-
}
87+
echo tsprintf(
88+
"**<bg:blue> %s </bg>** %s\n",
89+
pht('GENERATORS'),
90+
pht(
91+
'Selected generators: %s.',
92+
implode(', ', mpull($generators, 'getGeneratorName'))));
6393

64-
protected function infinitelyGenerate(array $supported_types) {
65-
$console = PhutilConsole::getConsole();
94+
echo tsprintf(
95+
"**<bg:yellow> %s </bg>** %s\n",
96+
pht('WARNING'),
97+
pht(
98+
'This command generates synthetic test data, including user '.
99+
'accounts. It is intended for use in development environments '.
100+
'so you can test features more easily. There is no easy way to '.
101+
'delete this data or undo the effects of this command. If you run '.
102+
'it in a production environment, it will pollute your data with '.
103+
'large amounts of meaningless garbage that you can not get rid of.'));
66104

67-
if (count($supported_types) == 0) {
105+
$prompt = pht('Are you sure you want to generate piles of garbage?');
106+
if (!phutil_console_confirm($prompt, true)) {
68107
return;
69108
}
70-
$console->writeOut(
71-
"%s: %s\n",
72-
pht('GENERATING'),
73-
implode(', ', array_keys($supported_types)));
109+
110+
echo tsprintf(
111+
"**<bg:green> %s </bg>** %s\n",
112+
pht('LIPSUM'),
113+
pht(
114+
'Generating synthetic test objects forever. '.
115+
'Use ^C to stop when satisfied.'));
116+
117+
$this->generate($generators);
118+
}
119+
120+
protected function generate(array $generators) {
121+
$viewer = $this->getViewer();
74122

75123
while (true) {
76-
$type = $supported_types[array_rand($supported_types)];
77-
$admin = $this->getViewer();
78-
79-
$taskgen = newv($type, array());
80-
$object = $taskgen->generate();
81-
$handle = id(new PhabricatorHandleQuery())
82-
->setViewer($admin)
83-
->withPHIDs(array($object->getPHID()))
84-
->executeOne();
85-
86-
$console->writeOut(
87-
"%s: %s\n",
88-
pht('Generated %s', $handle->getTypeName()),
89-
$handle->getFullName());
90-
91-
usleep(200000);
124+
$generator = $generators[array_rand($generators)];
125+
126+
$object = $generator->generateObject();
127+
$object_phid = $object->getPHID();
128+
129+
$handles = $viewer->loadHandles(array($object_phid));
130+
131+
echo tsprintf(
132+
"%s\n",
133+
pht(
134+
'Generated "%s": %s',
135+
$handles[$object_phid]->getTypeName(),
136+
$handles[$object_phid]->getFullName()));
137+
138+
sleep(1);
92139
}
93140
}
94141

src/applications/maniphest/lipsum/PhabricatorManiphestTaskTestDataGenerator.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
final class PhabricatorManiphestTaskTestDataGenerator
44
extends PhabricatorTestDataGenerator {
55

6-
public function generate() {
6+
public function getGeneratorName() {
7+
return pht('Maniphest Tasks');
8+
}
9+
10+
public function generateObject() {
711
$author_phid = $this->loadPhabrictorUserPHID();
812
$author = id(new PhabricatorUser())
913
->loadOneWhere('phid = %s', $author_phid);

src/applications/paste/lipsum/PhabricatorPasteTestDataGenerator.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33
final class PhabricatorPasteTestDataGenerator
44
extends PhabricatorTestDataGenerator {
55

6+
public function getGeneratorName() {
7+
return pht('Pastes');
8+
}
9+
610
// Better Support for this in the future
711
public $supportedLanguages = array(
812
'Java' => 'java',
913
'PHP' => 'php',
1014
);
1115

12-
public function generate() {
16+
public function generateObject() {
1317
$author = $this->loadPhabrictorUser();
1418
$authorphid = $author->getPHID();
1519
$language = $this->generateLanguage();

src/applications/people/lipsum/PhabricatorPeopleTestDataGenerator.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
final class PhabricatorPeopleTestDataGenerator
44
extends PhabricatorTestDataGenerator {
55

6-
public function generate() {
6+
public function getGeneratorName() {
7+
return pht('User Accounts');
8+
}
9+
10+
public function generateObject() {
711

812
while (true) {
913
try {

src/applications/pholio/lipsum/PhabricatorPholioMockTestDataGenerator.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
final class PhabricatorPholioMockTestDataGenerator
44
extends PhabricatorTestDataGenerator {
55

6-
public function generate() {
6+
public function getGeneratorName() {
7+
return pht('Pholio Mocks');
8+
}
9+
10+
public function generateObject() {
711
$author_phid = $this->loadPhabrictorUserPHID();
812
$author = id(new PhabricatorUser())
913
->loadOneWhere('phid = %s', $author_phid);

src/applications/project/lipsum/PhabricatorProjectTestDataGenerator.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ final class PhabricatorProjectTestDataGenerator
55

66
private $xactions = array();
77

8-
public function generate() {
8+
public function getGeneratorName() {
9+
return pht('Projects');
10+
}
11+
12+
public function generateObject() {
913
$title = $this->generateTitle();
1014
$author = $this->loadPhabrictorUser();
1115
$author_phid = $author->getPHID();

0 commit comments

Comments
 (0)