makeController static method

Future makeController(
  1. String className,
  2. String value, {
  3. String folderPath = controllersFolder,
  4. bool forceCreate = false,
  5. String? creationPath,
})

Creates a new Controller.

Implementation

static Future makeController(String className, String value,
    {String folderPath = controllersFolder,
    bool forceCreate = false,
    String? creationPath}) async {
  String name = className.replaceAll(RegExp(r'(_?controller)'), "");
  ReCase nameReCase = ReCase(name);

  // create missing directories in the project
  await createDirectoriesFromCreationPath(creationPath, folderPath);

  // create file path
  String filePath = createPathForDartFile(
      folderPath: folderPath,
      className: className,
      prefix: "controller",
      creationPath: creationPath);

  await _makeDirectory(folderPath);
  await _checkIfFileExists(filePath, shouldForceCreate: forceCreate);

  String classImport =
      "import '/app/controllers/${creationPath != null ? '$creationPath/' : ''}${nameReCase.snakeCase}_controller.dart';";

  await MetroService.addToConfig(
      configName: "decoders",
      classImport: classImport,
      createTemplate: (file) {
        String controllerName = "${nameReCase.pascalCase}Controller";
        if (file.contains(controllerName)) {
          return "";
        }

        if (file.contains("final Map<Type, dynamic> controllers")) {
          RegExp reg =
              RegExp(r'final Map<Type, dynamic> controllers = \{([^}]*)\};');
          String temp = """
final Map<Type, dynamic> controllers = {${reg.allMatches(file).map((e) => e.group(1)).toList()[0]}
$controllerName: () => $controllerName(),
};""";

          return file.replaceFirst(
            RegExp(r'final Map<Type, dynamic> controllers = \{([^}]*)\};'),
            temp,
          );
        }

        if (file.contains(
            "final Map<Type, BaseController Function()> controllers")) {
          RegExp reg = RegExp(
              r'final Map<Type, BaseController Function\(\)> controllers = \{([^}]*)\};');
          String temp = """
final Map<Type, BaseController Function()> controllers = {${reg.allMatches(file).map((e) => e.group(1)).toList()[0]}
$controllerName: () => $controllerName(),
};""";

          return file.replaceFirst(
            RegExp(
                r'final Map<Type, BaseController Function\(\)> controllers = \{([^}]*)\};'),
            temp,
          );
        }

        RegExp reg = RegExp(
            r'final Map<Type, BaseController> controllers = \{([^}]*)\};');
        String temp = """
final Map<Type, BaseController> controllers = {${reg.allMatches(file).map((e) => e.group(1)).toList()[0]}
$controllerName: $controllerName(),
};""";

        return file.replaceFirst(
          RegExp(
              r'final Map<Type, BaseController> controllers = \{([^}]*)\};'),
          temp,
        );
      });

  await _createNewFile(filePath, value, onSuccess: () {
    MetroConsole.writeInGreen(
        '[Controller] ${nameReCase.snakeCase}_controller created 🎉');
  });
}