Skip to content

Commit 51e5fbd

Browse files
author
Kapil Borle
committed
Process paths before settings discovery
1 parent 480b2e7 commit 51e5fbd

File tree

1 file changed

+36
-10
lines changed

1 file changed

+36
-10
lines changed

Engine/Commands/InvokeScriptAnalyzerCommand.cs

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands
3838
HelpUri = "http://go.microsoft.com/fwlink/?LinkId=525914")]
3939
public class InvokeScriptAnalyzerCommand : PSCmdlet, IOutputWriter
4040
{
41+
#region Private variables
42+
List<string> processedPaths;
43+
#endregion // Private variables
44+
4145
#region Parameters
4246
/// <summary>
4347
/// Path: The path to the file or folder to invoke PSScriptAnalyzer on.
@@ -218,12 +222,24 @@ protected override void BeginProcessing()
218222

219223
string[] rulePaths = Helper.ProcessCustomRulePaths(customRulePath,
220224
this.SessionState, recurseCustomRulePath);
225+
if (IsFileParameterSet())
226+
{
227+
ProcessPath();
228+
}
221229

222230
var settingFileHasErrors = false;
223-
if (settings == null)
231+
if (settings == null
232+
&& processedPaths != null
233+
&& processedPaths.Count == 1)
224234
{
225235
// add a directory separator character because if there is no trailing separator character, it will return the parent
226-
var directory = System.IO.Path.GetDirectoryName(path + System.IO.Path.DirectorySeparatorChar);
236+
var directory = processedPaths[0].TrimEnd(System.IO.Path.DirectorySeparatorChar);
237+
if (File.Exists(directory))
238+
{
239+
// if given path is a file, get its directory
240+
directory = System.IO.Path.GetDirectoryName(directory);
241+
}
242+
227243
this.WriteVerbose(
228244
String.Format(
229245
"Settings not provided. Will look for settings file in the given path {0}.",
@@ -244,7 +260,6 @@ protected override void BeginProcessing()
244260
settingsFilename,
245261
directory));
246262
settingFileHasErrors = !ScriptAnalyzer.Instance.ParseProfile(settingsFilepath, this.SessionState.Path, this);
247-
248263
}
249264
}
250265

@@ -330,15 +345,11 @@ protected override void StopProcessing()
330345
private void ProcessInput()
331346
{
332347
IEnumerable<DiagnosticRecord> diagnosticsList = Enumerable.Empty<DiagnosticRecord>();
333-
if (String.Equals(this.ParameterSetName, "File", StringComparison.OrdinalIgnoreCase))
348+
if (IsFileParameterSet())
334349
{
335-
// throws Item Not Found Exception
336-
Collection<PathInfo> paths = this.SessionState.Path.GetResolvedPSPathFromPSPath(path);
337-
foreach (PathInfo p in paths)
350+
foreach (var p in processedPaths)
338351
{
339-
diagnosticsList = ScriptAnalyzer.Instance.AnalyzePath(
340-
this.SessionState.Path.GetUnresolvedProviderPathFromPSPath(p.Path),
341-
this.recurse);
352+
diagnosticsList = ScriptAnalyzer.Instance.AnalyzePath(p, this.recurse);
342353
WriteToOutput(diagnosticsList);
343354
}
344355
}
@@ -359,6 +370,21 @@ private void WriteToOutput(IEnumerable<DiagnosticRecord> diagnosticRecords)
359370
}
360371
}
361372
}
373+
374+
private void ProcessPath()
375+
{
376+
Collection<PathInfo> paths = this.SessionState.Path.GetResolvedPSPathFromPSPath(path);
377+
processedPaths = new List<string>();
378+
foreach (PathInfo p in paths)
379+
{
380+
processedPaths.Add(this.SessionState.Path.GetUnresolvedProviderPathFromPSPath(p.Path));
381+
}
382+
}
383+
384+
private bool IsFileParameterSet()
385+
{
386+
return String.Equals(this.ParameterSetName, "File", StringComparison.OrdinalIgnoreCase);
387+
}
362388
#endregion
363389
}
364390
}

0 commit comments

Comments
 (0)