Skip to content

GitVersion produces incompilable code if AssemblyInformationalVersion not defined for F# #1553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[<assembly: AssemblyVersion("2.3.1.0")>]
[<assembly: AssemblyFileVersion("2.3.1.0")>]
[<assembly: AssemblyInformationalVersion("2.3.1+3.Branch.foo.Sha.hash")>]
do
()
Original file line number Diff line number Diff line change
@@ -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
25 changes: 24 additions & 1 deletion src/GitVersionCore.Tests/AssemblyInfoFileUpdaterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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", "[<assembly: AssemblyVersion(\"1.0.0.0\")>]\r\n[<assembly: AssemblyFileVersion(\"1.0.0.0\")>]\r\ndo\r\n()\r\n")]
[TestCase("vb", "<Assembly: AssemblyVersion(\"1.0.0.0\")>\r\n<Assembly: AssemblyFileVersion(\"1.0.0.0\")>\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)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ public class AssemblyInfoFileUpdater : IDisposable
readonly List<Action> restoreBackupTasks = new List<Action>();
readonly List<Action> cleanupBackupTasks = new List<Action>();

readonly IDictionary<string, Regex> assemblyAttributeRegexes = new Dictionary<string, Regex>
{
{".cs", new Regex( @"(\s*\[\s*assembly:\s*(?:.*)\s*\]\s*$(\r?\n)?)", RegexOptions.Multiline) },
{".fs", new Regex( @"(\s*\[\s*\<assembly:\s*(?:.*)\>\s*\]\s*$(\r?\n)?)", RegexOptions.Multiline) },
{".vb", new Regex( @"(\s*\<Assembly:\s*(?:.*)\>\s*$(\r?\n)?)", RegexOptions.Multiline) },
};

ISet<string> assemblyInfoFileNames;
string workingDirectory;
VersionVariables variables;
Expand All @@ -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<string> { assemblyInfoFileName }, workingDirectory, variables, fileSystem, ensureAssemblyInfo)
this(new HashSet<string> { assemblyInfoFileName }, workingDirectory, variables, fileSystem, ensureAssemblyInfo)
{ }

public AssemblyInfoFileUpdater(ISet<string> assemblyInfoFileNames, string workingDirectory, VersionVariables variables, IFileSystem fileSystem, bool ensureAssemblyInfo)
Expand Down Expand Up @@ -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)
{
Expand All @@ -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;
}

Expand Down