native2flutter function

Future<void> native2flutter(
  1. FlutterPlatformConfig flutterConfig,
  2. bool nullSafeSupport, {
  3. AndroidPlatformConfig? androidConfig,
  4. IosPlatformConfig? iosConfig,
})

Implementation

Future<void> native2flutter(
  FlutterPlatformConfig flutterConfig,
  bool nullSafeSupport, {
  AndroidPlatformConfig? androidConfig,
  IosPlatformConfig? iosConfig,
}) async {
  Directory directory =
      Directory(flutterConfig.sourceCodePath + "/native2flutter");
  if (!directory.existsSync()) {
    _genFlutterParse([], flutterConfig.sourceCodePath,
        flutterConfig.channelName, nullSafeSupport);
    return;
  }
  List<GenClassBean> list = await platforms_source_gen_init(
    flutterConfig.sourceCodePath + "/native2flutter", //you dart file path
  );
  if (list.isEmpty) {
    _genFlutterParse([], flutterConfig.sourceCodePath,
        flutterConfig.channelName, nullSafeSupport);
    return;
  }
  await autoCreateJsonParse(list, directory.path, nullSafeSupport);
  list.forEach((element) {
    element.methods.removeWhere((element) => element.name == "toJson");
  });
  list.forEach((element) {
    var fileConfig = FileConfig.copy(flutterConfig, androidConfig, iosConfig);
    //set custom save path
    String classPath = element.path.split(":")[1];
    classPath = "./lib" + classPath.substring(classPath.indexOf("/"));
    Map<int, String> lines = File(classPath).readAsLinesSync().asMap();
    for (int index in lines.keys) {
      if (index == 0) {
        if (lines[index]?.contains("FileConfig") != true) {
          Native2Flutter.fileConfigs.add(fileConfig);
          break;
        }
      } else if (lines[index]?.startsWith("//") == true) {
        if (lines[index]?.contains("androidSavePath") == true) {
          String savePath = lines[index]?.split("=")[1].trim() ?? "";
          fileConfig.flutterPlatformConfig.savePath = savePath;
          fileConfig.androidPlatformConfig?.savePath = savePath;
          // fileConfig.iosPlatformConfig?.savePath = savePath;
        } else if (lines[index]?.contains("channelName") == true) {
          String channelName = lines[index]?.split("=")[1].trim() ?? "";
          fileConfig.flutterPlatformConfig.channelName = channelName;
          fileConfig.androidPlatformConfig?.channelName = channelName;
          // fileConfig.iosPlatformConfig?.channelName = channelName;
        } else if (lines[index]?.contains("packageName") == true) {
          String packageName = lines[index]?.split("=")[1].trim() ?? "";
          fileConfig.androidPlatformConfig?.packageName = packageName;
        }
      } else {
        Native2Flutter.fileConfigs.add(fileConfig);
        break;
      }
    }
  });

  _genFlutterParse(list, flutterConfig.sourceCodePath,
      flutterConfig.channelName, nullSafeSupport);

  Native2Flutter.fileConfigs.asMap().forEach((index, config) {
    ////////////////////////android//////////////////////////
    ////////////////////////android//////////////////////////
    ////////////////////////android//////////////////////////
    if (androidConfig != null) {
      JavaFileUtils.genJavaCode(
          [list[index]],
          config.androidPlatformConfig!.packageName,
          config.androidPlatformConfig!.savePath,
          ".native2flutter",
          nullSafeSupport: nullSafeSupport);
      _gentJavaImpl(
          [list[index]],
          config.androidPlatformConfig!.packageName,
          config.androidPlatformConfig!.savePath,
          config.androidPlatformConfig!.channelName,
          nullSafeSupport);
    }
  });

  ////////////////////////ios//////////////////////////
  ////////////////////////ios//////////////////////////
  ////////////////////////ios//////////////////////////
  if (iosConfig != null) {
    ObjcFileUtils.genObjcCode(list, iosConfig.iosProjectPrefix,
        iosConfig.savePath, ".native2flutter");
    ObjcFileUtils.gentObjcImpl(
        list, iosConfig.iosProjectPrefix, iosConfig.savePath);
  }
}