Use more efficient platform detection API (#16760)

* Use more efficient platform detection API

Use static and const new OperatingSystem class API to avoid string comparisons and extra allocations.

* Remove unused variable
This commit is contained in:
Ilya
2022-01-27 08:53:52 -08:00
committed by GitHub
parent d45ff6a20a
commit 9f722ef950
9 changed files with 16 additions and 18 deletions
@@ -67,7 +67,7 @@ namespace Microsoft.PowerShell.Commands
protected override void BeginProcessing()
{
var args = "-P now";
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
if (Platform.IsMacOS)
{
args = "now";
}
@@ -137,7 +137,7 @@ namespace Microsoft.PowerShell.Commands
}
}
#else
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
if (Platform.IsLinux)
{
string errorMessage = UnblockFileStrings.LinuxNotSupported;
Exception e = new PlatformNotSupportedException(errorMessage);
@@ -26,7 +26,7 @@ namespace Microsoft.PowerShell.GlobalTool.Shim
public static int Main(string[] args)
{
var currentPath = new FileInfo(System.Reflection.Assembly.GetEntryAssembly().Location).Directory.FullName;
var isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
var isWindows = Platform.IsWindows;
string platformFolder = isWindows ? WinFolderName : UnixFolderName;
@@ -540,17 +540,17 @@ namespace System.Management.Automation
ext = string.Empty;
var processArch = RuntimeInformation.ProcessArchitecture.ToString().ToLowerInvariant();
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
if (Platform.IsWindows)
{
folderName = "win-" + processArch;
ext = ".dll";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
else if (Platform.IsLinux)
{
folderName = "linux-" + processArch;
ext = ".so";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
else if (Platform.IsMacOS)
{
folderName = "osx-x64";
ext = ".dylib";
@@ -22,7 +22,7 @@ namespace System.Management.Automation
{
get
{
return RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
return OperatingSystem.IsLinux();
}
}
@@ -33,7 +33,7 @@ namespace System.Management.Automation
{
get
{
return RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
return OperatingSystem.IsMacOS();
}
}
@@ -44,7 +44,7 @@ namespace System.Management.Automation
{
get
{
return RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
return OperatingSystem.IsWindows();
}
}
@@ -469,9 +469,7 @@ namespace Microsoft.PowerShell.Commands
#if !UNIX
// The placeholder mode management APIs Rtl(Set|Query)(Process|Thread)PlaceholderCompatibilityMode
// are only supported starting with Windows 10 version 1803 (build 17134)
Version minBuildForPlaceHolderAPIs = new Version(10, 0, 17134, 0);
if (Environment.OSVersion.Version >= minBuildForPlaceHolderAPIs)
if (OperatingSystem.IsWindowsVersionAtLeast(10, 0, 17134, 0))
{
// let's be safe, don't change the PlaceHolderCompatibilityMode if the current one is not what we expect
if (NativeMethods.RtlQueryProcessPlaceholderCompatibilityMode() == NativeMethods.PHCM_DISGUISE_PLACEHOLDER)
@@ -2724,8 +2722,7 @@ namespace Microsoft.PowerShell.Commands
// The new AllowUnprivilegedCreate is only available on Win10 build 14972 or newer
var flags = isDirectory ? NativeMethods.SymbolicLinkFlags.Directory : NativeMethods.SymbolicLinkFlags.File;
Version minBuildOfDeveloperMode = new Version(10, 0, 14972, 0);
if (Environment.OSVersion.Version >= minBuildOfDeveloperMode)
if (OperatingSystem.IsWindowsVersionAtLeast(10, 0, 14972, 0))
{
flags |= NativeMethods.SymbolicLinkFlags.AllowUnprivilegedCreate;
}
@@ -1213,7 +1213,7 @@ namespace System.Management.Automation
storeCU.Open(OpenFlags.ReadOnly);
X509Certificate2Collection storeCerts = storeCU.Certificates;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
if (Platform.IsWindows)
{
using (var storeLM = new X509Store("my", StoreLocation.LocalMachine))
{
+2 -1
View File
@@ -5,6 +5,7 @@
using System;
using System.IO;
using System.Management.Automation;
using System.Reflection;
using System.Runtime.InteropServices;
@@ -89,7 +90,7 @@ namespace Microsoft.PowerShell
return;
}
bool isLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
bool isLinux = Platform.IsLinux;
// The first byte (ASCII char) of the name of this process, used to detect '-' for login
byte procNameFirstByte;
+2 -2
View File
@@ -36,7 +36,7 @@ namespace Engine
yield return @"[System.IO.Path]::HasExtension('')";
// Test on COM method invocation.
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
if (Platform.IsWindows)
{
yield return @"$sh=New-Object -ComObject Shell.Application; $sh.Namespace('c:\')";
yield return @"$fs=New-Object -ComObject scripting.filesystemobject; $fs.Drives";
@@ -54,7 +54,7 @@ namespace Engine
// believe that there is no need to run many ops in each interation. However, the subsequent runs
// of this method is much faster than the first run, and this causes 'MinIterationTime' warnings
// to our benchmarks and make the benchmark results not reliable.
// Calling this method once in 'GlobalSetup' is a workaround.
// Calling this method once in 'GlobalSetup' is a workaround.
// See https://github.com/dotnet/BenchmarkDotNet/issues/837#issuecomment-828600157
scriptBlock.Invoke();
}