@@ -65,20 +65,20 @@ public ScriptFile GetFile(string filePath)
65
65
{
66
66
Validate . IsNotNullOrEmptyString ( "filePath" , filePath ) ;
67
67
68
- // Resolve the full file path
68
+ // Resolve the full file path
69
69
string resolvedFilePath = this . ResolveFilePath ( filePath ) ;
70
70
string keyName = resolvedFilePath . ToLower ( ) ;
71
71
72
72
// Make sure the file isn't already loaded into the workspace
73
73
ScriptFile scriptFile = null ;
74
74
if ( ! this . workspaceFiles . TryGetValue ( keyName , out scriptFile ) )
75
75
{
76
- // This method allows FileNotFoundException to bubble up
76
+ // This method allows FileNotFoundException to bubble up
77
77
// if the file isn't found.
78
78
using ( FileStream fileStream = new FileStream ( resolvedFilePath , FileMode . Open , FileAccess . Read ) )
79
79
using ( StreamReader streamReader = new StreamReader ( fileStream , Encoding . UTF8 ) )
80
80
{
81
- scriptFile =
81
+ scriptFile =
82
82
new ScriptFile (
83
83
resolvedFilePath ,
84
84
filePath ,
@@ -115,15 +115,15 @@ public ScriptFile GetFileBuffer(string filePath, string initialBuffer)
115
115
{
116
116
Validate . IsNotNullOrEmptyString ( "filePath" , filePath ) ;
117
117
118
- // Resolve the full file path
118
+ // Resolve the full file path
119
119
string resolvedFilePath = this . ResolveFilePath ( filePath ) ;
120
120
string keyName = resolvedFilePath . ToLower ( ) ;
121
121
122
122
// Make sure the file isn't already loaded into the workspace
123
123
ScriptFile scriptFile = null ;
124
124
if ( ! this . workspaceFiles . TryGetValue ( keyName , out scriptFile ) && initialBuffer != null )
125
125
{
126
- scriptFile =
126
+ scriptFile =
127
127
new ScriptFile (
128
128
resolvedFilePath ,
129
129
filePath ,
@@ -159,19 +159,19 @@ public void CloseFile(ScriptFile scriptFile)
159
159
}
160
160
161
161
/// <summary>
162
- /// Gets all file references by recursively searching
162
+ /// Gets all file references by recursively searching
163
163
/// through referenced files in a scriptfile
164
164
/// </summary>
165
165
/// <param name="scriptFile">Contains the details and contents of an open script file</param>
166
- /// <returns>A scriptfile array where the first file
166
+ /// <returns>A scriptfile array where the first file
167
167
/// in the array is the "root file" of the search</returns>
168
168
public ScriptFile [ ] ExpandScriptReferences ( ScriptFile scriptFile )
169
169
{
170
170
Dictionary < string , ScriptFile > referencedScriptFiles = new Dictionary < string , ScriptFile > ( ) ;
171
171
List < ScriptFile > expandedReferences = new List < ScriptFile > ( ) ;
172
172
173
173
// add original file so it's not searched for, then find all file references
174
- referencedScriptFiles . Add ( scriptFile . Id , scriptFile ) ;
174
+ referencedScriptFiles . Add ( scriptFile . Id , scriptFile ) ;
175
175
RecursivelyFindReferences ( scriptFile , referencedScriptFiles ) ;
176
176
177
177
// remove original file from referened file and add it as the first element of the
@@ -219,25 +219,60 @@ public string GetRelativePath(string filePath)
219
219
/// <returns>An enumerator over the PowerShell files found in the workspace</returns>
220
220
public IEnumerable < string > EnumeratePSFiles ( )
221
221
{
222
- if ( WorkspacePath == null
223
- || ! Directory . Exists ( WorkspacePath ) )
222
+ if ( WorkspacePath == null || ! Directory . Exists ( WorkspacePath ) )
224
223
{
225
- yield break ;
224
+ return Enumerable . Empty < string > ( ) ;
226
225
}
227
226
227
+ return this . RecursivelyEnumerateFiles ( WorkspacePath ) ;
228
+ }
229
+
230
+ #endregion
231
+
232
+ #region Private Methods
233
+
234
+ private IEnumerable < string > RecursivelyEnumerateFiles ( string folderPath )
235
+ {
236
+ var foundFiles = Enumerable . Empty < string > ( ) ;
228
237
var patterns = new string [ ] { @"*.ps1" , @"*.psm1" , @"*.psd1" } ;
229
- foreach ( var pattern in patterns )
238
+
239
+ try
230
240
{
231
- foreach ( var file in Directory . EnumerateFiles ( WorkspacePath , pattern , SearchOption . AllDirectories ) )
241
+ IEnumerable < string > subDirs = Directory . EnumerateDirectories ( folderPath ) ;
242
+ foreach ( string dir in subDirs )
232
243
{
233
- yield return file ;
244
+ foundFiles =
245
+ foundFiles . Concat (
246
+ RecursivelyEnumerateFiles ( dir ) ) ;
234
247
}
235
248
}
236
- }
249
+ catch ( UnauthorizedAccessException e )
250
+ {
251
+ Logger . WriteException (
252
+ $ "Could not enumerate files in the path '{ folderPath } ' due to the path not being accessible",
253
+ e ) ;
254
+ }
237
255
238
- #endregion
256
+ foreach ( var pattern in patterns )
257
+ {
258
+ try
259
+ {
260
+ foundFiles =
261
+ foundFiles . Concat (
262
+ Directory . EnumerateFiles (
263
+ folderPath ,
264
+ pattern ) ) ;
265
+ }
266
+ catch ( UnauthorizedAccessException e )
267
+ {
268
+ Logger . WriteException (
269
+ $ "Could not enumerate files in the path '{ folderPath } ' due to a file not being accessible",
270
+ e ) ;
271
+ }
272
+ }
239
273
240
- #region Private Methods
274
+ return foundFiles ;
275
+ }
241
276
242
277
/// <summary>
243
278
/// Recusrively searches through referencedFiles in scriptFiles
@@ -246,11 +281,11 @@ public IEnumerable<string> EnumeratePSFiles()
246
281
/// <param name="scriptFile">Details an contents of "root" script file</param>
247
282
/// <param name="referencedScriptFiles">A Dictionary of referenced script files</param>
248
283
private void RecursivelyFindReferences (
249
- ScriptFile scriptFile ,
284
+ ScriptFile scriptFile ,
250
285
Dictionary < string , ScriptFile > referencedScriptFiles )
251
286
{
252
287
// Get the base path of the current script for use in resolving relative paths
253
- string baseFilePath =
288
+ string baseFilePath =
254
289
GetBaseFilePath (
255
290
scriptFile . FilePath ) ;
256
291
@@ -347,12 +382,12 @@ private string GetBaseFilePath(string filePath)
347
382
// TODO: Assert instead?
348
383
throw new InvalidOperationException (
349
384
string . Format (
350
- "Must provide a full path for originalScriptPath: {0}" ,
385
+ "Must provide a full path for originalScriptPath: {0}" ,
351
386
filePath ) ) ;
352
387
}
353
388
354
389
// Get the directory of the file path
355
- return Path . GetDirectoryName ( filePath ) ;
390
+ return Path . GetDirectoryName ( filePath ) ;
356
391
}
357
392
358
393
private string ResolveRelativeScriptPath ( string baseFilePath , string relativePath )
0 commit comments