diff --git a/LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj b/LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj index 619be5603..68ea2cfd0 100644 --- a/LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj +++ b/LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj @@ -28,7 +28,6 @@ <_TestAppFile Include="@(TestAppExe->'%(RootDir)%(Directory)%(Filename).exe')" /> - <_TestAppFile Include="@(TestAppExe->'%(RootDir)%(Directory)%(Filename).exe.config')" /> <_TestAppFile Include="@(TestAppExe->'%(RootDir)%(Directory)%(Filename).pdb')" /> diff --git a/LibGit2Sharp/Core/NativeMethods.cs b/LibGit2Sharp/Core/NativeMethods.cs index b5aa8097e..fd0594384 100644 --- a/LibGit2Sharp/Core/NativeMethods.cs +++ b/LibGit2Sharp/Core/NativeMethods.cs @@ -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); } diff --git a/LibGit2Sharp/Core/Platform.cs b/LibGit2Sharp/Core/Platform.cs index e8d536475..52859cbe2 100644 --- a/LibGit2Sharp/Core/Platform.cs +++ b/LibGit2Sharp/Core/Platform.cs @@ -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; @@ -32,7 +51,7 @@ public static OperatingSystemType OperatingSystem { return OperatingSystemType.MacOSX; } - +#endif throw new PlatformNotSupportedException(); } } @@ -71,5 +90,70 @@ public static bool IsRunningOnNetFramework() /// 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 } } diff --git a/LibGit2Sharp/LibGit2Sharp.csproj b/LibGit2Sharp/LibGit2Sharp.csproj index 0d212b812..e46b9df9f 100644 --- a/LibGit2Sharp/LibGit2Sharp.csproj +++ b/LibGit2Sharp/LibGit2Sharp.csproj @@ -1,7 +1,7 @@  - netstandard2.0 + netstandard2.0;net461 true LibGit2Sharp brings all the might and speed of libgit2, a native Git implementation, to the managed world of .Net and Mono. LibGit2Sharp contributors @@ -29,7 +29,7 @@ - +