Skip to content

Commit 8c189ca

Browse files
committed
re #630 - Updates to allow multiple assembly info files to be listed
When specifying mulitple files the -ensureassemblyinfo switch can not be specified to protect against GitVersion generating multiple files with the same assembly attributes and breaking a build. -updateassemblyinfo Info.cs Info1.cs This will update any known version attributes in both files, if the file does not exist then it will be ignored -updateassemblyinfo Info.cs Info1.cs -ensureassemblyinfo This will cause a warning exception to be thrown as -ensureassemblyinfo can not be specified with multiple files -updateassemblyinfo ..\Common\Info.cs -ensureassemblyinfo Assuming ..\Common\Info.cs does not exist, this will generate an assembly info file at this location based on the embedded template for the file type (cs,vb,fs)
1 parent a45e2dc commit 8c189ca

File tree

8 files changed

+224
-66
lines changed

8 files changed

+224
-66
lines changed

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)

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
}

src/GitVersionExe.Tests/ArgumentParserTests.cs

Lines changed: 24 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,20 +194,37 @@ 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

208230
[Test]

src/GitVersionExe.Tests/AssemblyInfoFileUpdateTests.cs

Lines changed: 110 additions & 44 deletions
Large diffs are not rendered by default.

src/GitVersionExe/ArgumentParser.cs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ public static Arguments ParseArguments(string commandLineArguments)
1313
return ParseArguments(commandLineArguments.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList());
1414
}
1515

16+
static void EnsureArgumentValueCount(string[] values, int maxArguments = 1)
17+
{
18+
if(values != null && values.Length > maxArguments) throw new WarningException(string.Format("Could not parse command line parameter '{0}'.", values[1]));
19+
}
20+
1621
public static Arguments ParseArguments(List<string> commandLineArguments)
1722
{
1823
if (commandLineArguments.Count == 0)
@@ -73,79 +78,91 @@ public static Arguments ParseArguments(List<string> commandLineArguments)
7378
{
7479
//Currently, no arguments use more than one value, so having multiple values is an input error.
7580
//In the future, this exception can be removed to support multiple values for a switch.
76-
if (values.Length > 1) throw new WarningException(string.Format("Could not parse command line parameter '{0}'.", values[1]));
81+
// if (values.Length > 1) throw new WarningException(string.Format("Could not parse command line parameter '{0}'.", values[1]));
7782

7883
value = values.FirstOrDefault();
7984
}
8085

8186
if (IsSwitch("l", name))
8287
{
88+
EnsureArgumentValueCount(values);
8389
arguments.LogFilePath = value;
8490
continue;
8591
}
8692

8793
if (IsSwitch("targetpath", name))
8894
{
95+
EnsureArgumentValueCount(values);
8996
arguments.TargetPath = value;
9097
continue;
9198
}
9299

93100
if (IsSwitch("dynamicRepoLocation", name))
94101
{
102+
EnsureArgumentValueCount(values);
95103
arguments.DynamicRepositoryLocation = value;
96104
continue;
97105
}
98106

99107
if (IsSwitch("url", name))
100108
{
109+
EnsureArgumentValueCount(values);
101110
arguments.TargetUrl = value;
102111
continue;
103112
}
104113

105114
if (IsSwitch("b", name))
106115
{
116+
EnsureArgumentValueCount(values);
107117
arguments.TargetBranch = value;
108118
continue;
109119
}
110120

111121
if (IsSwitch("u", name))
112122
{
123+
EnsureArgumentValueCount(values);
113124
arguments.Authentication.Username = value;
114125
continue;
115126
}
116127

117128
if (IsSwitch("p", name))
118129
{
130+
EnsureArgumentValueCount(values);
119131
arguments.Authentication.Password = value;
120132
continue;
121133
}
122134

123135
if (IsSwitch("c", name))
124136
{
137+
EnsureArgumentValueCount(values);
125138
arguments.CommitId = value;
126139
continue;
127140
}
128141

129142
if (IsSwitch("exec", name))
130143
{
144+
EnsureArgumentValueCount(values);
131145
arguments.Exec = value;
132146
continue;
133147
}
134148

135149
if (IsSwitch("execargs", name))
136150
{
151+
EnsureArgumentValueCount(values);
137152
arguments.ExecArgs = value;
138153
continue;
139154
}
140155

141156
if (IsSwitch("proj", name))
142157
{
158+
EnsureArgumentValueCount(values);
143159
arguments.Proj = value;
144160
continue;
145161
}
146162

147163
if (IsSwitch("projargs", name))
148164
{
165+
EnsureArgumentValueCount(values);
149166
arguments.ProjArgs = value;
150167
continue;
151168
}
@@ -160,15 +177,26 @@ public static Arguments ParseArguments(List<string> commandLineArguments)
160177
{
161178
arguments.UpdateAssemblyInfo = false;
162179
}
180+
else if (values != null && values.Length > 1)
181+
{
182+
arguments.UpdateAssemblyInfo = true;
183+
foreach(var v in values) arguments.AddAssemblyInfoFileName(v);
184+
}
163185
else if (!IsSwitchArgument(value))
164186
{
165187
arguments.UpdateAssemblyInfo = true;
166-
arguments.UpdateAssemblyInfoFileName = value;
188+
arguments.AddAssemblyInfoFileName(value);
167189
}
168190
else
169191
{
170192
arguments.UpdateAssemblyInfo = true;
171193
}
194+
195+
if (arguments.UpdateAssemblyInfoFileName.Count > 1 && arguments.EnsureAssemblyInfo)
196+
{
197+
throw new WarningException("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");
198+
}
199+
172200
continue;
173201
}
174202

