createUsaCase method

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

Implementation

Future<void> createUsaCase(String featureName, String useCaseName,
    String requestModelName, String apiMethod) async {
  String destinationPath =
      PathHandler().getUseCasesPath(featureName: featureName);
  String destinationFilePath = '$destinationPath/$useCaseName$kDart';
  if (Utility.doesFileExist(filePath: destinationFilePath)) {
    Utility.errorPrinter(
        text: '$kFileAT $destinationFilePath $kExistChooseDifferentUseCase');
    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 dependencyName = Utility.getPackageNameAndVersion();
  String dependencyPath = PathHandler().getDependencyPath();
  if (!Directory(dependencyPath).existsSync()) {
    Utility.errorPrinter(
        text: '$kDependency $dependencyName $kNotFoundPubCacheDir');
    return;
  }
  String dependencyLibPath = path.join(dependencyPath, kLib);
  if (!Directory(dependencyLibPath).existsSync()) {
    Utility.errorPrinter(
        text: '$kDependency $dependencyName $kDoesNotContainLibFolder');
    return;
  }
  String sourceFolderPath =
      '$dependencyLibPath${PathHandler().getUseCasesSourcePath(featureName: featureName)}';
  Utility.grantPermissions(folderPath: sourceFolderPath);

  if (!Directory(sourceFolderPath).existsSync()) {
    Utility.errorPrinter(text: '$kError $sourceFolderPath $kFolderNotExist');
    return;
  }

  try {
    destinationPath = '$destinationPath';
    await Utility.copyFolder(
        sourcePath: sourceFolderPath, destinationPath: destinationPath);
    await Utility.renameFilesWithSubstring(
        path: destinationPath,
        substring: kBasicStructureLowerCase,
        newName: useCaseName);
    String targetFileName = '$useCaseName$kUseCasesPrefixUnderscore$kDart';
    await Utility.processFiles(
        path: destinationPath,
        substring: kStartComment,
        replacement: kWhitespaces,
        isPascalCase: false,
        targetFileName: targetFileName);
    await Utility.processFiles(
        path: destinationPath,
        substring: kEndComment,
        replacement: kWhitespaces,
        isPascalCase: false,
        targetFileName: targetFileName);
    await Utility.processFiles(
        path: destinationPath,
        substring: kBasicStructurePascalCase,
        replacement: useCaseName,
        isPascalCase: true,
        targetFileName: targetFileName);
    await Utility.processFiles(
        path: destinationPath,
        substring: kBasicStructureCamelCase,
        replacement: useCaseName,
        isPascalCase: true,
        targetFileName: targetFileName);
    await Utility.processFiles(
        path: destinationPath,
        substring: kCommonCap,
        replacement: camelCase(featureName),
        isPascalCase: true,
        targetFileName: targetFileName);
    await Utility.processFiles(
        path: destinationPath,
        substring: kBasicStructureLowerCase,
        replacement: useCaseName,
        isPascalCase: false,
        targetFileName: targetFileName);
    await Utility.processFiles(
        path: destinationPath,
        substring: kCommon.toLowerCase(),
        replacement: featureName,
        isPascalCase: false,
        targetFileName: targetFileName);
    String? name = Utility.getPubspecInfo();
    if (name != null) {
      await Utility.processFiles(
          path: destinationPath,
          substring: kFlutterCleanArchitecture,
          replacement: name,
          isPascalCase: false,
          targetFileName: targetFileName);
    }
    String useCaseFilePath =
        '$destinationPath/${useCaseName}$kUseCasesPrefixUnderscore$kDart';
    if (requestModelName.isNotEmpty) {
      await Utility.processFiles(
          path: destinationPath,
          substring: kParams,
          replacement:
              "${Utility.capitalizeFirstCharacter(input: requestModelName)}",
          isPascalCase: false,
          targetFileName: targetFileName);
      await Utility.processFiles(
          path: destinationPath,
          substring: kNoRequest,
          replacement: requestModelName,
          isPascalCase: true,
          targetFileName: targetFileName);
      await Utility.processFiles(
          path: destinationPath,
          substring: '$kCall${camelCase(useCaseName)}()',
          replacement:
              "$kCall${camelCase(useCaseName)}(${Utility.capitalizeFirstCharacter(input: requestModelName)})",
          isPascalCase: false,
          targetFileName: targetFileName);
      final usecaseFileImport = [
        PathHandler().getUseCaseRequestModelImport(
            featureName: featureName, useCaseName: useCaseName)
      ];
      await Utility.addImport(
        filePath: useCaseFilePath,
        importStatements: usecaseFileImport,
      );
    }

    print('$kUseCase $useCaseName $kCreatedAt $destinationPath');
    final nameOfFeature = featureName.toLowerCase();
    final featurePascalCase = featureName.toLowerCase();
    String pascalCase = Utility.toPascalCase(input: useCaseName);
    final filePath = PathHandler().getDIFilePath();
    final usecaseImport = PathHandler()
        .getUseCaseImport(featureName: featureName, useCaseName: useCaseName);
    final injectionContainerImports = [
      usecaseImport,
      // "import 'package:${name ?? ''}/features/$nameOfFeature/domain/repositories/${nameOfFeature}_repo.dart';"
    ];
    final injectionContainerStatements = CodeHandler()
        .getInjectionContainerStatements(
            pascalCase: pascalCase, featureName: featureName);
    await Utility().addImportAndChangesInFile(
        filePath: '$filePath$kInjectionContainer',
        importStatements: injectionContainerImports,
        methodCalls: injectionContainerStatements);
    Utility.successPrinter(text: '$kUseCaseAddedToInjection');

    ///Add use_case in page bloc
    await Utility.addImport(
      filePath: PathHandler().getPagePath(featureName: featureName),
      importStatements: [
        PathHandler().getInjectionContainerImport(),
      ],
    );
    await Utility.findAndReplace(
        filePath: PathHandler().getPagePath(featureName: featureName),
        searchString: CodeHandler().getPageSearchString(),
        replacement:
            CodeHandler().getPageReplacementString(useCaseName: useCaseName));
  } catch (e) {
    print('$kError $e');
  }
}