Skip to content

Commit 03acbd6

Browse files
committed
Refactor to use GetShippedModuleSubDirectory for settings and command data file paths
1 parent 7427a41 commit 03acbd6

File tree

3 files changed

+12
-43
lines changed

3 files changed

+12
-43
lines changed

Engine/Settings/Settings.cs

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public static string TryAutoDiscover(string path)
229229
public static string TryResolvePreset(string name)
230230
{
231231
// Get the path to the folder of preset settings files shipped with the module
232-
var settingsDir = GetShippedSettingsDirectory();
232+
var settingsDir = GetShippedModuleSubDirectory("Settings");
233233

234234
// If we can't locate it, return null
235235
if (settingsDir == null) return null;
@@ -298,10 +298,10 @@ public static string Serialise(SettingsData data, string format)
298298
}
299299

300300
/// <summary>
301-
/// Retrieves the Settings directory from the Module directory structure
301+
/// Retrieves a subdirectory from the Module directory structure
302302
/// </summary>
303-
/// <returns>The Settings directory path</returns>
304-
public static string GetShippedSettingsDirectory()
303+
/// <returns>The subdirectory path</returns>
304+
public static string GetShippedModuleSubDirectory(string subDirectoryName)
305305
{
306306
// Find the compatibility files in Settings folder
307307
var path = typeof(Helper).GetTypeInfo().Assembly.Location;
@@ -313,50 +313,19 @@ public static string GetShippedSettingsDirectory()
313313
// Find the compatibility files in Settings folder adjacent to the assembly.
314314
// Some builds place binaries in subfolders (coreclr/, PSv3/); in those cases,
315315
// the Settings folder lives in the parent (module root), so we also probe one level up.
316-
var settingsPath = Path.Combine(Path.GetDirectoryName(path), "Settings");
317-
if (!Directory.Exists(settingsPath))
316+
var subDirectoryPath = Path.Combine(Path.GetDirectoryName(path), subDirectoryName);
317+
if (!Directory.Exists(subDirectoryPath))
318318
{
319319
// Probe parent directory (module root) for Settings folder.
320320
var parentDir = Path.GetDirectoryName(Path.GetDirectoryName(path));
321-
settingsPath = Path.Combine(parentDir ?? string.Empty, "Settings");
322-
if (!Directory.Exists(settingsPath))
321+
subDirectoryPath = Path.Combine(parentDir ?? string.Empty, subDirectoryName);
322+
if (!Directory.Exists(subDirectoryPath))
323323
{
324324
return null;
325325
}
326326
}
327327

328-
return settingsPath;
329-
}
330-
331-
/// <summary>
332-
/// Retrieves the Settings directory from the Module directory structure
333-
/// </summary>
334-
/// <returns>The Settings directory path</returns>
335-
public static string GetShippedCommandDataFileDirectory()
336-
{
337-
// Find the compatibility files in Settings folder
338-
var path = typeof(Helper).GetTypeInfo().Assembly.Location;
339-
if (string.IsNullOrWhiteSpace(path))
340-
{
341-
return null;
342-
}
343-
344-
// Find the compatibility files in Settings folder adjacent to the assembly.
345-
// Some builds place binaries in subfolders (coreclr/, PSv3/); in those cases,
346-
// the Settings folder lives in the parent (module root), so we also probe one level up.
347-
var commandDataFilesPath = Path.Combine(Path.GetDirectoryName(path), "CommandDataFiles");
348-
if (!Directory.Exists(commandDataFilesPath))
349-
{
350-
// Probe parent directory (module root) for CommandDataFiles folder.
351-
var parentDir = Path.GetDirectoryName(Path.GetDirectoryName(path));
352-
commandDataFilesPath = Path.Combine(parentDir ?? string.Empty, "CommandDataFiles");
353-
if (!Directory.Exists(commandDataFilesPath))
354-
{
355-
return null;
356-
}
357-
}
358-
359-
return commandDataFilesPath;
328+
return subDirectoryPath;
360329
}
361330

362331
/// <summary>
@@ -367,7 +336,7 @@ public static string GetShippedCommandDataFileDirectory()
367336
/// </summary>
368337
public static IEnumerable<string> GetSettingPresets()
369338
{
370-
var settingsPath = GetShippedSettingsDirectory();
339+
var settingsPath = GetShippedModuleSubDirectory("Settings");
371340

372341
if (settingsPath == null)
373342
{

Rules/AvoidOverwritingBuiltInCmdlets.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
9090
}
9191

9292
var psVerList = PowerShellVersion;
93-
string commandDataFilesPath = Settings.GetShippedCommandDataFileDirectory();
93+
string commandDataFilesPath = Settings.GetShippedModuleSubDirectory("CommandDataFiles");
9494

9595
foreach (string reference in psVerList)
9696
{

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 commandDataFilesPath = Settings.GetShippedCommandDataFileDirectory();
309+
string commandDataFilesPath = Settings.GetShippedModuleSubDirectory("CommandDataFiles");
310310
#if DEBUG
311311
object modeObject;
312312
if (ruleArgs.TryGetValue("mode", out modeObject))

0 commit comments

Comments
 (0)