forked from libgit2/libgit2sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlatform.cs
More file actions
159 lines (136 loc) · 4.74 KB
/
Platform.cs
File metadata and controls
159 lines (136 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using System;
using System.Runtime.InteropServices;
namespace LibGit2Sharp.Core
{
internal enum OperatingSystemType
{
Windows,
Unix,
MacOSX
}
internal static class Platform
{
public static string ProcessorArchitecture => IntPtr.Size == 8 ? "x64" : "x86";
#if NETFRAMEWORK
private static bool? _isRunningOnMac;
private static bool IsRunningOnMac() => _isRunningOnMac ?? (_isRunningOnMac = TryGetIsRunningOnMac()) ?? false;
#endif
public static OperatingSystemType OperatingSystem
{
get
{
#if NETFRAMEWORK
var platform = (int)Environment.OSVersion.Platform;
if (platform <= 3 || platform == 5)
{
return OperatingSystemType.Windows;
}
if (IsRunningOnMac())
{
return OperatingSystemType.MacOSX;
}
if (platform == 4 || platform == 6 || platform == 128)
{
return OperatingSystemType.Unix;
}
#else
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return OperatingSystemType.Windows;
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
return OperatingSystemType.Unix;
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
return OperatingSystemType.MacOSX;
}
#endif
throw new PlatformNotSupportedException();
}
}
public static string GetNativeLibraryExtension()
{
switch (OperatingSystem)
{
case OperatingSystemType.MacOSX:
return ".dylib";
case OperatingSystemType.Unix:
return ".so";
case OperatingSystemType.Windows:
return ".dll";
}
throw new PlatformNotSupportedException();
}
/// <summary>
/// Returns true if the runtime is Mono.
/// </summary>
public static bool IsRunningOnMono()
=> Type.GetType("Mono.Runtime") != null;
/// <summary>
/// Returns true if the runtime is .NET Framework.
/// </summary>
public static bool IsRunningOnNetFramework()
=> typeof(object).Assembly.GetName().Name == "mscorlib" && !IsRunningOnMono();
/// <summary>
/// Returns true if the runtime is .NET Core.
/// </summary>
public static bool IsRunningOnNetCore()
=> typeof(object).Assembly.GetName().Name != "mscorlib";
#if NETFRAMEWORK
#pragma warning disable IDE1006 // Naming Styles
[DllImport("libc")]
private static extern int sysctlbyname(
[MarshalAs(UnmanagedType.LPStr)] string property,
IntPtr output,
IntPtr oldLen,
IntPtr newp,
uint newlen);
#pragma warning restore IDE1006 // Naming Styles
private static bool TryGetIsRunningOnMac()
{
const string OsType = "kern.ostype";
const string MacOsType = "Darwin";
return MacOsType == GetOsType();
string GetOsType()
{
try
{
IntPtr
pointerLength = IntPtr.Zero,
pointerString = IntPtr.Zero;
try
{
pointerLength = Marshal.AllocHGlobal(sizeof(int));
sysctlbyname(OsType, IntPtr.Zero, pointerLength, IntPtr.Zero, 0);
var length = Marshal.ReadInt32(pointerLength);
if (length <= 0)
{
return string.Empty;
}
pointerString = Marshal.AllocHGlobal(length);
sysctlbyname(OsType, pointerString, pointerLength, IntPtr.Zero, 0);
return Marshal.PtrToStringAnsi(pointerString);
}
finally
{
if (pointerLength != IntPtr.Zero)
{
Marshal.FreeHGlobal(pointerLength);
}
if (pointerString != IntPtr.Zero)
{
Marshal.FreeHGlobal(pointerString);
}
}
}
catch
{
return null;
}
}
}
#endif
}
}