Skip to content

Commit f830fc3

Browse files
author
Kapil Borle
committed
Determine settings mode before processing settings
1 parent c5b7edd commit f830fc3

File tree

1 file changed

+39
-59
lines changed

1 file changed

+39
-59
lines changed

Engine/Commands/InvokeScriptAnalyzerCommand.cs

Lines changed: 39 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -229,77 +229,57 @@ protected override void BeginProcessing()
229229
Helper.Instance.SetPSVersionTable(psVersionTable);
230230
}
231231

232-
string[] rulePaths = Helper.ProcessCustomRulePaths(customRulePath,
233-
this.SessionState, recurseCustomRulePath);
232+
string[] rulePaths = Helper.ProcessCustomRulePaths(
233+
customRulePath,
234+
this.SessionState,
235+
recurseCustomRulePath);
236+
234237
if (IsFileParameterSet())
235238
{
236239
ProcessPath();
237240
}
238241

239-
var settingFileHasErrors = false;
240-
if (settings == null
241-
&& processedPaths != null
242-
&& processedPaths.Count == 1)
242+
object settingsFound;
243+
var settingsMode = FindSettingsMode(out settingsFound);
244+
switch (settingsMode)
243245
{
244-
// add a directory separator character because if there is no trailing separator character, it will return the parent
245-
var directory = processedPaths[0].TrimEnd(System.IO.Path.DirectorySeparatorChar);
246-
if (File.Exists(directory))
247-
{
248-
// if given path is a file, get its directory
249-
directory = System.IO.Path.GetDirectoryName(directory);
250-
}
251-
252-
this.WriteVerbose(
253-
String.Format(
254-
"Settings not provided. Will look for settings file in the given path {0}.",
255-
path));
256-
var settingsFileAutoDiscovered = false;
257-
if (Directory.Exists(directory))
258-
{
259-
// if settings are not provided explicitly, look for it in the given path
260-
// check if pssasettings.psd1 exists
261-
var settingsFilename = "PSScriptAnalyzerSettings.psd1";
262-
var settingsFilepath = System.IO.Path.Combine(directory, settingsFilename);
263-
if (File.Exists(settingsFilepath))
264-
{
265-
settingsFileAutoDiscovered = true;
266-
this.WriteVerbose(
267-
String.Format(
268-
"Found {0} in {1}. Will use it to provide settings for this invocation.",
269-
settingsFilename,
270-
directory));
271-
settingFileHasErrors = !ScriptAnalyzer.Instance.ParseProfile(settingsFilepath, this.SessionState.Path, this);
272-
}
273-
}
274-
275-
if (!settingsFileAutoDiscovered)
276-
{
246+
case SettingsMode.Auto:
277247
this.WriteVerbose(
278248
String.Format(
279-
"Cannot find a settings file in the given path {0}.",
249+
"Settings not provided. Will look for settings file in the given path {0}.",
280250
path));
281-
}
282-
}
283-
else if (IsBuiltinSettingPreset(settings))
284-
{
285-
var settingsFilePath = Helper.GetSettingPresetFilePath(settings as string);
286-
this.WriteVerbose(String.Format("Using settings file at {0}", settingsFilePath));
287-
settingFileHasErrors = !ScriptAnalyzer.Instance.ParseProfile(
288-
settingsFilePath,
289-
this.SessionState.Path,
290-
this);
291-
}
292-
else
293-
{
294-
this.WriteVerbose(String.Format("Using settings file at {0}", this.settings));
295-
settingFileHasErrors = !ScriptAnalyzer.Instance.ParseProfile(this.settings, this.SessionState.Path, this);
251+
this.WriteVerbose(
252+
String.Format(
253+
"Found {0}. Will use it to provide settings for this invocation.",
254+
(string)settingsFound));
255+
break;
256+
257+
case SettingsMode.Preset:
258+
settingsFound = Helper.GetSettingPresetFilePath(this.settings as string);
259+
goto case SettingsMode.File;
260+
261+
case SettingsMode.File:
262+
this.WriteVerbose(String.Format("Using settings file at {0}", (string)settingsFound));
263+
break;
264+
265+
case SettingsMode.Hashtable:
266+
this.WriteVerbose(String.Format("Using settings hashtable."));
267+
break;
268+
269+
default: // case SettingsMode.None
270+
this.WriteVerbose(String.Format("Cannot find a settings file."));
271+
break;
296272
}
297273

298-
if (settingFileHasErrors)
274+
if (settingsMode != SettingsMode.None)
299275
{
300-
this.WriteWarning("Cannot parse settings. Will abort the invocation.");
301-
stopProcessing = true;
302-
return;
276+
var settingFileHasErrors = !ScriptAnalyzer.Instance.ParseProfile(settingsFound, this.SessionState.Path, this);
277+
if (settingFileHasErrors)
278+
{
279+
this.WriteWarning("Cannot parse settings. Will abort the invocation.");
280+
stopProcessing = true;
281+
return;
282+
}
303283
}
304284

305285
ScriptAnalyzer.Instance.Initialize(

0 commit comments

Comments
 (0)