Skip to content

Commit 4a7cf39

Browse files
committed
Merge pull request #760 from Philo/feature/630-create-assembly-info
Feature/630 create assembly info
2 parents f437c76 + 2249041 commit 4a7cf39

17 files changed

+632
-24
lines changed

docs/usage/command-line.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,34 @@ It will update the following assembly attributes:
2424

2525
Note that contrary to when using the [MSBuild Task](msbuild-task.md) the attributes must already exist in the `AssemblyInfo.cs` or `AssemblyInfo.vb` files prior to calling GitVersion.
2626

27-
By adding `/updateassemblyinfofilename` the name of AssemblyInfo file to update can be set.
27+
By adding `/updateassemblyinfo <filenames>` the name of AssemblyInfo file to update can be set. This switch can accept multiple files with the path to the file specified relative to the working directory.
28+
29+
GitVersion can generate an assembly info source file for you if it does not already exist. Use the `/ensureassemblyinfo` switch alongside `/updateassemblyinfo <filename>`, if the filename specified does not exist it will be generated based on a known template that adds:
30+
31+
* `AssemblyVersion` will be set to the `AssemblySemVer` variable.
32+
* `AssemblyFileVersion` will be set to the `MajorMinorPatch` variable with a appended `.0`.
33+
* `AssemblyInformationalVersion` will be set to the `InformationalVersion` variable.
34+
35+
This can be done for *.cs, *.vb and *.fs files.
36+
37+
When requesting that GitVersion generate an assembly info file you are limited to only specifying a single `<filename>` within the `/updateassemblyinfo` switch, this is to prevent the creation of mulitple assembly info files with the same assembly version attributes. If this occurs your build will fail.
38+
39+
### Example: When AssemblyInfo.cs does not exist
40+
`GitVersion.exe /updateassemblyinfo AssemblyInfo.cs /ensureassemblyinfo`
41+
42+
A file is generated that contains version attributes (`AssemblyVersion`, `AssemblyFileVersion`, `AssemblyInformationalVersion`)
43+
44+
### Example: When AssemblyInfo.cs already exists
45+
`GitVersion.exe /updateassemblyinfo AssemblyInfo.cs /ensureassemblyinfo`
46+
47+
All known attributes (`AssemblyVersion`, `AssemblyFileVersion`, `AssemblyInformationalVersion`) will be updated
48+
49+
### Example: When AssemblyInfo.cs and AssemblyVersionInfo.cs do not exist
50+
`GitVersion.exe /updateassemblyinfo AssemblyInfo.cs AssemblyVersionInfo.cs /ensureassemblyinfo`
51+
52+
Will result in command line argument error
53+
54+
### Example: When AssemblyInfo.cs and AssemblyVersionInfo.cs already exist
55+
`GitVersion.exe /updateassemblyinfo AssemblyInfo.cs AssemblyVersionInfo.cs`
56+
57+
Will iterate through each file and update known attributes (`AssemblyVersion`, `AssemblyFileVersion`, `AssemblyInformationalVersion`).

src/GitVersionCore.Tests/TestFileSystem.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,19 @@ public Stream OpenRead(string path)
6969

7070
public void CreateDirectory(string path)
7171
{
72+
if (fileSystem.ContainsKey(path))
73+
{
74+
fileSystem[path] = "";
75+
}
76+
else
77+
{
78+
fileSystem.Add(path, "");
79+
}
80+
}
81+
82+
public bool DirectoryExists(string path)
83+
{
84+
return fileSystem.ContainsKey(path);
7285
}
7386

7487
public long GetLastDirectoryWrite(string path)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace GitVersionCore.Extensions
2+
{
3+
using System.IO;
4+
5+
public static class ReadEmbeddedResourceExtensions
6+
{
7+
/// <summary>
8+
///
9+
/// </summary>
10+
/// <typeparam name="T"></typeparam>
11+
/// <param name="resourceName">Should include Namespace separated path to resource in assembly referenced by <typeparamref name="T"/></param>
12+
/// <returns></returns>
13+
public static string ReadAsStringFromEmbeddedResource<T>(this string resourceName)
14+
{
15+
using (var stream = resourceName.ReadFromEmbeddedResource<T>())
16+
{
17+
using (var rdr = new StreamReader(stream))
18+
{
19+
return rdr.ReadToEnd();
20+
}
21+
}
22+
}
23+
24+
public static Stream ReadFromEmbeddedResource<T>(this string resourceName)
25+
{
26+
var assembly = typeof(T).Assembly;
27+
return assembly.GetManifestResourceStream(resourceName);
28+
}
29+
}
30+
}

