Skip to content

Commit cbe3622

Browse files
committed
Bundle *all* RIDs supported by libgit2
The added source code added here is liberally copied from https://github.com/dotnet/sourcelink/tree/c092238370e0437eb95722f28c79273244dc7f1a/src/Microsoft.Build.Tasks.Git
1 parent afb38ac commit cbe3622

File tree

12 files changed

+622
-136
lines changed

12 files changed

+622
-136
lines changed

azure-pipelines.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ resources:
1515
- container: xenial
1616
image: andrewarnott/linux-buildagent
1717
- container: bionic
18-
image: andrewarnott/dotnet:2.1-sdk-bionic
18+
image: microsoft/dotnet:2.1-sdk-bionic
1919

2020
jobs:
2121
- job: Windows

doc/prereqs.md

Lines changed: 0 additions & 21 deletions
This file was deleted.

readme.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ You can install Nerdbank.GitVersioning into your projects via NuGet or NPM.
3232

3333
You must also create [a version.json file](doc/versionJson.md) in your repo. See [migration notes](doc/migrating.md) if your repo already has a version.txt or version.json file from using another system.
3434

35-
[Prereqs may apply for certain linux distros](doc/prereqs.md).
36-
3735
## How to leverage version stamping and runtime information
3836

3937
See relevant documentation for any of these topics:

src/MSBuildExtensionTask/MSBuildExtensionTask.csproj

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/NerdBank.GitVersioning.Tests/NerdBank.GitVersioning.Tests.csproj

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
<EmbeddedResource Include="repos\submodules.7z" />
2222
</ItemGroup>
2323
<ItemGroup>
24-
<ProjectReference Include="..\MSBuildExtensionTask\MSBuildExtensionTask.csproj" />
2524
<ProjectReference Include="..\Nerdbank.GitVersioning.Tasks\Nerdbank.GitVersioning.Tasks.csproj" />
2625
<ProjectReference Include="..\NerdBank.GitVersioning\NerdBank.GitVersioning.csproj" />
2726
</ItemGroup>
@@ -37,7 +36,11 @@
3736
<PackageReference Include="Nerdbank.GitVersioning.LKG" Version="1.6.20-beta-gfea83a8c9e" />
3837
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
3938
</ItemGroup>
40-
<ItemGroup>
41-
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
39+
<ItemGroup Condition="'$(TargetFramework)' == 'net461' ">
40+
<PackageReference Include="Microsoft.Build.Tasks.Core" Version="14.3" />
41+
</ItemGroup>
42+
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp2.0'">
43+
<PackageReference Include="Microsoft.Build.Tasks.Core" Version="15.1.548" />
44+
<PackageReference Include="System.Runtime.Loader" Version="4.3.0" />
4245
</ItemGroup>
4346
</Project>

src/NerdBank.GitVersioning/NerdBank.GitVersioning.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<ItemGroup>
1010
<PackageReference Include="DotNetMDDocs" Version="0.111.0" PrivateAssets="all" Condition=" '$(GenerateMarkdownApiDocs)' == 'true' " />
1111
<PackageReference Include="LibGit2Sharp" Version="0.26.0-preview-0070" PrivateAssets="none" />
12+
<PackageReference Include="Microsoft.DotNet.PlatformAbstractions" Version="2.1.0" />
1213
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
1314
<PackageReference Include="System.Diagnostics.Tools" Version="4.3.0" Condition=" '$(TargetFramework)' == 'netcoreapp2.0' " />
1415
<PackageReference Include="Validation" Version="2.4.18" />

src/MSBuildExtensionTask/ContextAwareTask.cs renamed to src/Nerdbank.GitVersioning.Tasks/ContextAwareTask.cs

Lines changed: 6 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,27 @@
44
using System.IO;
55
using System.Linq;
66
using System.Reflection;
7-
#if NETCOREAPP2_0
7+
#if NETCOREAPP
88
using System.Runtime.Loader;
99
#endif
1010
using Microsoft.Build.Framework;
1111
using Microsoft.Build.Utilities;
12+
#if NETCOREAPP
13+
using Nerdbank.GitVersioning;
14+
#endif
1215

