Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cake CoreCLR fix #434

Merged
merged 4 commits into from Mar 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 27 additions & 5 deletions src/Cake.GitVersioning/Cake.GitVersioning.csproj
Expand Up @@ -13,9 +13,10 @@
<PackageIcon>cake-contrib-medium.png</PackageIcon>
<PackageProjectUrl>http://github.com/aarnott/Nerdbank.GitVersioning</PackageProjectUrl>
<SignAssembly>false</SignAssembly>
<!-- We include the whole OutputPath in this tools package. -->
<IncludeBuildOutput>false</IncludeBuildOutput>

<TargetsForTfmSpecificContentInPackage>$(TargetsForTfmSpecificContentInPackage);PackBuildOutputs</TargetsForTfmSpecificContentInPackage>
<LibGit2SharpNativeBinaries>$(NuGetPackageRoot)libgit2sharp.nativebinaries\$(LibGit2SharpNativeVersion)\</LibGit2SharpNativeBinaries>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>

<!-- This is a tools package and should express no dependencies. -->
Expand All @@ -36,6 +37,15 @@

<ItemGroup>
<None Include="cake-contrib-medium.png" Pack="true" PackagePath="" />

<!-- Include native binaries -->
<None Include="$(LibGit2SharpNativeBinaries)runtimes\**\*.*" Pack="true" PackagePath="lib\netstandard2.0\lib\" LinkBase="lib" />

