This repository has been archived by the owner on Jun 8, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_docs.dart
116 lines (97 loc) · 2.59 KB
/
generate_docs.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import 'dart:io';
import 'package:args/command_runner.dart';
import 'package:path/path.dart' as path;
import 'package:utilx/utils.dart';
import '../src/core/commander.dart';
final String outputPath = path.join(Directory.current.path, 'dist/docs.md');
class CommandDoc {
const CommandDoc(
this.command, {
required this.fullCommand,
required this.level,
});
final Command<void> command;
final String fullCommand;
final int level;
@override
String toString() => basic(
level: level,
fullCommand: fullCommand,
usage: command.usage,
);
static String basic({
required final int level,
required final String fullCommand,
required final String usage,
}) =>
'''
${List<String>.filled(level, '#').join()} $fullCommand
```
$usage
```
''';
}
final Map<String, CommandDoc> commands = <String, CommandDoc>{};
Future<void> main() async {
final CommandRunner<void> runner = AppCommander.get();
final String fullCommand = runner.executableName;
for (final Command<void> x in runner.commands.values) {
final String xFullCommand = '$fullCommand ${x.name}';
if (!commands.containsKey(xFullCommand)) {
handleCommand(x, level: 2, fullCommand: xFullCommand);
}
}
final String docs = <String>[
CommandDoc.basic(
level: 1,
fullCommand: fullCommand,
usage: runner.usage,
),
...commands.values.map((final CommandDoc value) => value.toString()),
].join('\n');
final File outputFile = File(outputPath);
await FSUtils.ensureFile(outputFile);
await outputFile.writeAsString(docs);
}
void handleCommand(
final Command<void> command, {
required final int level,
required final String fullCommand,
}) {
commands[fullCommand] = CommandDoc(
command,
level: level,
fullCommand: fullCommand,
);
for (final Command<void> x in command.subcommands.values) {
final String xFullCommand = '$fullCommand ${x.name}';
if (!commands.containsKey(xFullCommand)) {
handleSubCommand(
x,
level: level + 1,
fullCommand: xFullCommand,
);
}
}
}
void handleSubCommand(
final Command<void> subcommand, {
required final int level,
required final String fullCommand,
}) {
commands[fullCommand] = CommandDoc(
subcommand,
level: level,
fullCommand: fullCommand,
);
for (final Command<void> x in subcommand.subcommands.values) {
final String xFullCommand = '$fullCommand ${x.name}';
if (!commands.containsKey(xFullCommand)) {
handleSubCommand(
x,
level: level + 1,
fullCommand: xFullCommand,
);
}
}
}