Skip to content

Commit 9f722ef

Browse files
authored
Use more efficient platform detection API (PowerShell#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
1 parent d45ff6a commit 9f722ef

File tree

9 files changed

+16
-18
lines changed

9 files changed

+16
-18
lines changed

src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public sealed class StopComputerCommand : CommandLineCmdletBase
6767
protected override void BeginProcessing()
6868
{
6969
var args = "-P now";
70-
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
70+
if (Platform.IsMacOS)
7171
{
7272
args = "now";
7373
}

src/Microsoft.PowerShell.Commands.Utility/commands/utility/UnblockFile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ protected override void ProcessRecord()
137137
}
138138
}
139139
#else
140-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
140+
if (Platform.IsLinux)
141141
{
142142
string errorMessage = UnblockFileStrings.LinuxNotSupported;
143143
Exception e = new PlatformNotSupportedException(errorMessage);

src/Microsoft.PowerShell.GlobalTool.Shim/GlobalToolShim.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static class EntryPoint
2626
public static int Main(string[] args)
2727
{
2828
var currentPath = new FileInfo(System.Reflection.Assembly.GetEntryAssembly().Location).Directory.FullName;
29-
var isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
29+
var isWindows = Platform.IsWindows;
3030

3131
string platformFolder = isWindows ? WinFolderName : UnixFolderName;
3232

src/System.Management.Automation/CoreCLR/CorePsAssemblyLoadContext.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -540,17 +540,17 @@ private static string GetNativeDllSubFolderName(out string ext)
540540
ext = string.Empty;
541541
var processArch = RuntimeInformation.ProcessArchitecture.ToString().ToLowerInvariant();
542542

543-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
543+
if (Platform.IsWindows)
544544
{
545545
folderName = "win-" + processArch;
546546
ext = ".dll";
547547
}
548-
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
548+
else if (Platform.IsLinux)
549549
{
550550
folderName = "linux-" + processArch;
551551
ext = ".so";
552552
}
553-
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
553+
else if (Platform.IsMacOS)
554554
{
555555
folderName = "osx-x64";
556556
ext = ".dylib";

src/System.Management.Automation/CoreCLR/CorePsPlatform.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static bool IsLinux
2222
{
2323
get
2424
{
25-
return RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
25+
return OperatingSystem.IsLinux();
2626
}
2727
}
2828

@@ -33,7 +33,7 @@ public static bool IsMacOS
3333
{
3434
get
3535
{
36-
return RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
36+
return OperatingSystem.IsMacOS();
3737
}
3838
}
3939

@@ -44,7 +44,7 @@ public static bool IsWindows
4444
{
4545
get
4646
{
47-
return RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
47+
return OperatingSystem.IsWindows();
4848
}
4949
}
5050

src/System.Management.Automation/namespaces/FileSystemProvider.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -469,9 +469,7 @@ protected override ProviderInfo Start(ProviderInfo providerInfo)
469469
#if !UNIX
470470
// The placeholder mode management APIs Rtl(Set|Query)(Process|Thread)PlaceholderCompatibilityMode
471471
// are only supported starting with Windows 10 version 1803 (build 17134)
472-
Version minBuildForPlaceHolderAPIs = new Version(10, 0, 17134, 0);
473-
474-
if (Environment.OSVersion.Version >= minBuildForPlaceHolderAPIs)
472+
if (OperatingSystem.IsWindowsVersionAtLeast(10, 0, 17134, 0))
475473
{
476474
// let's be safe, don't change the PlaceHolderCompatibilityMode if the current one is not what we expect
477475
if (NativeMethods.RtlQueryProcessPlaceholderCompatibilityMode() == NativeMethods.PHCM_DISGUISE_PLACEHOLDER)
@@ -2724,8 +2722,7 @@ private static bool WinCreateSymbolicLink(string path, string strTargetPath, boo
27242722
// The new AllowUnprivilegedCreate is only available on Win10 build 14972 or newer
27252723
var flags = isDirectory ? NativeMethods.SymbolicLinkFlags.Directory : NativeMethods.SymbolicLinkFlags.File;
27262724

2727-
Version minBuildOfDeveloperMode = new Version(10, 0, 14972, 0);
2728-
if (Environment.OSVersion.Version >= minBuildOfDeveloperMode)
2725+
if (OperatingSystem.IsWindowsVersionAtLeast(10, 0, 14972, 0))
27292726
{
27302727
flags |= NativeMethods.SymbolicLinkFlags.AllowUnprivilegedCreate;
27312728
}

src/System.Management.Automation/security/SecuritySupport.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1213,7 +1213,7 @@ private void ResolveFromStoreById(ResolutionPurpose purpose, out ErrorRecord err
12131213
storeCU.Open(OpenFlags.ReadOnly);
12141214
X509Certificate2Collection storeCerts = storeCU.Certificates;
12151215

1216-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
1216+
if (Platform.IsWindows)
12171217
{
12181218
using (var storeLM = new X509Store("my", StoreLocation.LocalMachine))
12191219
{

src/powershell/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
using System;
77
using System.IO;
8+
using System.Management.Automation;
89
using System.Reflection;
910
using System.Runtime.InteropServices;
1011

@@ -89,7 +90,7 @@ private static void AttemptExecPwshLogin(string[] args)
8990
return;
9091
}
9192

92-
bool isLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
93+
bool isLinux = Platform.IsLinux;
9394

9495
// The first byte (ASCII char) of the name of this process, used to detect '-' for login
9596
byte procNameFirstByte;

test/perf/benchmarks/Engine.ScriptBlock.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public IEnumerable<string> ValuesForScript()
3636
yield return @"[System.IO.Path]::HasExtension('')";
3737

3838
// Test on COM method invocation.
39-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
39+
if (Platform.IsWindows)
4040
{
4141
yield return @"$sh=New-Object -ComObject Shell.Application; $sh.Namespace('c:\')";
4242
yield return @"$fs=New-Object -ComObject scripting.filesystemobject; $fs.Drives";
@@ -54,7 +54,7 @@ public void GlobalSetup()
5454
// believe that there is no need to run many ops in each interation. However, the subsequent runs
5555
// of this method is much faster than the first run, and this causes 'MinIterationTime' warnings
5656
// to our benchmarks and make the benchmark results not reliable.
57-
// Calling this method once in 'GlobalSetup' is a workaround.
57+
// Calling this method once in 'GlobalSetup' is a workaround.
5858
// See https://github.com/dotnet/BenchmarkDotNet/issues/837#issuecomment-828600157
5959
scriptBlock.Invoke();
6060
}

0 commit comments

Comments
 (0)