src/GitVersionCore/GitVersionCore.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
<Compile Include="Configuration\Init\StepResult.cs" />
110110
<Compile Include="EffectiveConfiguration.cs" />
111111
<Compile Include="ExecuteCore.cs" />
112+
<Compile Include="Extensions\ReadEmbeddedResourceExtensions.cs" />
112113
<Compile Include="GitPreparer.cs" />
113114
<Compile Include="GitVersionCache.cs" />
114115
<Compile Include="Helpers\FileSystem.cs" />
@@ -121,6 +122,8 @@
121122
<Compile Include="SemanticVersionExtensions.cs" />
122123
<Compile Include="SemanticVersionFormatValues.cs" />
123124
<Compile Include="StringFormatWith.cs" />
125+
<Compile Include="VersionAssemblyInfoResources\AssemblyVersionInfoTemplates.cs" />
126+
<EmbeddedResource Include="VersionAssemblyInfoResources\VersionAssemblyInfo.cs" />
124127
<Compile Include="VersionCalculation\BaseVersionCalculator.cs" />
125128
<Compile Include="VersionCalculation\BaseVersionCalculators\BaseVersion.cs" />
126129
<Compile Include="VersionCalculation\BaseVersionCalculators\ConfigNextVersionBaseVersionStrategy.cs" />
@@ -161,6 +164,8 @@
161164
<Content Include="FodyWeavers.xml">
162165
<SubType>Designer</SubType>
163166
</Content>
167+
<EmbeddedResource Include="VersionAssemblyInfoResources\VersionAssemblyInfo.vb" />
168+
<EmbeddedResource Include="VersionAssemblyInfoResources\VersionAssemblyInfo.fs" />
164169
</ItemGroup>
165170
<ItemGroup>
166171
<None Include="NugetAssets\GitVersion.nuspec">

src/GitVersionCore/Helpers/FileSystem.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ public void CreateDirectory(string path)
5656
Directory.CreateDirectory(path);
5757
}
5858

