createUsaCase function

Future<void> createUsaCase(
  1. String featureName,
  2. String useCaseName,
  3. String requestModelName
)

Implementation

Future<void> createUsaCase(
    String featureName, String useCaseName, String requestModelName) async {
  String destinationPath = 'lib/features/$featureName/domain/usecases';
  String destinationFilePath = '$destinationPath/$useCaseName.dart';
  if (doesFileExist(destinationFilePath)) {
    print(
        'File at $destinationFilePath already exists. Please choose a different usecase name');
    exit(1);
  }

  String currentFilePath = Platform.script.toFilePath();
  List<String> pathSegments = currentFilePath.split(Platform.pathSeparator);
  if (pathSegments.length > 2) {
    pathSegments.removeRange(pathSegments.length - 5, pathSegments.length);
  }

  String packageName = 'jt_flutter_cli';
  String version = getPackageVersion(packageName);
  String dependencyName = '$packageName-$version';
  String pubCachePath = Platform.environment['PUB_CACHE'] ??
      '${Platform.environment['HOME']}/.pub-cache';
  String dependencyPath =
      path.join(pubCachePath, 'hosted', 'pub.dev', dependencyName);
  if (!Directory(dependencyPath).existsSync()) {
    print('Dependency $dependencyName not found in .pub-cache directory');
    return;
  }
  String dependencyLibPath = path.join(dependencyPath, 'lib');
  if (!Directory(dependencyLibPath).existsSync()) {
    print('Dependency $dependencyName does not contain a lib folder');
    return;
  }
  String sourceFolderPath = '$dependencyLibPath/add_api_call/usecases';
  grantPermissions(sourceFolderPath);

  if (!Directory(sourceFolderPath).existsSync()) {
    print('Error: $sourceFolderPath folder does not exist');
    return;
  }

  try {
    destinationPath = '$destinationPath';
    await copyFolder(sourceFolderPath, destinationPath);
    processFiles(destinationPath, '/*', '', false);
    processFiles(destinationPath, '*/', '', false);

    renameFilesWithSubstring(destinationPath, 'basic_structure', useCaseName);
    processFiles(destinationPath, 'BasicStructure', useCaseName, true);
    processFiles(destinationPath, 'basicStructure', useCaseName, true);
    processFiles(destinationPath, 'Common', camelCase(featureName), true);
    processFiles(destinationPath, 'basic_structure', useCaseName, false);
    processFiles(destinationPath, 'common', featureName, false);
    String? name = getPubspecInfo('name');
    if (name != null) {
      processFiles(destinationPath, 'flutter_clean_architecture', name, false);
    }
    String useCaseFilePath = '$destinationPath/${useCaseName}_usecase.dart';
    if (requestModelName.isNotEmpty) {
      processFiles(destinationPath, 'params',
          "${capitalizeFirstCharacter(requestModelName)}", false);
      processFiles(destinationPath, 'NoRequest', requestModelName, true);
      processFiles(
          destinationPath,
          'get${camelCase(useCaseName)}()',
          "get${camelCase(useCaseName)}(${capitalizeFirstCharacter(requestModelName)})",
          false);
      final usecaseFileImport = [
        "import 'package:$name/features/${featureName}/data/model/${useCaseName}_request_model.dart';"
      ];
      await addImport(
        useCaseFilePath,
        usecaseFileImport,
      );
    }

    print('Usecase $useCaseName created at $destinationPath');
    final nameOfFeature = featureName.toLowerCase();
    final featurePascalCase = featureName.toLowerCase();
    String pascalCase = toPascalCase(useCaseName);
    final filePath = 'lib/core/di/';
    final usecaseImport =
        "import 'package:${name ?? ''}/features/$nameOfFeature/domain/usecases/${useCaseName}_usecase.dart';";
    final injectionContainerImports = [
      usecaseImport,
      // "import 'package:${name ?? ''}/features/$nameOfFeature/domain/repositories/${nameOfFeature}_repo.dart';"
    ];
    final injectionContainerStatements = [
      "  sl.registerLazySingleton("
          "() => ${pascalCase}UseCase(getter.get${camelCase(featureName)}Repo()),"
          ");"
    ];
    addImportAndChangesInFile('$filePath/injection_container.dart',
        injectionContainerImports, injectionContainerStatements);
    print('UseCase added to the injection container successfully');

    ///Add use_case in page bloc
    await addImport(
      'lib/features/$nameOfFeature/${nameOfFeature}_page.dart',
      ["import 'package:${name ?? ''}/core/di/injection_container.dart';"],
    );
    await findAndReplace(
        'lib/features/$nameOfFeature/${nameOfFeature}_page.dart',
        "apiBaseBloc: BlocProvider.of<ApiBaseBloc>(context),",
        "apiBaseBloc: BlocProvider.of<ApiBaseBloc>(context),\n${useCaseName}UseCase: sl(),");
  } catch (e) {
    print('Error : $e');
  }
}