Skip to content

Commit 9952a79

Browse files
committed
Fix ps1 scripts to find native libgit2 binaries
1 parent e7d29b3 commit 9952a79

File tree

5 files changed

+67
-30
lines changed

5 files changed

+67
-30
lines changed

src/NerdBank.GitVersioning/GitExtensions.cs

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
using System.Collections.Generic;
55
using System.IO;
66
using System.Linq;
7+
using System.Runtime.InteropServices;
78
using System.Text;
8-
using System.Threading.Tasks;
99
using LibGit2Sharp;
1010
using Validation;
1111
using Version = System.Version;
@@ -15,6 +15,11 @@
1515
/// </summary>
1616
public static class GitExtensions
1717
{
18+
/// <summary>
19+
/// An AppDomain-wide variable used on
20+
/// </summary>
21+
private static bool libgit2PathInitialized;
22+
1823
/// <summary>
1924
/// The 0.0 version.
2025
/// </summary>
@@ -272,6 +277,61 @@ public static IEnumerable<Commit> GetCommitsFromVersion(this Repository repo, Ve
272277
return possibleCommits;
273278
}
274279

280+
/// <summary>
281+
/// Assists the operating system in finding the appropriate native libgit2 module.
282+
/// </summary>
283+
/// <param name="basePath">The path to the directory that contains the lib folder.</param>
284+
/// <exception cref="ArgumentException">Thrown if the provided path does not lead to an existing directory.</exception>
285+
public static void HelpFindLibGit2NativeBinaries(string basePath)
286+
{
287+
if (!TryHelpFindLibGit2NativeBinaries(basePath, out string attemptedDirectory))
288+
{
289+
throw new ArgumentException($"Unable to find native binaries under directory: \"{attemptedDirectory}\".");
290+
}
291+
}
292+
293+
/// <summary>
294+
/// Assists the operating system in finding the appropriate native libgit2 module.
295+
/// </summary>
296+
/// <param name="basePath">The path to the directory that contains the lib folder.</param>
297+
/// <returns><c>true</c> if the libgit2 native binaries have been found; <c>false</c> otherwise.</returns>
298+
public static bool TryHelpFindLibGit2NativeBinaries(string basePath)
299+
{
300+
return TryHelpFindLibGit2NativeBinaries(basePath, out string attemptedDirectory);
301+
}
302+
303+
/// <summary>
304+
/// Assists the operating system in finding the appropriate native libgit2 module.
305+
/// </summary>
306+
/// <param name="basePath">The path to the directory that contains the lib folder.</param>
307+
/// <param name="attemptedDirectory">Receives the directory that native binaries are expected.</param>
308+
/// <returns><c>true</c> if the libgit2 native binaries have been found; <c>false</c> otherwise.</returns>
309+
public static bool TryHelpFindLibGit2NativeBinaries(string basePath, out string attemptedDirectory)
310+
{
311+
attemptedDirectory = null;
312+
if (!libgit2PathInitialized)
313+
{
314+
#if !NET45
315+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
316+
#endif
317+
{
318+
attemptedDirectory = Path.Combine(basePath, "lib", "win32", IntPtr.Size == 4 ? "x86" : "x64");
319+
if (Directory.Exists(attemptedDirectory))
320+
{
321+
Environment.SetEnvironmentVariable("PATH", Environment.GetEnvironmentVariable("PATH") + Path.PathSeparator + attemptedDirectory);
322+
}
323+
else
324+
{
325+
return false;
326+
}
327+
}
328+
329+
libgit2PathInitialized = true;
330+
}
331+
332+
return libgit2PathInitialized;
333+
}
334+
275335
/// <summary>
276336
/// Tests whether a commit is of a specified version, comparing major and minor components
277337
/// with the version.txt file defined by that commit.
@@ -438,7 +498,7 @@ private static Version GetIdAsVersionHelper(Commit commit, VersionOptions versio
438498
: 0;
439499
}
440500

441-
int build = versionHeight.Value == 0 ? 0 : versionHeight.Value + (versionOptions?.BuildNumberOffset ?? 0);
501+
int build = versionHeight.Value == 0 ? 0 : versionHeight.Value + (versionOptions?.BuildNumberOffset ?? 0);
442502
Verify.Operation(build <= MaximumBuildNumberOrRevisionComponent, "Git height is {0}, which is greater than the maximum allowed {0}.", build, MaximumBuildNumberOrRevisionComponent);
443503
int revision = commit != null
444504
? Math.Min(MaximumBuildNumberOrRevisionComponent, commit.GetTruncatedCommitIdAsUInt16())

src/Nerdbank.GitVersioning.NuGet/tools/Get-CommitId.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ $null = [Reflection.Assembly]::LoadFile((Resolve-Path "$DependencyBasePath\Valid
2727
$null = [Reflection.Assembly]::LoadFile((Resolve-Path "$DependencyBasePath\NerdBank.GitVersioning.dll"))
2828
$null = [Reflection.Assembly]::LoadFile((Resolve-Path "$DependencyBasePath\LibGit2Sharp.dll"))
2929
$null = [Reflection.Assembly]::LoadFile((Resolve-Path "$DependencyBasePath\Newtonsoft.Json.dll"))
30+
[Nerdbank.GitVersioning.GitExtensions]::HelpFindLibGit2NativeBinaries("$DependencyBasePath\..")
3031

3132
$ProjectDirectory = (Resolve-Path $ProjectDirectory).ProviderPath
3233
$GitPath = $ProjectDirectory

src/Nerdbank.GitVersioning.NuGet/tools/Get-Version.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ $null = [Reflection.Assembly]::LoadFile((Resolve-Path "$DependencyBasePath\Newto
1919
$ProjectDirectory = (Resolve-Path $ProjectDirectory).ProviderPath
2020

2121
try {
22+
[Nerdbank.GitVersioning.GitExtensions]::HelpFindLibGit2NativeBinaries("$DependencyBasePath\..")
2223
$CloudBuild = [Nerdbank.GitVersioning.CloudBuild]::Active
2324
$VersionOracle = [Nerdbank.GitVersioning.VersionOracle]::Create($ProjectDirectory, $null, $CloudBuild)
2425
$VersionOracle

src/Nerdbank.GitVersioning.Tasks/GetBuildVersion.cs

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@
1515

1616
public class GetBuildVersion : Task
1717
{
18-
/// <summary>
19-
/// An AppDomain-wide variable used on
20-
/// </summary>
21-
private static bool libgit2PathInitialized;
22-
2318
/// <summary>
2419
/// Initializes a new instance of the <see cref="GetBuildVersion"/> class.
2520
/// </summary>
@@ -162,7 +157,7 @@ public override bool Execute()
162157
{
163158
try
164159
{
165-
this.HelpFindLibGit2NativeBinaries();
160+
GitExtensions.TryHelpFindLibGit2NativeBinaries(this.TargetsPath);
166161

167162
var cloudBuild = CloudBuild.Active;
168163
var oracle = VersionOracle.Create(Directory.GetCurrentDirectory(), this.GitRepoRoot, cloudBuild);
@@ -208,26 +203,5 @@ public override bool Execute()
208203

209204
return true;
210205
}
211-
212-
private void HelpFindLibGit2NativeBinaries()
213-
{
214-
if (!libgit2PathInitialized)
215-
{
216-
string nativeDllPath = null;
217-
#if !NET45
218-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
219-
#endif
220-
{
221-
nativeDllPath = Path.Combine(this.TargetsPath, "lib", "win32", IntPtr.Size == 4 ? "x86" : "x64");
222-
}
223-
224-
if (nativeDllPath != null)
225-
{
226-
Environment.SetEnvironmentVariable("PATH", Environment.GetEnvironmentVariable("PATH") + Path.PathSeparator + nativeDllPath);
227-
}
228-
229-
libgit2PathInitialized = true;
230-
}
231-
}
232206
}
233207
}

src/Nerdbank.GitVersioning.sln

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
4-
VisualStudioVersion = 15.0.26210.2
4+
VisualStudioVersion = 15.0.26215.1
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FF286327-C783-4F7A-AB73-9BCBAD0D4460}") = "Nerdbank.GitVersioning.NuGet", "Nerdbank.GitVersioning.NuGet\Nerdbank.GitVersioning.NuGet.nuproj", "{B7DE4122-9E90-43DE-BE44-454315CFAE99}"
77
EndProject
88
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{4BD1A7CD-6F52-4F5A-825B-50E4D8C3ECFF}"
99
ProjectSection(SolutionItems) = preProject
1010
..\.gitignore = ..\.gitignore
1111
..\appveyor.yml = ..\appveyor.yml
12+
..\build.ps1 = ..\build.ps1
1213
..\init.ps1 = ..\init.ps1
1314
..\nuget.config = ..\nuget.config
1415
..\readme.md = ..\readme.md

0 commit comments

Comments
 (0)