HTFileSystemResourceContext constructor

HTFileSystemResourceContext({
  1. String? root,
  2. Map<String, HTSource>? cache,
  3. List<HTFilterConfig> includedFilter = const [],
  4. List<HTFilterConfig> excludedFilter = const [],
  5. List<String> binaryFileExtensions = const [],
  6. bool doScanRoot = false,
})

Implementation

HTFileSystemResourceContext(
    {String? root,
    Map<String, HTSource>? cache,
    List<HTFilterConfig> includedFilter = const [],
    List<HTFilterConfig> excludedFilter = const [],
    this.binaryFileExtensions = const [],
    bool doScanRoot = false})
    : _cached = cache ?? <String, HTSource>{} {
  if (root != null) {
    this.root = root = getAbsolutePath(dirName: root);
  } else {
    this.root = root = getAbsolutePath(dirName: path.current);
  }

  if (doScanRoot) {
    final dir = Directory(root);
    final folderFilter = HTFilterConfig(root);
    final entities = dir.listSync(recursive: true);
    for (final filter in includedFilter) {
      filter.folder = getAbsolutePath(key: filter.folder, dirName: root);
    }
    for (final filter in excludedFilter) {
      filter.folder = getAbsolutePath(key: filter.folder, dirName: root);
    }
    for (final entity in entities) {
      if (entity is! File) {
        continue;
      }
      final fileFullName = getAbsolutePath(key: entity.path, dirName: root);
      var isIncluded = false;
      if (includedFilter.isEmpty) {
        isIncluded = folderFilter.isWithin(fileFullName);
      } else {
        for (final filter in includedFilter) {
          if (filter.isWithin(fileFullName)) {
            isIncluded = true;
            break;
          }
        }
      }
      if (isIncluded) {
        if (excludedFilter.isNotEmpty) {
          for (final filter in excludedFilter) {
            final isExcluded = filter.isWithin(fileFullName);
            if (isExcluded) {
              isIncluded = false;
              break;
            }
          }
        }
      }
      if (isIncluded) {
        included.add(fileFullName);
      }
    }
  }
}