Skip to content

Commit

Permalink
Switching from RuntimeInformation.IsOSPlatform(XYZ) to OperatingSyste…
Browse files Browse the repository at this point in the history
…m.IsXYZ() (#28233)

* Changed Hosting

* Changed Http

* Changed Identity

* Changed Middleware

* Changed Security

* Changed Servers

* Changed from RuntimeInterop to OperatingSystem

* Revert changes in DotNetMuxer

Co-authored-by: Pranav K <[email protected]>
  • Loading branch information
stefannikolei and pranavkm authored Dec 2, 2020
1 parent 5c897ac commit 7ce071b
Show file tree
Hide file tree
Showing 41 changed files with 103 additions and 132 deletions.
4 changes: 2 additions & 2 deletions eng/helix/content/RunTests/ProcessUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static Task CaptureDumpAsync(int pid)
public static Task CaptureDumpAsync(int pid, string dumpFilePath)
{
// Skip this on OSX, we know it's unsupported right now
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
if (OperatingSystem.IsMacOS())
{
// Can we capture stacks or do a gcdump instead?
return Task.CompletedTask;
Expand Down Expand Up @@ -178,7 +178,7 @@ public static async Task<ProcessResult> RunAsync(
await CaptureDumpAsync(process.Id, dumpFilePath);
}

if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
if (!OperatingSystem.IsWindows())
{
sys_kill(process.Id, sig: 2); // SIGINT

Expand Down
2 changes: 1 addition & 1 deletion eng/helix/content/RunTests/TestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ await ProcessUtil.RunAsync($"{Options.DotnetRoot}/dotnet",
cancellationToken: new CancellationTokenSource(TimeSpan.FromMinutes(2)).Token);

// ';' is the path separator on Windows, and ':' on Unix
Options.Path += RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ";" : ":";
Options.Path += OperatingSystem.IsWindows() ? ";" : ":";
Options.Path += $"{Environment.GetEnvironmentVariable("DOTNET_CLI_HOME")}/.dotnet/tools";
EnvironmentVariables["PATH"] = Options.Path;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Components/Components/src/PlatformInfo.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Runtime.InteropServices;
using System;

namespace Microsoft.AspNetCore.Components
{
Expand All @@ -11,7 +11,7 @@ internal static class PlatformInfo

static PlatformInfo()
{
IsWebAssembly = RuntimeInformation.IsOSPlatform(OSPlatform.Create("BROWSER"));
IsWebAssembly = OperatingSystem.IsBrowser();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private readonly ConcurrentDictionary<string, IDataProtector> _cachedDataProtect
private protected ProtectedBrowserStorage(string storeName, IJSRuntime jsRuntime, IDataProtectionProvider dataProtectionProvider)
{
// Performing data protection on the client would give users a false sense of security, so we'll prevent this.
if (RuntimeInformation.IsOSPlatform(OSPlatform.Create("BROWSER")))
if (OperatingSystem.IsBrowser())
{
throw new PlatformNotSupportedException($"{GetType()} cannot be used when running in a browser.");
}
Expand Down
10 changes: 5 additions & 5 deletions src/Components/WebAssembly/Server/src/TargetPickerUi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,17 +162,17 @@ private string GetLaunchChromeInstructions(string targetApplicationUrl)
var profilePath = Path.Combine(Path.GetTempPath(), "blazor-chrome-debug");
var debuggerPort = new Uri(_browserHost).Port;

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
if (OperatingSystem.IsWindows())
{
return $@"<p>Press Win+R and enter the following:</p>
<p><strong><code>chrome --remote-debugging-port={debuggerPort} --user-data-dir=""{profilePath}"" {targetApplicationUrl}</code></strong></p>";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
else if (OperatingSystem.IsLinux())
{
return $@"<p>In a terminal window execute the following:</p>
<p><strong><code>google-chrome --remote-debugging-port={debuggerPort} --user-data-dir={profilePath} {targetApplicationUrl}</code></strong></p>";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
else if (OperatingSystem.IsMacOS())
{
return $@"<p>Execute the following:</p>
<p><strong><code>open /Applications/Google\ Chrome.app --args --remote-debugging-port={debuggerPort} --user-data-dir={profilePath} {targetApplicationUrl}</code></strong></p>";
Expand All @@ -188,12 +188,12 @@ private string GetLaunchEdgeInstructions(string targetApplicationUrl)
var profilePath = Path.Combine(Path.GetTempPath(), "blazor-edge-debug");
var debuggerPort = new Uri(_browserHost).Port;

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
if (OperatingSystem.IsWindows())
{
return $@"<p>Press Win+R and enter the following:</p>
<p><strong><code>msedge --remote-debugging-port={debuggerPort} --user-data-dir=""{profilePath}"" --no-first-run {targetApplicationUrl}</code></strong></p>";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
else if (OperatingSystem.IsMacOS())
{
return $@"<p>In a terminal window execute the following:</p>
<p><strong><code>open /Applications/Microsoft\ Edge\ Dev.app --args --remote-debugging-port={debuggerPort} --user-data-dir={profilePath} {targetApplicationUrl}</code></strong></p>";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace Microsoft.AspNetCore.Components.WebAssembly.Rendering
{
Expand All @@ -19,8 +18,7 @@ internal static class RendererRegistry

static RendererRegistry()
{
bool _isWebAssembly = RuntimeInformation.IsOSPlatform(OSPlatform.Create("BROWSER"));
if (_isWebAssembly)
if (OperatingSystem.IsBrowser())
{
_renderers = new Dictionary<int, WebAssemblyRenderer>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Loader;
using System.Threading.Tasks;
using Microsoft.JSInterop;
Expand Down Expand Up @@ -48,7 +47,7 @@ public LazyAssemblyLoader(IJSRuntime jsRuntime)
/// <returns>A list of the loaded <see cref="Assembly"/></returns>
public async Task<IEnumerable<Assembly>> LoadAssembliesAsync(IEnumerable<string> assembliesToLoad)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Create("BROWSER")))
if (OperatingSystem.IsBrowser())
{
return await LoadAssembliesInClientAsync(assembliesToLoad);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Primitives;
Expand All @@ -28,7 +27,7 @@ namespace Microsoft.AspNetCore.Hosting.StaticWebAssets
// <<mylibrarypath>>\wwwroot\** to _content/mylibrary/**
internal class StaticWebAssetsFileProvider : IFileProvider
{
private static readonly StringComparison FilePathComparison = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ?
private static readonly StringComparison FilePathComparison = OperatingSystem.IsWindows() ?
StringComparison.OrdinalIgnoreCase :
StringComparison.Ordinal;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System;
using System.IO;
using System.Runtime.InteropServices;
using Xunit;

namespace Microsoft.AspNetCore.Hosting.StaticWebAssets
Expand Down Expand Up @@ -87,7 +86,7 @@ public void GetDirectoryContents_PartialMatchFails(string requestedUrl)
// Assert
Assert.Empty(directory);
}

[Fact]
public void GetDirectoryContents_HandlersEmptyPath()
{
Expand Down Expand Up @@ -164,7 +163,7 @@ public void StaticWebAssetsFileProviderWithEmptyBasePath_FindsFile()
public void GetFileInfo_DoesNotMatch_IncompletePrefixSegments()
{
// Arrange
var expectedResult = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
var expectedResult = OperatingSystem.IsWindows();
var provider = new StaticWebAssetsFileProvider(
"_cont",
Path.GetDirectoryName(typeof(StaticWebAssetsFileProviderTests).Assembly.Location));
Expand All @@ -180,7 +179,7 @@ public void GetFileInfo_DoesNotMatch_IncompletePrefixSegments()
public void GetFileInfo_Prefix_RespectsOsCaseSensitivity()
{
// Arrange
var expectedResult = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
var expectedResult = OperatingSystem.IsWindows();
var provider = new StaticWebAssetsFileProvider(
"_content",
Path.GetDirectoryName(typeof(StaticWebAssetsFileProviderTests).Assembly.Location));
Expand All @@ -196,7 +195,7 @@ public void GetFileInfo_Prefix_RespectsOsCaseSensitivity()
public void GetDirectoryContents_Prefix_RespectsOsCaseSensitivity()
{
// Arrange
var expectedResult = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
var expectedResult = OperatingSystem.IsWindows();
var provider = new StaticWebAssetsFileProvider(
"_content",
Path.GetDirectoryName(typeof(StaticWebAssetsFileProviderTests).Assembly.Location));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ public virtual Task<PublishedApplication> Publish(DeploymentParameters deploymen
private static string GetRuntimeIdentifier(DeploymentParameters deploymentParameters)
{
var architecture = deploymentParameters.RuntimeArchitecture;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
if (OperatingSystem.IsWindows())
{
return "win-" + architecture;
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
if (OperatingSystem.IsLinux())
{
return "linux-" + architecture;
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
if (OperatingSystem.IsMacOS())
{
return "osx-" + architecture;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static string GetDotNetInstallDir(RuntimeArchitecture arch)
{
var dotnetDir = DotNetHome;
var archSpecificDir = Path.Combine(dotnetDir, arch.ToString());
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && Directory.Exists(archSpecificDir))
if (OperatingSystem.IsWindows() && Directory.Exists(archSpecificDir))
{
dotnetDir = archSpecificDir;
}
Expand All @@ -77,7 +77,7 @@ public static string GetDotNetExecutable(RuntimeArchitecture arch)

var dotnetFile = "dotnet";

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
if (OperatingSystem.IsWindows())
{
dotnetFile += ".exe";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.Net;
using System.Net.Http;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Server.IntegrationTesting.Common;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -42,7 +41,7 @@ public override async Task<DeploymentResult> DeployAsync()
if (uri.Port == 0)
{
var builder = new UriBuilder(uri);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
if (OperatingSystem.IsLinux())
{
// This works with nginx 1.9.1 and later using the reuseport flag, available on Ubuntu 16.04.
// Keep it open so nobody else claims the port
Expand Down Expand Up @@ -112,7 +111,7 @@ private string GetUserName()
return retVal;
}

if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
if (!OperatingSystem.IsWindows())
{
using (var process = new Process
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -88,7 +87,7 @@ public override async Task<DeploymentResult> DeployAsync()
var executableArgs = string.Empty;
var workingDirectory = string.Empty;
var executableExtension = DeploymentParameters.ApplicationType == ApplicationType.Portable ? ".dll"
: (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".exe" : "");
: (OperatingSystem.IsWindows() ? ".exe" : "");

if (DeploymentParameters.PublishApplicationBeforeDeployment)
{
Expand Down
8 changes: 4 additions & 4 deletions src/Hosting/Server.IntegrationTesting/src/TestMatrix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,13 @@ private static string SkipIfServerIsNotSupportedOnThisOS(ServerType server)
case ServerType.IIS:
case ServerType.IISExpress:
case ServerType.HttpSys:
skip = !RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
skip = !OperatingSystem.IsWindows();
break;
case ServerType.Kestrel:
break;
case ServerType.Nginx:
// Technically it's possible but we don't test it.
skip = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
skip = OperatingSystem.IsWindows();
break;
default:
throw new ArgumentException(server.ToString());
Expand Down Expand Up @@ -195,7 +195,7 @@ private bool CheckTfmIsSupportedForServer(string tfm, ServerType server)

private static string SkipIfTfmIsNotSupportedOnThisOS(string tfm)
{
if (Tfm.Matches(Tfm.Net461, tfm) && !RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
if (Tfm.Matches(Tfm.Net461, tfm) && !OperatingSystem.IsWindows())
{
return "This TFM is not supported on this operating system.";
}
Expand Down Expand Up @@ -263,7 +263,7 @@ private string SkipIfArchitectureNotSupportedOnCurrentSystem(RuntimeArchitecture
}

// No x86 runtimes available on MacOS or Linux.
return RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? null : $"No {arch} available for non-Windows systems.";
return OperatingSystem.IsWindows() ? null : $"No {arch} available for non-Windows systems.";
}

private bool IsArchitectureSupportedOnServer(RuntimeArchitecture arch, ServerType server)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Xml.Linq;

namespace Microsoft.AspNetCore.Server.IntegrationTesting
Expand All @@ -16,7 +15,7 @@ public class IISExpressAncmSchema

static IISExpressAncmSchema()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
if (!OperatingSystem.IsWindows())
{
SkipReason = "IIS Express tests can only be run on Windows";
return;
Expand Down Expand Up @@ -52,4 +51,4 @@ static IISExpressAncmSchema()
SkipReason = SupportsInProcessHosting ? null : "IIS Express must be upgraded to support in-process hosting.";
}
}
}
}
5 changes: 2 additions & 3 deletions src/Http/Http/src/BindingAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;

namespace Microsoft.AspNetCore.Http
{
Expand Down Expand Up @@ -55,7 +54,7 @@ public string UnixPipePath
private static string GetUnixPipePath(string host)
{
var unixPipeHostPrefixLength = UnixPipeHostPrefix.Length;
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
if (!OperatingSystem.IsWindows())
{
// "/" character in unix refers to root. Windows has drive letters and volume separator (c:)
unixPipeHostPrefixLength--;
Expand Down Expand Up @@ -117,7 +116,7 @@ public static BindingAddress Parse(string address)
else
{
var unixPipeHostPrefixLength = UnixPipeHostPrefix.Length;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
if (OperatingSystem.IsWindows())
{
// Windows has drive letters and volume separator (c:)
unixPipeHostPrefixLength += 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using Microsoft.AspNetCore.ApiAuthorization.IdentityServer.Configuration;
using Microsoft.Extensions.Configuration;
Expand Down Expand Up @@ -102,8 +101,8 @@ public SigningCredentials LoadKey()

private X509KeyStorageFlags GetStorageFlags(KeyDefinition key)
{
var defaultFlags = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ?
UnsafeEphemeralKeySet : (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? X509KeyStorageFlags.PersistKeySet :
var defaultFlags = OperatingSystem.IsLinux() ?
UnsafeEphemeralKeySet : (OperatingSystem.IsMacOS() ? X509KeyStorageFlags.PersistKeySet :
X509KeyStorageFlags.DefaultKeySet);

if (key.StorageFlags == null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.Configuration;
Expand All @@ -18,8 +18,8 @@ public class ConfigureSigningCredentialsTests
// due to the fact that is not part of .NET Standard. This value is only used with non-windows
// platforms (all .NET Core) for which the value is defined on the underlying platform.
private const X509KeyStorageFlags UnsafeEphemeralKeySet = (X509KeyStorageFlags)32;
private static readonly X509KeyStorageFlags DefaultFlags = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ?
UnsafeEphemeralKeySet : (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? X509KeyStorageFlags.PersistKeySet :
private static readonly X509KeyStorageFlags DefaultFlags = OperatingSystem.IsLinux() ?
UnsafeEphemeralKeySet : (OperatingSystem.IsMacOS() ? X509KeyStorageFlags.PersistKeySet :
X509KeyStorageFlags.DefaultKeySet);

[ConditionalFact]
Expand Down
Loading

0 comments on commit 7ce071b

Please sign in to comment.