7
7
8
8
namespace PSRule . Pipeline ;
9
9
10
- //public interface IPathBuilder
11
- //{
12
- // void Add(string path);
13
-
14
- // void Add(FileInfo[] fileInfo);
15
-
16
- // void Add(PathInfo[] pathInfo);
17
-
18
- // InputFileInfo[] Build();
19
- //}
20
-
21
- internal abstract class PathBuilder
10
+ internal abstract class PathBuilder ( IPipelineWriter logger , string basePath , string searchPattern , PathFilter filter , PathFilter required )
22
11
{
23
12
// Path separators
24
13
private const char Slash = '/' ;
@@ -28,27 +17,16 @@ internal abstract class PathBuilder
28
17
private const string CurrentPath = "." ;
29
18
private const string RecursiveSearchOperator = "**" ;
30
19
31
- private static readonly char [ ] PathLiteralStopCharacters = new char [ ] { '*' , '[' , '?' } ;
32
- private static readonly char [ ] PathSeparatorCharacters = new char [ ] { '\\ ' , '/' } ;
20
+ private static readonly char [ ] PathLiteralStopCharacters = [ '*' , '[' , '?' ] ;
21
+ private static readonly char [ ] PathSeparatorCharacters = [ '\\ ' , '/' ] ;
33
22
34
- private readonly IPipelineWriter _Logger ;
35
- private readonly List < InputFileInfo > _Files ;
36
- private readonly HashSet < string > _Paths ;
37
- private readonly string _BasePath ;
38
- private readonly string _DefaultSearchPattern ;
39
- private readonly PathFilter _GlobalFilter ;
40
- private readonly PathFilter _Required ;
41
-
42
- protected PathBuilder ( IPipelineWriter logger , string basePath , string searchPattern , PathFilter filter , PathFilter required )
43
- {
44
- _Logger = logger ;
45
- _Files = [ ] ;
46
- _Paths = [ ] ;
47
- _BasePath = NormalizePath ( Environment . GetRootedBasePath ( basePath ) ) ;
48
- _DefaultSearchPattern = searchPattern ;
49
- _GlobalFilter = filter ;
50
- _Required = required ;
51
- }
23
+ private readonly IPipelineWriter _Logger = logger ;
24
+ private readonly List < InputFileInfo > _Files = [ ] ;
25
+ private readonly HashSet < string > _Paths = [ ] ;
26
+ private readonly string _BasePath = NormalizePath ( Environment . GetRootedBasePath ( basePath ) ) ;
27
+ private readonly string _DefaultSearchPattern = searchPattern ;
28
+ private readonly PathFilter _GlobalFilter = filter ;
29
+ private readonly PathFilter _Required = required ;
52
30
53
31
/// <summary>
54
32
/// The number of files found.
@@ -89,7 +67,7 @@ public InputFileInfo[] Build()
89
67
{
90
68
try
91
69
{
92
- return _Files . ToArray ( ) ;
70
+ return [ .. _Files ] ;
93
71
}
94
72
finally
95
73
{
@@ -105,9 +83,14 @@ private void FindFiles(string path)
105
83
106
84
var pathLiteral = GetSearchParameters ( path , out var searchPattern , out var searchOption , out var filter ) ;
107
85
var files = Directory . EnumerateFiles ( pathLiteral , searchPattern , searchOption ) ;
86
+
108
87
foreach ( var file in files )
88
+ {
109
89
if ( ShouldInclude ( file , filter ) )
90
+ {
110
91
AddFile ( file ) ;
92
+ }
93
+ }
111
94
}
112
95
113
96
private bool TryUrl ( string path )
@@ -128,9 +111,7 @@ private bool TryPath(string path, out string normalPath)
128
111
var rootedPath = GetRootedPath ( path ) ;
129
112
if ( Directory . Exists ( rootedPath ) || path == CurrentPath )
130
113
{
131
- if ( IsBasePath ( rootedPath ) )
132
- normalPath = CurrentPath ;
133
-
114
+ normalPath = IsBasePath ( rootedPath ) ? CurrentPath : NormalizeDirectoryPath ( path ) ;
134
115
return false ;
135
116
}
136
117
if ( ! File . Exists ( rootedPath ) )
@@ -144,8 +125,7 @@ private bool TryPath(string path, out string normalPath)
144
125
145
126
private bool IsBasePath ( string path )
146
127
{
147
- path = IsSeparator ( path [ path . Length - 1 ] ) ? path : string . Concat ( path , Path . DirectorySeparatorChar ) ;
148
- return NormalizePath ( path ) == _BasePath ;
128
+ return NormalizeDirectoryPath ( path ) == _BasePath ;
149
129
}
150
130
151
131
private void ErrorNotFound ( string path )
@@ -251,12 +231,20 @@ private static bool IsSeparator(char c)
251
231
[ DebuggerStepThrough ]
252
232
private static bool UseSimpleSearch ( string s )
253
233
{
254
- return s . IndexOf ( RecursiveSearchOperator , System . StringComparison . OrdinalIgnoreCase ) == - 1 ;
234
+ return s . IndexOf ( RecursiveSearchOperator , StringComparison . OrdinalIgnoreCase ) == - 1 ;
255
235
}
256
236
257
237
[ DebuggerStepThrough ]
258
238
private static string NormalizePath ( string path )
259
239
{
260
240
return path . Replace ( Path . AltDirectorySeparatorChar , Path . DirectorySeparatorChar ) ;
261
241
}
242
+
243
+ [ DebuggerStepThrough ]
244
+ private static string NormalizeDirectoryPath ( string path )
245
+ {
246
+ return NormalizePath (
247
+ IsSeparator ( path [ path . Length - 1 ] ) ? path : string . Concat ( path , Path . DirectorySeparatorChar )
248
+ ) ;
249
+ }
262
250
}
0 commit comments