Skip to content

Commit 248563c

Browse files
authored
feat: add docs generator (#7)
* feat: add docs generator * fix: remove unintended addition
1 parent fd8f906 commit 248563c

File tree

4 files changed

+204
-0
lines changed

4 files changed

+204
-0
lines changed

lib/cli_tools.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export 'better_command_runner.dart';
55
export 'local_storage_manager.dart';
66
export 'logger.dart';
77
export 'package_version.dart';
8+
export 'docs_generator.dart';

lib/docs_generator.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export 'src/documentation_generator/documentation_generator.dart';
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import '../../better_command_runner.dart';
2+
3+
class CommandDocumentationGenerator {
4+
final BetterCommandRunner commandRunner;
5+
6+
CommandDocumentationGenerator(this.commandRunner);
7+
8+
Map<String, String> generateMarkdown() {
9+
var commands = commandRunner.commands.values;
10+
11+
var files = <String, String>{};
12+
13+
for (var command in commands) {
14+
StringBuffer markdown = StringBuffer();
15+
markdown.writeln('## Usage\n');
16+
17+
if (command.argParser.options.isNotEmpty) {
18+
markdown.writeln('```console');
19+
markdown.writeln(command.usage);
20+
markdown.writeln('```\n');
21+
}
22+
23+
if (command.subcommands.isNotEmpty) {
24+
var numberOfSubcommands = command.subcommands.length;
25+
markdown.writeln('### Sub commands\n');
26+
for (var (i, subcommand) in command.subcommands.entries.indexed) {
27+
markdown.writeln('#### `${subcommand.key}`\n');
28+
markdown.writeln('```console');
29+
markdown.writeln(subcommand.value.usage);
30+
markdown.writeln('```');
31+
if (i < numberOfSubcommands - 1) {
32+
markdown.writeln();
33+
}
34+
}
35+
}
36+
37+
files['${command.name}.md'] = markdown.toString();
38+
}
39+
40+
return files;
41+
}
42+
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
import 'package:args/command_runner.dart';
2+
import 'package:cli_tools/cli_tools.dart';
3+
import 'package:test/test.dart';
4+
5+
class AddSpiceCommand extends Command {
6+
@override
7+
final String name = 'add';
8+
9+
@override
10+
final String description = 'Add something to the spice mix';
11+
12+
AddSpiceCommand() {
13+
argParser.addOption('curry', help: 'Include curry in the spice mix.');
14+
argParser.addOption('pepper', help: 'Include pepper in the spice mix.');
15+
}
16+
17+
@override
18+
void run() {}
19+
}
20+
21+
class RemoveSpiceCommand extends Command {
22+
@override
23+
final String name = 'remove';
24+
25+
@override
26+
final String description = 'Remove something from the spice mix';
27+
28+
RemoveSpiceCommand() {
29+
argParser.addOption('curry', help: 'Remove curry from the spice mix.');
30+
argParser.addOption('pepper', help: 'Remove pepper from the spice mix.');
31+
}
32+
33+
@override
34+
void run() {}
35+
}
36+
37+
class AddVegetableCommand extends Command {
38+
@override
39+
final String name = 'add';
40+
41+
@override
42+
final String description = 'Add a vegetable to your dish.';
43+
44+
AddVegetableCommand() {
45+
argParser.addOption('carrot', help: 'Adds a fresh carrot to the dish.');
46+
}
47+
48+
@override
49+
void run() {}
50+
}
51+
52+
class SpiceCommand extends BetterCommand {
53+
@override
54+
final String name = 'spice';
55+
56+
@override
57+
final String description = 'Modifies the spice mix in your dish.';
58+
59+
SpiceCommand() {
60+
addSubcommand(AddSpiceCommand());
61+
addSubcommand(RemoveSpiceCommand());
62+
}
63+
64+
@override
65+
void run() {}
66+
}
67+
68+
class VegetableCommand extends BetterCommand {
69+
@override
70+
final String name = 'vegetable';
71+
72+
@override
73+
final String description = 'Add or remove vegatables to your dish.';
74+
75+
VegetableCommand() {
76+
addSubcommand(AddVegetableCommand());
77+
}
78+
79+
@override
80+
void run() {}
81+
}
82+
83+
void main() {
84+
group('Given commands when generating markdown', () {
85+
late Map<String, String> output;
86+
87+
setUpAll(() async {
88+
var commandRunner =
89+
BetterCommandRunner('cookcli', 'A cli to create wonderful dishes.')
90+
..addCommand(SpiceCommand())
91+
..addCommand(VegetableCommand());
92+
var generator = CommandDocumentationGenerator(commandRunner);
93+
output = generator.generateMarkdown();
94+
});
95+
96+
test('then outputs each command into a separate file', () {
97+
expect(output.keys, containsAll(['vegetable.md', 'spice.md']));
98+
});
99+
100+
test('then output starts with the main command', () async {
101+
var vegetableCommandOutput = output['spice.md'];
102+
103+
expect(
104+
vegetableCommandOutput,
105+
startsWith(
106+
'## Usage\n'
107+
'\n'
108+
'```console\n'
109+
'Modifies the spice mix in your dish.\n'
110+
'\n'
111+
'Usage: cookcli spice <subcommand> [arguments]\n'
112+
'-h, --help Print this usage information.\n'
113+
'\n'
114+
'Available subcommands:\n'
115+
' add Add something to the spice mix\n'
116+
' remove Remove something from the spice mix\n'
117+
'\n'
118+
'Run "cookcli help" to see global options.\n'
119+
'```\n'
120+
'\n',
121+
));
122+
});
123+
124+
test('then output ends with the sub commands', () async {
125+
var vegetableCommandOutput = output['spice.md'];
126+
127+
expect(
128+
vegetableCommandOutput,
129+
endsWith(
130+
'### Sub commands\n'
131+
'\n'
132+
'#### `add`\n'
133+
'\n'
134+
'```console\n'
135+
'Add something to the spice mix\n'
136+
'\n'
137+
'Usage: cookcli spice add [arguments]\n'
138+
'-h, --help Print this usage information.\n'
139+
' --curry Include curry in the spice mix.\n'
140+
' --pepper Include pepper in the spice mix.\n'
141+
'\n'
142+
'Run "cookcli help" to see global options.\n'
143+
'```\n'
144+
'\n'
145+
'#### `remove`\n'
146+
'\n'
147+
'```console\n'
148+
'Remove something from the spice mix\n'
149+
'\n'
150+
'Usage: cookcli spice remove [arguments]\n'
151+
'-h, --help Print this usage information.\n'
152+
' --curry Remove curry from the spice mix.\n'
153+
' --pepper Remove pepper from the spice mix.\n'
154+
'\n'
155+
'Run "cookcli help" to see global options.\n'
156+
'```\n',
157+
));
158+
});
159+
});
160+
}

0 commit comments

Comments
 (0)