toScriptGenerate method

List<ScriptGenerator> toScriptGenerate({
  1. Directory? directoryBase,
  2. required ScriptGeneratorOptions scriptGeneratorOptions,
})

Implementation

List<ScriptGenerator> toScriptGenerate({
  Directory? directoryBase,
  required ScriptGeneratorOptions scriptGeneratorOptions,
}) {
  final List<ScriptGenerator> sles = [];
  local_sort();
  final FileSystemEntity? fileSystemEntity_base = firstWhereOrNull((element) {
    if (element is Directory) {
      return true;
    }
    return false;
  });

  final Directory directory = () {
    if (directoryBase != null) {
      return directoryBase;
    }
    if (fileSystemEntity_base != null) {
      return fileSystemEntity_base.parent;
    }
    return Directory.current;
  }();

  final List<String> file_system_entity_ignores =
      scriptGeneratorOptions.fileSystemEntityIgnore.toGlob();

  for (final element in FileSystemEntityIgnore.getFileIgnoresByDirectory(
      currentPath: directory.uri.toFilePath())) {
    if (file_system_entity_ignores.contains(element) == false) {
      file_system_entity_ignores.add(element);
    }
  }
  final List<RegExp> file_system_entity_ignores_regexp =
      file_system_entity_ignores.map((e) => RegExp(e)).toList();

  for (var i = 0; i < length; i++) {
    final FileSystemEntity fileSystemEntity = this[i];
    if (file_system_entity_ignores_regexp
        .globContains(fileSystemEntity.path)) {
      continue;
    }
    if (fileSystemEntity is File) {
      final String base_name =
          path.basename(fileSystemEntity.uri.toFilePath());
      if (["pubspec.lock"].contains(base_name.toLowerCase().trim())) {
        if (scriptGeneratorOptions.isVerbose) {
          print(
              "SKIP ON FILE: ${fileSystemEntity.statSync().type} ${path.relative(fileSystemEntity.uri.toFilePath(), from: directory.path)}");
        }
        continue;
      }
      final List<String> paths_folders =
          path.split(fileSystemEntity.uri.toFilePath());
      if (paths_folders.contains(".dart_tool")) {
        if (scriptGeneratorOptions.isVerbose) {
          print(
              "SKIP ON FILE: ${fileSystemEntity.statSync().type} ${path.relative(fileSystemEntity.uri.toFilePath(), from: directory.path)}");
        }
        continue;
      }

      final File file = File(fileSystemEntity.uri.toFilePath());
      String data = "";
      try {
        data = file.readAsStringSync();
      } catch (e) {
        continue;
      }
      sles.add(ScriptGenerator(
        is_generate: false,
        directory_base: directory,
        file_system_entity: fileSystemEntity,
        state_data: {},
        value: data,
        file_system_entity_type: fileSystemEntity.statSync().type,
        children: [],
      ));
    } else if (fileSystemEntity is Directory) {
      final String base_name =
          path.basename(fileSystemEntity.uri.toFilePath());
      final List<String> paths_folders =
          path.split(fileSystemEntity.uri.toFilePath());
      if (RegExp(r"^([.])", caseSensitive: false).hasMatch(base_name)) {
        if ([".github", ".idea"].contains(base_name) == false) {
          continue;
        }
      }
      if (paths_folders.contains(".dart_tool")) {
        if (scriptGeneratorOptions.isVerbose) {
          print(
              "SKIP ON DIR: ${fileSystemEntity.statSync().type} ${path.relative(fileSystemEntity.uri.toFilePath(), from: directory.path)}");
        }
        continue;
      }
      sles.add(ScriptGenerator(
        is_generate: false,
        directory_base: directory,
        file_system_entity: fileSystemEntity,
        state_data: {},
        value: "",
        file_system_entity_type: fileSystemEntity.statSync().type,
        children: fileSystemEntity.listSync().toScriptGenerate(
              directoryBase: directory,
              scriptGeneratorOptions: scriptGeneratorOptions.copyWith(
                fileSystemEntityIgnore: file_system_entity_ignores.join("\n"),
              ),
            ),
      ));
    } else {
      final String base_name =
          path.basename(fileSystemEntity.uri.toFilePath());
      if (RegExp(r"^([.])", caseSensitive: false).hasMatch(base_name)) {
        continue;
      }
      sles.add(ScriptGenerator(
        is_generate: false,
        directory_base: directory,
        file_system_entity: fileSystemEntity,
        state_data: {},
        value: "",
        file_system_entity_type: fileSystemEntity.statSync().type,
        children: [],
      ));
    }
  }
  return sles;
}