createTemplateByDirectory method

Future<ProjectData> createTemplateByDirectory({
  1. required String name,
  2. bool is_force = false,
  3. required List<String> templates,
  4. required Directory? template_directory,
  5. required Directory directory_out,
  6. required FutureOr<void> onProcces(
    1. String text
    ),
})

Implementation

Future<general_lib_scheme.ProjectData> createTemplateByDirectory({
  required String name,
  bool is_force = false,
  required List<String> templates,
  required Directory? template_directory,
  required Directory directory_out,
  required FutureOr<void> Function(String text) onProcces,
}) async {
  template_directory ??= Directory(join(base_directory.path, "template"));
  try {
    Directory directory_create = Directory(join(directory_out.path, name));
    if (name == ".") {
      name = basename(Directory.current.path);
      directory_create = Directory(join(directory_out.path));
    }
    if (directory_create.existsSync()) {
      if (!is_force) {
        return general_lib_scheme.ProjectData({
          "@type": "error",
          "message": "directory_out_exists",
          "description":
              "Directory ${directory_create.path} already exists (use '--force' to force project generation)"
        });
      }
    }
    await directory_create.create(recursive: true);
    if (templates.isEmpty) {
      templates = ["app_template"];
    }
    if (templates.first == "fullstack_template") {
      templates = [
        "app_template",
        "server_template",
        "client_template",
      ];
    }
    if (templates.first == "fullstack_api_template") {
      templates = [
        "app_api_template",
        "server_api_template",
        "client_api_template",
      ];
    }
    logger.info("Creating ${name} using template ${templates.join(",")}...");
    // await onProcces("Creating ${name} using template ${templates.join(",")}...");
    Progress progress = logger.progress("Starting Create Template");
    await Future.delayed(Duration(milliseconds: 500));
    if (templates.length > 1) {
      for (var i = 0; i < templates.length; i++) {
        String template = templates[i];
        Directory directory_template_package =
            Directory(join(template_directory.path, template));
        if (!directory_template_package.existsSync()) {
          return general_lib_scheme.ProjectData({
            "@type": "error",
            "message": "template_not_found",
            "description":
                "Failed Creating ${name} using template ${template} karena tidak ada template"
          });
        }

        // await onProcces("Create ${template}");

        progress.update("Create ${template}");
        await Future.delayed(Duration(milliseconds: 500));
        Directory directory_create_folder =
            Directory(join(directory_create.path, template));
        if (!directory_create_folder.existsSync()) {
          await directory_create_folder.create(recursive: true);
        }
        directory_template_package.copyTo(
          directory_create_folder,
          ignoreDirList: [
            "build",
            ".dart_tool",
            "node_modules",
          ],
          ignoreFileList: [
            "pubspec.lock",
          ],
        );
        progress.update("Install File");
        await Future.delayed(Duration(milliseconds: 500));

        directory_create_folder.renameRecursive(
          origin_name: template,
          new_name: "${name}_${template.split("_").first}",
          ignoreDirList: [
            "build",
            ".dart_tool",
            "node_modules",
          ],
          ignoreFileList: [
            "pubspec.lock",
          ],
        );

        progress.update("Rename");
        await Future.delayed(Duration(milliseconds: 500));

        directory_create.renameRecursive(
          origin_name: template,
          new_name: "${name}_${template.split("_").first}",
          ignoreDirList: [
            "build",
            ".dart_tool",
            "node_modules",
          ],
          ignoreFileList: [
            "pubspec.lock",
          ],
        );
        progress.update("Rename");
        await Future.delayed(Duration(milliseconds: 500));

        directory_create.renameRecursive(
          origin_name: template.split("_").join(" "),
          new_name:
              "${name}_${template.split("_").first}".split("_").map((e) {
            String text_name = "";
            for (var index_name_loop_data = 0;
                index_name_loop_data < e.length;
                index_name_loop_data++) {
              String text_loop_name = e[index_name_loop_data];
              if (index_name_loop_data == 0) {
                text_name += text_loop_name.toUpperCase();
              } else {
                text_name += text_loop_name.toLowerCase();
              }
            }
            return text_name;
          }).join(" "),
          ignoreDirList: [
            "build",
            ".dart_tool",
            "node_modules",
          ],
          ignoreFileList: [
            "pubspec.lock",
          ],
        );
        progress.update("Rename");
        await Future.delayed(Duration(milliseconds: 500));
        directory_create.renameRecursive(
          origin_name: template.camelCaseClass(),
          new_name: "${name}_${template.split("_").first}".camelCaseClass(),
          ignoreDirList: [
            "build",
            ".dart_tool",
            "node_modules",
          ],
          ignoreFileList: [
            "pubspec.lock",
          ],
        );
        directory_create.renameRecursive(
          origin_name: template.camelCaseClass().toLowerCaseFirstData(),
          new_name: "${name}_${template.split("_").first}"
              .camelCaseClass()
              .toLowerCaseFirstData(),
          ignoreDirList: [
            "build",
            ".dart_tool",
            "node_modules",
          ],
          ignoreFileList: [
            "pubspec.lock",
          ],
        );
        progress.update("Finished Create Template: ${template}");
        await Future.delayed(Duration(milliseconds: 500));
        // directory_create.renameRecursive(
        //   origin_name: template.split("_").join(" "),
        //   new_name: "${name}_${template.split("_").first}".camelCaseClass(),
        //   ignoreDirList: [
        //     "build",
        //     ".dart_tool",
        //     "node_modules",
        //   ],
        //   ignoreFileList: [
        //     "pubspec.lock",
        //   ],
        // );
      }
    } else {
      String template = templates.first;
      Directory directory_template_package =
          Directory(join(template_directory.path, template));

      if (!directory_template_package.existsSync()) {
        return general_lib_scheme.ProjectData({
          "@type": "error",
          "message": "template_not_found",
          "description":
              "Failed Creating ${name} using template ${template} karena tidak ada template"
        });
      }

      progress.update("Copy Files");

      directory_template_package.copyTo(
        directory_create,
        ignoreDirList: [
          "build",
          ".dart_tool",
          "node_modules",
        ],
      );

      progress.update("Rename Template");
      directory_create.renameRecursive(
        origin_name: template,
        new_name: name,
        ignoreDirList: [
          "build",
          ".dart_tool",
          "node_modules",
        ],
      );

      directory_create.renameRecursive(
        origin_name: template.split("_").join(" "),
        new_name: name.split("_").map((e) {
          String text_name = "";
          for (var index_name_loop_data = 0;
              index_name_loop_data < e.length;
              index_name_loop_data++) {
            String text_loop_name = e[index_name_loop_data];
            if (index_name_loop_data == 0) {
              text_name += text_loop_name.toUpperCase();
            } else {
              text_name += text_loop_name.toLowerCase();
            }
          }
          return text_name;
        }).join(" "),
        ignoreDirList: [
          "build",
          ".dart_tool",
          "node_modules",
        ],
      );

      progress.update("Rename Script File");
      directory_create.renameRecursive(
        origin_name: template.camelCaseClass(),
        new_name: name.camelCaseClass(),
        ignoreDirList: [
          "build",
          ".dart_tool",
          "node_modules",
        ],
      );
      directory_create.renameRecursive(
        origin_name: template.camelCaseClass().toLowerCaseFirstData(),
        new_name: name.camelCaseClass().toLowerCaseFirstData(),
        ignoreDirList: [
          "build",
          ".dart_tool",
          "node_modules",
        ],
      );
    }

    progress.complete("Done");
    return general_lib_scheme.ProjectData({
      "@type": "projectData",
      "name": name,
      "directory_out_path": directory_out.path,
      "message": """
Created project ${name} ! In order to get started, run the following commands:

cd ${name}
flutter clean
flutter create .
flutter pub get
"""
    });
  } catch (e) {
    return general_lib_scheme.ProjectData({
      "@type": "error",
      "message": "script_error",
      "description": "Error Script: ${e}"
    });
  }
}