8000 Update Version to not rely on embedded resources · libgit2/libgit2sharp@1b6ad9a · GitHub
[go: up one dir, main page]

Skip to content

Commit 1b6ad9a

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

File tree

2 files changed

+49
-38
lines changed

2 files changed

+49
-38
lines changed

LibGit2Sharp/CodeGenerator.targets

Lines changed: 45 additions & 12 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

@@ -16,13 +21,13 @@
1621

1722
<PropertyGroup>
1823
<NativeDllNameSourceLines>
19-
namespace LibGit2Sharp.Core
20-
{
21-
internal static class NativeDllName
22-
{
24+
namespace LibGit2Sharp.Core
25+
{
26+
internal static class NativeDllName
27+
{
2328
public const string Name = "$(libgit2FileName)"%3b
24-
}
25-
}
29+
}
30+
}
2631
</NativeDllNameSourceLines>
2732
</PropertyGroup>
2833

@@ -40,13 +45,13 @@ namespace LibGit2Sharp.Core
4045

4146
<PropertyGroup>
4247
<UniqueIdSourceLines>
43-
namespace LibGit2Sharp.Core
44-
{
45-
internal static class UniqueId
46-
{
48+
namespace LibGit2Sharp.Core
49+
{
50+
internal static class UniqueId
51+
{
4752
public const string UniqueIdentifier = "$([System.Guid]::NewGuid())"%3b
48-
}
49-
}
53+
}
54+
}
5055
</UniqueIdSourceLines>
5156
</PropertyGroup>
5257

@@ -72,4 +77,32 @@ 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+
namespace LibGit2Sharp
89+
{
90+
internal static class AssemblyCommitIds
91+
{
92+
public const string LibGit2CommitSha = "$(libgit2hash)"%3b
93+
public const string LibGit2SharpCommitSha = "$(GitCommitId)"%3b
94+
}
95+
}
96+
</AssemblyCommitIdsSourceLines>
97+
</PropertyGroup>
98+
99+
<WriteLinesToFile File="$(AssemblyCommitIdsPath)" Lines="$(AssemblyCommitIdsSourceLines)" Overwrite="true" />
100+
101+
<ItemGroup>
102+
<Compile Include="$(AssemblyCommitIdsPath)" />
103+
<FileWrites Include="$(AssemblyCommitIdsPath)" />
104+
</ItemGroup>
105+
106+
</Target>
107+
75108
</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(AssemblyCommitIds.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(AssemblyCommitIds.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)
0