forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMUIFileSearcher.cs
More file actions
378 lines (320 loc) · 12.9 KB
/
Copy pathMUIFileSearcher.cs
File metadata and controls
378 lines (320 loc) · 12.9 KB
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
namespace System.Management.Automation
{
internal class MUIFileSearcher
{
/// <summary>
/// Constructor. It is private so that MUIFileSearcher is used only internal for this class.
/// To access functionality in this class, static api should be used.
/// </summary>
/// <param name="target"></param>
/// <param name="searchPaths"></param>
/// <param name="searchMode"></param>
private MUIFileSearcher(string target, Collection<String> searchPaths, SearchMode searchMode)
{
Target = target;
SearchPaths = searchPaths;
SearchMode = searchMode;
}
/// <summary>
/// A constructor to make searchMode optional.
/// </summary>
/// <param name="target"></param>
/// <param name="searchPaths"></param>
private MUIFileSearcher(string target, Collection<String> searchPaths)
: this(target, searchPaths, SearchMode.Unique)
{
}
#region Basic Properties
/// <summary>
/// Search target. It can be
/// 1. a file name
/// 2. a search pattern
/// It can also include a path, in that case,
/// 1. the path will be searched first for the existense of the files.
/// </summary>
internal string Target { get; } = null;
/// <summary>
/// Search path as provided by user.
/// </summary>
internal Collection<String> SearchPaths { get; } = null;
/// <summary>
/// Search mode for this file search.
/// </summary>
internal SearchMode SearchMode { get; } = SearchMode.Unique;
private Collection<String> _result = null;
/// <summary>
/// Result of the search.
/// </summary>
internal Collection<String> Result
{
get
{
if (_result == null)
{
_result = new Collection<String>();
// SearchForFiles will fill the result collection.
SearchForFiles();
}
return _result;
}
}
#endregion
#region File Search
/// <summary>
/// _uniqueMatches is used to track matches already found during the search process.
/// This is useful for ignoring duplicates in the case of unique search.
/// </summary>
private Hashtable _uniqueMatches = new Hashtable(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// search for files using the target, searchPaths member of this class.
/// </summary>
private void SearchForFiles()
{
if (String.IsNullOrEmpty(this.Target))
return;
string pattern = Path.GetFileName(this.Target);
if (String.IsNullOrEmpty(pattern))
return;
Collection<String> normalizedSearchPaths = NormalizeSearchPaths(this.Target, this.SearchPaths);
foreach (string directory in normalizedSearchPaths)
{
SearchForFiles(pattern, directory);
if (this.SearchMode == SearchMode.First && this.Result.Count > 0)
{
return;
}
}
}
private string[] GetFiles(string path, string pattern)
{
#if UNIX
// On Linux, file names are case sensitive, so we need to add
// extra logic to select the files that match the given pattern.
ArrayList result = new ArrayList();
string[] files = Directory.GetFiles(path);
foreach (string filePath in files)
{
if (filePath.IndexOf(pattern, StringComparison.OrdinalIgnoreCase) >= 0)
{
result.Add(filePath);
}
}
return (String[])result.ToArray(typeof(string));
#else
return Directory.GetFiles(path, pattern);
#endif
}
private void AddFiles(string muiDirectory, string directory, string pattern)
{
if (Directory.Exists(muiDirectory))
{
string[] files = GetFiles(muiDirectory, pattern);
if (files == null)
return;
foreach (string file in files)
{
string path = Path.Combine(muiDirectory, file);
switch (this.SearchMode)
{
case SearchMode.All:
_result.Add(path);
break;
case SearchMode.Unique:
// Construct a Unique filename for this directory.
// Remember the file may belong to one of the sub-culture
// directories. In this case we should not be returning
// same files that are residing in 2 or more sub-culture
// directories.
string leafFileName = Path.GetFileName(file);
string uniqueToDirectory = Path.Combine(directory, leafFileName);
if (!_uniqueMatches.Contains(uniqueToDirectory))
{
_result.Add(path);
_uniqueMatches[uniqueToDirectory] = true;
}
break;
case SearchMode.First:
_result.Add(path);
return;
default:
break;
}
}
}
}
/// <summary>
/// Search for files of a particular pattern under a particular directory.
/// This will do MUI search in which appropriate language directories are
/// searched in order.
/// </summary>
/// <param name="pattern"></param>
/// <param name="directory"></param>
private void SearchForFiles(string pattern, string directory)
{
List<string> cultureNameList = new List<string>();
CultureInfo culture = CultureInfo.CurrentUICulture;
while (culture != null && !String.IsNullOrEmpty(culture.Name))
{
cultureNameList.Add(culture.Name);
culture = culture.Parent;
}
cultureNameList.Add("");
// Add en-US and en as fallback languages
if (!cultureNameList.Contains("en-US"))
{
cultureNameList.Add("en-US");
}
if (!cultureNameList.Contains("en"))
{
cultureNameList.Add("en");
}
foreach (string name in cultureNameList)
{
string muiDirectory = Path.Combine(directory, name);
AddFiles(muiDirectory, directory, pattern);
if (this.SearchMode == SearchMode.First && this.Result.Count > 0)
{
return;
}
}
return;
}
/// <summary>
/// A help file is located in 3 steps
/// 1. If file itself contains a path itself, try to locate the file
/// from path. LocateFile will fail if this file doesn't exist.
/// 2. Try to locate the file from searchPaths. Normally the searchPaths will
/// contain the cmdlet/provider assembly directory if currently we are searching
/// help for cmdlet and providers.
/// 3. Try to locate the file in the default PowerShell installation directory.
/// </summary>
/// <param name="target"></param>
/// <param name="searchPaths"></param>
/// <returns></returns>
private static Collection<String> NormalizeSearchPaths(string target, Collection<String> searchPaths)
{
Collection<String> result = new Collection<String>();
// step 1: if target has path attached, directly locate
// file from there.
if (!String.IsNullOrEmpty(target) && !String.IsNullOrEmpty(Path.GetDirectoryName(target)))
{
string directory = Path.GetDirectoryName(target);
if (Directory.Exists(directory))
{
result.Add(Path.GetFullPath(directory));
}
//user specifically wanted to search in a particular directory
//so return..
return result;
}
// step 2: add directories specified in to search path.
if (searchPaths != null)
{
foreach (string directory in searchPaths)
{
if (!result.Contains(directory) && Directory.Exists(directory))
{
result.Add(directory);
}
}
}
// step 3: locate the file in the default PowerShell installation directory.
string defaultPSPath = GetMshDefaultInstallationPath();
if (defaultPSPath != null &&
!result.Contains(defaultPSPath) &&
Directory.Exists(defaultPSPath))
{
result.Add(defaultPSPath);
}
return result;
}
/// <summary>
/// Helper method which returns the default monad installation path based on ShellID
/// registry key.
/// </summary>
/// <returns>string representing path.</returns>
/// <remarks>
/// If ShellID is not defined or Path property is not defined returns null.
/// </remarks>
private static string GetMshDefaultInstallationPath()
{
string returnValue = CommandDiscovery.GetShellPathFromRegistry(Utils.DefaultPowerShellShellID);
if (returnValue != null)
{
returnValue = Path.GetDirectoryName(returnValue);
}
// returnValue can be null.
return returnValue;
}
#endregion
#region Static API's
/// <summary>
/// Search for files in default search paths.
/// </summary>
/// <param name="pattern"></param>
/// <returns></returns>
internal static Collection<String> SearchFiles(string pattern)
{
return SearchFiles(pattern, new Collection<String>());
}
/// <summary>
/// Search for files in specified search paths.
/// </summary>
/// <param name="pattern"></param>
/// <param name="searchPaths"></param>
/// <returns></returns>
internal static Collection<String> SearchFiles(string pattern, Collection<String> searchPaths)
{
MUIFileSearcher searcher = new MUIFileSearcher(pattern, searchPaths);
return searcher.Result;
}
/// <summary>
/// Locate a file in default search paths
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
internal static string LocateFile(string file)
{
return LocateFile(file, new Collection<String>());
}
/// <summary>
/// Get the file in different search paths corresponding to current culture.
///
/// The file name to search is the filename part of path parameter. (Normally path
/// parameter should contain only the filename part. But it is possible for
/// RunspaceConfiguration to directly specify a hard coded path for help file there).
///
/// </summary>
/// <param name="file">This is the path to the file. If it has a path, we need to search under that path first</param>
/// <param name="searchPaths">Additional search paths</param>
/// <returns></returns>
internal static string LocateFile(string file, Collection<String> searchPaths)
{
MUIFileSearcher searcher = new MUIFileSearcher(file, searchPaths, SearchMode.First);
if (searcher.Result == null || searcher.Result.Count == 0)
return null;
return searcher.Result[0];
}
#endregion
}
/// <summary>
/// This enum defines different search mode for the MUIFileSearcher
/// </summary>
internal enum SearchMode
{
// return the first match
First,
// return all matches, with duplicates allowed
All,
// return all matches, with duplicates ignored
Unique
}
}