@@ -246,7 +274,12 @@ public static Arguments ParseArguments(List<string> commandLineArguments)
246274
{
247275
arguments.EnsureAssemblyInfo = true;
248276
}
249-
277+
278+
if (arguments.UpdateAssemblyInfoFileName.Count > 1 && arguments.EnsureAssemblyInfo)
279+
{
280+
throw new WarningException("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");
281+
}
282+
250283
continue;
251284
}
252285

src/GitVersionExe/Arguments.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
namespace GitVersion
22
{
3+
using System.Collections.Generic;
4+
35
public class Arguments
46
{
57
public Arguments()
68
{
79
Authentication = new Authentication();
810
Output = OutputType.Json;
11+
UpdateAssemblyInfoFileName = new HashSet<string>();
912
}
1013

1114
public Authentication Authentication;
@@ -31,10 +34,15 @@ public Arguments()
3134
public string ExecArgs;
3235

3336
public bool UpdateAssemblyInfo;
34-
public string UpdateAssemblyInfoFileName;
37+
public ISet<string> UpdateAssemblyInfoFileName;
3538
public bool EnsureAssemblyInfo;
3639

3740
public bool ShowConfig;
3841
public bool NoFetch;
42+
43+
public void AddAssemblyInfoFileName(string fileName)
44+
{
45+
UpdateAssemblyInfoFileName.Add(fileName);
46+
}
3947
}
4048
}

