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 {
final rawFeatureName = argResults?.rest.firstOrNull;
if (rawFeatureName == null) {
throw UsageException('Missing feature name.', usage);
}
final featureName = rawFeatureName.toSnakeCase();
if (featureName.isEmpty) {
throw UsageException('Invalid feature name "$rawFeatureName".', usage);
}
final projectDirectory = Directory(
argResults?['project-dir'] as String? ?? Directory.current.path,
);
final force = argResults?['force'] as bool? ?? false;
final shouldRunSlang = argResults?['run-slang'] as bool? ?? true;
final pubspec = File(p.join(projectDirectory.path, 'pubspec.yaml'));
if (!pubspec.existsSync()) {
throw UsageException(
'Could not find pubspec.yaml. Run this command from a Flutter project root.',
usage,
);
}
final packageName = readPackageName(pubspec);
if (packageName == null) {
throw UsageException(
'Could not read the package name from pubspec.yaml.',
usage,
);
}
final featureDirectory = Directory(
p.join(projectDirectory.path, 'lib', 'features', featureName),
);
if (featureDirectory.existsSync() && !force) {
throw UsageException(
'Feature "$featureName" already exists. Use --force to overwrite it.',
usage,
);
}
final languages = _resolveLanguages(
projectDirectory,
argResults?['languages'] as String?,
);
FeatureWriter(
featureName: featureName,
packageName: packageName,
projectDirectory: projectDirectory,
languages: languages,
force: force,
).write();
_logger.success('Feature "$featureName" created');
if (shouldRunSlang &&
File(p.join(projectDirectory.path, 'slang.yaml')).existsSync()) {
final generated = await _processRunner.run(
message: 'Generating translations',
executable: 'dart',
arguments: ['run', 'slang'],
workingDirectory: projectDirectory.path,
failureMessage: 'Slang generation failed',
successMessage: 'Translations generated',
);
if (!generated) return;
}
_logger
..info('')
..info('Next steps:')
..info(' Import ${featureName.toPascalCase()}Screen where you need it')
..info(' Add a route or navigation entry if this feature needs one');
}