Skip to content

Commit 0455e0b

Browse files
committed
Replace AssemblyVersionInfoTemplates with TemplateManager
1 parent 057fcb5 commit 0455e0b

13 files changed

+99
-88
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<Assembly: AssemblyVersion("2.3.1.0")>
22
<Assembly: AssemblyFileVersion("2.3.1.0")>
3-
<assembly: AssemblyInformationalVersion("2.3.1+3.Branch.foo.Sha.hash")>
3+
<Assembly: AssemblyInformationalVersion("2.3.1+3.Branch.foo.Sha.hash")>
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
<Assembly: AssemblyFileVersion("2.3.1.0")>
2-
<assembly: AssemblyInformationalVersion("2.3.1+3.Branch.foo.Sha.hash")>
2+
<Assembly: AssemblyInformationalVersion("2.3.1+3.Branch.foo.Sha.hash")>
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
<Assembly: AssemblyFileVersion("2.3.1.0")>
2-
<assembly: AssemblyInformationalVersion("2.3.1+3.Branch.foo.Sha.hash")>
2+
<Assembly: AssemblyInformationalVersion("2.3.1+3.Branch.foo.Sha.hash")>

src/GitVersionCore/GitVersionCore.csproj

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,15 @@
133133
<Compile Include="SemanticVersionExtensions.cs" />
134134
<Compile Include="SemanticVersionFormatValues.cs" />
135135
<Compile Include="VerbosityLevel.cs" />
136+
<EmbeddedResource Include="VersionAssemblyInfoResources\AddFormats\VersionAssemblyInfo.cs" />
136137
<Compile Include="VersionAssemblyInfoResources\AssemblyInfoFileUpdater.cs" />
138+
<Compile Include="TemplateManager.cs" />
137139
<Compile Include="VersionCalculation\MainlineVersionCalculator.cs" />
138140
<Compile Include="VersionFilters\MinDateVersionFilter.cs" />
139141
<Compile Include="VersionFilters\IVersionFilter.cs" />
140142
<Compile Include="VersionFilters\ShaVersionFilter.cs" />
141143
<Compile Include="StringFormatWith.cs" />
142-
<Compile Include="VersionAssemblyInfoResources\AssemblyVersionInfoTemplates.cs" />
143-
<EmbeddedResource Include="VersionAssemblyInfoResources\VersionAssemblyInfo.cs" />
144+
<EmbeddedResource Include="VersionAssemblyInfoResources\Templates\VersionAssemblyInfo.cs" />
144145
<Compile Include="VersionCalculation\BaseVersionCalculator.cs" />
145146
<Compile Include="VersionCalculation\BaseVersionCalculators\BaseVersion.cs" />
146147
<Compile Include="VersionCalculation\BaseVersionCalculators\ConfigNextVersionBaseVersionStrategy.cs" />
@@ -181,8 +182,9 @@
181182
<None Include="FodyWeavers.xml">
182183
<SubType>Designer</SubType>
183184
</None>
184-
<EmbeddedResource Include="VersionAssemblyInfoResources\VersionAssemblyInfo.vb" />
185-
<EmbeddedResource Include="VersionAssemblyInfoResources\VersionAssemblyInfo.fs" />
185+
<EmbeddedResource Include="VersionAssemblyInfoResources\Templates\VersionAssemblyInfo.vb" />
186+
<EmbeddedResource Include="VersionAssemblyInfoResources\Templates\VersionAssemblyInfo.fs" />
187+
<EmbeddedResource Include="VersionAssemblyInfoResources\AddFormats\VersionAssemblyInfo.fs" />
186188
</ItemGroup>
187189
<ItemGroup>
188190
<None Include="app.config" />
@@ -191,6 +193,10 @@
191193
</None>
192194
<None Include="packages.config" />
193195
</ItemGroup>
196+
<ItemGroup />
197+
<ItemGroup>
198+
<EmbeddedResource Include="VersionAssemblyInfoResources\AddFormats\VersionAssemblyInfo.vb" />
199+
</ItemGroup>
194200
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
195201
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
196202
<PropertyGroup>

src/GitVersionCore/TemplateManager.cs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
namespace GitVersion
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using System.Linq;
7+
using GitVersionCore.Extensions;
8+
9+
enum TemplateType
10+
{
11+
VersionAssemblyInfoResources,
12+
GitVersionInformationResources
13+
}
14+
15+
class TemplateManager
16+
{
17+
readonly Dictionary<string, string> templates;
18+
readonly Dictionary<string, string> addFormats;
19+
20+
public TemplateManager(TemplateType templateType)
21+
{
22+
templates = GetEmbeddedTemplates(templateType, "Templates").ToDictionary(k => Path.GetExtension(k), v => v);
23+
addFormats = GetEmbeddedTemplates(templateType, "AddFormats").ToDictionary(k => Path.GetExtension(k), v => v);
24+
}
25+
26+
public string GetTemplateFor(string file)
27+
{
28+
string result = null;
29+
var fileExtension = Path.GetExtension(file);
30+
string template;
31+
32+
if (templates.TryGetValue(fileExtension, out template) && template != null)
33+
{
34+
result = template.ReadAsStringFromEmbeddedResource<TemplateManager>();
35+
}
36+
37+
return result;
38+
}
39+
40+
public string GetAddFormatFor(string fileExtension)
41+
{
42+
string result = null;
43+
string addFormat;
44+
45+
if(addFormats.TryGetValue(fileExtension, out addFormat) && addFormat != null)
46+
{
47+
result = addFormat.ReadAsStringFromEmbeddedResource<TemplateManager>();
48+
}
49+
50+
return result;
51+
}
52+
53+
public bool IsSupported(string fileExtension)
54+
{
55+
if (fileExtension == null)
56+
{
57+
throw new ArgumentNullException(nameof(fileExtension));
58+
}
59+
60+
return addFormats.Keys.Contains(fileExtension, StringComparer.OrdinalIgnoreCase);
61+
}
62+
63+
static IEnumerable<string> GetEmbeddedTemplates(TemplateType templateType, string blah)
64+
{
65+
foreach (var name in typeof(TemplateManager).Assembly.GetManifestResourceNames())
66+
{
67+
if (name.Contains(templateType.ToString()) && name.Contains(blah))
68+
{
69+
yield return name;
70+
}
71+
}
72+
}
73+
}
74+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[assembly: {0}]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[<assembly: {0}>]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<Assembly: {0}>

