Skip to content

Commit 7ab6a5d

Browse files
committed
build: annotate platform-specific code
Annotate any OS or platform specific code with the new (Un)SupportedOSPlatform(Guard) attributes and update various platform util methods to use the OperatingSystem.Is<platform> methods. This will help ensure we're not missing any OS checks in the future. For test projects we ignore these warnings since we're using skipping Xunit tests that are not applicable for the current platform already, making these warnings just noise.
1 parent b2152d9 commit 7ab6a5d

17 files changed

+57
-3
lines changed

Directory.Build.targets

+5
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,9 @@
3333
</ItemGroup>
3434
</Target>
3535

36+
<PropertyGroup>
37+
<!-- Ignore platform API compatibilty checks in test projects -->
38+
<NoWarn Condition="'$(IsTestProject)'=='true'">$(NoWarn);CA1416</NoWarn>
39+
</PropertyGroup>
40+
3641
</Project>

src/shared/Core/Constants.cs

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ public static class Constants
1616

1717
public const string GcmDataDirectoryName = ".gcm";
1818

19+
public const string WindowsPlatformName = "windows";
20+
public const string LinuxPlatformName = "linux";
21+
public const string MacOSPlatformName = "osx";
22+
1923
public static readonly Guid DevBoxPartnerId = new("e3171dd9-9a5f-e5be-b36c-cc7c4f3f3bcf");
2024

2125
/// <summary>

src/shared/Core/Interop/Linux/LinuxFileSystem.cs

+2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System;
22
using System.IO;
3+
using System.Runtime.Versioning;
34
using GitCredentialManager.Interop.Posix;
45

