Skip to content

Commit 1dbf68a

Browse files
author
Kapil Borle
committed
Allow settings paramter to take preset value
Currently, we can provide either a PowerShell data file or a hashtable to the settings parameter. But we would like users to take advantage of the builtin setting files {PSGallery,DSC,etc} by providing only their names as values. This provides a easier way to use these builtin setting files.
1 parent d027b4e commit 1dbf68a

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

Engine/Commands/InvokeScriptAnalyzerCommand.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,13 @@ protected override void BeginProcessing()
278278
path));
279279
}
280280
}
281+
else if (IsBuiltinSettingPreset(settings))
282+
{
283+
settingFileHasErrors = !ScriptAnalyzer.Instance.ParseProfile(
284+
Helper.GetSettingPresetFilePath(settings as string),
285+
this.SessionState.Path,
286+
this);
287+
}
281288
else
282289
{
283290
settingFileHasErrors = !ScriptAnalyzer.Instance.ParseProfile(this.settings, this.SessionState.Path, this);
@@ -348,7 +355,19 @@ protected override void StopProcessing()
348355

349356
#endregion
350357

351-
#region Methods
358+
#region Private Methods
359+
360+
private static bool IsBuiltinSettingPreset(object settingPreset)
361+
{
362+
var preset = settingPreset as string;
363+
if (preset != null)
364+
{
365+
return Helper.GetBuiltinSettingPresets().Contains(preset, StringComparer.OrdinalIgnoreCase);
366+
}
367+
368+
return false;
369+
}
370+
352371
private void ProcessInput()
353372
{
354373
IEnumerable<DiagnosticRecord> diagnosticsList = Enumerable.Empty<DiagnosticRecord>();
@@ -392,6 +411,6 @@ private bool IsFileParameterSet()
392411
{
393412
return String.Equals(this.ParameterSetName, "File", StringComparison.OrdinalIgnoreCase);
394413
}
395-
#endregion
414+
#endregion // Private Methods
396415
}
397416
}

Engine/Helper.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,42 @@ public static string GetShippedSettingsDirectory()
201201
return settingsPath;
202202
}
203203

204+
/// <summary>
205+
/// Returns the builtin setting presets
206+
///
207+
/// Looks for powershell data files (*.psd1) in the PSScriptAnalyzer module settings directory
208+
/// and returns the names of the files without extension
209+
/// </summary>
210+
internal static IEnumerable<string> GetBuiltinSettingPresets()
211+
{
212+
var settingsPath = GetShippedSettingsDirectory();
213+
if (settingsPath != null)
214+
{
215+
foreach (var filepath in System.IO.Directory.EnumerateFiles(settingsPath, "*.psd1"))
216+
{
217+
yield return System.IO.Path.GetFileNameWithoutExtension(filepath);
218+
}
219+
}
220+
}
221+
222+
/// <summary>
223+
/// Gets the path to the settings file corresponding to the given preset.
224+
///
225+
/// If the corresponding preset file is not found, the method returns null.
226+
/// </summary>
227+
internal static string GetSettingPresetFilePath(string settingPreset)
228+
{
229+
var settingsPath = GetShippedSettingsDirectory();
230+
if (settingsPath != null)
231+
{
232+
if (GetBuiltinSettingPresets().Contains(settingPreset, StringComparer.OrdinalIgnoreCase))
233+
{
234+
return System.IO.Path.Combine(settingsPath, settingPreset + ".psd1");
235+
}
236+
}
237+
238+
return null;
239+
}
204240

205241
/// <summary>
206242
/// Returns all the rule arguments

0 commit comments

Comments
 (0)