create method

Stream<TelegramClientApiStatus> create({
  1. required String newName,
  2. required Directory directoryBase,
  3. required TelegramClientProjectTemplate telegramClientProjectTemplate,
})

Telegram Client Api For create project

Implementation

Stream<TelegramClientApiStatus> create({
  required String newName,
  required Directory directoryBase,
  required TelegramClientProjectTemplate telegramClientProjectTemplate,
}) async* {
  Directory directory_project = await Future(() async {
    return Directory(
        Directory(path.join(directoryBase.uri.toFilePath(), newName.trim()))
            .uri
            .toFilePath());
  });
  String project_name = path.basename(directory_project.path);

  yield TelegramClientApiStatus(
      telegramClientApiStatusType: TelegramClientApiStatusType.info,
      value: "Starting Create Project: ${project_name}");

  File file_pubspec = File(path.join(directory_project.path, "pubspec.yaml"));
  yield TelegramClientApiStatus(
      telegramClientApiStatusType: TelegramClientApiStatusType.info,
      value: "Check: ${path.basename(file_pubspec.path)}");
  if (!file_pubspec.existsSync()) {
    List<String> arguments = () {
      List<String> defaults_args = [
        "create",
        newName,
        "--no-pub",
      ];
      if (telegramClientProjectTemplate.is_application) {
        defaults_args.addAll([
          "--offline",
        ]);
      } else {
        defaults_args.addAll([
          "--force",
        ]);
      }
      return defaults_args;
    }();
    Process process = await Process.start(
      (telegramClientProjectTemplate.is_application) ? "flutter" : "dart",
      arguments,
      workingDirectory: directory_project.parent.uri.toFilePath(),
    );
    process.stderr.listen((event) {
      stderr.add(event);
    });
    process.stdout.listen((event) {
      stdout.add(event);
    });
    int exit_code = await (process.exitCode);
    if (exit_code != 0) {
      yield (TelegramClientApiStatus(
          telegramClientApiStatusType: TelegramClientApiStatusType.failed,
          value: "Failed Create"));
      return;
    }
  }

  List<ScriptGenerator> scripts =
      await telegramClientProjectTemplate.scripts();

  yield (TelegramClientApiStatus(
      telegramClientApiStatusType: TelegramClientApiStatusType.progress_start,
      value: "Starting Generate: ${directory_project.path}"));

  await for (var event
      in scripts.generateToDirectory(directoryBase: directory_project)) {
    await Future.delayed(Duration(microseconds: 50));
    yield TelegramClientApiStatus(
        telegramClientApiStatusType: TelegramClientApiStatusType.progress,
        value:
            "Generate: ${path.relative(event.file_system_entity.path, from: directory_project.path)}");
  }
  yield (TelegramClientApiStatus(
      telegramClientApiStatusType: TelegramClientApiStatusType.progress_start,
      value: "Finished Generate: ${directory_project.path}"));

  Map yaml_code =
      (yaml.loadYaml(file_pubspec.readAsStringSync(), recover: true) as Map);

  Map pubspecPackageFullTemplate = (yaml_code.clone());

  File file_guide =
      File(path.join(directory_project.path, "guide-telegram_client.md"));
  yield TelegramClientApiStatus(
      telegramClientApiStatusType: TelegramClientApiStatusType.info,
      value: "Check File Guide: ${path.basename(file_guide.path)}");

  await file_guide.writeAsString(guide_telegram_client_markdown());

  // default configuration pubspec
  Map pubspecPackageFullTemplate_default = {
    "repository": "https://github.com/azkadev/telegram_client",
    "homepage": "https://github.com/azkadev/telegram_client",
    "issue_tracker": "https://github.com/azkadev/telegram_client/issues",
    "documentation":
        "https://github.com/azkadev/telegram_client/tree/main/docs",
    "funding": [
      "https://github.com/sponsors/azkadev",
    ],
    "dependencies": {
      "telegram_client": "any",
      "general_lib": "any",
    },
  };

  // update pubspec default
  pubspecPackageFullTemplate
      .general_lib_utils_updateMapIfNotSameOrEmptyOrNull(
    data: pubspecPackageFullTemplate_default,
    ignoreKeys: [
      "@type",
    ],
  );
  pubspecPackageFullTemplate
      .general_lib_utils_removeRecursiveByKeys(keyDatas: ["@type"]);

  String yaml_documents_new = YamlWriter().write(pubspecPackageFullTemplate);
  await file_pubspec.writeAsString(yaml_documents_new);
  // finished update pubspec
  yield (TelegramClientApiStatus(
      telegramClientApiStatusType: TelegramClientApiStatusType.succes,
      value: "Finished"));
}