generateFeature method

Future<void> generateFeature(
  1. Context context
)

Creates directories and generates all boilerplate files for the feature.

Implementation

Future<void> generateFeature(Context context) async {
  final featureName = context.nameLowerCase;
  final basePath = '${context.projectRoot}/lib/features/$featureName';
  final generateUseCase = context.generateUseCase;

  // Resolve templates from within the feature_gen_cli package itself.
  final packageUri = Uri.parse('package:feature_gen_cli/');
  final libUri = await Isolate.resolvePackageUri(packageUri);
  if (libUri == null) {
    throw StateError('Could not resolve package:feature_gen_cli – is the package activated?');
  }
  final templateBasePath = libUri.resolve('template').toFilePath();

  // Create feature folders in a conventional clean-architecture layout.
  final folders = [
    '$basePath/data/models',
    '$basePath/data/repositories',
    '$basePath/data/datasources',
    '$basePath/domain/entities',
    '$basePath/domain/repositories',
    if (generateUseCase) '$basePath/domain/usecases',
    if (context.config.bloc == true) '$basePath/presentation/bloc',
    if (context.config.riverpod == true) '$basePath/presentation/riverpod',
    if (context.config.bloc == true || context.config.riverpod == true)
      '$basePath/presentation/screen',
  ];

  if (generateUseCase) {
    folders.add('${context.projectRoot}/lib/features/shared/usecase');
  }

  // Create core folders
  folders.add('${context.projectRoot}/lib/core/di');

  for (var folder in folders) {
    Directory(folder).createSync(recursive: true);
  }

  generateBoilerplate(
    featureName: featureName,
    basePath: basePath,
    templateBasePath: templateBasePath,
    context: context,
  );
}