Skip to content

Commit 61877f9

Browse files
author
Kapil Borle
committed
Add feature to discover settings file
1 parent 219794d commit 61877f9

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

Engine/Commands/InvokeScriptAnalyzerCommand.cs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,51 @@ protected override void BeginProcessing()
219219
string[] rulePaths = Helper.ProcessCustomRulePaths(customRulePath,
220220
this.SessionState, recurseCustomRulePath);
221221

222-
if (!ScriptAnalyzer.Instance.ParseProfile(this.settings, this.SessionState.Path, this))
222+
var settingFileHasErrors = false;
223+
if (settings == null)
223224
{
225+
// 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);
227+
this.WriteVerbose(
228+
String.Format(
229+
"Settings not provided. Will look for settings file in the given path {0}.",
230+
path));
231+
var settingsFileAutoDiscovered = false;
232+
if (Directory.Exists(directory))
233+
{
234+
// if settings are not provided explicitly, look for it in the given path
235+
// check if pssasettings.psd1 exists
236+
var settingsFilename = "PSScriptAnalyzerSettings.psd1";
237+
var settingsFilepath = System.IO.Path.Combine(directory, settingsFilename);
238+
if (File.Exists(settingsFilepath))
239+
{
240+
settingsFileAutoDiscovered = true;
241+
this.WriteVerbose(
242+
String.Format(
243+
"Found {0} in {1}. Will use it to provide settings for this invocation.",
244+
settingsFilename,
245+
directory));
246+
settingFileHasErrors = !ScriptAnalyzer.Instance.ParseProfile(settingsFilepath, this.SessionState.Path, this);
247+
248+
}
249+
}
250+
251+
if (!settingsFileAutoDiscovered)
252+
{
253+
this.WriteVerbose(
254+
String.Format(
255+
"Cannot find a settings file in the given path {0}.",
256+
path));
257+
}
258+
}
259+
else
260+
{
261+
settingFileHasErrors = !ScriptAnalyzer.Instance.ParseProfile(this.settings, this.SessionState.Path, this);
262+
}
263+
264+
if (settingFileHasErrors)
265+
{
266+
this.WriteWarning("Cannot parse settings. Will abort the invocation.");
224267
stopProcessing = true;
225268
return;
226269
}

0 commit comments

Comments
 (0)