Skip to content

Commit ee9c4b4

Browse files
committed
Update Version to not rely on embedded resources
1 parent 66aeba8 commit ee9c4b4

File tree

2 files changed

+34
-26
lines changed

2 files changed

+34
-26
lines changed

LibGit2Sharp/CodeGenerator.targets

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
<Project>
22

3+
<PropertyGroup>
4+
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
5+
</PropertyGroup>
6+
37
<Target Name="DefineProperties">
48
<PropertyGroup>
59
<NativeDllNamePath>$(IntermediateOutputPath)NativeDllName.g.cs</NativeDllNamePath>
610
<UniqueIdentifierPath>$(IntermediateOutputPath)UniqueIdentifier.g.cs</UniqueIdentifierPath>
11+
<AssemblyCommitIdsPath>$(IntermediateOutputPath)AssemblyCommitIds.g.cs</AssemblyCommitIdsPath>
712
</PropertyGroup>
813
</Target>
914

@@ -72,4 +77,29 @@ namespace LibGit2Sharp.Core
7277

7378
</Target>
7479

80+
<Target Name="GenerateAssemblyCommitIdsCs" Inputs="@(EmbeddedResource);$(VersionSourceFile)" Outputs="$(AssemblyCommitIdsPath)" BeforeTargets="CoreCompile" DependsOnTargets="DefineProperties;GenerateAssemblyVersionInfo">
81+
82+
<ReadLinesFromFile File="@(EmbeddedResource)" Condition=" '%(Filename)%(Extension)' == 'libgit2_hash.txt' ">
83+
<Output TaskParameter="Lines" PropertyName="libgit2hash" />
84+
</ReadLinesFromFile>
85+
86+
<PropertyGroup>
87+
<AssemblyCommitIdsSourceLines>
88+
internal sealed partial class ThisAssembly
89+
{
90+
internal const string LibGit2CommitSha = "$(libgit2hash)"%3b
91+
internal const string LibGit2SharpCommitSha = "$(GitCommitId)"%3b
92+
}
93+
</AssemblyCommitIdsSourceLines>
94+
</PropertyGroup>
95+
96+
<WriteLinesToFile File="$(AssemblyCommitIdsPath)" Lines="$(AssemblyCommitIdsSourceLines)" Overwrite="true" />
97+
98+
<ItemGroup>
99+
<Compile Include="$(AssemblyCommitIdsPath)" />
100+
<FileWrites Include="$(AssemblyCommitIdsPath)" />
101+
</ItemGroup>
102+
103+
</Target>
104+
75105
</Project>

LibGit2Sharp/Version.cs

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
using System.Globalization;
2-
using System.IO;
3-
using System.Linq;
4-
using System.Reflection;
52
using LibGit2Sharp.Core;
63

74
namespace LibGit2Sharp
@@ -11,8 +8,6 @@ namespace LibGit2Sharp
118
/// </summary>
129
public class Version
1310
{
14-
private readonly Assembly assembly = typeof(Repository).Assembly;
15-
1611
/// <summary>
1712
/// Needed for mocking purposes.
1813
/// </summary>
@@ -42,23 +37,15 @@ public virtual BuiltInFeatures Features
4237
/// <summary>
4338
/// Returns the SHA hash for the libgit2 library.
4439
/// </summary>
45-
public virtual string LibGit2CommitSha
46-
{
47-
get { return RetrieveAbbrevShaFrom("libgit2_hash.txt"); }
48-
}
40+
public virtual string LibGit2CommitSha => RetrieveAbbrevShaFrom(ThisAssembly.LibGit2CommitSha);
4941

5042
/// <summary>
5143
/// Returns the SHA hash for the LibGit2Sharp library.
5244
/// </summary>
53-
public virtual string LibGit2SharpCommitSha
54-
{
55-
get { return RetrieveAbbrevShaFrom("libgit2sharp_hash.txt"); }
56-
}
45+
public virtual string LibGit2SharpCommitSha => RetrieveAbbrevShaFrom(ThisAssembly.LibGit2SharpCommitSha);
5746

58-
private string RetrieveAbbrevShaFrom(string name)
47+
private string RetrieveAbbrevShaFrom(string sha)
5948
{
60-
string sha = ReadContentFromResource(assembly, name) ?? "unknown";
61-
6249
var index = sha.Length > 7 ? 7 : sha.Length;
6350
return sha.Substring(0, index);
6451
}
@@ -68,7 +55,7 @@ private string RetrieveAbbrevShaFrom(string name)
6855
/// </summary>
6956
/// <para>
7057
/// The format of the version number is as follows:
71-
/// <para>Major.Minor.Patch-LibGit2Sharp_abbrev_hash-libgit2_abbrev_hash (x86|x64 - features)</para>
58+
/// <para>Major.Minor.Patch+g{LibGit2Sharp_abbrev_hash}.libgit2-{libgit2_abbrev_hash} (x86|x64 - features)</para>
7259
/// </para>
7360
/// <returns></returns>
7461
public override string ToString()
@@ -86,14 +73,5 @@ private string RetrieveVersion()
8673
Platform.ProcessorArchitecture,
8774
features);
8875
}
89-
90-
private string ReadContentFromResource(Assembly assembly, string partialResourceName)
91-
{
92-
string name = string.Format(CultureInfo.InvariantCulture, "LibGit2Sharp.{0}", partialResourceName);
93-
using (var sr = new StreamReader(assembly.GetManifestResourceStream(name)))
94-
{
95-
return sr.ReadLine();
96-
}
97-
}
9876
}
9977
}

0 commit comments

Comments
 (0)