src/GitVersionCore/VersionAssemblyInfoResources/AssemblyInfoFileUpdater.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ namespace GitVersion
66
using System.IO;
77
using System.Linq;
88
using System.Text.RegularExpressions;
9-
using GitVersion.VersionAssemblyInfoResources;
109

1110
public class AssemblyInfoFileUpdater : IDisposable
1211
{
@@ -18,6 +17,7 @@ public class AssemblyInfoFileUpdater : IDisposable
1817
VersionVariables variables;
1918
IFileSystem fileSystem;
2019
bool ensureAssemblyInfo;
20+
TemplateManager templateManager;
2121

2222
public AssemblyInfoFileUpdater(string assemblyInfoFileName, string workingDirectory, VersionVariables variables, IFileSystem fileSystem, bool ensureAssemblyInfo) :
2323
this(new HashSet<string> { assemblyInfoFileName }, workingDirectory, variables, fileSystem, ensureAssemblyInfo)
@@ -30,6 +30,8 @@ public AssemblyInfoFileUpdater(ISet<string> assemblyInfoFileNames, string workin
3030
this.variables = variables;
3131
this.fileSystem = fileSystem;
3232
this.ensureAssemblyInfo = ensureAssemblyInfo;
33+
34+
templateManager = new TemplateManager(TemplateType.VersionAssemblyInfoResources);
3335
}
3436

3537
public void Update()
@@ -98,9 +100,9 @@ public void Update()
98100
}
99101
}
100102

101-
static string ReplaceOrAppend(Regex replaceRegex, string inputString, string replaceString, string fileExtension, ref bool appendedAttributes)
103+
string ReplaceOrAppend(Regex replaceRegex, string inputString, string replaceString, string fileExtension, ref bool appendedAttributes)
102104
{
103-
var assemblyAddFormat = AssemblyVersionInfoTemplates.GetAssemblyInfoAddFormatFor(fileExtension);
105+
var assemblyAddFormat = templateManager.GetAddFormatFor(fileExtension);
104106

105107
if (replaceRegex.IsMatch(inputString))
106108
{
@@ -115,7 +117,7 @@ static string ReplaceOrAppend(Regex replaceRegex, string inputString, string rep
115117
return inputString;
116118
}
117119

118-
static IEnumerable<FileInfo> GetAssemblyInfoFiles(string workingDirectory, ISet<string> assemblyInfoFileNames, IFileSystem fileSystem, bool ensureAssemblyInfo)
120+
IEnumerable<FileInfo> GetAssemblyInfoFiles(string workingDirectory, ISet<string> assemblyInfoFileNames, IFileSystem fileSystem, bool ensureAssemblyInfo)
119121
{
120122
if (assemblyInfoFileNames != null && assemblyInfoFileNames.Any(x => !string.IsNullOrWhiteSpace(x)))
121123
{
@@ -135,15 +137,15 @@ static IEnumerable<FileInfo> GetAssemblyInfoFiles(string workingDirectory, ISet<
135137
{
136138
var assemblyInfoFile = new FileInfo(item);
137139

138-
if (AssemblyVersionInfoTemplates.IsSupported(assemblyInfoFile.Extension))
140+
if (templateManager.IsSupported(assemblyInfoFile.Extension))
139141
{
140142
yield return assemblyInfoFile;
141143
}
142144
}
143145
}
144146
}
145147

146-
static bool EnsureVersionAssemblyInfoFile(bool ensureAssemblyInfo, IFileSystem fileSystem, string fullPath)
148+
bool EnsureVersionAssemblyInfoFile(bool ensureAssemblyInfo, IFileSystem fileSystem, string fullPath)
147149
{
148150
if (fileSystem.Exists(fullPath))
149151
{
@@ -155,7 +157,7 @@ static bool EnsureVersionAssemblyInfoFile(bool ensureAssemblyInfo, IFileSystem f
155157
return false;
156158
}
157159

158-
var assemblyInfoSource = AssemblyVersionInfoTemplates.GetAssemblyInfoTemplateFor(fullPath);
160+
var assemblyInfoSource = templateManager.GetTemplateFor(fullPath);
159161

160162
if (!string.IsNullOrWhiteSpace(assemblyInfoSource))
161163
{

src/GitVersionCore/VersionAssemblyInfoResources/AssemblyVersionInfoTemplates.cs

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

0 commit comments

Comments
 (0)