Skip to content

Commit d7b23a9

Browse files
author
Kapil Borle
committed
Move settings method from Helper to Settings
1 parent 5513dcc commit d7b23a9

File tree

4 files changed

+70
-69
lines changed

4 files changed

+70
-69
lines changed

Engine/Commands/InvokeScriptAnalyzerCommand.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ protected override void BeginProcessing()
253253
break;
254254

255255
case SettingsMode.Preset:
256-
settingsFound = Helper.GetSettingPresetFilePath(this.settings as string);
256+
settingsFound = Generic.Settings.GetSettingPresetFilePath(this.settings as string);
257257
goto case SettingsMode.File;
258258

259259
case SettingsMode.File:
@@ -349,7 +349,7 @@ private static bool IsBuiltinSettingPreset(object settingPreset)
349349
var preset = settingPreset as string;
350350
if (preset != null)
351351
{
352-
return Helper.GetSettingPresets().Contains(preset, StringComparer.OrdinalIgnoreCase);
352+
return Generic.Settings.GetSettingPresets().Contains(preset, StringComparer.OrdinalIgnoreCase);
353353
}
354354

355355
return false;
@@ -437,7 +437,7 @@ private SettingsMode FindSettingsMode(out object settingsFound)
437437
if (IsBuiltinSettingPreset(settingsFilePath))
438438
{
439439
settingsMode = SettingsMode.Preset;
440-
settingsFound = Helper.GetSettingPresetFilePath(settingsFilePath);
440+
settingsFound = Generic.Settings.GetSettingPresetFilePath(settingsFilePath);
441441
}
442442
else
443443
{

Engine/Generic/Settings.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.IO;
1818
using System.Linq;
1919
using System.Management.Automation.Language;
20+
using System.Reflection;
2021

2122
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic
2223
{
@@ -89,6 +90,71 @@ public Settings(object settings) : this(settings, null)
8990
{
9091
}
9192

93+
/// <summary>
94+
/// Retrieves the Settings directory from the Module directory structure
95+
/// </summary>
96+
public static string GetShippedSettingsDirectory()
97+
{
98+
// Find the compatibility files in Settings folder
99+
var path = typeof(Helper).GetTypeInfo().Assembly.Location;
100+
if (String.IsNullOrWhiteSpace(path))
101+
{
102+
return null;
103+
}
104+
105+
var settingsPath = Path.Combine(Path.GetDirectoryName(path), "Settings");
106+
if (!Directory.Exists(settingsPath))
107+
{
108+
// try one level down as the PSScriptAnalyzer module structure is not consistent
109+
// CORECLR binaries are in PSScriptAnalyzer/coreclr/, PowerShell v3 binaries are in PSScriptAnalyzer/PSv3/
110+
// and PowerShell v5 binaries are in PSScriptAnalyzer/
111+
settingsPath = Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(path)), "Settings");
112+
if (!Directory.Exists(settingsPath))
113+
{
114+
return null;
115+
}
116+
}
117+
118+
return settingsPath;
119+
}
120+
121+
/// <summary>
122+
/// Returns the builtin setting presets
123+
///
124+
/// Looks for powershell data files (*.psd1) in the PSScriptAnalyzer module settings directory
125+
/// and returns the names of the files without extension
126+
/// </summary>
127+
public static IEnumerable<string> GetSettingPresets()
128+
{
129+
var settingsPath = GetShippedSettingsDirectory();
130+
if (settingsPath != null)
131+
{
132+
foreach (var filepath in System.IO.Directory.EnumerateFiles(settingsPath, "*.psd1"))
133+
{
134+
yield return System.IO.Path.GetFileNameWithoutExtension(filepath);
135+
}
136+
}
137+
}
138+
139+
/// <summary>
140+
/// Gets the path to the settings file corresponding to the given preset.
141+
///
142+
/// If the corresponding preset file is not found, the method returns null.
143+
/// </summary>
144+
public static string GetSettingPresetFilePath(string settingPreset)
145+
{
146+
var settingsPath = GetShippedSettingsDirectory();
147+
if (settingsPath != null)
148+
{
149+
if (GetSettingPresets().Contains(settingPreset, StringComparer.OrdinalIgnoreCase))
150+
{
151+
return System.IO.Path.Combine(settingsPath, settingPreset + ".psd1");
152+
}
153+
}
154+
155+
return null;
156+
}
157+
92158
/// <summary>
93159
/// Recursively convert hashtable to dictionary
94160
/// </summary>

Engine/Helper.cs

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -173,71 +173,6 @@ public void Initialize()
173173
}
174174
}
175175

176-
/// <summary>
177-
/// Retrieves the Settings directory from the Module directory structure
178-
/// </summary>
179-
public static string GetShippedSettingsDirectory()
180-
{
181-
// Find the compatibility files in Settings folder
182-
var path = typeof(Helper).GetTypeInfo().Assembly.Location;
183-
if (String.IsNullOrWhiteSpace(path))
184-
{
185-
return null;
186-
}
187-
188-
var settingsPath = Path.Combine(Path.GetDirectoryName(path), "Settings");
189-
if (!Directory.Exists(settingsPath))
190-
{
191-
// try one level down as the PSScriptAnalyzer module structure is not consistent
192-
// CORECLR binaries are in PSScriptAnalyzer/coreclr/, PowerShell v3 binaries are in PSScriptAnalyzer/PSv3/
193-
// and PowerShell v5 binaries are in PSScriptAnalyzer/
194-
settingsPath = Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(path)), "Settings");
195-
if (!Directory.Exists(settingsPath))
196-
{
197-
return null;
198-
}
199-
}
200-
201-
return settingsPath;
202-
}
203-
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-
public static IEnumerable<string> GetSettingPresets()
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-
public static string GetSettingPresetFilePath(string settingPreset)
228-
{
229-
var settingsPath = GetShippedSettingsDirectory();
230-
if (settingsPath != null)
231-
{
232-
if (GetSettingPresets().Contains(settingPreset, StringComparer.OrdinalIgnoreCase))
233-
{
234-
return System.IO.Path.Combine(settingsPath, settingPreset + ".psd1");
235-
}
236-
}
237-
238-
return null;
239-
}
240-
241176
/// <summary>
242177
/// Returns all the rule arguments
243178
/// </summary>

Rules/UseCompatibleCmdlets.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ private void SetupCmdletsDictionary()
306306
return;
307307
}
308308

309-
string settingsPath = Helper.GetShippedSettingsDirectory();
309+
string settingsPath = Settings.GetShippedSettingsDirectory();
310310
#if DEBUG
311311
object modeObject;
312312
if (ruleArgs.TryGetValue("mode", out modeObject))

0 commit comments

Comments
 (0)