Add cache for GetApplicationBase (#3969)

This commit is contained in:
Ilya
2017-06-20 15:08:02 -07:00
committed by Dongbo Wang
parent 5a84e60c13
commit 598cebf7b2
19 changed files with 59 additions and 69 deletions
@@ -24,7 +24,7 @@ namespace Microsoft.PowerShell
// The path to the semaphore file which enables telemetry
private static string TelemetrySemaphoreFilePath = Path.Combine(
Utils.GetApplicationBase(Utils.DefaultPowerShellShellID),
Utils.DefaultPowerShellAppBase,
TelemetrySemaphoreFilename);
// Telemetry client to be reused when we start sending more telemetry
@@ -159,7 +159,7 @@ namespace System.Management.Automation
if (IsNanoServer || IsIoT)
{
_isInbox = string.Equals(
Utils.GetApplicationBase(Utils.DefaultPowerShellShellID),
Utils.DefaultPowerShellAppBase,
Utils.GetApplicationBaseFromRegistry(Utils.DefaultPowerShellShellID),
StringComparison.OrdinalIgnoreCase);
}
@@ -306,8 +306,7 @@ namespace System.Management.Automation.Runspaces
/// <returns></returns>
public static FormatTable LoadDefaultFormatFiles()
{
string shellId = Utils.DefaultPowerShellShellID;
string psHome = Utils.GetApplicationBase(shellId);
string psHome = Utils.DefaultPowerShellAppBase;
List<string> defaultFormatFiles = new List<string>();
if (!string.IsNullOrEmpty(psHome))
{
@@ -490,7 +490,7 @@ namespace Microsoft.PowerShell.Commands.Internal.Format
{
var builtInGenerators = new Dictionary<string, Tuple<bool, TypeGenerator>>(StringComparer.OrdinalIgnoreCase);
var psHome = Utils.GetApplicationBase(Utils.DefaultPowerShellShellID);
var psHome = Utils.DefaultPowerShellAppBase;
builtInGenerators.Add(Path.Combine(psHome, "Certificate.format.ps1xml"), GetBuiltin(false, Certificate_Format_Ps1Xml.GetFormatData));
builtInGenerators.Add(Path.Combine(psHome, "Diagnostics.Format.ps1xml"), GetBuiltin(false, Diagnostics_Format_Ps1Xml.GetFormatData));
@@ -105,7 +105,7 @@ namespace System.Management.Automation
string psHome = null;
try
{
psHome = Utils.GetApplicationBase(Utils.DefaultPowerShellShellID);
psHome = Utils.DefaultPowerShellAppBase;
}
catch (System.Security.SecurityException)
{
@@ -46,7 +46,7 @@ namespace System.Management.Automation.Runspaces
{
// Building the catalog is expensive, so force that to happen early on a background thread, and do so
// on a file we are very likely to read anyway.
var pshome = Utils.GetApplicationBase(Utils.DefaultPowerShellShellID);
var pshome = Utils.DefaultPowerShellAppBase;
var unused = SecuritySupport.IsProductBinary(Path.Combine(pshome, "Modules", "Microsoft.PowerShell.Utility", "Microsoft.PowerShell.Utility.psm1"));
});
@@ -1597,7 +1597,7 @@ namespace System.Management.Automation.Runspaces
private static void IncludePowerShellCoreFormats(InitialSessionState iss)
{
string psHome = Utils.GetApplicationBase(Utils.DefaultPowerShellShellID);
string psHome = Utils.DefaultPowerShellAppBase;
if (string.IsNullOrEmpty(psHome))
{
return;
@@ -4207,7 +4207,7 @@ namespace System.Management.Automation.Runspaces
// We skip checking if the file exists when it's in $PSHOME because of magic
// where we have the former contents of those files built into the engine directly.
var psHome = Utils.GetApplicationBase(Utils.DefaultPowerShellShellID);
var psHome = Utils.DefaultPowerShellAppBase;
foreach (string file in psSnapInInfo.Types)
{
@@ -1768,11 +1768,11 @@ namespace Microsoft.PowerShell.Commands
{
if (m.Name.Equals(moduleName, StringComparison.OrdinalIgnoreCase))
{
m.SetModuleBase(Utils.GetApplicationBase(Utils.DefaultPowerShellShellID));
m.SetModuleBase(Utils.DefaultPowerShellAppBase);
// Also set ModuleBase for nested modules of Engine modules
foreach (var nestedModule in m.NestedModules)
{
nestedModule.SetModuleBase(Utils.GetApplicationBase(Utils.DefaultPowerShellShellID));
nestedModule.SetModuleBase(Utils.DefaultPowerShellAppBase);
}
}
}
@@ -1781,11 +1781,11 @@ namespace Microsoft.PowerShell.Commands
{
if (m.Name.Equals(moduleName, StringComparison.OrdinalIgnoreCase))
{
m.SetModuleBase(Utils.GetApplicationBase(Utils.DefaultPowerShellShellID));
m.SetModuleBase(Utils.DefaultPowerShellAppBase);
// Also set ModuleBase for nested modules of Engine modules
foreach (var nestedModule in m.NestedModules)
{
nestedModule.SetModuleBase(Utils.GetApplicationBase(Utils.DefaultPowerShellShellID));
nestedModule.SetModuleBase(Utils.DefaultPowerShellAppBase);
}
}
}
@@ -2768,19 +2768,19 @@ namespace Microsoft.PowerShell.Commands
}
if (scriptsToProcess != null)
{
Version savedBaseMinimumVersion = BaseMinimumVersion;
Version savedBaseMaximumVersion = BaseMaximumVersion;
Version savedBaseRequiredVersion = BaseRequiredVersion;
{
Version savedBaseMinimumVersion = BaseMinimumVersion;
Version savedBaseMaximumVersion = BaseMaximumVersion;
Version savedBaseRequiredVersion = BaseRequiredVersion;
Guid? savedBaseGuid = BaseGuid;
try
{
BaseMinimumVersion = null;
BaseMaximumVersion = null;
BaseRequiredVersion = null;
BaseGuid = null;
try
{
BaseMinimumVersion = null;
BaseMaximumVersion = null;
BaseRequiredVersion = null;
BaseGuid = null;
foreach (string scriptFile in scriptsToProcess)
{
bool found = false;
@@ -2812,14 +2812,14 @@ namespace Microsoft.PowerShell.Commands
module.ExportedAliases[detectedAlias].Definition);
}
}
}
}
finally
{
BaseMinimumVersion = savedBaseMinimumVersion;
BaseMaximumVersion = savedBaseMaximumVersion;
BaseRequiredVersion = savedBaseRequiredVersion;
BaseGuid = savedBaseGuid;
}
}
finally
{
BaseMinimumVersion = savedBaseMinimumVersion;
BaseMaximumVersion = savedBaseMaximumVersion;
BaseRequiredVersion = savedBaseRequiredVersion;
BaseGuid = savedBaseGuid;
}
}
@@ -4123,10 +4123,10 @@ namespace Microsoft.PowerShell.Commands
Dictionary<ModuleSpecification, List<ModuleSpecification>> requiredModules = new Dictionary<ModuleSpecification, List<ModuleSpecification>>(new ModuleSpecificationComparer());
if (currentModule != null)
{
requiredModules.Add(new ModuleSpecification(currentModule), new List<ModuleSpecification> { requiredModuleSpecification });
requiredModules.Add(new ModuleSpecification(currentModule), new List<ModuleSpecification> { requiredModuleSpecification });
}
if (requiredModuleSpecification != null)
{
if (requiredModuleSpecification != null)
{
requiredModules.Add(requiredModuleSpecification, new List<ModuleSpecification>(requiredModuleInfo.RequiredModulesSpecification));
}
@@ -4498,13 +4498,13 @@ namespace Microsoft.PowerShell.Commands
// Only add if this required module has nested required modules (meaning, it could lead to a circular reference)
if (list.Count > 0)
{
nonCyclicRequiredModules.Add(requiredModuleSpecification, list);
// We always need to check against the module specification and not the file name
if (HasRequiredModulesCyclicReference(requiredModuleSpecification, list, availableModules, nonCyclicRequiredModules, out error))
{
return true;
nonCyclicRequiredModules.Add(requiredModuleSpecification, list);
// We always need to check against the module specification and not the file name
if (HasRequiredModulesCyclicReference(requiredModuleSpecification, list, availableModules, nonCyclicRequiredModules, out error))
{
return true;
}
}
}
}
}
}
@@ -4680,7 +4680,7 @@ namespace Microsoft.PowerShell.Commands
if (listOfStrings != null)
{
var psHome = Utils.GetApplicationBase(Utils.DefaultPowerShellShellID);
var psHome = Utils.DefaultPowerShellAppBase;
string alternateDirToCheck = null;
if (moduleBase.StartsWith(psHome, StringComparison.OrdinalIgnoreCase))
{
@@ -557,7 +557,7 @@ namespace System.Management.Automation
try
{
string psHome = Utils.GetApplicationBase(Utils.DefaultPowerShellShellID);
string psHome = Utils.DefaultPowerShellAppBase;
if (!string.IsNullOrEmpty(psHome))
{
// Win8: 584267 Powershell Modules are listed twice in x86, and cannot be removed
@@ -410,7 +410,7 @@ namespace System.Management.Automation.Internal
tempModuleInfo = new PSModuleInfo(moduleShortName, modulePath, null, null);
if (InitialSessionState.IsEngineModule(moduleShortName))
{
tempModuleInfo.SetModuleBase(Utils.GetApplicationBase(Utils.DefaultPowerShellShellID));
tempModuleInfo.SetModuleBase(Utils.DefaultPowerShellAppBase);
}
//moduleVersionRequired is bypassed by FullyQualifiedModule from calling method. This is the only place where guid will be involved.
@@ -3440,8 +3440,7 @@ namespace System.Management.Automation.Runspaces
string typesFilePath = string.Empty;
string typesV3FilePath = string.Empty;
string shellId = Utils.DefaultPowerShellShellID;
var psHome = Utils.GetApplicationBase(shellId);
var psHome = Utils.DefaultPowerShellAppBase;
if (!string.IsNullOrEmpty(psHome))
{
typesFilePath = Path.Combine(psHome, "types.ps1xml");
@@ -4404,7 +4403,7 @@ namespace System.Management.Automation.Runspaces
var result = false;
var errorCount = errors.Count;
var psHome = Utils.GetApplicationBase(Utils.DefaultPowerShellShellID);
var psHome = Utils.DefaultPowerShellAppBase;
if (string.Equals(Path.Combine(psHome, "types.ps1xml"), filePath, StringComparison.OrdinalIgnoreCase))
{
ProcessTypeData(filePath, errors, Types_Ps1Xml.Get());
@@ -238,20 +238,12 @@ namespace System.Management.Automation
}
#endif
/// <summary>
/// Gets the application base for current monad version
/// </summary>
/// <returns>
/// applicationbase path for current monad version installation
/// </returns>
/// <exception cref="SecurityException">
/// if caller doesn't have permission to read the key
/// </exception>
internal static string DefaultPowerShellAppBase { get; } = GetApplicationBase(DefaultPowerShellShellID);
internal static string GetApplicationBase(string shellId)
{
#if CORECLR
// Use the location of SMA.dll as the application base
// Assembly.GetEntryAssembly and GAC are not in CoreCLR.
// Use the location of SMA.dll as the application base.
Assembly assembly = typeof(PSObject).GetTypeInfo().Assembly;
return Path.GetDirectoryName(assembly.Location);
#else
@@ -312,7 +304,7 @@ namespace System.Management.Automation
List<string> baseDirectories = new List<string>();
// Retrieve the application base from the registry
string appBase = GetApplicationBase(DefaultPowerShellShellID);
string appBase = Utils.DefaultPowerShellAppBase;
if (!string.IsNullOrEmpty(appBase))
{
baseDirectories.Add(appBase);
@@ -381,7 +373,7 @@ namespace System.Management.Automation
/// </summary>
internal static bool IsRunningFromSysWOW64()
{
return Utils.GetApplicationBase(Utils.DefaultPowerShellShellID).Contains("SysWOW64");
return DefaultPowerShellAppBase.Contains("SysWOW64");
}
/// <summary>
@@ -37,10 +37,10 @@ namespace System.Management.Automation.Runspaces
static PowerShellProcessInstance()
{
#if UNIX
s_PSExePath = Path.Combine(Utils.GetApplicationBase(Utils.DefaultPowerShellShellID),
s_PSExePath = Path.Combine(Utils.DefaultPowerShellAppBase,
"powershell");
#else
s_PSExePath = Path.Combine(Utils.GetApplicationBase(Utils.DefaultPowerShellShellID),
s_PSExePath = Path.Combine(Utils.DefaultPowerShellAppBase,
"powershell.exe");
#endif
}
@@ -964,7 +964,7 @@ else
}
}
string destPath = System.IO.Path.Combine(Utils.GetApplicationBase(Utils.DefaultPowerShellShellID), "SessionConfig",
string destPath = System.IO.Path.Combine(Utils.DefaultPowerShellAppBase, "SessionConfig",
shellName + "_" + sessionGuid.ToString() + StringLiterals.PowerShellDISCFileExtension);
if (string.Equals(ProcessorArchitecture, "x86", StringComparison.OrdinalIgnoreCase))
{
@@ -3414,7 +3414,7 @@ Set-PSSessionConfiguration $args[0] $args[1] $args[2] $args[3] $args[4] $args[5]
_gmsaAccount = _configTable[ConfigFileConstants.GMSAAccount] as string;
}
string destPath = System.IO.Path.Combine(Utils.GetApplicationBase(Utils.DefaultPowerShellShellID), "SessionConfig",
string destPath = System.IO.Path.Combine(Utils.DefaultPowerShellAppBase, "SessionConfig",
shellName + "_" + sessionGuid.ToString() + StringLiterals.PowerShellDISCFileExtension);
// If the config file with the same guid name already exists then it would be overwritten.
@@ -381,7 +381,7 @@ namespace Microsoft.PowerShell.Commands
#if !CORECLR // Side-By-Side directories are not present in OneCore environments.
if (IsSystemModule(module.ModuleName) && Environment.Is64BitOperatingSystem)
{
string path = Utils.GetApplicationBase(Utils.DefaultPowerShellShellID).Replace("System32", "SysWOW64");
string path = Utils.DefaultPowerShellAppBase.Replace("System32", "SysWOW64");
destPaths.Add(path);
}
@@ -103,7 +103,7 @@ namespace System.Management.Automation.Runspaces
Collection<PSSnapInTypeAndFormatErrors> returnValue = new Collection<PSSnapInTypeAndFormatErrors>();
string baseFolder = GetBaseFolder(runspaceConfiguration, independentErrors);
var psHome = Utils.GetApplicationBase(Utils.DefaultPowerShellShellID);
var psHome = Utils.DefaultPowerShellAppBase;
// this hashtable will be used to check whether this is duplicated file for types or formats.
HashSet<string> fullFileNameSet = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
@@ -386,7 +386,7 @@ namespace System.Management.Automation
if (!Signature.CatalogApiAvailable.HasValue)
{
string productFile = Path.Combine(Utils.GetApplicationBase(Utils.DefaultPowerShellShellID), "Modules\\Microsoft.PowerShell.Utility\\Microsoft.PowerShell.Utility.psm1");
string productFile = Path.Combine(Utils.DefaultPowerShellAppBase, "Modules\\Microsoft.PowerShell.Utility\\Microsoft.PowerShell.Utility.psm1");
if (signature.Status != SignatureStatus.Valid)
{
if (string.Equals(filename, productFile, StringComparison.OrdinalIgnoreCase))
@@ -896,7 +896,7 @@ namespace System.Management.Automation
internal static void ReadRegistryInfo(out Version assemblyVersion, out string publicKeyToken, out string culture, out string architecture, out string applicationBase, out Version psVersion)
{
applicationBase = Utils.GetApplicationBase(Utils.DefaultPowerShellShellID);
applicationBase = Utils.DefaultPowerShellAppBase;
Dbg.Assert(!string.IsNullOrEmpty(applicationBase),
string.Format(CultureInfo.CurrentCulture, "{0} is empty or null", RegistryStrings.MonadEngine_ApplicationBase));
@@ -168,7 +168,7 @@ namespace Microsoft.PowerShell.Telemetry.Internal
var companyName = foundModule.CompanyName;
bool couldBeMicrosoftModule =
(modulePath != null &&
(modulePath.StartsWith(Utils.GetApplicationBase(Utils.DefaultPowerShellShellID), StringComparison.OrdinalIgnoreCase) ||
(modulePath.StartsWith(Utils.DefaultPowerShellAppBase, StringComparison.OrdinalIgnoreCase) ||
// The following covers both 64 and 32 bit Program Files by assuming 32bit is just ...\Program Files + " (x86)"
modulePath.StartsWith(Platform.GetFolderPath(Environment.SpecialFolder.ProgramFiles), StringComparison.OrdinalIgnoreCase))) ||
(companyName != null &&