From 891f5c2c966f760bf52ae48a0ece4bac8c59e6ed Mon Sep 17 00:00:00 2001 From: Robert Holt Date: Tue, 26 Mar 2019 09:37:18 -0700 Subject: [PATCH 1/3] Change module root find function --- Rules/CompatibilityRules/CompatibilityRule.cs | 42 ++++++++++++------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/Rules/CompatibilityRules/CompatibilityRule.cs b/Rules/CompatibilityRules/CompatibilityRule.cs index 0d4b52bd3..433cfe924 100644 --- a/Rules/CompatibilityRules/CompatibilityRule.cs +++ b/Rules/CompatibilityRules/CompatibilityRule.cs @@ -205,27 +205,37 @@ private string NormalizeProfileNameToAbsolutePath(string profileName) return Path.Combine(_profileDir.FullName, profileName); } + /// + /// Get the path of PSScriptAnalyzer on the file system. + /// + /// The absolute path of the PSScriptAnalyzer module root. private static string GetModuleRootDirPath() { + // Start from the directory containing the Rules DLL, + // which may be in the module root or in a child directory (ex: coreclr) string asmDirLocation = Path.GetDirectoryName(typeof(CompatibilityRule).Assembly.Location); - // We check our assembly location and then parent, looking for PSScriptAnalyzer.psd1, - // because the assembly might be in the root of the module or in a child directory (ex: coreclr). - // That's the base where we will find our compatibility zip file. - // We can't hunt for the directory 'PSScriptAnalyzer' because we may be installed in - // PSScriptAnalyzer/1.18.0 or PSScriptAnalyzer. - const string psdFile = "PSScriptAnalyzer.psd1"; - string nonNormalizedRoot = asmDirLocation; - string psmPath = Path.Combine(nonNormalizedRoot, psdFile); - if ( ! File.Exists(psmPath) ) { - nonNormalizedRoot = Path.Combine(nonNormalizedRoot, ".."); - psmPath = Path.Combine(nonNormalizedRoot, psdFile); - if ( ! File.Exists(psmPath) ) { - // Couldn't find it, give up - return String.Empty; - } + + // Search down the directory structure from the assembly location looking for the module root + // We may be in a versioned directory ("PSScriptAnalyzer/1.18.0" vs "PSScriptAnalyzer"), so can't search that way + // Instead we look for the PSSA module manifest + const string manifestName = "PSScriptAnalyzer.psd1"; + + // Look for PSScriptAnalyzer.psd1 next to the Rules DLL + string manifestPath = Path.Combine(asmDirLocation, manifestName); + if (File.Exists(manifestPath)) + { + return asmDirLocation; + } + + // Look for PSScriptAnalyzer.psd1 in the directory above the Rules DLL + string dirUpOneLevel = Path.GetDirectoryName(asmDirLocation); + manifestPath = Path.Combine(dirUpOneLevel, manifestName); + if (File.Exists(manifestPath)) + { + return dirUpOneLevel; } - return Path.GetFullPath(nonNormalizedRoot); + throw new FileNotFoundException("Unable to find the PSScriptAnalyzer module root"); } } From e18a7752896c1fd1e8aa22f6725f227b5a0977f9 Mon Sep 17 00:00:00 2001 From: Robert Holt Date: Tue, 26 Mar 2019 09:40:03 -0700 Subject: [PATCH 2/3] Fix whitespace --- Rules/CompatibilityRules/CompatibilityRule.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Rules/CompatibilityRules/CompatibilityRule.cs b/Rules/CompatibilityRules/CompatibilityRule.cs index 433cfe924..9eed9621f 100644 --- a/Rules/CompatibilityRules/CompatibilityRule.cs +++ b/Rules/CompatibilityRules/CompatibilityRule.cs @@ -237,7 +237,6 @@ private static string GetModuleRootDirPath() throw new FileNotFoundException("Unable to find the PSScriptAnalyzer module root"); } - } /// From 641a6d9fae63ce8e7390e39143d99ed9327d4a52 Mon Sep 17 00:00:00 2001 From: Robert Holt Date: Tue, 26 Mar 2019 09:42:09 -0700 Subject: [PATCH 3/3] Add a comment --- Rules/CompatibilityRules/CompatibilityRule.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Rules/CompatibilityRules/CompatibilityRule.cs b/Rules/CompatibilityRules/CompatibilityRule.cs index 9eed9621f..0953b7831 100644 --- a/Rules/CompatibilityRules/CompatibilityRule.cs +++ b/Rules/CompatibilityRules/CompatibilityRule.cs @@ -235,6 +235,7 @@ private static string GetModuleRootDirPath() return dirUpOneLevel; } + // Unable to find the root of the module where it should be, so we give up throw new FileNotFoundException("Unable to find the PSScriptAnalyzer module root"); } }