1316
public abstract class ContextAwareTask : Task
1417
{
15-
#if NETCOREAPP2_0
16-
/// <summary>
17-
/// Our custom <see cref="AssemblyLoadContext"/> that we create to host our Task.
18-
/// </summary>
19-
/// <remarks>
20-
/// We only create *one* of these, and reuse it for subsequent task invocations,
21-
/// because creating multiple of them hit https://github.com/dotnet/coreclr/issues/19654
22-
/// </remarks>
23-
private static CustomAssemblyLoader Loader;
24-
#endif
25-
2618
protected virtual string ManagedDllDirectory => Path.GetDirectoryName(new Uri(this.GetType().GetTypeInfo().Assembly.CodeBase).LocalPath);
2719

2820
protected virtual string UnmanagedDllDirectory => null;
2921

3022
public override bool Execute()
3123
{
32-
#if NETCOREAPP2_0
24+
#if NETCOREAPP
3325
string taskAssemblyPath = new Uri(this.GetType().GetTypeInfo().Assembly.CodeBase).LocalPath;
34-
if (Loader == null)
35-
{
36-
Loader = new CustomAssemblyLoader();
37-
}
3826

39-
Loader.LoaderTask = this;
40-
Assembly inContextAssembly = Loader.LoadFromAssemblyPath(taskAssemblyPath);
27+
Assembly inContextAssembly = GitLoaderContext.Instance.LoadFromAssemblyPath(taskAssemblyPath);
4128
Type innerTaskType = inContextAssembly.GetType(this.GetType().FullName);
4229
object innerTask = Activator.CreateInstance(innerTaskType);
4330

@@ -83,60 +70,5 @@ public override bool Execute()
8370
}
8471

8572
protected abstract bool ExecuteInner();
86-
87-
#if NETCOREAPP2_0
88-
private class CustomAssemblyLoader : AssemblyLoadContext
89-
{
90-
private ContextAwareTask loaderTask;
91-
92-
internal CustomAssemblyLoader()
93-
{
94-
}
95-
96-
internal ContextAwareTask LoaderTask
97-
{
98-
get => this.loaderTask;
99-
set => this.loaderTask = value;
100-
}
101-
102-
protected override Assembly Load(AssemblyName assemblyName)
103-
{
104-
if (this.loaderTask == null)
105-
{
106-
throw new InvalidOperationException(nameof(this.loaderTask) + " must be set first.");
107-
}
108-
109-
string assemblyPath = Path.Combine(this.loaderTask.ManagedDllDirectory, assemblyName.Name) + ".dll";
110-
if (File.Exists(assemblyPath))
111-
{
112-
return this.LoadFromAssemblyPath(assemblyPath);
113-
}
114-
115-
return Default.LoadFromAssemblyName(assemblyName);
116-
}
117-
118-
protected override IntPtr LoadUnmanagedDll(string unmanagedDllName)
119-
{
120-
if (this.loaderTask == null)
121-
{
122-
throw new InvalidOperationException(nameof(this.loaderTask) + " must be set first.");
123-
}
124-
125-
string unmanagedDllPath = Directory.EnumerateFiles(
126-
this.loaderTask.UnmanagedDllDirectory,
127-
$"{unmanagedDllName}.*").Concat(
128-
Directory.EnumerateFiles(
129-
this.loaderTask.UnmanagedDllDirectory,
130-
$"lib{unmanagedDllName}.*"))
131-
.FirstOrDefault();
132-
if (unmanagedDllPath != null)
133-
{
134-
return this.LoadUnmanagedDllFromPath(unmanagedDllPath);
135-
}
136-
137-
return base.LoadUnmanagedDll(unmanagedDllName);
138-
}
139-
}
140-
#endif
14173
}
14274
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// This code originally copied from https://github.com/dotnet/sourcelink/tree/c092238370e0437eb95722f28c79273244dc7f1a/src/Microsoft.Build.Tasks.Git
2+
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
#if NETCOREAPP
4+
5+
using System;
6+
using System.IO;
7+
using System.Reflection;
8+
using System.Runtime.InteropServices;
9+
using System.Runtime.Loader;
10+
using RuntimeEnvironment = Microsoft.DotNet.PlatformAbstractions.RuntimeEnvironment;
11+
12+
namespace Nerdbank.GitVersioning
13+
{
14+
internal class GitLoaderContext : AssemblyLoadContext
15+
{
16+
public static readonly GitLoaderContext Instance = new GitLoaderContext();
17+
18+
protected override Assembly Load(AssemblyName assemblyName)
19+
{
20+
if (assemblyName.Name == "LibGit2Sharp")
21+
{
22+
var path = Path.Combine(Path.GetDirectoryName(typeof(GitLoaderContext).Assembly.Location), assemblyName.Name + ".dll");
23+
return LoadFromAssemblyPath(path);
24+
}
25+
26+
return Default.LoadFromAssemblyName(assemblyName);
27+
}
28+
29+
protected override IntPtr LoadUnmanagedDll(string unmanagedDllName)
30+
{
31+
var modulePtr = IntPtr.Zero;
32+
33+
if (unmanagedDllName.StartsWith("git2-", StringComparison.Ordinal) ||
34+
unmanagedDllName.StartsWith("libgit2-", StringComparison.Ordinal))
35+
{
36+
var directory = GetNativeLibraryDirectory();
37+
var extension = GetNativeLibraryExtension();
38+
39+
if (!unmanagedDllName.EndsWith(extension, StringComparison.Ordinal))
40+
{
41+
unmanagedDllName += extension;
42+
}
43+
44+
var nativeLibraryPath = Path.Combine(directory, unmanagedDllName);
45+
if (!File.Exists(nativeLibraryPath))
46+
{
47+
nativeLibraryPath = Path.Combine(directory, "lib" + unmanagedDllName);
48+
}
49+
50+
modulePtr = LoadUnmanagedDllFromPath(nativeLibraryPath);
51+
}
52+
53+
return (modulePtr != IntPtr.Zero) ? modulePtr : base.LoadUnmanagedDll(unmanagedDllName);
54+
}
55+
56+
internal static string GetNativeLibraryDirectory()
57+
{
58+
var dir = Path.GetDirectoryName(typeof(GitLoaderContext).Assembly.Location);
59+
return Path.Combine(dir, "runtimes", RuntimeIdMap.GetNativeLibraryDirectoryName(RuntimeEnvironment.GetRuntimeIdentifier()), "native");
60+
}
61+
62+
private static string GetNativeLibraryExtension()
63+
{
64+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
65+
{
66+
return ".dll";
67+
}
68+
69+
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
70+
{
71+
return ".dylib";
72+
}
73+
74+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
75+
{
76+
return ".so";
77+
}
78+
79+
throw new PlatformNotSupportedException();
80+
}
81+
}
82+
}
83+
#endif