src/GitVersionExe/AssemblyInfoFileUpdate.cs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ public AssemblyInfoFileUpdate(Arguments args, string workingDirectory, VersionVa
2121

2222
var assemblyInfoFiles = GetAssemblyInfoFiles(workingDirectory, args, fileSystem);
2323

24-
foreach (var assemblyInfoFile in assemblyInfoFiles)
24+
foreach (var assemblyInfoFile in assemblyInfoFiles.Select(f => new FileInfo(f)))
2525
{
26-
var backupAssemblyInfo = assemblyInfoFile + ".bak";
27-
var localAssemblyInfo = assemblyInfoFile;
28-
fileSystem.Copy(assemblyInfoFile, backupAssemblyInfo, true);
26+
var backupAssemblyInfo = assemblyInfoFile.FullName + ".bak";
27+
var localAssemblyInfo = assemblyInfoFile.FullName;
28+
fileSystem.Copy(assemblyInfoFile.FullName, backupAssemblyInfo, true);
2929
restoreBackupTasks.Add(() =>
3030
{
3131
if (fileSystem.Exists(localAssemblyInfo))
@@ -37,32 +37,37 @@ public AssemblyInfoFileUpdate(Arguments args, string workingDirectory, VersionVa
3737
var assemblyVersion = variables.AssemblySemVer;
3838
var assemblyInfoVersion = variables.InformationalVersion;
3939
var assemblyFileVersion = variables.MajorMinorPatch + ".0";
40-
var fileContents = fileSystem.ReadAllText(assemblyInfoFile)
40+
var fileContents = fileSystem.ReadAllText(assemblyInfoFile.FullName)
4141
.RegexReplace(@"AssemblyVersion\(""[^""]*""\)", string.Format("AssemblyVersion(\"{0}\")", assemblyVersion))
4242
.RegexReplace(@"AssemblyInformationalVersion\(""[^""]*""\)", string.Format("AssemblyInformationalVersion(\"{0}\")", assemblyInfoVersion))
4343
.RegexReplace(@"AssemblyFileVersion\(""[^""]*""\)", string.Format("AssemblyFileVersion(\"{0}\")", assemblyFileVersion));
4444

45-
fileSystem.WriteAllText(assemblyInfoFile, fileContents);
45+
fileSystem.WriteAllText(assemblyInfoFile.FullName, fileContents);
4646
}
4747
}
4848

4949
static IEnumerable<string> GetAssemblyInfoFiles(string workingDirectory, Arguments args, IFileSystem fileSystem)
5050
{
51-
if (args.UpdateAssemblyInfoFileName != null)
51+
if (args.UpdateAssemblyInfoFileName != null && args.UpdateAssemblyInfoFileName.Any())
5252
{
53-
var fullPath = Path.Combine(workingDirectory, args.UpdateAssemblyInfoFileName);
54-
55-
if (EnsureVersionAssemblyInfoFile(args, fileSystem, fullPath))
53+
foreach (var item in args.UpdateAssemblyInfoFileName)
5654
{
57-
return new[]
55+
var fullPath = Path.Combine(workingDirectory, item);
56+
57+
if (EnsureVersionAssemblyInfoFile(args, fileSystem, fullPath))
5858
{
59-
fullPath
60-
};
59+
yield return fullPath;
60+
}
61+
}
62+
}
63+
else
64+
{
65+
foreach (var item in fileSystem.DirectoryGetFiles(workingDirectory, "AssemblyInfo.*", SearchOption.AllDirectories)
66+
.Where(f => f.EndsWith(".cs", StringComparison.OrdinalIgnoreCase) || f.EndsWith(".vb", StringComparison.OrdinalIgnoreCase)))
67+
{
68+
yield return item;
6169
}
6270
}
63-
64-
return fileSystem.DirectoryGetFiles(workingDirectory, "AssemblyInfo.*", SearchOption.AllDirectories)
65-
.Where(f => f.EndsWith(".cs", StringComparison.OrdinalIgnoreCase) || f.EndsWith(".vb", StringComparison.OrdinalIgnoreCase));
6671
}
6772

6873
static bool EnsureVersionAssemblyInfoFile(Arguments arguments, IFileSystem fileSystem, string fullPath)
@@ -74,6 +79,11 @@ static bool EnsureVersionAssemblyInfoFile(Arguments arguments, IFileSystem fileS
7479
var assemblyInfoSource = AssemblyVersionInfoTemplates.GetAssemblyInfoTemplateFor(fullPath);
7580
if (!string.IsNullOrWhiteSpace(assemblyInfoSource))
7681
{
82+
var fileInfo = new FileInfo(fullPath);
83+
if (!fileSystem.DirectoryExists(fileInfo.Directory.FullName))
84+
{
85+
fileSystem.CreateDirectory(fileInfo.Directory.FullName);
86+
}
7787
fileSystem.WriteAllText(fullPath, assemblyInfoSource);
7888
return true;
7989
}

0 commit comments

Comments
 (0)