Skip to content

Commit a39ea13

Browse files
authored
Merge branch 'master' into branch_regex_fix
2 parents 22686d1 + 9467801 commit a39ea13

5 files changed

+66
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[assembly: AssemblyVersion("2.3.1.0")]
2+
[assembly: AssemblyFileVersion("2.3.1.0")]
3+
[assembly: AssemblyInformationalVersion("2.3.1+3.Branch.foo.Sha.hash")]
4+
// comment
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[<assembly: AssemblyVersion("2.3.1.0")>]
2+
[<assembly: AssemblyFileVersion("2.3.1.0")>]
3+
[<assembly: AssemblyInformationalVersion("2.3.1+3.Branch.foo.Sha.hash")>]
4+
do
5+
()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<Assembly: AssemblyVersion("2.3.1.0")>
2+
<Assembly: AssemblyFileVersion("2.3.1.0")>
3+
<Assembly: AssemblyInformationalVersion("2.3.1+3.Branch.foo.Sha.hash")>
4+
' comment

src/GitVersionCore.Tests/AssemblyInfoFileUpdaterTests.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,30 @@ public void ShouldAddAssemblyInformationalVersionWhenUpdatingAssemblyVersionFile
423423
using (var assemblyInfoFileUpdater = new AssemblyInfoFileUpdater(assemblyInfoFile, workingDir, variables, fileSystem, false))
424424
{
425425
assemblyInfoFileUpdater.Update();
426-
426+
427+
assemblyFileContent = fileSystem.ReadAllText(fileName);
428+
assemblyFileContent.ShouldMatchApproved(c => c.SubFolder(Path.Combine("Approved", fileExtension)));
429+
}
430+
});
431+
}
432+
433+
[TestCase("cs", "[assembly: AssemblyVersion(\"1.0.0.0\")]\r\n[assembly: AssemblyFileVersion(\"1.0.0.0\")]\r\n// comment\r\n")]
434+
[TestCase("fs", "[<assembly: AssemblyVersion(\"1.0.0.0\")>]\r\n[<assembly: AssemblyFileVersion(\"1.0.0.0\")>]\r\ndo\r\n()\r\n")]
435+
[TestCase("vb", "<Assembly: AssemblyVersion(\"1.0.0.0\")>\r\n<Assembly: AssemblyFileVersion(\"1.0.0.0\")>\r\n' comment\r\n")]
436+
[Category("NoMono")]
437+
[Description("Won't run on Mono due to source information not being available for ShouldMatchApproved.")]
438+
public void Issue1183_ShouldAddFSharpAssemblyInformationalVersionBesideOtherAttributes(string fileExtension, string assemblyFileContent)
439+
{
440+
var workingDir = Path.GetTempPath();
441+
var assemblyInfoFile = "AssemblyInfo." + fileExtension;
442+
var fileName = Path.Combine(workingDir, assemblyInfoFile);
443+
444+
VerifyAssemblyInfoFile(assemblyFileContent, fileName, verify: (fileSystem, variables) =>
445+
{
446+
using (var assemblyInfoFileUpdater = new AssemblyInfoFileUpdater(assemblyInfoFile, workingDir, variables, fileSystem, false))
447+
{
448+
assemblyInfoFileUpdater.Update();
449+
427450
assemblyFileContent = fileSystem.ReadAllText(fileName);
428451
assemblyFileContent.ShouldMatchApproved(c => c.SubFolder(Path.Combine("Approved", fileExtension)));
429452
}

src/GitVersionCore/VersionAssemblyInfoResources/AssemblyInfoFileUpdater.cs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ public class AssemblyInfoFileUpdater : IDisposable
1212
readonly List<Action> restoreBackupTasks = new List<Action>();
1313
readonly List<Action> cleanupBackupTasks = new List<Action>();
1414

15+
readonly IDictionary<string, Regex> assemblyAttributeRegexes = new Dictionary<string, Regex>
16+
{
17+
{".cs", new Regex( @"(\s*\[\s*assembly:\s*(?:.*)\s*\]\s*$(\r?\n)?)", RegexOptions.Multiline) },
18+
{".fs", new Regex( @"(\s*\[\s*\<assembly:\s*(?:.*)\>\s*\]\s*$(\r?\n)?)", RegexOptions.Multiline) },
19+
{".vb", new Regex( @"(\s*\<Assembly:\s*(?:.*)\>\s*$(\r?\n)?)", RegexOptions.Multiline) },
20+
};
21+
1522
ISet<string> assemblyInfoFileNames;
1623
string workingDirectory;
1724
VersionVariables variables;
@@ -20,7 +27,7 @@ public class AssemblyInfoFileUpdater : IDisposable
2027
TemplateManager templateManager;
2128

2229
public AssemblyInfoFileUpdater(string assemblyInfoFileName, string workingDirectory, VersionVariables variables, IFileSystem fileSystem, bool ensureAssemblyInfo) :
23-
this(new HashSet<string> { assemblyInfoFileName }, workingDirectory, variables, fileSystem, ensureAssemblyInfo)
30+
this(new HashSet<string> { assemblyInfoFileName }, workingDirectory, variables, fileSystem, ensureAssemblyInfo)
2431
{ }
2532

2633
public AssemblyInfoFileUpdater(ISet<string> assemblyInfoFileNames, string workingDirectory, VersionVariables variables, IFileSystem fileSystem, bool ensureAssemblyInfo)
@@ -77,15 +84,15 @@ public void Update()
7784

7885
if (!string.IsNullOrWhiteSpace(assemblyVersion))
7986
{
80-
fileContents = ReplaceOrAppend(assemblyVersionRegex, fileContents, assemblyVersionString, assemblyInfoFile.Extension, ref appendedAttributes);
87+
fileContents = ReplaceOrInsertAfterLastAssemblyAttributeOrAppend(assemblyVersionRegex, fileContents, assemblyVersionString, assemblyInfoFile.Extension, ref appendedAttributes);
8188
}
8289

8390
if (!string.IsNullOrWhiteSpace(assemblyFileVersion))
8491
{
85-
fileContents = ReplaceOrAppend(assemblyFileVersionRegex, fileContents, assemblyFileVersionString, assemblyInfoFile.Extension, ref appendedAttributes);
92+
fileContents = ReplaceOrInsertAfterLastAssemblyAttributeOrAppend(assemblyFileVersionRegex, fileContents, assemblyFileVersionString, assemblyInfoFile.Extension, ref appendedAttributes);
8693
}
8794

88-
fileContents = ReplaceOrAppend(assemblyInfoVersionRegex, fileContents, assemblyInfoVersionString, assemblyInfoFile.Extension, ref appendedAttributes);
95+
fileContents = ReplaceOrInsertAfterLastAssemblyAttributeOrAppend(assemblyInfoVersionRegex, fileContents, assemblyInfoVersionString, assemblyInfoFile.Extension, ref appendedAttributes);
8996

9097
if (appendedAttributes)
9198
{
@@ -100,20 +107,32 @@ public void Update()
100107
}
101108
}
102109

103-
string ReplaceOrAppend(Regex replaceRegex, string inputString, string replaceString, string fileExtension, ref bool appendedAttributes)
110+
string ReplaceOrInsertAfterLastAssemblyAttributeOrAppend(Regex replaceRegex, string inputString, string replaceString, string fileExtension, ref bool appendedAttributes)
104111
{
105112
var assemblyAddFormat = templateManager.GetAddFormatFor(fileExtension);
106113

107114
if (replaceRegex.IsMatch(inputString))
108115
{
109-
inputString = replaceRegex.Replace(inputString, replaceString);
116+
return replaceRegex.Replace(inputString, replaceString);
110117
}
111-
else
118+
119+
Regex assemblyRegex;
120+
if (assemblyAttributeRegexes.TryGetValue(fileExtension, out assemblyRegex))
112121
{
113-
inputString += Environment.NewLine + string.Format(assemblyAddFormat, replaceString);
114-
appendedAttributes = true;
122+
var assemblyMatches = assemblyRegex.Matches(inputString);
123+
if (assemblyMatches.Count > 0)
124+
{
125+
var lastMatch = assemblyMatches[assemblyMatches.Count - 1];
126+
var replacementString = lastMatch.Value;
127+
if (!lastMatch.Value.EndsWith(Environment.NewLine)) replacementString += Environment.NewLine;
128+
replacementString += string.Format(assemblyAddFormat, replaceString);
129+
replacementString += Environment.NewLine;
130+
return inputString.Replace(lastMatch.Value, replacementString);
131+
}
115132
}
116-
133+
134+
inputString += Environment.NewLine + string.Format(assemblyAddFormat, replaceString);
135+
appendedAttributes = true;
117136
return inputString;
118137
}
119138

0 commit comments

Comments
 (0)