forked from dotnet/msbuild
/
MSBuildGlob.cs
285 lines (243 loc) · 11.1 KB
/
MSBuildGlob.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.IO;
using System.Text.RegularExpressions;
using Microsoft.Build.Collections;
using Microsoft.Build.Shared;
using Microsoft.NET.StringTools;
namespace Microsoft.Build.Globbing
{
/// <summary>
/// Represents a parsed MSBuild glob.
/// An MSBuild glob is composed of three parts:
/// - fixed directory part: "a/b/" in "a/b/**/*test*/**/*.cs"
/// - wildcard directory part: "**/*test*/**/" in "a/b/**/*test*/**/*.cs"
/// - file name part: "*.cs" in "a/b/**/*test*/**/*.cs"
/// </summary>
public class MSBuildGlob : IMSBuildGlob
{
private readonly struct GlobState
{
public string GlobRoot { get; }
public string FileSpec { get; }
public bool IsLegal { get; }
public string FixedDirectoryPart { get; }
public string WildcardDirectoryPart { get; }
public string FilenamePart { get; }
public bool NeedsRecursion { get; }
public Regex Regex { get; }
public GlobState(string globRoot, string fileSpec, bool isLegal, string fixedDirectoryPart, string wildcardDirectoryPart, string filenamePart, bool needsRecursion, Regex regex)
{
GlobRoot = globRoot;
FileSpec = fileSpec;
IsLegal = isLegal;
FixedDirectoryPart = fixedDirectoryPart;
WildcardDirectoryPart = wildcardDirectoryPart;
FilenamePart = filenamePart;
NeedsRecursion = needsRecursion;
Regex = regex;
}
}
// Cache of Regex objects that we have created and are still alive.
private static WeakValueDictionary<string, Regex> s_regexCache = new WeakValueDictionary<string, Regex>();
private readonly Lazy<GlobState> _state;
internal string TestOnlyGlobRoot => _state.Value.GlobRoot;
internal string TestOnlyFileSpec => _state.Value.FileSpec;
internal bool TestOnlyNeedsRecursion => _state.Value.NeedsRecursion;
internal Regex TestOnlyRegex => _state.Value.Regex;
/// <summary>
/// The fixed directory part.
/// </summary>
public string FixedDirectoryPart => _state.Value.FixedDirectoryPart;
/// <summary>
/// The wildcard directory part
/// </summary>
public string WildcardDirectoryPart => _state.Value.WildcardDirectoryPart;
/// <summary>
/// The file name part
/// </summary>
public string FilenamePart => _state.Value.FilenamePart;
/// <summary>
/// Whether the glob was parsed sucsesfully from a string.
/// Illegal glob strings contain:
/// - invalid path characters (other than the wildcard characters themselves)
/// - "..."
/// - ":"
/// In addition, the wildcard directory part:
/// - cannot contain ".."
/// - if ** is present it must appear alone between slashes
/// </summary>
public bool IsLegal => _state.Value.IsLegal;
private MSBuildGlob(Lazy<GlobState> state)
{
this._state = state;
}
/// <inheritdoc />
public bool IsMatch(string stringToMatch)
{
ErrorUtilities.VerifyThrowArgumentNull(stringToMatch, nameof(stringToMatch));
if (!IsLegal)
{
return false;
}
if (FileUtilities.PathIsInvalid(stringToMatch))
{
return false;
}
var normalizedString = NormalizeMatchInput(stringToMatch);
return _state.Value.Regex.IsMatch(normalizedString);
}
/// <summary>
/// Similar to <see cref="IsMatch" /> but also provides the match groups for the glob parts
/// </summary>
/// <param name="stringToMatch"></param>
/// <returns></returns>
public MatchInfoResult MatchInfo(string stringToMatch)
{
ErrorUtilities.VerifyThrowArgumentNull(stringToMatch, nameof(stringToMatch));
if (FileUtilities.PathIsInvalid(stringToMatch) || !IsLegal)
{
return MatchInfoResult.Empty;
}
string normalizedInput = NormalizeMatchInput(stringToMatch);
FileMatcher.GetRegexMatchInfo(
normalizedInput,
_state.Value.Regex,
out bool isMatch,
out string fixedDirectoryPart,
out string wildcardDirectoryPart,
out string filenamePart);
return new MatchInfoResult(isMatch, fixedDirectoryPart, wildcardDirectoryPart, filenamePart);
}
private string NormalizeMatchInput(string stringToMatch)
{
var rootedInput = Path.Combine(_state.Value.GlobRoot, stringToMatch);
var normalizedInput = FileUtilities.GetFullPathNoThrow(rootedInput);
// Degenerate case when the string to match is empty.
// Ensure trailing slash because the fixed directory part has a trailing slash.
if (string.IsNullOrEmpty(stringToMatch))
{
normalizedInput += Path.DirectorySeparatorChar;
}
return normalizedInput;
}
/// <summary>
/// Parse the given <paramref name="fileSpec" /> into a <see cref="MSBuildGlob" /> using a given
/// <paramref name="globRoot" />.
/// </summary>
/// <param name="globRoot">
/// The root of the glob.
/// The fixed directory part of the glob and the match arguments (<see cref="IsMatch" /> and <see cref="MatchInfo" />)
/// will get normalized against this root.
/// If empty, the current working directory is used.
/// Cannot be null, and cannot contain invalid path arguments.
/// </param>
/// <param name="fileSpec">The string to parse</param>
/// <returns></returns>
public static MSBuildGlob Parse(string globRoot, string fileSpec)
{
ErrorUtilities.VerifyThrowArgumentNull(globRoot, nameof(globRoot));
ErrorUtilities.VerifyThrowArgumentNull(fileSpec, nameof(fileSpec));
ErrorUtilities.VerifyThrowArgumentInvalidPath(globRoot, nameof(globRoot));
if (string.IsNullOrEmpty(globRoot))
{
globRoot = Directory.GetCurrentDirectory();
}
globRoot = Strings.WeakIntern(FileUtilities.NormalizePath(globRoot).WithTrailingSlash());
var lazyState = new Lazy<GlobState>(() =>
{
FileMatcher.Default.GetFileSpecInfo(
fileSpec,
out string fixedDirectoryPart,
out string wildcardDirectoryPart,
out string filenamePart,
out bool needsRecursion,
out bool isLegalFileSpec,
(fixedDirPart, wildcardDirPart, filePart) =>
{
var normalizedFixedPart = NormalizeTheFixedDirectoryPartAgainstTheGlobRoot(fixedDirPart, globRoot);
return (normalizedFixedPart, wildcardDirPart, filePart);
});
Regex regex = null;
if (isLegalFileSpec)
{
string matchFileExpression = FileMatcher.RegularExpressionFromFileSpec(fixedDirectoryPart, wildcardDirectoryPart, filenamePart);
lock (s_regexCache)
{
s_regexCache.TryGetValue(matchFileExpression, out regex);
}
if (regex == null)
{
// compile the regex since it's expected to be used multiple times
Regex newRegex = new Regex(matchFileExpression, FileMatcher.DefaultRegexOptions | RegexOptions.Compiled);
lock (s_regexCache)
{
if (!s_regexCache.TryGetValue(matchFileExpression, out regex))
{
s_regexCache[matchFileExpression] = newRegex;
}
}
regex ??= newRegex;
}
}
return new GlobState(globRoot, fileSpec, isLegalFileSpec, fixedDirectoryPart, wildcardDirectoryPart, filenamePart, needsRecursion, regex);
},
true);
return new MSBuildGlob(lazyState);
}
private static string NormalizeTheFixedDirectoryPartAgainstTheGlobRoot(string fixedDirPart, string globRoot)
{
// todo: glob normalization is duplicated with EngineFileUtilities.CreateRegex
// concatenate the glob parent to the fixed dir part
var parentedFixedPart = Path.Combine(globRoot, fixedDirPart);
var normalizedFixedPart = FileUtilities.GetFullPathNoThrow(parentedFixedPart);
return normalizedFixedPart.WithTrailingSlash();
}
/// <summary>
/// See <see cref="Parse(string,string)" />.
/// The glob root, if the glob is not fully qualified, will be the current working directory.
/// </summary>
/// <param name="fileSpec"></param>
/// <returns></returns>
public static MSBuildGlob Parse(string fileSpec)
{
return Parse(string.Empty, fileSpec);
}
/// <summary>
/// Return type of <see cref="MSBuildGlob.MatchInfo" />
/// </summary>
public struct MatchInfoResult
{
/// <summary>
/// Whether the <see cref="MSBuildGlob.MatchInfo" /> argument was matched against the glob
/// </summary>
public bool IsMatch { get; }
/// <summary>
/// The fixed directory part match
/// </summary>
public string FixedDirectoryPartMatchGroup { get; }
/// <summary>
/// The wildcard directory part match
/// </summary>
public string WildcardDirectoryPartMatchGroup { get; }
/// <summary>
/// The file name part match
/// </summary>
public string FilenamePartMatchGroup { get; }
internal static MatchInfoResult Empty
=> new MatchInfoResult(false, string.Empty, string.Empty, string.Empty);
internal MatchInfoResult(
bool isMatch,
string fixedDirectoryPartMatchGroup,
string wildcardDirectoryPartMatchGroup,
string filenamePartMatchGroup)
{
IsMatch = isMatch;
FixedDirectoryPartMatchGroup = fixedDirectoryPartMatchGroup;
WildcardDirectoryPartMatchGroup = wildcardDirectoryPartMatchGroup;
FilenamePartMatchGroup = filenamePartMatchGroup;
}
}
}
}