56
namespace GitCredentialManager.Interop.Linux
67
{
8+
[SupportedOSPlatform(Constants.LinuxPlatformName)]
79
public class LinuxFileSystem : PosixFileSystem
810
{
911
public override bool IsSamePath(string a, string b)

src/shared/Core/Interop/Linux/LinuxSessionManager.cs

+2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
using System.Runtime.Versioning;
12
using GitCredentialManager.Interop.Posix;
23

34
namespace GitCredentialManager.Interop.Linux;
45

6+
[SupportedOSPlatform(Constants.LinuxPlatformName)]
57
public class LinuxSessionManager : PosixSessionManager
68
{
79
private bool? _isWebBrowserAvailable;

src/shared/Core/Interop/Linux/LinuxTerminal.cs

+2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
using System;
2+
using System.Runtime.Versioning;
23
using GitCredentialManager.Interop.Linux.Native;
34
using GitCredentialManager.Interop.Posix;
45
using GitCredentialManager.Interop.Posix.Native;
56

67
namespace GitCredentialManager.Interop.Linux
78
{
9+
[SupportedOSPlatform(Constants.LinuxPlatformName)]
810
public class LinuxTerminal : PosixTerminal
911
{
1012
public LinuxTerminal(ITrace trace, ITrace2 trace2)

src/shared/Core/Interop/MacOS/MacOSEnvironment.cs

+2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
using System.Collections.Generic;
33
using System.Diagnostics;
44
using System.IO;
5+
using System.Runtime.Versioning;
56
using System.Threading;
67
using GitCredentialManager.Interop.Posix;
78

89
namespace GitCredentialManager.Interop.MacOS
910
{
11+
[SupportedOSPlatform(Constants.MacOSPlatformName)]
1012
public class MacOSEnvironment : PosixEnvironment
1113
{
1214
private ICollection<string> _pathsToIgnore;

src/shared/Core/Interop/MacOS/MacOSFileSystem.cs

+2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System;
22
using System.IO;
3+
using System.Runtime.Versioning;
34
using GitCredentialManager.Interop.Posix;
45

56
namespace GitCredentialManager.Interop.MacOS
67
{
8+
[SupportedOSPlatform(Constants.MacOSPlatformName)]
79
public class MacOSFileSystem : PosixFileSystem
810
{
911
public override bool IsSamePath(string a, string b)

src/shared/Core/Interop/MacOS/MacOSSessionManager.cs

+2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
using System.Runtime.Versioning;
12
using GitCredentialManager.Interop.MacOS.Native;
23
using GitCredentialManager.Interop.Posix;
34

45
namespace GitCredentialManager.Interop.MacOS
56
{
7+
[SupportedOSPlatform(Constants.MacOSPlatformName)]
68
public class MacOSSessionManager : PosixSessionManager
79
{
810
public MacOSSessionManager(IEnvironment env, IFileSystem fs) : base(env, fs)

src/shared/Core/Interop/MacOS/MacOSTerminal.cs

+2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
using System;
2+
using System.Runtime.Versioning;
23
using GitCredentialManager.Interop.MacOS.Native;
34
using GitCredentialManager.Interop.Posix;
45
using GitCredentialManager.Interop.Posix.Native;
56

67
namespace GitCredentialManager.Interop.MacOS
78
{
9+
[SupportedOSPlatform(Constants.MacOSPlatformName)]
810
public class MacOSTerminal : PosixTerminal
911
{
1012
public MacOSTerminal(ITrace trace, ITrace2 trace2)

src/shared/Core/Interop/Posix/PosixEnvironment.cs

+3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Runtime.Versioning;
45

56
namespace GitCredentialManager.Interop.Posix
67
{
8+
[SupportedOSPlatform(Constants.LinuxPlatformName)]
9+
[SupportedOSPlatform(Constants.MacOSPlatformName)]
710
public class PosixEnvironment : EnvironmentBase
811
{
912
public PosixEnvironment(IFileSystem fileSystem)

src/shared/Core/Interop/Windows/WindowsEnvironment.cs

+2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
using System.Diagnostics;
44
using System.IO;
55
using System.Linq;
6+
using System.Runtime.Versioning;
67
using System.Text;
78

89
namespace GitCredentialManager.Interop.Windows
910
{
11+
[SupportedOSPlatform(Constants.WindowsPlatformName)]
1012
public class WindowsEnvironment : EnvironmentBase
1113
{
1214
public WindowsEnvironment(IFileSystem fileSystem)

src/shared/Core/Interop/Windows/WindowsFileSystem.cs

+2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System;
22
using System.IO;
3+
using System.Runtime.Versioning;
34

45
namespace GitCredentialManager.Interop.Windows
56
{
7+
[SupportedOSPlatform(Constants.WindowsPlatformName)]
68
public class WindowsFileSystem : FileSystem
79
{
810
public override bool IsSamePath(string a, string b)

src/shared/Core/Interop/Windows/WindowsProcessManager.cs

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
using System.Runtime.Versioning;
2+
13
namespace GitCredentialManager.Interop.Windows;
24

5+
[SupportedOSPlatform(Constants.WindowsPlatformName)]
36
public class WindowsProcessManager : ProcessManager
47
{
58
public WindowsProcessManager(ITrace2 trace2) : base(trace2)

src/shared/Core/Interop/Windows/WindowsSessionManager.cs

+2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System;
2+
using System.Runtime.Versioning;
23
using GitCredentialManager.Interop.Windows.Native;
34

45
namespace GitCredentialManager.Interop.Windows
56
{
7+
[SupportedOSPlatform(Constants.WindowsPlatformName)]
68
public class WindowsSessionManager : SessionManager
79
{
810
public WindowsSessionManager(IEnvironment env, IFileSystem fs) : base(env, fs)

src/shared/Core/Interop/Windows/WindowsSettings.cs

+3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11

2+
using System.Runtime.Versioning;
3+
24
namespace GitCredentialManager.Interop.Windows
35
{
46
/// <summary>
57
/// Reads settings from Git configuration, environment variables, and defaults from the Windows Registry.
68
/// </summary>
9+
[SupportedOSPlatform(Constants.WindowsPlatformName)]
710
public class WindowsSettings : Settings
811
{
912
private readonly ITrace _trace;

src/shared/Core/Interop/Windows/WindowsTerminal.cs

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Runtime.InteropServices;
3+
using System.Runtime.Versioning;
34
using System.Text;
45
using GitCredentialManager.Interop.Windows.Native;
56
using Microsoft.Win32.SafeHandles;
@@ -9,6 +10,7 @@ namespace GitCredentialManager.Interop.Windows
910
/// <summary>
1011
/// Represents a thin wrapper around the Windows console device.
1112
/// </summary>
13+
[SupportedOSPlatform(Constants.WindowsPlatformName)]
1214
public class WindowsTerminal : ITerminal
1315
{
1416
// ReadConsole 32768 fail, 32767 OK @linquize [https://github.com/Microsoft/Git-Credential-Manager-for-Windows/commit/a62b9a19f430d038dcd85a610d97e5f763980f85]

src/shared/Core/PlatformUtils.cs

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4+
using System.Diagnostics.CodeAnalysis;
45
using System.IO;
56
using System.Linq;
67
using System.Runtime.InteropServices;
8+
using System.Runtime.Versioning;
79
using GitCredentialManager.Interop.Posix.Native;
810

911
namespace GitCredentialManager
@@ -24,6 +26,7 @@ public static PlatformInformation GetPlatformInformation(ITrace2 trace2)
2426
return new PlatformInformation(osType, osVersion, cpuArch, clrVersion);
2527
}
2628

29+
[SupportedOSPlatformGuard(Constants.WindowsPlatformName)]
2730
public static bool IsDevBox()
2831
{
2932
if (!IsWindows())
@@ -49,6 +52,7 @@ public static bool IsDevBox()
4952
}
5053
}
5154

55+
[SupportedOSPlatformGuard(Constants.WindowsPlatformName)]
5256
public static bool IsWindowsBrokerSupported()
5357
{
5458
if (!IsWindows())
@@ -93,33 +97,38 @@ public static bool IsWindowsBrokerSupported()
9397
/// Check if the current Operating System is macOS.
9498
/// </summary>
9599
/// <returns>True if running on macOS, false otherwise.</returns>
100+
[SupportedOSPlatformGuard(Constants.MacOSPlatformName)]
96101
public static bool IsMacOS()
97102
{
98-
return RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
103+
return OperatingSystem.IsMacOS();
99104
}
100105

101106
/// <summary>
102107
/// Check if the current Operating System is Windows.
103108
/// </summary>
104109
/// <returns>True if running on Windows, false otherwise.</returns>
110+
[SupportedOSPlatformGuard(Constants.WindowsPlatformName)]
105111
public static bool IsWindows()
106112
{
107-
return RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
113+
return OperatingSystem.IsWindows();
108114
}
109115

110116
/// <summary>
111117
/// Check if the current Operating System is Linux-based.
112118
/// </summary>
113119
/// <returns>True if running on a Linux distribution, false otherwise.</returns>
120+
[SupportedOSPlatformGuard(Constants.LinuxPlatformName)]
114121
public static bool IsLinux()
115122
{
116-
return RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
123+
return OperatingSystem.IsLinux();
117124
}
118125

119126
/// <summary>
120127
/// Check if the current Operating System is POSIX-compliant.
121128
/// </summary>
122129
/// <returns>True if running on a POSIX-compliant Operating System, false otherwise.</returns>
130+
[SupportedOSPlatformGuard(Constants.LinuxPlatformName)]
131+
[SupportedOSPlatformGuard(Constants.MacOSPlatformName)]
123132
public static bool IsPosix()
124133
{
125134
return IsMacOS() || IsLinux();
@@ -129,6 +138,7 @@ public static bool IsPosix()
129138
/// Ensure the current Operating System is macOS, fail otherwise.
130139
/// </summary>
131140
/// <exception cref="PlatformNotSupportedException">Thrown if the current OS is not macOS.</exception>
141+
[SupportedOSPlatformGuard(Constants.MacOSPlatformName)]
132142
public static void EnsureMacOS()
133143
{
134144
if (!IsMacOS())
@@ -141,6 +151,7 @@ public static void EnsureMacOS()
141151
/// Ensure the current Operating System is Windows, fail otherwise.
142152
/// </summary>
143153
/// <exception cref="PlatformNotSupportedException">Thrown if the current OS is not Windows.</exception>
154+
[SupportedOSPlatformGuard(Constants.WindowsPlatformName)]
144155
public static void EnsureWindows()
145156
{
146157
if (!IsWindows())
@@ -153,6 +164,7 @@ public static void EnsureWindows()
153164
/// Ensure the current Operating System is Linux-based, fail otherwise.
154165
/// </summary>
155166
/// <exception cref="PlatformNotSupportedException">Thrown if the current OS is not Linux-based.</exception>
167+
[SupportedOSPlatformGuard(Constants.LinuxPlatformName)]
156168
public static void EnsureLinux()
157169
{
158170
if (!IsLinux())
@@ -165,6 +177,8 @@ public static void EnsureLinux()
165177
/// Ensure the current Operating System is POSIX-compliant, fail otherwise.
166178
/// </summary>
167179
/// <exception cref="PlatformNotSupportedException">Thrown if the current OS is not POSIX-compliant.</exception>
180+
[SupportedOSPlatformGuard(Constants.LinuxPlatformName)]
181+
[SupportedOSPlatformGuard(Constants.MacOSPlatformName)]
168182
public static void EnsurePosix()
169183
{
170184
if (!IsPosix())

0 commit comments

Comments
 (0)