setup method

Stream<GeneralLibraryApiStatus> setup({
  1. required Directory directoryBase,
})

Server Universe

Implementation

Stream<GeneralLibraryApiStatus> setup({
  required Directory directoryBase,
}) async* {
  Directory directory_project = Directory(directoryBase.uri.toFilePath());
  // ignore: unused_local_variable
  String project_name = path.basename(directory_project.path);

  File file_pubspec = File(path.join(directory_project.path, "pubspec.yaml"));
  if (!file_pubspec.existsSync()) {
    yield GeneralLibraryApiStatus(serverUniverseApiStatusType: GeneralLibraryApiStatusType.failed, value: "Failed Create Project:");
    return;
  }

  // android
  Directory directory_android = Directory(path.join(directory_project.uri.toFilePath(), "android"));

  if (directory_android.existsSync()) {
    yield GeneralLibraryApiStatus(serverUniverseApiStatusType: GeneralLibraryApiStatusType.info, value: "Started Setup Android");

    // auto setup settings gradle
    File file_settings_gradle = File(path.join(directory_android.uri.toFilePath(), "settings.gradle"));
    if (file_settings_gradle.existsSync()) {
      yield GeneralLibraryApiStatus(serverUniverseApiStatusType: GeneralLibraryApiStatusType.info, value: "Check File: android/settings.gradle");
      List<String> data_origins = file_settings_gradle.readAsStringSync().trim().split("\n");
      String new_gradle_version = "1.9.22";
      bool is_found_new_update_gradle_file = false;
      RegExp regExp = RegExp("(([ ]+)?id([ ]+)\"org.jetbrains.kotlin.android\"([ ]+)version([ ]+)(.*)([ ]+)apply([ ]+)false)", caseSensitive: false);
      for (var i = 0; i < data_origins.length; i++) {
        String data_origin = data_origins[i];
        if (regExp.hashData(data_origin.trim())) {
          RegExpMatch? regExpMatch = regExp.firstMatch(data_origin);
          if (regExpMatch == null) {
            continue;
          }
          String version_gradle = (regExpMatch.group(6) ?? "").trim();

          if (version_gradle != json.encode(new_gradle_version)) {
            yield GeneralLibraryApiStatus(serverUniverseApiStatusType: GeneralLibraryApiStatusType.info, value: "Upgrade Gradle Version ${version_gradle} To ${new_gradle_version}");
            data_origins[i] = data_origins[i].replaceAll(regExp, "${(regExpMatch.group(2) ?? "")}id \"org.jetbrains.kotlin.android\" version ${json.encode(new_gradle_version)} apply false");
            is_found_new_update_gradle_file = true;
          }
          break;
        }
      }
      if (is_found_new_update_gradle_file) {
        file_settings_gradle.writeAsStringSync(data_origins.join("\n"));
      }
    }
    // finished auto setup settings gradle android

//
    Directory directory_android_kotlin = Directory(path.join(directory_android.uri.toFilePath(), "app/src/main/kotlin"));
    if (directory_android_kotlin.existsSync()) {
      List<FileSystemEntity> file_kotlins = directory_android_kotlin.listSync(recursive: true);
      for (var file_kotlin in file_kotlins) {
        if (file_kotlin is File) {
          if (path.extension(file_kotlin.uri.toFilePath()) == ".kt") {
            List<String> data_origins = file_kotlin.readAsStringSync().trim().split("\n");
            String package_name_kotlin = data_origins.firstWhere((data) => RegExp("^(package([ ]+))", caseSensitive: false).hasMatch(data));
            yield GeneralLibraryApiStatus(serverUniverseApiStatusType: GeneralLibraryApiStatusType.info, value: "Update package kotlin: ${package_name_kotlin}");
            /// if use gamepads but gamepads is ugly developer fucking stupid
//               final String new_content_kotlin = """
// ${package_name_kotlin}

// import android.hardware.input.InputManager
// import android.os.Handler
// import android.view.InputDevice
// import android.view.KeyEvent
// import android.view.MotionEvent
// import io.flutter.embedding.android.FlutterActivity
// import org.flame_engine.gamepads_android.GamepadsCompatibleActivity
// import io.flutter.embedding.android.FlutterFragmentActivity

// class MainActivity: FlutterFragmentActivity(), GamepadsCompatibleActivity {
//     var keyListener: ((KeyEvent) -> Boolean)? = null
//     var motionListener: ((MotionEvent) -> Boolean)? = null

//     override fun dispatchGenericMotionEvent(motionEvent: MotionEvent): Boolean {
//         return motionListener?.invoke(motionEvent) ?: false
//     }

//     override fun dispatchKeyEvent(keyEvent: KeyEvent): Boolean {
//         return keyListener?.invoke(keyEvent) ?: false
//     }

//     override fun registerInputDeviceListener(
//       listener: InputManager.InputDeviceListener, handler: Handler?) {
//         val inputManager = getSystemService(INPUT_SERVICE) as InputManager
//         inputManager.registerInputDeviceListener(listener, null)
//     }

//     override fun registerKeyEventHandler(handler: (KeyEvent) -> Boolean) {
//         keyListener = handler
//     }

//     override fun registerMotionEventHandler(handler: (MotionEvent) -> Boolean) {
//         motionListener = handler
//     }
// }
// """;
            // file_kotlin.writeAsStringSync(new_content_kotlin);

            final String new_content_kotlin = """
${package_name_kotlin}


import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity()

""";
            file_kotlin.writeAsStringSync(new_content_kotlin);

          }
        }
      }
    }

    File file_android_build_gradle = File(path.join(directory_android.uri.toFilePath(), "app/build.gradle"));
    if (file_android_build_gradle.existsSync()) {
      yield GeneralLibraryApiStatus(serverUniverseApiStatusType: GeneralLibraryApiStatusType.info, value: "Check File: app/build.gradle");
      List<String> data_origins = file_android_build_gradle.readAsStringSync().trim().split("\n");
      String new_compile_sdk = "34";
      String new_min_sdk_version = "23";
      String new_target_sdk_version = "34";

      RegExp regExp_compile_sdk = RegExp("(([ ]+)?compileSdk([ ]+)(.*))", caseSensitive: false);
      bool is_found_regex_compile_sdk = false;

      RegExp regExp_min_sdk_version = RegExp("(([ ]+)?(minSdk(Version)?)([ ]+)(.*))", caseSensitive: false);
      bool is_found_regex_min_sdk_version = false;

      RegExp regExp_target_sdk_version = RegExp("(([ ]+)?(targetSdk(Version)?)([ ]+)(.*))", caseSensitive: false);
      bool is_found_regex_target_sdk_version = false;

      bool is_found_new_update_build_gradle_file = false;
      for (var i = 0; i < data_origins.length; i++) {
        String data_origin = data_origins[i];
        if (is_found_regex_compile_sdk == false) {
          if (regExp_compile_sdk.hashData(data_origin.trim())) {
            is_found_regex_compile_sdk = true;
            RegExpMatch? regExpMatch = regExp_compile_sdk.firstMatch(data_origin);
            if (regExpMatch == null) {
              continue;
            }
            String version_compile_sdk = (regExpMatch.group(4) ?? "").trim();

            if (version_compile_sdk != new_compile_sdk) {
              yield GeneralLibraryApiStatus(serverUniverseApiStatusType: GeneralLibraryApiStatusType.info, value: "Update Build Gradle Compile Sdk ${version_compile_sdk} To ${new_compile_sdk}");
              data_origins[i] = data_origins[i].replaceAll(regExp_compile_sdk, "${(regExpMatch.group(2) ?? "")}compileSdk ${new_compile_sdk}");
              is_found_new_update_build_gradle_file = true;
            }
          }
          continue;
        }
        if (is_found_regex_min_sdk_version == false) {
          if (regExp_min_sdk_version.hashData(data_origin.trim())) {
            is_found_regex_min_sdk_version = true;
            RegExpMatch? regExpMatch = regExp_min_sdk_version.firstMatch(data_origin);
            if (regExpMatch == null) {
              continue;
            }
            String version_min_sdk_version = (regExpMatch.group(6) ?? "").trim();

            if (version_min_sdk_version != new_min_sdk_version) {
              yield GeneralLibraryApiStatus(serverUniverseApiStatusType: GeneralLibraryApiStatusType.info, value: "Update Build Gradle Compile Sdk ${version_min_sdk_version} To ${new_min_sdk_version}");
              data_origins[i] = data_origins[i].replaceAll(regExp_min_sdk_version, "${(regExpMatch.group(2) ?? "")}${regExpMatch.group(3)} ${new_min_sdk_version}");
              is_found_new_update_build_gradle_file = true;
            }
          }
          continue;
        }

        if (is_found_regex_target_sdk_version == false) {
          if (regExp_target_sdk_version.hashData(data_origin.trim())) {
            is_found_regex_target_sdk_version = true;
            RegExpMatch? regExpMatch = regExp_target_sdk_version.firstMatch(data_origin);
            if (regExpMatch == null) {
              continue;
            }
            String version_target_sdk_version = (regExpMatch.group(6) ?? "").trim();

            if (version_target_sdk_version != new_target_sdk_version) {
              yield GeneralLibraryApiStatus(serverUniverseApiStatusType: GeneralLibraryApiStatusType.info, value: "Update Build Gradle Compile Sdk ${version_target_sdk_version} To ${new_target_sdk_version}");
              data_origins[i] = data_origins[i].replaceAll(regExp_target_sdk_version, "${(regExpMatch.group(2) ?? "")}${regExpMatch.group(3)} ${new_target_sdk_version}");
              is_found_new_update_build_gradle_file = true;
            }
          }
          continue;
        }
      }
      if (is_found_new_update_build_gradle_file) {
        file_android_build_gradle.writeAsStringSync(data_origins.join("\n"));
      }
    }

    //

    // // implement not finished
    // File file_android_manifest = File(path.join(directory_android.uri.toFilePath(), "app/src/main/AndroidManifest.xml"));
    // if (file_android_manifest.existsSync()) {
    //   yield GeneralLibraryApiStatus(serverUniverseApiStatusType: GeneralLibraryApiStatusType.info, value: "Check File: app/src/main/AndroidManifest.xml");
    //   List<String> data_origins = file_android_manifest.readAsStringSync().trim().split("\n");
    //   String new_gradle_version = "1.9.22";
    //   bool is_found_new_update_gradle_file = false;
    //   for (var i = 0; i < data_origins.length; i++) {
    //     String data_origin = data_origins[i];
    //     RegExp regExp = RegExp("(([ ]+)?id([ ]+)\"org.jetbrains.kotlin.android\"([ ]+)version([ ]+)(.*)([ ]+)apply([ ]+)false)");
    //     if (regExp.hashData(data_origin.trim())) {
    //       RegExpMatch? regExpMatch = regExp.firstMatch(data_origin);
    //       if (regExpMatch == null) {
    //         continue;
    //       }
    //       String version_gradle = (regExpMatch.group(6) ?? "").trim();

    //       if (version_gradle != json.encode(new_gradle_version)) {
    //         yield GeneralLibraryApiStatus(serverUniverseApiStatusType: GeneralLibraryApiStatusType.info, value: "Upgrade Gradle Version ${version_gradle} To ${new_gradle_version}");
    //         data_origins[i] = data_origins[i].replaceAll(regExp, "${(regExpMatch.group(2) ?? "")}id \"org.jetbrains.kotlin.android\" version ${json.encode(new_gradle_version)} apply false");
    //         is_found_new_update_gradle_file = true;
    //       }
    //       break;
    //     }
    //   }
    //   if (is_found_new_update_gradle_file) {
    //     file_settings_gradle.writeAsStringSync(data_origins.join("\n"));
    //   }
    // }
    //
    //
  }
}