Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change module root find function in CompatibilityRule #1196

Merged
merged 3 commits into from
Mar 28, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 27 additions & 17 deletions Rules/CompatibilityRules/CompatibilityRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,29 +205,39 @@ private string NormalizeProfileNameToAbsolutePath(string profileName)
return Path.Combine(_profileDir.FullName, profileName);
}

/// <summary>
/// Get the path of PSScriptAnalyzer on the file system.
/// </summary>
/// <returns>The absolute path of the PSScriptAnalyzer module root.</returns>
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;
}

return Path.GetFullPath(nonNormalizedRoot);
}
// 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;
}

// 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");
}
}

/// <summary>
Expand Down