src/Nerdbank.GitVersioning.Tasks/Nerdbank.GitVersioning.Tasks.csproj

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,15 @@
7878
<PackageReference Include="Nerdbank.GitVersioning.LKG" Version="1.6.20-beta-gfea83a8c9e" />
7979
</ItemGroup>
8080
<ItemGroup>
81-
<ProjectReference Include="..\MSBuildExtensionTask\MSBuildExtensionTask.csproj" />
8281
<ProjectReference Include="..\NerdBank.GitVersioning\NerdBank.GitVersioning.csproj" />
8382
</ItemGroup>
83+
<ItemGroup Condition="'$(TargetFramework)' == 'net461' ">
84+
<PackageReference Include="Microsoft.Build.Tasks.Core" Version="14.3" />
85+
</ItemGroup>
86+
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp2.0'">
87+
<PackageReference Include="Microsoft.Build.Tasks.Core" Version="15.1.548" />
88+
<PackageReference Include="System.Runtime.Loader" Version="4.3.0" />
89+
</ItemGroup>
8490
<ItemGroup>
8591
<Compile Include="..\Shared\**\*.cs" LinkBase="Shared" />
8692
</ItemGroup>

src/Nerdbank.GitVersioning.Tasks/Nerdbank.GitVersioning.nuspec

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,11 @@
1717
<files>
1818
<file src="$BaseOutputPath$net461\LibGit2Sharp.dll" target="build\MSBuildFull\LibGit2Sharp.dll" />
1919
<file src="$BaseOutputPath$net461\LibGit2Sharp.dll.config" target="build\MSBuildFull\LibGit2Sharp.dll.config" />
20-
<file src="$BaseOutputPath$net461\MSBuildExtensionTask.dll" target="build\MSBuildFull\MSBuildExtensionTask.dll" />
2120
<file src="$BaseOutputPath$net461\NerdBank.GitVersioning.dll" target="build\MSBuildFull\NerdBank.GitVersioning.dll" />
2221
<file src="$BaseOutputPath$net461\Nerdbank.GitVersioning.Tasks.dll" target="build\MSBuildFull\Nerdbank.GitVersioning.Tasks.dll" />
2322
<file src="$BaseOutputPath$net461\Newtonsoft.Json.dll" target="build\MSBuildFull\Newtonsoft.Json.dll" />
2423
<file src="$BaseOutputPath$net461\Validation.dll" target="build\MSBuildFull\Validation.dll" />
25-
<file src="$LibGit2SharpNativeBinaries$runtimes\win-x64\native\git2-a904fc6.dll" target="build\lib\win32\x64\git2-a904fc6.dll" />
26-
<file src="$LibGit2SharpNativeBinaries$runtimes\win-x64\native\git2-a904fc6.pdb" target="build\lib\win32\x64\git2-a904fc6.pdb" />
27-
<file src="$LibGit2SharpNativeBinaries$runtimes\win-x86\native\git2-a904fc6.dll" target="build\lib\win32\x86\git2-a904fc6.dll" />
28-
<file src="$LibGit2SharpNativeBinaries$runtimes\win-x86\native\git2-a904fc6.pdb" target="build\lib\win32\x86\git2-a904fc6.pdb" />
29-
<file src="$LibGit2SharpNativeBinaries$runtimes\osx\native\libgit2-a904fc6.dylib" target="build\lib\osx\libgit2-a904fc6.dylib" />
30-
<file src="$LibGit2SharpNativeBinaries$runtimes\linux-x64\native\libgit2-a904fc6.so" target="build\lib\linux\x86_64\libgit2-a904fc6.so" />
24+
<file src="$LibGit2SharpNativeBinaries$runtimes\**" target="build\lib\" />
3125

