-
-
Notifications
You must be signed in to change notification settings - Fork 729
/
Globber.cs
65 lines (58 loc) · 2.15 KB
/
Globber.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Linq;
using Cake.Core.IO.Globbing;
namespace Cake.Core.IO
{
/// <summary>
/// The file system globber.
/// </summary>
public sealed class Globber : IGlobber
{
private readonly GlobParser _parser;
private readonly GlobVisitor _visitor;
private readonly PathComparer _comparer;
private readonly ICakeEnvironment _environment;
/// <summary>
/// Initializes a new instance of the <see cref="Globber"/> class.
/// </summary>
/// <param name="fileSystem">The file system.</param>
/// <param name="environment">The environment.</param>
public Globber(IFileSystem fileSystem, ICakeEnvironment environment)
{
if (fileSystem == null)
{
throw new ArgumentNullException(nameof(fileSystem));
}
if (environment == null)
{
throw new ArgumentNullException(nameof(environment));
}
_environment = environment;
_parser = new GlobParser(environment);
_visitor = new GlobVisitor(fileSystem, environment);
_comparer = new PathComparer(environment.Platform.IsUnix());
}
/// <inheritdoc/>
public IEnumerable<Path> Match(GlobPattern pattern, GlobberSettings settings)
{
if (pattern == null)
{
throw new ArgumentNullException(nameof(pattern));
}
if (string.IsNullOrWhiteSpace(pattern?.Pattern))
{
return Enumerable.Empty<Path>();
}
// Parse the pattern into an AST.
var root = _parser.Parse(pattern, settings);
// Visit all nodes in the parsed patterns and filter the result.
return _visitor.Walk(root, settings)
.Select(x => x.Path)
.Distinct(_comparer);
}
}
}