Skip to content

Commit 9fce228

Browse files
committed
Added .NET 4.6.1 target
* adds Pollyfil for System.Runtime.InteropServices.RuntimeInformation * fixes #1605
1 parent 5139749 commit 9fce228

File tree

4 files changed

+92
-4
lines changed

4 files changed

+92
-4
lines changed

LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj

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

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 NETSTANDARD
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: 86 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 !NETSTANDARD
17+
private static bool? _isRunningOnMac;
18+
private static bool IsRunningOnMac() => _isRunningOnMac ?? (_isRunningOnMac = TryGetIsRunningOnMac()) ?? false;
19+
#endif
1620

1721
public static OperatingSystemType OperatingSystem
1822
{
1923
get
2024
{
25+
#if NETSTANDARD
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,71 @@ public static bool IsRunningOnNetFramework()
7190
/// </summary>
7291
public static bool IsRunningOnNetCore()
7392
=> typeof(object).Assembly.GetName().Name != "mscorlib";
93+
94+
#if !NETSTANDARD
95+
#pragma warning disable IDE1006 // Naming Styles
96+
[DllImport("libc")]
97+
private static extern int sysctlbyname(
98+
[MarshalAs(UnmanagedType.LPStr)] string property,
99+
IntPtr output,
100+
IntPtr oldLen,
101+
IntPtr newp,
102+
uint newlen);
103+
#pragma warning restore IDE1006 // Naming Styles
104+
105+
private static bool TryGetIsRunningOnMac()
106+
{
107+
const string OsType = "kern.ostype";
108+
const string MacOsType = "Darwin";
109+
110+
var osType = GetOsType();
111+
return MacOsType == osType;
112+
113+
string GetOsType()
114+
{
115+
try
116+
{
117+
IntPtr
118+
pointerLength = IntPtr.Zero,
119+
pointerString = IntPtr.Zero;
120+
121+
try
122+
{
123+
pointerLength = Marshal.AllocHGlobal(sizeof(int));
124+
125+
sysctlbyname(OsType, IntPtr.Zero, pointerLength, IntPtr.Zero, 0);
126+
127+
var length = Marshal.ReadInt32(pointerLength);
128+
129+
if (length <= 0)
130+
{
131+
return string.Empty;
132+
}
133+
134+
pointerString = Marshal.AllocHGlobal(length);
135+
136+
sysctlbyname(OsType, pointerString, pointerLength, IntPtr.Zero, 0);
137+
138+
return Marshal.PtrToStringAnsi(pointerString);
139+
}
140+
finally
141+
{
142+
if (pointerLength != IntPtr.Zero)
143+
{
144+
Marshal.FreeHGlobal(pointerLength);
145+
}
146+
if (pointerString != IntPtr.Zero)
147+
{
148+
Marshal.FreeHGlobal(pointerString);
149+
}
150+
}
151+
}
152+
catch
153+
{
154+
return null;
155+
}
156+
}
157+
}
158+
#endif
74159
}
75160
}

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 2 additions & 2 deletions
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>
@@ -29,7 +29,7 @@
2929
</ItemGroup>
3030

3131
<ItemGroup>
32-
<PackageReference Include="LibGit2Sharp.NativeBinaries" Version="[1.0.226]" PrivateAssets="none" />
32+
<PackageReference Include="LibGit2Sharp.NativeBinaries" Version="[1.0.233]" PrivateAssets="none" />
3333
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-beta-63127-02" PrivateAssets="all" />
3434
<PackageReference Include="Nerdbank.GitVersioning" Version="2.2.13" PrivateAssets="all" />
3535
</ItemGroup>

0 commit comments

Comments
 (0)