diff --git a/src/GitVersionCore.Tests/Approved/cs/AssemblyInfoFileUpdaterTests.Issue1183_ShouldAddFSharpAssemblyInformationalVersionBesideOtherAttributes.approved.txt b/src/GitVersionCore.Tests/Approved/cs/AssemblyInfoFileUpdaterTests.Issue1183_ShouldAddFSharpAssemblyInformationalVersionBesideOtherAttributes.approved.txt new file mode 100644 index 0000000000..344e5a6b41 --- /dev/null +++ b/src/GitVersionCore.Tests/Approved/cs/AssemblyInfoFileUpdaterTests.Issue1183_ShouldAddFSharpAssemblyInformationalVersionBesideOtherAttributes.approved.txt @@ -0,0 +1,4 @@ +[assembly: AssemblyVersion("2.3.1.0")] +[assembly: AssemblyFileVersion("2.3.1.0")] +[assembly: AssemblyInformationalVersion("2.3.1+3.Branch.foo.Sha.hash")] +// comment diff --git a/src/GitVersionCore.Tests/Approved/fs/AssemblyInfoFileUpdaterTests.Issue1183_ShouldAddFSharpAssemblyInformationalVersionBesideOtherAttributes.approved.txt b/src/GitVersionCore.Tests/Approved/fs/AssemblyInfoFileUpdaterTests.Issue1183_ShouldAddFSharpAssemblyInformationalVersionBesideOtherAttributes.approved.txt new file mode 100644 index 0000000000..df0a35a6f8 --- /dev/null +++ b/src/GitVersionCore.Tests/Approved/fs/AssemblyInfoFileUpdaterTests.Issue1183_ShouldAddFSharpAssemblyInformationalVersionBesideOtherAttributes.approved.txt @@ -0,0 +1,5 @@ +[] +[] +[] +do +() diff --git a/src/GitVersionCore.Tests/Approved/vb/AssemblyInfoFileUpdaterTests.Issue1183_ShouldAddFSharpAssemblyInformationalVersionBesideOtherAttributes.approved.txt b/src/GitVersionCore.Tests/Approved/vb/AssemblyInfoFileUpdaterTests.Issue1183_ShouldAddFSharpAssemblyInformationalVersionBesideOtherAttributes.approved.txt new file mode 100644 index 0000000000..45fdd1b2bc --- /dev/null +++ b/src/GitVersionCore.Tests/Approved/vb/AssemblyInfoFileUpdaterTests.Issue1183_ShouldAddFSharpAssemblyInformationalVersionBesideOtherAttributes.approved.txt @@ -0,0 +1,4 @@ + + + +' comment diff --git a/src/GitVersionCore.Tests/AssemblyInfoFileUpdaterTests.cs b/src/GitVersionCore.Tests/AssemblyInfoFileUpdaterTests.cs index f78515c563..6a64b01123 100644 --- a/src/GitVersionCore.Tests/AssemblyInfoFileUpdaterTests.cs +++ b/src/GitVersionCore.Tests/AssemblyInfoFileUpdaterTests.cs @@ -423,7 +423,30 @@ public void ShouldAddAssemblyInformationalVersionWhenUpdatingAssemblyVersionFile using (var assemblyInfoFileUpdater = new AssemblyInfoFileUpdater(assemblyInfoFile, workingDir, variables, fileSystem, false)) { assemblyInfoFileUpdater.Update(); - + + assemblyFileContent = fileSystem.ReadAllText(fileName); + assemblyFileContent.ShouldMatchApproved(c => c.SubFolder(Path.Combine("Approved", fileExtension))); + } + }); + } + + [TestCase("cs", "[assembly: AssemblyVersion(\"1.0.0.0\")]\r\n[assembly: AssemblyFileVersion(\"1.0.0.0\")]\r\n// comment\r\n")] + [TestCase("fs", "[]\r\n[]\r\ndo\r\n()\r\n")] + [TestCase("vb", "\r\n\r\n' comment\r\n")] + [Category("NoMono")] + [Description("Won't run on Mono due to source information not being available for ShouldMatchApproved.")] + public void Issue1183_ShouldAddFSharpAssemblyInformationalVersionBesideOtherAttributes(string fileExtension, string assemblyFileContent) + { + var workingDir = Path.GetTempPath(); + var assemblyInfoFile = "AssemblyInfo." + fileExtension; + var fileName = Path.Combine(workingDir, assemblyInfoFile); + + VerifyAssemblyInfoFile(assemblyFileContent, fileName, verify: (fileSystem, variables) => + { + using (var assemblyInfoFileUpdater = new AssemblyInfoFileUpdater(assemblyInfoFile, workingDir, variables, fileSystem, false)) + { + assemblyInfoFileUpdater.Update(); + assemblyFileContent = fileSystem.ReadAllText(fileName); assemblyFileContent.ShouldMatchApproved(c => c.SubFolder(Path.Combine("Approved", fileExtension))); } diff --git a/src/GitVersionCore/VersionAssemblyInfoResources/AssemblyInfoFileUpdater.cs b/src/GitVersionCore/VersionAssemblyInfoResources/AssemblyInfoFileUpdater.cs index 5e1456ace1..d217de3d62 100644 --- a/src/GitVersionCore/VersionAssemblyInfoResources/AssemblyInfoFileUpdater.cs +++ b/src/GitVersionCore/VersionAssemblyInfoResources/AssemblyInfoFileUpdater.cs @@ -12,6 +12,13 @@ public class AssemblyInfoFileUpdater : IDisposable readonly List restoreBackupTasks = new List(); readonly List cleanupBackupTasks = new List(); + readonly IDictionary assemblyAttributeRegexes = new Dictionary + { + {".cs", new Regex( @"(\s*\[\s*assembly:\s*(?:.*)\s*\]\s*$(\r?\n)?)", RegexOptions.Multiline) }, + {".fs", new Regex( @"(\s*\[\s*\\s*\]\s*$(\r?\n)?)", RegexOptions.Multiline) }, + {".vb", new Regex( @"(\s*\\s*$(\r?\n)?)", RegexOptions.Multiline) }, + }; + ISet assemblyInfoFileNames; string workingDirectory; VersionVariables variables; @@ -20,7 +27,7 @@ public class AssemblyInfoFileUpdater : IDisposable TemplateManager templateManager; public AssemblyInfoFileUpdater(string assemblyInfoFileName, string workingDirectory, VersionVariables variables, IFileSystem fileSystem, bool ensureAssemblyInfo) : - this(new HashSet { assemblyInfoFileName }, workingDirectory, variables, fileSystem, ensureAssemblyInfo) + this(new HashSet { assemblyInfoFileName }, workingDirectory, variables, fileSystem, ensureAssemblyInfo) { } public AssemblyInfoFileUpdater(ISet assemblyInfoFileNames, string workingDirectory, VersionVariables variables, IFileSystem fileSystem, bool ensureAssemblyInfo) @@ -77,15 +84,15 @@ public void Update() if (!string.IsNullOrWhiteSpace(assemblyVersion)) { - fileContents = ReplaceOrAppend(assemblyVersionRegex, fileContents, assemblyVersionString, assemblyInfoFile.Extension, ref appendedAttributes); + fileContents = ReplaceOrInsertAfterLastAssemblyAttributeOrAppend(assemblyVersionRegex, fileContents, assemblyVersionString, assemblyInfoFile.Extension, ref appendedAttributes); } if (!string.IsNullOrWhiteSpace(assemblyFileVersion)) { - fileContents = ReplaceOrAppend(assemblyFileVersionRegex, fileContents, assemblyFileVersionString, assemblyInfoFile.Extension, ref appendedAttributes); + fileContents = ReplaceOrInsertAfterLastAssemblyAttributeOrAppend(assemblyFileVersionRegex, fileContents, assemblyFileVersionString, assemblyInfoFile.Extension, ref appendedAttributes); } - fileContents = ReplaceOrAppend(assemblyInfoVersionRegex, fileContents, assemblyInfoVersionString, assemblyInfoFile.Extension, ref appendedAttributes); + fileContents = ReplaceOrInsertAfterLastAssemblyAttributeOrAppend(assemblyInfoVersionRegex, fileContents, assemblyInfoVersionString, assemblyInfoFile.Extension, ref appendedAttributes); if (appendedAttributes) { @@ -100,20 +107,32 @@ public void Update() } } - string ReplaceOrAppend(Regex replaceRegex, string inputString, string replaceString, string fileExtension, ref bool appendedAttributes) + string ReplaceOrInsertAfterLastAssemblyAttributeOrAppend(Regex replaceRegex, string inputString, string replaceString, string fileExtension, ref bool appendedAttributes) { var assemblyAddFormat = templateManager.GetAddFormatFor(fileExtension); if (replaceRegex.IsMatch(inputString)) { - inputString = replaceRegex.Replace(inputString, replaceString); + return replaceRegex.Replace(inputString, replaceString); } - else + + Regex assemblyRegex; + if (assemblyAttributeRegexes.TryGetValue(fileExtension, out assemblyRegex)) { - inputString += Environment.NewLine + string.Format(assemblyAddFormat, replaceString); - appendedAttributes = true; + var assemblyMatches = assemblyRegex.Matches(inputString); + if (assemblyMatches.Count > 0) + { + var lastMatch = assemblyMatches[assemblyMatches.Count - 1]; + var replacementString = lastMatch.Value; + if (!lastMatch.Value.EndsWith(Environment.NewLine)) replacementString += Environment.NewLine; + replacementString += string.Format(assemblyAddFormat, replaceString); + replacementString += Environment.NewLine; + return inputString.Replace(lastMatch.Value, replacementString); + } } - + + inputString += Environment.NewLine + string.Format(assemblyAddFormat, replaceString); + appendedAttributes = true; return inputString; }