execute method

PatternMatchingResult execute(
  1. DirectoryInfoBase directoryInfo
)

Executes the matcher against the specified directory.

Returns a PatternMatchingResult containing all files that match the include patterns and don't match any exclude patterns.

Implementation

PatternMatchingResult execute(DirectoryInfoBase directoryInfo) {
  final rootPath = directoryInfo.fullName;
  final matches = <FilePatternMatch>[];
  final matchedPaths = <String>{};

  // Process include patterns
  for (final pattern in _includePatterns) {
    final glob = Glob(pattern);

    try {
      // List all files recursively from the root
      final rootDir = Directory(rootPath);
      if (!rootDir.existsSync()) {
        continue;
      }

      final entities = rootDir.listSync(recursive: true, followLinks: false);

      for (final entity in entities) {
        if (entity is File) {
          try {
            final relativePath = p.relative(entity.path, from: rootPath);

            // Check if the file matches the include pattern
            if (glob.matches(relativePath)) {
              // Check against exclude patterns
              if (!_isExcluded(relativePath)) {
                // Avoid duplicates
                if (matchedPaths.add(relativePath)) {
                  matches.add(FilePatternMatch(
                    relativePath,
                    _calculateStem(pattern, relativePath),
                  ));
                }
              }
            }
          } catch (e) {
            // Skip files we can't access
          }
        }
      }
    } catch (e) {
      // Skip patterns that fail to process
    }
  }

  return PatternMatchingResult(matches);
}