Skip to content

Commit 450a223

Browse files
committed
Added .NET 4.6.1 target
* adds Pollyfil for System.Runtime.InteropServices.RuntimeInformation * fixes #1605
1 parent f8e2d42 commit 450a223

File tree

5 files changed

+69
-3
lines changed

5 files changed

+69
-3
lines changed

Directory.Build.props

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@
77
<DefineConstants Condition=" '$(ExtraDefine)' != '' ">$(DefineConstants);$(ExtraDefine)</DefineConstants>
88
</PropertyGroup>
99

10+
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
11+
<DefineConstants>$(DefineConstants);NETCORE</DefineConstants>
12+
</PropertyGroup>
13+
1014
</Project>

LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
<Target Name="CopyTestAppExes" AfterTargets="ResolveProjectReferences">
3030
<ItemGroup>
3131
<_TestAppFile Include="@(TestAppExe->'%(RootDir)%(Directory)%(Filename).exe')" />
32-
<_TestAppFile Include="@(TestAppExe->'%(RootDir)%(Directory)%(Filename).exe.config')" />
3332
<_TestAppFile Include="@(TestAppExe->'%(RootDir)%(Directory)%(Filename).pdb')" />
3433
</ItemGroup>
3534

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ static NativeMethods()
3737
// Try to load the .dll from the path explicitly.
3838
// If this call succeeds further DllImports will find the library loaded and not attempt to load it again.
3939
// If it fails the next DllImport will load the library from safe directories.
40+
#if NETCORE
4041
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
42+
#else
43+
if (Platform.OperatingSystem == OperatingSystemType.Windows)
44+
#endif
4145
{
4246
LoadWindowsLibrary(nativeLibraryPath);
4347
}

LibGit2Sharp/Core/Platform.cs

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@ internal enum OperatingSystemType
1313
internal static class Platform
1414
{
1515
public static string ProcessorArchitecture => IntPtr.Size == 8 ? "x64" : "x86";
16+
#if !NETCORE
17+
private static bool? _isRunningOnMac;
18+
public static bool IsRunningOnMac() => _isRunningOnMac ?? (_isRunningOnMac = TryGetIsRunningOnMac()) ?? false;
19+
#endif
1620

1721
public static OperatingSystemType OperatingSystem
1822
{
1923
get
2024
{
25+
#if NETCORE
2126
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
2227
{
2328
return OperatingSystemType.Windows;
@@ -32,7 +37,21 @@ public static OperatingSystemType OperatingSystem
3237
{
3338
return OperatingSystemType.MacOSX;
3439
}
35-
40+
#else
41+
var platform = (int)Environment.OSVersion.Platform;
42+
if (platform <= 3 || platform == 5)
43+
{
44+
return OperatingSystemType.Windows;
45+
}
46+
if (IsRunningOnMac())
47+
{
48+
return OperatingSystemType.MacOSX;
49+
}
50+
if (platform == 4 || platform == 6 || platform == 128)
51+
{
52+
return OperatingSystemType.Unix;
53+
}
54+
#endif
3655
throw new PlatformNotSupportedException();
3756
}
3857
}
@@ -71,5 +90,45 @@ public static bool IsRunningOnNetFramework()
7190
/// </summary>
7291
public static bool IsRunningOnNetCore()
7392
=> typeof(object).Assembly.GetName().Name != "mscorlib";
93+
94+
#if !NETCORE
95+
#pragma warning disable IDE1006 // Naming Styles
96+
[DllImport("libc")]
97+
internal static extern int uname(IntPtr buf);
98+
#pragma warning restore IDE1006 // Naming Styles
99+
100+
private static bool TryGetIsRunningOnMac()
101+
{
102+
try
103+
{
104+
IntPtr buf = IntPtr.Zero;
105+
try
106+
{
107+
buf = Marshal.AllocHGlobal(8192);
108+
if (uname(buf) == 0)
109+
{
110+
string os = Marshal.PtrToStringAnsi(buf);
111+
if (os == "Darwin")
112+
{
113+
return true;
114+
}
115+
}
116+
}
117+
finally
118+
{
119+
if (buf != IntPtr.Zero)
120+
{
121+
Marshal.FreeHGlobal(buf);
122+
}
123+
}
124+
}
125+
catch
126+
{
127+
// Ignore any other possible failures on non-OSX platforms
128+
}
129+
130+
return false;
131+
}
132+
#endif
74133
}
75134
}

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard2.0</TargetFramework>
4+
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
55
<GenerateDocumentationFile>true</GenerateDocumentationFile>
66
<Description>LibGit2Sharp brings all the might and speed of libgit2, a native Git implementation, to the managed world of .Net and Mono.</Description>
77
<Company>LibGit2Sharp contributors</Company>

0 commit comments

Comments
 (0)