run method
Implementation
@override
String run(ParsedInputs inputs, List<String> args) {
final name = inputs.valueOf(commandName);
final targetPath = inputs.valueOf(fileName);
final isGroup = inputs.valueOf(group);
final shouldAppend = inputs.valueOf(append);
if (targetPath != null && !shouldAppend) {
throw MambaException('The file argument requires --append.');
}
if (targetPath == null && shouldAppend) {
throw MambaException('--append requires a file argument.');
}
final file = targetPath == null
? File('${_parentDirectory.path}/lib/$name.dart')
: File(targetPath);
if (shouldAppend && !file.existsSync()) {
throw MambaException('Cannot append $name: ${file.path} does not exist.');
}
if (!shouldAppend && file.existsSync()) {
throw MambaException('Cannot create $name: the file already exists.');
}
final className = '${name[0].toUpperCase()}${name.substring(1)}Command';
final implementation = isGroup
? "final class $className extends GroupCommand {\n new() : super([]);\n @override String get name => '$name';\n @override String get shortDescription => 'Describe $name.';\n}\n"
: "final class $className extends Command {\n @override String get name => '$name';\n @override String get shortDescription => 'Describe $name.';\n @override String run(ParsedInputs inputs, List<String> args) => 'Completed $name.';\n}\n";
if (shouldAppend) {
file.writeAsStringSync('\n$implementation', mode: FileMode.append);
} else {
file.parent.createSync(recursive: true);
file.writeAsStringSync(
"import 'package:mamba/mamba.dart';\n\n$implementation",
);
}
return shouldAppend
? 'Appended command to ${file.path}.'
: 'Created command in ${file.path}.';
}