|
6 | 6 | using System.Diagnostics; |
7 | 7 | using System.IO; |
8 | 8 | using System.Linq; |
| 9 | + using System.Runtime.InteropServices; |
9 | 10 | using System.Text; |
10 | 11 | using System.Text.RegularExpressions; |
11 | 12 | using Microsoft.Build.Framework; |
|
14 | 15 |
|
15 | 16 | public class GetBuildVersion : Task |
16 | 17 | { |
| 18 | +#if !NET45 |
| 19 | + /// <summary> |
| 20 | + /// An AppDomain-wide variable used on |
| 21 | + /// </summary> |
| 22 | + private static bool libgit2PathInitialized; |
| 23 | +#endif |
| 24 | + |
17 | 25 | /// <summary> |
18 | 26 | /// Initializes a new instance of the <see cref="GetBuildVersion"/> class. |
19 | 27 | /// </summary> |
@@ -43,6 +51,15 @@ public GetBuildVersion() |
43 | 51 | /// </summary> |
44 | 52 | public string GitRepoRoot { get; set; } |
45 | 53 |
|
| 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 | + |
46 | 63 | /// <summary> |
47 | 64 | /// Gets or sets a value indicating whether the project is building |
48 | 65 | /// in PublicRelease mode. |
@@ -146,6 +163,11 @@ public override bool Execute() |
146 | 163 | { |
147 | 164 | try |
148 | 165 | { |
| 166 | + if (!this.HelpFindLibGit2NativeBinaries()) |
| 167 | + { |
| 168 | + return false; |
| 169 | + } |
| 170 | + |
149 | 171 | var cloudBuild = CloudBuild.Active; |
150 | 172 | var oracle = VersionOracle.Create(Directory.GetCurrentDirectory(), this.GitRepoRoot, cloudBuild); |
151 | 173 | if (!string.IsNullOrEmpty(this.DefaultPublicRelease)) |
@@ -190,5 +212,25 @@ public override bool Execute() |
190 | 212 |
|
191 | 213 | return true; |
192 | 214 | } |
| 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 | + } |
193 | 235 | } |
194 | 236 | } |
0 commit comments