run method
Runs this command.
The return value is wrapped in a Future
if necessary and returned by
CommandRunner.runCommand
.
Implementation
@override
Future<void> run() async {
Utils.printGreen('$name generator');
// source directory
final String? dir = argResults?['directory'] as String?;
if (dir == null) {
throw UsageException(
'Source directory path doesn"t exist',
'Use correct source directory file path',
);
}
final String rootFolder = Directory.current.path;
final String absDirPath = p.normalize(p.join(rootFolder, dir));
final Set<String> filePaths = FileUtil.getDartFilesFromFolders(
<String>[absDirPath],
rootFolder,
excludedExtensions: _excludedExtensions,
);
// output path
if (argResults?['output'] == null) {
throw UsageException('Output file path doesn"t exist', 'Use correct output file path');
}
final String outputPath = argResults?['output'] as String;
final String absOutputPath = p.join(Directory.current.path, outputPath);
final File outputFile = File(absOutputPath);
final bool outputExists = outputFile.existsSync();
if (!outputExists) {
outputFile.createSync();
}
// format
final String format = argResults?['format'] as String;
Utils.printCyan('$format. input dir: $absDirPath, output file: $absOutputPath');
// generate and write to file
final Map<String, Iterable<DartType>> genMap = await _generate(filePaths: filePaths);
late String text;
switch (format) {
case _kUmlFormat:
text = generateUmlFile(genMap);
break;
case _kHtmlFormat:
text = generateHtmlFile(genMap);
break;
default:
final String fmts = _kFormatList.join('; ');
throw UsageException(
'Unknown output format',
'Use one of those formats: $fmts',
);
}
outputFile.writeAsStringSync(text);
}