3226
<!-- Additional copies to work around DllNotFoundException on Mono (https://github.com/AArnott/Nerdbank.GitVersioning/issues/222) -->
3327
<file src="$LibGit2SharpNativeBinaries$runtimes\osx\native\libgit2-a904fc6.dylib" target="build\MSBuildFull\lib\osx\libgit2-a904fc6.dylib" />
@@ -39,7 +33,6 @@
3933

4034
<file src="$LibGit2SharpNativeBinaries$libgit2\LibGit2Sharp.dll.config" target="build\MSBuildCore\LibGit2Sharp.dll.config" />
4135
<file src="$BaseOutputPath$netcoreapp2.0\LibGit2Sharp.dll" target="build\MSBuildCore\LibGit2Sharp.dll" />
42-
<file src="$BaseOutputPath$netcoreapp2.0\MSBuildExtensionTask.dll" target="build\MSBuildCore\MSBuildExtensionTask.dll" />
4336
<file src="$BaseOutputPath$netcoreapp2.0\NerdBank.GitVersioning.dll" target="build\MSBuildCore\NerdBank.GitVersioning.dll" />
4437
<file src="$BaseOutputPath$netcoreapp2.0\Nerdbank.GitVersioning.Tasks.dll" target="build\MSBuildCore\Nerdbank.GitVersioning.Tasks.dll" />
4538
<file src="$BaseOutputPath$netcoreapp2.0\Newtonsoft.Json.dll" target="build\MSBuildCore\Newtonsoft.Json.dll" />

0 commit comments

Comments
 (0)