create static method

void create(
  1. String packageName,
  2. String defaultSavePath,
  3. List<GenClassBean> genClassBeans, {
  4. bool nullSafe = true,
})

Implementation

static void create(String packageName, String defaultSavePath,
    List<GenClassBean> genClassBeans,
    {bool nullSafe = true}) {
  Directory androidTargetDir = Directory(defaultSavePath);
  bool exists = androidTargetDir.existsSync();
  if (!exists) {
    androidTargetDir.createSync(recursive: true);
  }
  genClassBeans.forEach((value) {
    var path = defaultSavePath;
    if (value.savePath.isNotEmpty) {
      path = value.savePath;
    }
    File javaFile = File(path + "/" + value.classInfo.name + ".java");

    //package
    String allContent = "package " + packageName + ";\n";

    //import
    List<String> imports = [
      "import java.util.ArrayList;\n",
      "import java.util.HashMap;\n",
      "import org.jetbrains.annotations.NotNull;\n",
      "import org.jetbrains.annotations.Nullable;\n",
    ];
    imports
        .addAll(value.imports.where((element) => !element.contains(".dart")));
    String importStr = imports
        .toString()
        .replaceAll("[", "")
        .replaceAll("]", "")
        .replaceAll(",", "");

    //property
    String propertyStr = property(value, value.properties);

    //method
    String methodStr = method(value.methods);

    String absStr = "class";
    if (value.classInfo.type == ClassType.abstract) {
      absStr = "interface";
    } else if (value.classInfo.type == ClassType.enumType) {
      absStr = "enum";
    }
    allContent +=
        "${importStr}public ${absStr} ${value.classInfo.name} {\n$propertyStr ${methodStr} \n}";
    if (!nullSafe) {
      allContent = GenFileEdit.removeJavaNullSafe(allContent);
    }
    if (!javaFile.parent.existsSync()) {
      javaFile.parent.createSync(recursive: true);
    }
    javaFile.writeAsStringSync(allContent);
    if (!javaFile.existsSync()) {
      //if not create use dart io, use shell
      _savePath(allContent, javaFile.path);
    }
  });
}