59+
public bool DirectoryExists(string path)
60+
{
61+
return Directory.Exists(path);
62+
}
63+
5964
public long GetLastDirectoryWrite(string path)
6065
{
6166
return new DirectoryInfo(path)

src/GitVersionCore/Helpers/IFileSystem.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public interface IFileSystem
1515
Stream OpenWrite(string path);
1616
Stream OpenRead(string path);
1717
void CreateDirectory(string path);
18+
bool DirectoryExists(string path);
1819
long GetLastDirectoryWrite(string path);
1920
}
2021
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
namespace GitVersion.VersionAssemblyInfoResources
2+
{
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using System.Linq;
6+
using GitVersionCore.Extensions;
7+
8+
public class AssemblyVersionInfoTemplates
9+
{
10+
static IDictionary<string, FileInfo> assemblyInfoSourceList;
11+
12+
static AssemblyVersionInfoTemplates()
13+
{
14+
var enclosingNamespace = typeof(AssemblyVersionInfoTemplates).Namespace;
15+
16+
var files = typeof(AssemblyVersionInfoTemplates)
17+
.Assembly
18+
.GetManifestResourceNames()
19+
.Where(n => n.StartsWith(enclosingNamespace ?? string.Empty)).Select(f => new FileInfo(f));
20+
21+
assemblyInfoSourceList = files.ToDictionary(k => k.Extension, v => v);
22+
}
23+
24+
public static string GetAssemblyInfoTemplateFor(string assemblyInfoFile)
25+
{
26+
var fi = new FileInfo(assemblyInfoFile);
27+
if (assemblyInfoSourceList.ContainsKey(fi.Extension))
28+
{
29+
var template = assemblyInfoSourceList[fi.Extension];
30+
if (template != null)
31+
{
32+
return template.Name.ReadAsStringFromEmbeddedResource<AssemblyVersionInfoTemplates>();
33+
}
34+
}
35+
return null;
36+
}
37+
}
38+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by GitVersion.
4+
//
5+
// You can modify this code as we will not overwrite it when re-executing GitVersion
6+
// </auto-generated>
7+
//------------------------------------------------------------------------------
8+
9+
using System.Reflection;
10+
11+
[assembly: AssemblyFileVersion("0.0.0.0")]
12+
[assembly: AssemblyVersion("0.0.0.0")]
13+
[assembly: AssemblyInformationalVersion("0.0.0.0")]
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 GitVersion.
4+
//
5+
// You can modify this code as we will not overwrite it when re-executing GitVersion
6+
// </auto-generated>
7+
//------------------------------------------------------------------------------
8+
9+
open System.Reflection
10+
11+
[<assembly: AssemblyFileVersion("0.0.0.0")>]
12+
[<assembly: AssemblyVersion("0.0.0.0")>]
13+
[<assembly: AssemblyInformationalVersion("0.0.0.0")>]
14+
()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
''------------------------------------------------------------------------------
2+
'' <auto-generated>
3+
'' This code was generated by GitVersion.
4+
''
5+
'' You can modify this code as we will not overwrite it when re-executing GitVersion
6+
'' </auto-generated>
7+
''------------------------------------------------------------------------------
8+
9+
Imports System.Reflection
10+
11+
<assembly: AssemblyFileVersion("0.0.0.0")>
12+
<assembly: AssemblyVersion("0.0.0.0")>
13+
<assembly: AssemblyInformationalVersion("0.0.0.0")>

src/GitVersionExe.Tests/ArgumentParserTests.cs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using GitVersion;
45
using NUnit.Framework;
56
using Shouldly;
@@ -175,6 +176,10 @@ public void Unknown_argument_should_throw()
175176
[TestCase("-updateAssemblyInfo 1")]
176177
[TestCase("-updateAssemblyInfo")]
177178
[TestCase("-updateAssemblyInfo -proj foo.sln")]
179+
[TestCase("-updateAssemblyInfo assemblyInfo.cs")]
180+
[TestCase("-updateAssemblyInfo assemblyInfo.cs -ensureassemblyinfo")]
181+
[TestCase("-updateAssemblyInfo assemblyInfo.cs otherAssemblyInfo.cs")]
182+
[TestCase("-updateAssemblyInfo Assembly.cs Assembly.cs -ensureassemblyinfo")]
178183
public void update_assembly_info_true(string command)
179184
{
180185
var arguments = ArgumentParser.ParseArguments(command);
@@ -189,22 +194,60 @@ public void update_assembly_info_false(string command)
189194
arguments.UpdateAssemblyInfo.ShouldBe(false);
190195
}
191196

197+
[TestCase("-updateAssemblyInfo Assembly.cs Assembly1.cs -ensureassemblyinfo")]
198+
public void create_mulitple_assembly_info_protected(string command)
199+
{
200+
var exception = Assert.Throws<WarningException>(() => ArgumentParser.ParseArguments(command));
201+
exception.Message.ShouldBe("Can't specify multiple assembly info files when using -ensureassemblyinfo switch, either use a single assembly info file or do not specify -ensureassemblyinfo and create assembly info files manually");
202+
}
203+
192204
[Test]
193205
public void update_assembly_info_with_filename()
194206
{
195207
var arguments = ArgumentParser.ParseArguments("-updateAssemblyInfo CommonAssemblyInfo.cs");
196208
arguments.UpdateAssemblyInfo.ShouldBe(true);
197-
arguments.UpdateAssemblyInfoFileName.ShouldBe("CommonAssemblyInfo.cs");
209+
arguments.UpdateAssemblyInfoFileName.ShouldContain("CommonAssemblyInfo.cs");
210+
}
211+
212+
[Test]
213+
public void update_assembly_info_with_multiple_filenames()
214+
{
215+
var arguments = ArgumentParser.ParseArguments("-updateAssemblyInfo CommonAssemblyInfo.cs VersionAssemblyInfo.cs");
216+
arguments.UpdateAssemblyInfo.ShouldBe(true);
217+
arguments.UpdateAssemblyInfoFileName.Count.ShouldBe(2);
218+
arguments.UpdateAssemblyInfoFileName.ShouldContain("CommonAssemblyInfo.cs");
219+
arguments.UpdateAssemblyInfoFileName.ShouldContain("VersionAssemblyInfo.cs");
198220
}
199221

200222
[Test]
201223
public void update_assembly_info_with_relative_filename()
202224
{
203225
var arguments = ArgumentParser.ParseArguments("-updateAssemblyInfo ..\\..\\CommonAssemblyInfo.cs");
204226
arguments.UpdateAssemblyInfo.ShouldBe(true);
205-
arguments.UpdateAssemblyInfoFileName.ShouldBe("..\\..\\CommonAssemblyInfo.cs");
227+
arguments.UpdateAssemblyInfoFileName.ShouldContain("..\\..\\CommonAssemblyInfo.cs");
206228
}
207229

230+
[Test]
231+
public void ensure_assembly_info_true_when_found()
232+
{
233+
var arguments = ArgumentParser.ParseArguments("-ensureAssemblyInfo");
234+
arguments.EnsureAssemblyInfo.ShouldBe(true);
235+
}
236+
237+
[Test]
238+
public void ensure_assembly_info_true()
239+
{
240+
var arguments = ArgumentParser.ParseArguments("-ensureAssemblyInfo true");
241+
arguments.EnsureAssemblyInfo.ShouldBe(true);
242+
}
243+
244+
[Test]
245+
public void ensure_assembly_info_false()
246+
{
247+
var arguments = ArgumentParser.ParseArguments("-ensureAssemblyInfo false");
248+
arguments.EnsureAssemblyInfo.ShouldBe(false);
249+
}
250+
208251
[Test]
209252
public void dynamicRepoLocation()
210253
{

0 commit comments

Comments
 (0)