Skip to content

Commit

Permalink
Merge pull request #1606 from devlead/feature/net461nodep
Browse files Browse the repository at this point in the history
Provide .NET 4.6.1 framework target
  • Loading branch information
ethomson authored Oct 4, 2018
2 parents 5139749 + d6f6322 commit 57b6079
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 4 deletions.
1 change: 0 additions & 1 deletion LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
<Target Name="CopyTestAppExes" AfterTargets="ResolveProjectReferences">
<ItemGroup>
<_TestAppFile Include="@(TestAppExe->'%(RootDir)%(Directory)%(Filename).exe')" />
<_TestAppFile Include="@(TestAppExe->'%(RootDir)%(Directory)%(Filename).exe.config')" />
<_TestAppFile Include="@(TestAppExe->'%(RootDir)%(Directory)%(Filename).pdb')" />
</ItemGroup>

Expand Down
4 changes: 4 additions & 0 deletions LibGit2Sharp/Core/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ static NativeMethods()
// Try to load the .dll from the path explicitly.
// If this call succeeds further DllImports will find the library loaded and not attempt to load it again.
// If it fails the next DllImport will load the library from safe directories.
#if NETFRAMEWORK
if (Platform.OperatingSystem == OperatingSystemType.Windows)
#else
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
#endif
{
LoadWindowsLibrary(nativeLibraryPath);
}
Expand Down
86 changes: 85 additions & 1 deletion LibGit2Sharp/Core/Platform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,30 @@ internal enum OperatingSystemType
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;
Expand All @@ -32,7 +51,7 @@ public static OperatingSystemType OperatingSystem
{
return OperatingSystemType.MacOSX;
}

#endif
throw new PlatformNotSupportedException();
}
}
Expand Down Expand Up @@ -71,5 +90,70 @@ public static bool IsRunningOnNetFramework()
/// </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
}
}
4 changes: 2 additions & 2 deletions LibGit2Sharp/LibGit2Sharp.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Description>LibGit2Sharp brings all the might and speed of libgit2, a native Git implementation, to the managed world of .Net and Mono.</Description>
<Company>LibGit2Sharp contributors</Company>
Expand Down Expand Up @@ -29,7 +29,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="LibGit2Sharp.NativeBinaries" Version="[1.0.226]" PrivateAssets="none" />
<PackageReference Include="LibGit2Sharp.NativeBinaries" Version="[1.0.233]" PrivateAssets="none" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-beta-63127-02" PrivateAssets="all" />
<PackageReference Include="Nerdbank.GitVersioning" Version="2.2.13" PrivateAssets="all" />
</ItemGroup>
Expand Down

0 comments on commit 57b6079

Please sign in to comment.