/
SourceFileMatcher.cs
43 lines (33 loc) · 1.61 KB
/
SourceFileMatcher.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using Microsoft.Extensions.FileSystemGlobbing;
namespace Microsoft.CodeAnalysis.Tools.Utilities
{
internal sealed class SourceFileMatcher
{
private static string[] AllFilesList => new[] { @"**/*.*" };
public static SourceFileMatcher CreateMatcher(string[] include, string[] exclude)
=> new SourceFileMatcher(include, exclude);
private readonly Matcher _matcher = new Matcher(StringComparison.OrdinalIgnoreCase);
private readonly bool _shouldMatchAll;
public ImmutableArray<string> Include { get; }
public ImmutableArray<string> Exclude { get; }
private SourceFileMatcher(string[] include, string[] exclude)
{
_shouldMatchAll = include.Length == 0 && exclude.Length == 0;
Include = include.Length > 0
? include.ToImmutableArray()
: AllFilesList.ToImmutableArray();
Exclude = exclude.ToImmutableArray();
_matcher = new Matcher(StringComparison.OrdinalIgnoreCase);
_matcher.AddIncludePatterns(Include);
_matcher.AddExcludePatterns(Exclude);
}
public bool HasMatches(string filePath)
=> _shouldMatchAll || _matcher.Match(filePath).HasMatches;
public IEnumerable<string> GetResultsInFullPath(string directoryPath)
=> _matcher.GetResultsInFullPath(directoryPath);
}
}