Skip to content

Commit f2ac21d

Browse files
committed
Fix loading native libgit2 binary
1 parent ea323c7 commit f2ac21d

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/Nerdbank.GitVersioning.NuGet/build/Nerdbank.GitVersioning.targets

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
<GenerateAssemblyInformationalVersionAttribute>false</GenerateAssemblyInformationalVersionAttribute>
3232
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
3333
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
34+
35+
<NerdbankGitVersioningTasksPath Condition=" '$(NerdbankGitVersioningTasksPath)' == '' ">$(MSBuildThisFileDirectory)</NerdbankGitVersioningTasksPath>
3436
</PropertyGroup>
3537

3638
<UsingTask AssemblyFile="$(NerdbankGitVersioningTasksPath)Nerdbank.GitVersioning.Tasks.dll" TaskName="Nerdbank.GitVersioning.Tasks.AssemblyVersionInfo"/>
@@ -55,7 +57,8 @@
5557
BuildingRef="$(_NBGV_BuildingRef)"
5658
BuildMetadata="@(BuildMetadata)"
5759
DefaultPublicRelease="$(PublicRelease)"
58-
GitRepoRoot="$(GitRepoRoot)">
60+
GitRepoRoot="$(GitRepoRoot)"
61+
TaskAssemblyPath="$(NerdbankGitVersioningTasksPath)">
5962

6063
<Output TaskParameter="Version" PropertyName="BuildVersion" />
6164
<Output TaskParameter="AssemblyInformationalVersion" PropertyName="AssemblyInformationalVersion" />

src/Nerdbank.GitVersioning.Tasks/GetBuildVersion.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Diagnostics;
77
using System.IO;
88
using System.Linq;
9+
using System.Runtime.InteropServices;
910
using System.Text;
1011
using System.Text.RegularExpressions;
1112
using Microsoft.Build.Framework;
@@ -14,6 +15,13 @@
1415

1516
public class GetBuildVersion : Task
1617
{
18+
#if !NET45
19+
/// <summary>
20+
/// An AppDomain-wide variable used on
21+
/// </summary>
22+
private static bool libgit2PathInitialized;
23+
#endif
24+
1725
/// <summary>
1826
/// Initializes a new instance of the <see cref="GetBuildVersion"/> class.
1927
/// </summary>
@@ -43,6 +51,15 @@ public GetBuildVersion()
4351
/// </summary>
4452
public string GitRepoRoot { get; set; }
4553

54+
/// <summary>
55+
/// Gets or sets the path to the folder that contains this task's assembly.
56+
/// </summary>
57+
/// <remarks>
58+
/// This is particularly useful in .NET Core where discovering one's own assembly path
59+
/// is not allowed before .NETStandard 2.0.
60+
/// </remarks>
61+
public string TaskAssemblyPath { get; set; }
62+
4663
/// <summary>
4764
/// Gets or sets a value indicating whether the project is building
4865
/// in PublicRelease mode.
@@ -146,6 +163,11 @@ public override bool Execute()
146163
{
147164
try
148165
{
166+
if (!this.HelpFindLibGit2NativeBinaries())
167+
{
168+
return false;
169+
}
170+
149171
var cloudBuild = CloudBuild.Active;
150172
var oracle = VersionOracle.Create(Directory.GetCurrentDirectory(), this.GitRepoRoot, cloudBuild);
151173
if (!string.IsNullOrEmpty(this.DefaultPublicRelease))
@@ -190,5 +212,25 @@ public override bool Execute()
190212

191213
return true;
192214
}
215+
216+
private bool HelpFindLibGit2NativeBinaries()
217+
{
218+
#if !NET45
219+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !libgit2PathInitialized)
220+
{
221+
if (string.IsNullOrWhiteSpace(this.TaskAssemblyPath))
222+
{
223+
this.Log.LogError("The TaskAssemblyPath parameter is required on .NET Core running on Windows.");
224+
return false;
225+
}
226+
227+
string nativeDllPath = Path.Combine(this.TaskAssemblyPath, "lib", "win32", IntPtr.Size == 4 ? "x86" : "x64");
228+
Environment.SetEnvironmentVariable("PATH", Environment.GetEnvironmentVariable("PATH") + Path.PathSeparator + nativeDllPath);
229+
libgit2PathInitialized = true;
230+
}
231+
#endif
232+
233+
return true;
234+
}
193235
}
194236
}

0 commit comments

Comments
 (0)