run method

  1. @override
Future<int> run()
override

Runs this command.

The return value is wrapped in a Future if necessary and returned by CommandRunner.runCommand.

Implementation

@override
Future<int> run() async {
  if (argResults == null || argResults!.rest.isEmpty) {
    _logger.err('Error: Please specify the feature name to add.');
    _logger.info('Usage: zuq add feature <feature_name>');
    return 1;
  }

  final featureName = argResults!.rest.first;

  final projectConfig = _configParser.readConfig(fallbackProjectName: '');
  if (projectConfig == null) {
    _logger.err('Error: No zuq.yaml configuration found in this directory.');
    _logger.err('Make sure you are at the root of a zuq project.');
    return 1;
  }

  final projectPath = Directory.current.path;
  final libPath = p.join(projectPath, 'lib');
  final featuresPath = p.join(libPath, 'features');

  final progress = _logger.progress('Scaffolding feature $featureName...');

  try {
    final templatesPath = TemplatesLocator.getTemplatesPath();
    final brickPath = p.join(templatesPath, 'feature');
    final brick = Brick.path(brickPath);
    final generator = await MasonGenerator.fromBrick(brick);

    final variables = <String, dynamic>{
      'name': featureName,
      'isRiverpod': projectConfig.stateManagement == 'riverpod',
      'isBloc': projectConfig.stateManagement == 'bloc',
      'isProvider': projectConfig.stateManagement == 'provider',
      'isNone': projectConfig.stateManagement == 'none',
    };

    // Target lib/features folder
    final target = DirectoryGeneratorTarget(Directory(featuresPath));
    await generator.generate(target, vars: variables, logger: Logger());

    progress.complete('Feature $featureName files generated.');
  } catch (e) {
    progress.fail('Failed to generate feature $featureName: $e');
    return 1;
  }

  _logger.success('Successfully added feature $featureName under lib/features!');
  return 0;
}