<!-- Additional copies to work around DllNotFoundException on Mono (https://github.com/AArnott/Nerdbank.GitVersioning/issues/222) -->
<None Include="$(LibGit2SharpNativeBinaries)runtimes\osx\native\*.dylib" Pack="true" PackagePath="lib\netstandard2.0\lib\osx\" LinkBase="lib\osx" />
<None Include="$(LibGit2SharpNativeBinaries)runtimes\linux-x64\native\*.so" Pack="true" PackagePath="lib\netstandard2.0\lib\linux\x86_64\" LinkBase="lib\linux\x86_64" />
<None Include="$(LibGit2SharpNativeBinaries)runtimes\win-x64\native\*.dll" Pack="true" PackagePath="lib\netstandard2.0\lib\win32\x64\" LinkBase="lib\win32\x64" />
<None Include="$(LibGit2SharpNativeBinaries)runtimes\win-x86\native\*.dll" Pack="true" PackagePath="lib\netstandard2.0\lib\win32\x86\" LinkBase="lib\win32\x86" />
</ItemGroup>

<ItemGroup>
Expand All @@ -44,9 +54,21 @@

<Target Name="PackBuildOutputs" DependsOnTargets="SatelliteDllsProjectOutputGroup;DebugSymbolsProjectOutputGroup">
<ItemGroup>
<TfmSpecificPackageFile Include="$(OutputPath)\**\*" Exclude="$(OutputPath)\**\*.xml;$(OutputPath)\**\*.pdb;$(OutputPath)\**\Cake.Core.dll">
<PackagePath>lib\$(TargetFramework)\</PackagePath>
<TfmSpecificPackageFile
Include="
$(OutputPath)LibGit2Sharp.dll*;
$(OutputPath)Nerdbank.GitVersioning.*dll;
$(OutputPath)Newtonsoft.Json.dll;
$(OutputPath)Validation.dll;
"

Exclude="
$(OutputPath)Microsoft.*.dll
$(OutputPath)System.*.dll
"
>
<PackagePath>lib\$(TargetFramework)</PackagePath>
</TfmSpecificPackageFile>
</ItemGroup>
</Target>
</Project>
</Project>
35 changes: 34 additions & 1 deletion src/Cake.GitVersioning/GitVersioningAliases.cs
Expand Up @@ -6,6 +6,11 @@

namespace Cake.GitVersioning
{
using System;
using System.Linq;

using Validation;

/// <summary>
/// Contains functionality for using Nerdbank.GitVersioning.
/// </summary>
Expand All @@ -30,7 +35,35 @@ public static VersionOracle GitVersioningGetVersion(this ICakeContext context, s
{
var fullProjectDirectory = (new DirectoryInfo(projectDirectory)).FullName;

GitExtensions.HelpFindLibGit2NativeBinaries(Path.GetDirectoryName(Assembly.GetAssembly(typeof(GitVersioningAliases)).Location));
string directoryName = Path.GetDirectoryName(Assembly.GetAssembly(typeof(GitVersioningAliases)).Location);

if (string.IsNullOrWhiteSpace(directoryName))
{
throw new InvalidOperationException("Could not locate the Cake.GitVersioning library");
}

// Even after adding the folder containing the native libgit2 DLL to the PATH, DllNotFoundException is still thrown
// Workaround this by copying the contents of the found folder to the current directory
GitExtensions.HelpFindLibGit2NativeBinaries(directoryName, out var attemptedDirectory);

// The HelpFindLibGit2NativeBinaries method throws if the directory does not exist
var directoryInfo = new DirectoryInfo(attemptedDirectory);

// There should only be a single file in the directory, but we do not know its extension
// So, we will just get a list of all files rather than trying to determine the correct name and extension
// If there are other files there for some reason, it should not matter as long as we don't overwrite anything in the current directory
var fileInfos = directoryInfo.GetFiles();

foreach (var fileInfo in fileInfos)
{
// Copy the file to the Cake.GitVersioning DLL directory, without overwriting anything
string destFileName = Path.Combine(directoryName, fileInfo.Name);

if (!File.Exists(destFileName))
{
File.Copy(fileInfo.FullName, destFileName);
}
}

return VersionOracle.Create(fullProjectDirectory, null, CloudBuild.Active);
}
Expand Down
4 changes: 4 additions & 0 deletions src/Directory.Build.props
Expand Up @@ -19,6 +19,10 @@
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
</PropertyGroup>
<PropertyGroup>
<!-- LibGit2Sharp Native Binary version - used in both main project and Cake addin -->
<LibGit2SharpNativeVersion>2.0.298</LibGit2SharpNativeVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-beta2-19367-01" PrivateAssets="All" />
</ItemGroup>
Expand Down
15 changes: 13 additions & 2 deletions src/NerdBank.GitVersioning/GitExtensions.cs
Expand Up @@ -353,7 +353,18 @@ public static IEnumerable<Commit> GetCommitsFromVersion(this Repository repo, Ve
/// <exception cref="ArgumentException">Thrown if the provided path does not lead to an existing directory.</exception>
public static void HelpFindLibGit2NativeBinaries(string basePath)
{
if (!TryHelpFindLibGit2NativeBinaries(basePath, out string attemptedDirectory))
HelpFindLibGit2NativeBinaries(basePath, out string _);
}

/// <summary>
/// Assists the operating system in finding the appropriate native libgit2 module.
/// </summary>
/// <param name="basePath">The path to the directory that contains the lib folder.</param>
/// <param name="attemptedDirectory">Receives the directory that native binaries are expected.</param>
/// <exception cref="ArgumentException">Thrown if the provided path does not lead to an existing directory.</exception>
public static void HelpFindLibGit2NativeBinaries(string basePath, out string attemptedDirectory)
{
if (!TryHelpFindLibGit2NativeBinaries(basePath, out attemptedDirectory))
{
throw new ArgumentException($"Unable to find native binaries under directory: \"{attemptedDirectory}\".");
}
Expand All @@ -366,7 +377,7 @@ public static void HelpFindLibGit2NativeBinaries(string basePath)
/// <returns><c>true</c> if the libgit2 native binaries have been found; <c>false</c> otherwise.</returns>
public static bool TryHelpFindLibGit2NativeBinaries(string basePath)
{
return TryHelpFindLibGit2NativeBinaries(basePath, out string attemptedDirectory);
return TryHelpFindLibGit2NativeBinaries(basePath, out string _);
}

/// <summary>
Expand Down
Expand Up @@ -18,7 +18,7 @@

<Target Name="SetNuSpecProperties" BeforeTargets="GenerateNuspec" DependsOnTargets="GetBuildVersion">
<PropertyGroup>
<LibGit2SharpNativeBinaries>$(NuGetPackageRoot)libgit2sharp.nativebinaries\2.0.298\</LibGit2SharpNativeBinaries>
<LibGit2SharpNativeBinaries>$(NuGetPackageRoot)libgit2sharp.nativebinaries\$(LibGit2SharpNativeVersion)\</LibGit2SharpNativeBinaries>
<NuspecProperties>$(NuspecProperties);Version=$(Version);commit=$(GitCommitId);BaseOutputPath=$(OutputPath);LibGit2SharpNativeBinaries=$(LibGit2SharpNativeBinaries)</NuspecProperties>
</PropertyGroup>
</Target>
Expand Down