Skip to content

Commit 9f63545

Browse files
committed
Add initial GitVersionInformationGenerator implementation
1 parent 0455e0b commit 9f63545

File tree

10 files changed

+143
-1
lines changed

10 files changed

+143
-1
lines changed

src/GitVersionCore.Tests/GitVersionCore.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
<Compile Include="DocumentationTests.cs" />
102102
<Compile Include="ConfigProviderTests.cs" />
103103
<Compile Include="GitVersionContextTests.cs" />
104+
<Compile Include="GitVersionInformationGeneratorTests.cs" />
104105
<Compile Include="Helpers\DirectoryHelper.cs" />
105106
<Compile Include="IntegrationTests\FileSystemTests.cs" />
106107
<Compile Include="IntegrationTests\MainlineDevelopmentMode.cs" />
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using GitVersion;
8+
using NUnit.Framework;
9+
10+
namespace GitVersionCore.Tests
11+
{
12+
[TestFixture]
13+
public class GitVersionInformationGeneratorTests
14+
{
15+
[Test]
16+
public void ShouldCreateFile()
17+
{
18+
var fileSystem = new TestFileSystem();
19+
var directory = Path.GetTempPath();
20+
var fileName = "GitVersionInformation.g.cs";
21+
var fullPath = Path.Combine(directory, fileName);
22+
var variables = VariableProvider.GetVariablesFor(SemanticVersion.Parse("1.0.0", "v"), new TestEffectiveConfiguration(), false);
23+
24+
var generator = new GitVersionInformationGenerator(fileName, directory, variables, fileSystem);
25+
26+
generator.Generate();
27+
28+
var fileContents = fileSystem.ReadAllText(fullPath);
29+
}
30+
}
31+
}

src/GitVersionCore/GitVersionCore.csproj

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@
119119
<Compile Include="GitVersionCacheKey.cs" />
120120
<Compile Include="GitVersionCacheKeyFactory.cs" />
121121
<Compile Include="GitVersionException.cs" />
122+
<EmbeddedResource Include="GitVersionInformationResources\Templates\GitVersionInformation.cs" />
123+
<EmbeddedResource Include="GitVersionInformationResources\AddFormats\GitVersionInformation.cs" />
124+
<Compile Include="GitVersionInformationResources\GitVersionInformationGenerator.cs" />
122125
<Compile Include="Helpers\EncodingHelper.cs" />
123126
<Compile Include="Helpers\FileSystem.cs" />
124127
<Compile Include="Helpers\IFileSystem.cs" />
@@ -188,6 +191,8 @@
188191
</ItemGroup>
189192
<ItemGroup>
190193
<None Include="app.config" />
194+
<EmbeddedResource Include="GitVersionInformationResources\Templates\GitVersionInformation.fs" />
195+
<EmbeddedResource Include="GitVersionInformationResources\AddFormats\GitVersionInformation.fs" />
191196
<None Include="NugetAssets\GitVersion.nuspec">
192197
<SubType>Designer</SubType>
193198
</None>
@@ -197,6 +202,12 @@
197202
<ItemGroup>
198203
<EmbeddedResource Include="VersionAssemblyInfoResources\AddFormats\VersionAssemblyInfo.vb" />
199204
</ItemGroup>
205+
<ItemGroup>
206+
<EmbeddedResource Include="GitVersionInformationResources\Templates\GitVersionInformation.vb" />
207+
</ItemGroup>
208+
<ItemGroup>
209+
<EmbeddedResource Include="GitVersionInformationResources\AddFormats\GitVersionInformation.vb" />
210+
</ItemGroup>
200211
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
201212
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
202213
<PropertyGroup>
@@ -228,4 +239,4 @@
228239
</Target>
229240
<Import Project="..\packages\PepitaPackage.1.21.4\build\PepitaPackage.targets" Condition="Exists('..\packages\PepitaPackage.1.21.4\build\PepitaPackage.targets')" />
230241
<Import Project="..\packages\Fody.2.0.8\build\portable-net+sl+win+wpa+wp\Fody.targets" Condition="Exists('..\packages\Fody.2.0.8\build\portable-net+sl+win+wpa+wp\Fody.targets')" />
231-
</Project>
242+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
public static string {0} = "{1}";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
let {0} = "{1}"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Public Shared {0} As String = "{1}"
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
namespace GitVersion
2+
{
3+
using System;
4+
using System.IO;
5+
using System.Linq;
6+
using Helpers;
7+
8+
public class GitVersionInformationGenerator
9+
{
10+
string fileName;
11+
string directory;
12+
VersionVariables variables;
13+
IFileSystem fileSystem;
14+
15+
TemplateManager templateManager;
16+
17+
public GitVersionInformationGenerator(string fileName, string directory, VersionVariables variables, IFileSystem fileSystem)
18+
{
19+
this.fileName = fileName;
20+
this.directory = directory;
21+
this.variables = variables;
22+
this.fileSystem = fileSystem;
23+
24+
templateManager = new TemplateManager(TemplateType.GitVersionInformationResources);
25+
}
26+
27+
public void Generate()
28+
{
29+
var filePath = Path.Combine(directory, fileName);
30+
31+
string originalFileContents = null;
32+
33+
if (File.Exists(filePath))
34+
{
35+
originalFileContents = fileSystem.ReadAllText(filePath);
36+
}
37+
38+
var template = templateManager.GetTemplateFor(fileName);
39+
var addFormat = templateManager.GetAddFormatFor(Path.GetExtension(fileName));
40+
41+
var members = string.Join(Environment.NewLine, variables.Select(v => string.Format(" " + addFormat, v.Key, v.Value)));
42+
43+
var fileContents = string.Format(template, members);
44+
45+
if (fileContents != originalFileContents)
46+
{
47+
fileSystem.WriteAllText(filePath, fileContents);
48+
}
49+
}
50+
}
51+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by a tool.
4+
// GitVersion
5+
//
6+
// Changes to this file may cause incorrect behavior and will be lost if
7+
// the code is regenerated.
8+
// </auto-generated>
9+
//------------------------------------------------------------------------------
10+
11+
[global::System.Runtime.CompilerServices.CompilerGenerated]
12+
static class GitVersionInformation
13+
{{
14+
{0}
15+
}}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by a tool.
4+
// GitVersion
5+
//
6+
// Changes to this file may cause incorrect behavior and will be lost if
7+
// the code is regenerated.
8+
// </auto-generated>
9+
//------------------------------------------------------------------------------
10+
11+
[<AbstractClass; Sealed>]
12+
[<global.System.Runtime.CompilerServices.CompilerGenerated>]
13+
module GitVersionInformation =
14+
{0}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'------------------------------------------------------------------------------
2+
' <auto-generated>
3+
' This code was generated by a tool.
4+
' GitVersion
5+
'
6+
' Changes to this file may cause incorrect behavior and will be lost if
7+
' the code is regenerated.
8+
' </auto-generated>
9+
'------------------------------------------------------------------------------
10+
11+
<Global.System.Runtime.CompilerServices.CompilerGenerated()>
12+
NotInheritable Class GitVersionInformation
13+
Private Sub New()
14+
End Sub
15+
{0}
16+
End Class

0 commit comments

Comments
 (0)