generateToDirectory method

Stream<ScriptGeneratorStatus> generateToDirectory({
  1. required Directory directoryBase,
  2. OnWrittingScriptGenerator? onWritting,
  3. bool isSkipAutoRename = false,
})

Implementation

Stream<ScriptGeneratorStatus> generateToDirectory({
  required Directory directoryBase,
  OnWrittingScriptGenerator? onWritting,
  bool isSkipAutoRename = false,
}) async* {
  // String base_path = directoryNew.uri.toFilePath();
  /// parse directory, jika tidak
  /// misal data path_slebew/.
  /// akhiran titik
  /// akan di baca base name jadi titik
  final Directory directory_base = Directory(directoryBase.uri.toFilePath());

  /// mendapatkan nama folder

  final String base_name = path.basename(directory_base.path);
  if (directory_base.existsSync() == false) {
    await directory_base.create(
      recursive: true,
    );
  }

  /// loop
  for (final ScriptGenerator scriptGenerator in this) {
    ///
    if (scriptGenerator.file_system_entity_type ==
        FileSystemEntityType.file) {
      /// parse_path
      final String path_origin_file = () {
        if ((path
                .basename(scriptGenerator.directory_base.uri.toFilePath())
                .toLowerCase() ==
            path
                .basenameWithoutExtension(
                    scriptGenerator.file_system_entity.uri.toFilePath())
                .toLowerCase())) {
          String result_path = "";
          if (scriptGenerator.is_generate) {
            result_path = scriptGenerator.file_system_entity.uri.toFilePath();
          } else {
            result_path = path.relative(
              scriptGenerator.file_system_entity.uri.toFilePath(),
              from: scriptGenerator.directory_base.uri.toFilePath(),
            );
          }
          // change name origin if same to new file name
          // return result_path.replaceAll(path.basename(scriptGenerator.directory_base.uri.toFilePath()), base_name);
          return ScriptGenerator.replacerData(
            value: result_path,
            originProjectName: path
                .basename(scriptGenerator.directory_base.uri.toFilePath()),
            newProjectName: base_name,
            isSkipAutoRename: isSkipAutoRename,
            name: path.basename(result_path),
            is_file: true,
          );
        } else {
          String result_path = "";
          if (scriptGenerator.is_generate) {
            result_path = scriptGenerator.file_system_entity.uri.toFilePath();
          } else {
            result_path = path.relative(
              scriptGenerator.file_system_entity.uri.toFilePath(),
              from: scriptGenerator.directory_base.uri.toFilePath(),
            );
          }
          // change name origin if same to new file name
          // old work
          // return result_path.replaceAll(path.basename(scriptGenerator.directory_base.uri.toFilePath()), base_name);
          return ScriptGenerator.replacerData(
            value: result_path,
            originProjectName: path
                .basename(scriptGenerator.directory_base.uri.toFilePath()),
            newProjectName: base_name,
            isSkipAutoRename: isSkipAutoRename,
            name: path.basename(result_path),
            is_file: true,
          );
        }
      }();
      // end parse

      final File file =
          File(path.join(directory_base.uri.toFilePath(), path_origin_file));
      yield ScriptGeneratorStatus(
        file_system_entity: file,
        text: "Check Parent Directory",
      );

      if (file.parent.existsSync() == false) {
        file.parent.createSync(recursive: true);
      } else {
        yield ScriptGeneratorStatus(
          file_system_entity: file,
          text: "Parent Exist",
        );
      }
      final OnWrittingScriptGenerator? onWrittingScriptGenerator_script =
          onWritting;
      if (onWrittingScriptGenerator_script != null) {
        await file.writeAsString(
            (await onWrittingScriptGenerator_script(scriptGenerator, file)));
      } else {
        // await file.writeAsString(scriptGenerator.value.replaceAll(path.basename(scriptGenerator.directory_base.uri.toFilePath()), base_name));
        await file.writeAsString(
          ScriptGenerator.replacerData(
            value: scriptGenerator.value,
            originProjectName: path
                .basename(scriptGenerator.directory_base.uri.toFilePath()),
            newProjectName: base_name,
            isSkipAutoRename: isSkipAutoRename,
            name: path.basename(file.path),
            is_file: false,
          ),
        );
      }
    } else if (scriptGenerator.file_system_entity_type ==
        FileSystemEntityType.directory) {
      yield ScriptGeneratorStatus(
        file_system_entity: scriptGenerator.file_system_entity,
        text: "Generate Children",
      );
      final stream = scriptGenerator.children.generateToDirectory(
        directoryBase: directory_base,
        onWritting: onWritting,
        isSkipAutoRename: isSkipAutoRename,
      );
      await for (var event in stream) {
        yield event;
      }
    } else {
      // yield ScriptGeneratorStatus(
      //   file_system_entity: scriptGenerator.file_system_entity,
      //   text: "Unimplemented: ${}",
      // );

      // unimplemented link
    }
  }

  return;
}