packageFullTemplateDartCli function

Future<void> packageFullTemplateDartCli(
  1. List<String> args_raw
)

Telegram Client Api For Interact With Library

Implementation

Future<void> packageFullTemplateDartCli(List<String> args_raw) async {
  TelegramClientApi packageFullTemplateDartApi = TelegramClientApi();
  Args args = Args(args_raw);

  String executable = Dart.executable.trim();

  if (path.split(executable).contains("dart")) {
    executable = "telegram_client";
  }

  bool is_interactive = true;
  if (Platform.environment["no_interactive"] == "true") {
    is_interactive = false;
  }
  // bool is_help = (args.contains("-h")) ? true : args.contains("--help");

  String command = (args.arguments.firstOrNull ?? "").toLowerCase();
  List<String> commands = [
    "build",
    "create",
    "deploy",
    "install",
    "run",
    "reload",
    "telegram_bot_api",
  ];
  commands.sort();
  if (commands.contains(command) == false) {
    if (is_interactive) {
      command = logger.chooseOne(
        "Pilih",
        choices: commands,
        display: (choice) {
          return choice.toUpperCaseFirstData();
        },
      );
    } else {
      print("Please use command: ${commands.join("\n")}");
      exit(1);
    }
  }

  if (command == "create") {
    List<TelegramClientProjectTemplate> telegramClientProjectTemplates =
        TelegramClientProjectTemplate.templates;
    telegramClientProjectTemplates.sort((a, b) => a.name.compareTo(b.name));
    if (["--help", "-h"].contains(args.after(command) ?? "")) {
      String help_create = """
For Create Program With Template Please Use Command

Commands:

${executable} create name_poject options

OPTIONS:

--template - ${telegramClientProjectTemplates.map((e) => e.name).join(", ")}

Example: ${executable} create name_project --template ${telegramClientProjectTemplates.first.name}
""";

      print(help_create.trim());
      exit(0);
    }
    Directory directory_current = Directory.current;
    String new_project_name = await Future(() async {
      String new_project_name_from_command =
          (args.after(command) ?? "").toLowerCase();
      if (new_project_name_from_command.isNotEmpty) {
        return new_project_name_from_command;
      }
      if (is_interactive) {
        while (true) {
          await Future.delayed(Duration(microseconds: 1));
          String server_universeDartPlatformType = logger.prompt(
            "Name New Project: ",
          );

          if (server_universeDartPlatformType.isNotEmpty) {
            return server_universeDartPlatformType;
          }
        }
      }
      return "";
    });
    TelegramClientProjectTemplate telegramClientProjectTemplate =
        await Future(() async {
      String type_procces =
          (args.after("--template") ?? "").toLowerCase().trim();
      TelegramClientProjectTemplate? telegramClientProjectTemplate =
          telegramClientProjectTemplates.firstWhereOrNull(
              (element) => element.name.toLowerCase() == type_procces);
      if (telegramClientProjectTemplate != null) {
        return telegramClientProjectTemplate;
      }
      if (is_interactive) {
        while (true) {
          await Future.delayed(Duration(microseconds: 1));
          TelegramClientProjectTemplate telegramClientProjectTemplate =
              logger.chooseOne(
            "Template: ",
            choices: telegramClientProjectTemplates,
            display: (choice) {
              return choice.name;
            },
          );

          return telegramClientProjectTemplate;
        }
      }
      return telegramClientProjectTemplates.first;
    });
    if (new_project_name.isEmpty) {
      print("Failed");
      exit(1);
    }
    var strm = packageFullTemplateDartApi
        .create(
            newName: new_project_name,
            directoryBase: directory_current,
            telegramClientProjectTemplate: telegramClientProjectTemplate)
        .listen((event) {
      printed(event);
    });

    await strm.asFuture();
    exit(0);
  }
  if (command == "install") {
    List<String> script_types = [
      "library",
      "script",
    ];
    script_types.sort();
    String command_after = (args.after(command) ?? "").trim();

    if (["--help", "-h"].contains(command_after)) {
      String help_create = """
For Install

Commands:

${executable} install {script_types}

Script Types:
${script_types.map((e) => "${" " * 2} - ${e}").join("\n")}

Example: ${executable} install library tdlib
""";

      print(help_create.trim());
      exit(0);
    }
    if (script_types.contains(command_after) == false) {
      command_after = await Future(() async {
        if (is_interactive) {
          while (true) {
            await Future.delayed(Duration(microseconds: 1));
            String script_type = logger.chooseOne(
              "Install Type: ",
              choices: script_types,
              display: (choice) {
                return choice;
              },
            );

            return script_type;
          }
        }
        return script_types.first;
      });
    }
    if (command_after == "library") {
      List<String> library_types = [
        "tdlib",
      ];
      String command_after_library = (args.after(command_after) ?? "").trim();

      if (["--help", "-h"].contains(command_after_library)) {
        String help_create = """
For Install Library

Commands:

${executable} install library {library_types}

Library Types:
${library_types.map((e) => "${" " * 2} - ${e}").join("\n")}

Example: ${executable} install library ${library_types.first}
""";

        print(help_create.trim());
        exit(0);
      }
      TelegramClientLibraryType telegramClientLibraryType =
          await Future(() async {
        // TelegramClientLibraryType? telegramClientLibraryType = TelegramClientLibraryType.values.firstWhereOrNull((element) => element.name.toLowerCase() == command_after_library);

        if (is_interactive) {
          while (true) {
            await Future.delayed(Duration(microseconds: 1));
            TelegramClientLibraryType telegramClientLibraryType =
                logger.chooseOne(
              "Library Type: ",
              choices: TelegramClientLibraryType.values,
              display: (choice) {
                return choice.name;
              },
            );

            return telegramClientLibraryType;
          }
        }
        return TelegramClientLibraryType.tdlib;
      });
      var strm = packageFullTemplateDartApi
          .installLibrary(telegramClientLibraryType: telegramClientLibraryType)
          .listen((event) {
        printed(event);
      });

      await strm.asFuture();

      exit(0);
      //
      //
    }
  }
  if (command == "telegram_bot_api") {
    List<Map<dynamic, dynamic>> parametersRequest = [];
    String input_file =
        (args.after("-i") ?? args.after("--input") ?? "").trim();

    File file_input = File(input_file);
    if (file_input.existsSync()) {
      try {
        parametersRequest = json.decode(file_input.readAsStringSync());
      } catch (e) {}
    }

    Map<dynamic, dynamic> json_data = <dynamic, dynamic>{};
    if (args.contains("--@type") || args.contains("-@type")) {
      String parameters_key = "";
      for (var element in args.arguments) {
        if (RegExp(r"(-(-)?(@)?[a-z]+)", caseSensitive: false)
            .hasMatch(element)) {
          parameters_key = element.replaceAll(RegExp("-(-)?"), "");
          json_data[parameters_key] = "";
          continue;
        }
        if (parameters_key.isEmpty) {
          continue;
        }
        if ([
          "chat_id",
          "message_id",
          "from_chat_id",
          "reply_to_message_id",
        ].contains(parameters_key)) {
          if (RegExp("^(@)", caseSensitive: false).hasMatch(element)) {
            json_data[parameters_key] = element;
          } else if (RegExp("([a-z_]+)", caseSensitive: false)
              .hasMatch(element)) {
            json_data[parameters_key] = "@${element.trim()}";
          } else {
            json_data[parameters_key] = (num.tryParse(element) ?? 0).toInt();
          }
        } else {
          try {
            json_data[parameters_key] = json.decode(element);
          } catch (e) {
            json_data[parameters_key] = element;
          }
        }
      }
      if (json_data.isNotEmpty) {
        parametersRequest.insert(0, json_data);
      }
    }

    await packageFullTemplateDartApi
        .telegramBotApi(parametersRequest: parametersRequest)
        .listen((event) {
      printed(event);
    }).asFuture();

    exit(0);
  }
  if (command == "reload") {
    File? file = getPackageDirectory();
    if (file == null) {
      exit(1);
    }
    Directory directory = file.parent.parent;
    Directory directory_pub =
        Directory(path.join(directory.path, ".dart_tool", "pub"));
    if (directory_pub.existsSync()) {
      await directory_pub.delete(recursive: true);
    }
    logger.info("Succes Reload");
    exit(0);
  }
  exit(1);
}