From 4bf8d6a5e2cd7b0f93e3f41d7858d32d86030ec8 Mon Sep 17 00:00:00 2001 From: Aaron Sherber Date: Fri, 23 Dec 2016 16:40:58 -0500 Subject: [PATCH 1/8] Add more tokens to README --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index f9fdfe8..cdf7630 100644 --- a/README.md +++ b/README.md @@ -31,10 +31,14 @@ The tokens are: - `%version2%` is replaced with the major and minor version (1.0) - `%version3%` is replaced with the major, minor, and revision version (1.0.0) - `%version4%` is replaced with the major, minor, revision, and build version (1.0.0.0) +- `%now%` is replaced with the current short date +- `%utcnow%` is replaced with the current utc short date - `%githash%` is replaced with the SHA1 hash of the branch tip of the repository - `%shorthash%` is replaced with the first eight characters of %githash% - `%branch%` is replaced with the branch name of the repository - `%haschanges%` is replaced with the string defined in the ChangeString attribute in the configuration, see below. +- `%user%` is replaced with the current user name +- `%machinename%` is replaced with the current machine name > NOTE: if you already have an AssemblyInformationalVersion attribute and it doesn't use replacement tokens, it will not be modified at all. From 58635c2056e3d0698c31dd8efd081e30b5cf3965 Mon Sep 17 00:00:00 2001 From: Aaron Sherber Date: Fri, 23 Dec 2016 16:57:01 -0500 Subject: [PATCH 2/8] Refactor a little --- Fody/Configuration.cs | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/Fody/Configuration.cs b/Fody/Configuration.cs index f5c693f..61e1882 100644 --- a/Fody/Configuration.cs +++ b/Fody/Configuration.cs @@ -16,20 +16,27 @@ public Configuration(XElement config) var attr = config.Attribute("UseProjectGit"); if (attr != null) { - try - { - UseProject = Convert.ToBoolean(attr.Value); - } - catch (Exception) - { - throw new WeavingException($"Unable to parse '{attr.Value}' as a boolean, please use true or false."); - } + UseProject = ConvertAndThrowIfNotBoolean(attr.Value); } attr = config.Attribute("ChangeString"); - if (!string.IsNullOrWhiteSpace(attr?.Value)) + if (HasValue(attr)) { - ChangeString = config.Attribute("ChangeString").Value; + ChangeString = attr.Value; } } + + private bool HasValue(XAttribute attr) + { + return !String.IsNullOrWhiteSpace(attr?.Value); + } + + private bool ConvertAndThrowIfNotBoolean(string value) + { + bool result; + if (Boolean.TryParse(value, out result)) + return result; + else + throw new WeavingException($"Unable to parse '{value}' as a boolean; please use 'true' or 'false'."); + } } From 26c6fddc6cc3138264d8acb96c43fdf07b598af5 Mon Sep 17 00:00:00 2001 From: Aaron Sherber Date: Fri, 23 Dec 2016 16:59:21 -0500 Subject: [PATCH 3/8] Add Configuration.UseAssemblyFileVersion --- Fody/Configuration.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Fody/Configuration.cs b/Fody/Configuration.cs index 61e1882..33a8b10 100644 --- a/Fody/Configuration.cs +++ b/Fody/Configuration.cs @@ -3,7 +3,8 @@ public class Configuration { - public bool UseProject; + public bool UseProject = false; + public bool UseAssemblyFileVersion = false; public string ChangeString = "HasChanges"; public Configuration(XElement config) @@ -19,6 +20,12 @@ public Configuration(XElement config) UseProject = ConvertAndThrowIfNotBoolean(attr.Value); } + attr = config.Attribute("UseAssemblyFileVersion"); + if (HasValue(attr)) + { + UseAssemblyFileVersion = ConvertAndThrowIfNotBoolean(attr.Value); + } + attr = config.Attribute("ChangeString"); if (HasValue(attr)) { From 8be5aa150f28407fffa31c1f9ccd21927ef2d27e Mon Sep 17 00:00:00 2001 From: Aaron Sherber Date: Sat, 24 Dec 2016 10:05:00 -0500 Subject: [PATCH 4/8] Use AssemblyFileVersion to format AssemblyInformationalVersion if UseFileVersion is true --- Fody/Configuration.cs | 16 +++++--- Fody/FormatStringTokenResolver.cs | 13 +++---- Fody/ModuleWeaver.cs | 63 ++++++++++++++++++++++--------- Tests/Tests.csproj | 3 ++ 4 files changed, 64 insertions(+), 31 deletions(-) diff --git a/Fody/Configuration.cs b/Fody/Configuration.cs index 33a8b10..b5877d1 100644 --- a/Fody/Configuration.cs +++ b/Fody/Configuration.cs @@ -4,7 +4,7 @@ public class Configuration { public bool UseProject = false; - public bool UseAssemblyFileVersion = false; + public bool UseFileVersion = false; public string ChangeString = "HasChanges"; public Configuration(XElement config) @@ -20,10 +20,10 @@ public Configuration(XElement config) UseProject = ConvertAndThrowIfNotBoolean(attr.Value); } - attr = config.Attribute("UseAssemblyFileVersion"); + attr = config.Attribute("UseFileVersion"); if (HasValue(attr)) { - UseAssemblyFileVersion = ConvertAndThrowIfNotBoolean(attr.Value); + UseFileVersion = ConvertAndThrowIfNotBoolean(attr.Value); } attr = config.Attribute("ChangeString"); @@ -40,10 +40,14 @@ private bool HasValue(XAttribute attr) private bool ConvertAndThrowIfNotBoolean(string value) { - bool result; - if (Boolean.TryParse(value, out result)) + try + { + var result = Convert.ToBoolean(value); return result; - else + } + catch + { throw new WeavingException($"Unable to parse '{value}' as a boolean; please use 'true' or 'false'."); + } } } diff --git a/Fody/FormatStringTokenResolver.cs b/Fody/FormatStringTokenResolver.cs index bb1fa15..047d436 100644 --- a/Fody/FormatStringTokenResolver.cs +++ b/Fody/FormatStringTokenResolver.cs @@ -12,16 +12,15 @@ public class FormatStringTokenResolver static DateTime now = DateTime.Now; static DateTime utcNow = DateTime.UtcNow; - public string ReplaceTokens(string template, ModuleDefinition moduleDefinition, Repository repo, string changestring) + public string ReplaceTokens(string template, System.Version version, Repository repo, string changestring) { - var assemblyVersion = moduleDefinition.Assembly.Name.Version; var branch = repo.Head; - template = template.Replace("%version%", assemblyVersion.ToString()); - template = template.Replace("%version1%", assemblyVersion.ToString(1)); - template = template.Replace("%version2%", assemblyVersion.ToString(2)); - template = template.Replace("%version3%", assemblyVersion.ToString(3)); - template = template.Replace("%version4%", assemblyVersion.ToString(4)); + template = template.Replace("%version%", version.ToString()); + template = template.Replace("%version1%", version.ToString(1)); + template = template.Replace("%version2%", version.ToString(2)); + template = template.Replace("%version3%", version.ToString(3)); + template = template.Replace("%version4%", version.ToString(4)); template = template.Replace("%now%", now.ToShortDateString()); template = template.Replace("%utcnow%", utcNow.ToShortDateString()); diff --git a/Fody/ModuleWeaver.cs b/Fody/ModuleWeaver.cs index 92b43b2..9bdaa70 100644 --- a/Fody/ModuleWeaver.cs +++ b/Fody/ModuleWeaver.cs @@ -7,6 +7,8 @@ using Version = System.Version; using Fody.PeImage; using Fody.VersionResources; +using Mono.Collections.Generic; +using System.Collections.Generic; public class ModuleWeaver { @@ -21,9 +23,12 @@ public class ModuleWeaver static bool isPathSet; readonly FormatStringTokenResolver formatStringTokenResolver; string assemblyInfoVersion; - Version assemblyVersion; + Version versionToUse; bool dotGitDirExists; + const string INFO_ATTRIBUTE = "AssemblyInformationalVersionAttribute"; + const string FILE_ATTRIBUTE = "AssemblyFileAttribute"; + public ModuleWeaver() { LogInfo = s => { }; @@ -62,13 +67,16 @@ public void Execute() return; } - assemblyVersion = ModuleDefinition.Assembly.Name.Version; + if (!config.UseFileVersion) + versionToUse = ModuleDefinition.Assembly.Name.Version; + else + versionToUse = GetAssemblyFileVersion(customAttributes); - var customAttribute = customAttributes.FirstOrDefault(x => x.AttributeType.Name == "AssemblyInformationalVersionAttribute"); + var customAttribute = GetCustomAttribute(customAttributes, INFO_ATTRIBUTE); if (customAttribute != null) { assemblyInfoVersion = (string) customAttribute.ConstructorArguments[0].Value; - assemblyInfoVersion = formatStringTokenResolver.ReplaceTokens(assemblyInfoVersion, ModuleDefinition, repo, config.ChangeString); + assemblyInfoVersion = formatStringTokenResolver.ReplaceTokens(assemblyInfoVersion, versionToUse, repo, config.ChangeString); VerifyStartsWithVersion(assemblyInfoVersion); customAttribute.ConstructorArguments[0] = new CustomAttributeArgument(ModuleDefinition.TypeSystem.String, assemblyInfoVersion); } @@ -78,7 +86,7 @@ public void Execute() var constructor = ModuleDefinition.ImportReference(versionAttribute.Methods.First(x => x.IsConstructor)); customAttribute = new CustomAttribute(constructor); - assemblyInfoVersion = $"{assemblyVersion} Head:'{repo.Head.FriendlyName}' Sha:{branch.Tip.Sha}{(repo.IsClean() ? "" : " " + config.ChangeString)}"; + assemblyInfoVersion = $"{versionToUse} Head:'{repo.Head.FriendlyName}' Sha:{branch.Tip.Sha}{(repo.IsClean() ? "" : " " + config.ChangeString)}"; customAttribute.ConstructorArguments.Add(new CustomAttributeArgument(ModuleDefinition.TypeSystem.String, assemblyInfoVersion)); customAttributes.Add(customAttribute); @@ -86,6 +94,26 @@ public void Execute() } } + private CustomAttribute GetCustomAttribute(Collection attributes, string attributeName) + { + return attributes.FirstOrDefault(x => x.AttributeType.Name == attributeName); + } + + private Version GetAssemblyFileVersion(Collection customAttributes) + { + var afvAttribute = GetCustomAttribute(customAttributes, FILE_ATTRIBUTE); + if (afvAttribute != null) + { + var assemblyFileVersionString = (string)afvAttribute.ConstructorArguments[0].Value; + VerifyStartsWithVersion(assemblyFileVersionString); + return Version.Parse(assemblyFileVersionString); + } + else + { + throw new WeavingException("AssemblyFileVersion attribute could not be found."); + } + } + void VerifyStartsWithVersion(string versionString) { var prefix = new string(versionString.TakeWhile(x => char.IsDigit(x) || x == '.').ToArray()); @@ -137,13 +165,13 @@ static string GetProcessorArchitecture() TypeDefinition GetVersionAttribute() { var msCoreLib = ModuleDefinition.AssemblyResolver.Resolve("mscorlib"); - var msCoreAttribute = msCoreLib.MainModule.Types.FirstOrDefault(x => x.Name == "AssemblyInformationalVersionAttribute"); + var msCoreAttribute = msCoreLib.MainModule.Types.FirstOrDefault(x => x.Name == INFO_ATTRIBUTE); if (msCoreAttribute != null) { return msCoreAttribute; } var systemRuntime = ModuleDefinition.AssemblyResolver.Resolve("System.Runtime"); - return systemRuntime.MainModule.Types.First(x => x.Name == "AssemblyInformationalVersionAttribute"); + return systemRuntime.MainModule.Types.First(x => x.Name == INFO_ATTRIBUTE); } int? GetVerPatchWaitTimeout() @@ -187,21 +215,14 @@ public void AfterWeaving() var versions = reader.Read(); var fixedFileInfo = versions.FixedFileInfo.Value; - fixedFileInfo.FileVersion = assemblyVersion; - fixedFileInfo.ProductVersion = assemblyVersion; + fixedFileInfo.FileVersion = versionToUse; + fixedFileInfo.ProductVersion = versionToUse; versions.FixedFileInfo = fixedFileInfo; foreach (var stringTable in versions.StringFileInfo) { - if (stringTable.Values.ContainsKey("FileVersion")) - { - stringTable.Values["FileVersion"] = assemblyVersion.ToString(); - } - - if (stringTable.Values.ContainsKey("ProductVersion")) - { - stringTable.Values["ProductVersion"] = assemblyInfoVersion; - } + SetTableValue(stringTable.Values, "FileVersion", versionToUse.ToString()); + SetTableValue(stringTable.Values, "ProductVersion", assemblyInfoVersion); } versionStream.Position = 0; @@ -217,4 +238,10 @@ public void AfterWeaving() throw new WeavingException($"Failed to update the assembly information. {ex.Message}"); } } + + private void SetTableValue(Dictionary dict, string key, string value) + { + if (dict.ContainsKey(key)) + dict[key] = value; + } } \ No newline at end of file diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 15f4e03..aeb9bae 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -96,6 +96,9 @@ Fody + + + From 449db14338bb68210f4e2f141a8019a7d20ae9fb Mon Sep 17 00:00:00 2001 From: Aaron Sherber Date: Sat, 24 Dec 2016 10:28:12 -0500 Subject: [PATCH 5/8] Add OverwriteFileVersion in config (default true) --- Fody/Configuration.cs | 14 +++++++++++++- Fody/ModuleWeaver.cs | 20 ++++++++++++-------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/Fody/Configuration.cs b/Fody/Configuration.cs index b5877d1..b2a8c66 100644 --- a/Fody/Configuration.cs +++ b/Fody/Configuration.cs @@ -5,6 +5,7 @@ public class Configuration { public bool UseProject = false; public bool UseFileVersion = false; + public bool OverwriteFileVersion = true; public string ChangeString = "HasChanges"; public Configuration(XElement config) @@ -15,7 +16,7 @@ public Configuration(XElement config) } var attr = config.Attribute("UseProjectGit"); - if (attr != null) + if (HasValue(attr)) { UseProject = ConvertAndThrowIfNotBoolean(attr.Value); } @@ -31,6 +32,17 @@ public Configuration(XElement config) { ChangeString = attr.Value; } + + if (UseFileVersion) + OverwriteFileVersion = false; + else + { + attr = config.Attribute("OverwriteFileVersion"); + if (HasValue(attr)) + { + OverwriteFileVersion = ConvertAndThrowIfNotBoolean(attr.Value); + } + } } private bool HasValue(XAttribute attr) diff --git a/Fody/ModuleWeaver.cs b/Fody/ModuleWeaver.cs index 9bdaa70..7b91efd 100644 --- a/Fody/ModuleWeaver.cs +++ b/Fody/ModuleWeaver.cs @@ -26,6 +26,8 @@ public class ModuleWeaver Version versionToUse; bool dotGitDirExists; + Configuration _config; + const string INFO_ATTRIBUTE = "AssemblyInformationalVersionAttribute"; const string FILE_ATTRIBUTE = "AssemblyFileAttribute"; @@ -40,14 +42,14 @@ public void Execute() { SetSearchPath(); - var config = new Configuration(Config); + _config = new Configuration(Config); - LogInfo("Starting search for git repository in " + (config.UseProject ? "ProjectDir" : "SolutionDir")); + LogInfo("Starting search for git repository in " + (_config.UseProject ? "ProjectDir" : "SolutionDir")); var customAttributes = ModuleDefinition.Assembly.CustomAttributes; - var gitDir = Repository.Discover( config.UseProject ? ProjectDirectoryPath : SolutionDirectoryPath ); + var gitDir = Repository.Discover( _config.UseProject ? ProjectDirectoryPath : SolutionDirectoryPath ); if (gitDir == null) { LogWarning("No .git directory found."); @@ -67,7 +69,7 @@ public void Execute() return; } - if (!config.UseFileVersion) + if (!_config.UseFileVersion) versionToUse = ModuleDefinition.Assembly.Name.Version; else versionToUse = GetAssemblyFileVersion(customAttributes); @@ -76,7 +78,7 @@ public void Execute() if (customAttribute != null) { assemblyInfoVersion = (string) customAttribute.ConstructorArguments[0].Value; - assemblyInfoVersion = formatStringTokenResolver.ReplaceTokens(assemblyInfoVersion, versionToUse, repo, config.ChangeString); + assemblyInfoVersion = formatStringTokenResolver.ReplaceTokens(assemblyInfoVersion, versionToUse, repo, _config.ChangeString); VerifyStartsWithVersion(assemblyInfoVersion); customAttribute.ConstructorArguments[0] = new CustomAttributeArgument(ModuleDefinition.TypeSystem.String, assemblyInfoVersion); } @@ -86,7 +88,7 @@ public void Execute() var constructor = ModuleDefinition.ImportReference(versionAttribute.Methods.First(x => x.IsConstructor)); customAttribute = new CustomAttribute(constructor); - assemblyInfoVersion = $"{versionToUse} Head:'{repo.Head.FriendlyName}' Sha:{branch.Tip.Sha}{(repo.IsClean() ? "" : " " + config.ChangeString)}"; + assemblyInfoVersion = $"{versionToUse} Head:'{repo.Head.FriendlyName}' Sha:{branch.Tip.Sha}{(repo.IsClean() ? "" : " " + _config.ChangeString)}"; customAttribute.ConstructorArguments.Add(new CustomAttributeArgument(ModuleDefinition.TypeSystem.String, assemblyInfoVersion)); customAttributes.Add(customAttribute); @@ -215,13 +217,15 @@ public void AfterWeaving() var versions = reader.Read(); var fixedFileInfo = versions.FixedFileInfo.Value; - fixedFileInfo.FileVersion = versionToUse; + if (_config.OverwriteFileVersion) + fixedFileInfo.FileVersion = versionToUse; fixedFileInfo.ProductVersion = versionToUse; versions.FixedFileInfo = fixedFileInfo; foreach (var stringTable in versions.StringFileInfo) { - SetTableValue(stringTable.Values, "FileVersion", versionToUse.ToString()); + if (_config.OverwriteFileVersion) + SetTableValue(stringTable.Values, "FileVersion", versionToUse.ToString()); SetTableValue(stringTable.Values, "ProductVersion", assemblyInfoVersion); } From 5d2517f62146cd9016b3f8686633012ab328233a Mon Sep 17 00:00:00 2001 From: Aaron Sherber Date: Sat, 24 Dec 2016 11:10:08 -0500 Subject: [PATCH 6/8] Refactor and fix tests --- AssemblyToProcess/AssemblyInfo.cs | 1 + .../AssemblyInfo.cs | 1 + Fody/ModuleWeaver.cs | 2 +- Tests/TaskTests.cs | 19 ++++++----- Tests/TokenResolverTests.cs | 33 ++++++++++--------- 5 files changed, 31 insertions(+), 25 deletions(-) diff --git a/AssemblyToProcess/AssemblyInfo.cs b/AssemblyToProcess/AssemblyInfo.cs index cecf80a..a7ff42b 100644 --- a/AssemblyToProcess/AssemblyInfo.cs +++ b/AssemblyToProcess/AssemblyInfo.cs @@ -3,4 +3,5 @@ [assembly: AssemblyTitle("AssemblyToProcess")] [assembly: AssemblyProduct("AssemblyToProcess")] [assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("4.5.6.7")] //[assembly: AssemblyInformationalVersionAttribute("1.0.0.0/aString")] diff --git a/AssemblyToProcessExistingAttribute/AssemblyInfo.cs b/AssemblyToProcessExistingAttribute/AssemblyInfo.cs index c13160f..9f1a935 100644 --- a/AssemblyToProcessExistingAttribute/AssemblyInfo.cs +++ b/AssemblyToProcessExistingAttribute/AssemblyInfo.cs @@ -3,4 +3,5 @@ [assembly: AssemblyTitle("AssemblyToProcessExistingAttribute")] [assembly: AssemblyProduct("AssemblyToProcessExistingAttribute")] [assembly: AssemblyVersion("1.0.0")] +[assembly: AssemblyFileVersion("4.5.6.7")] [assembly: AssemblyInformationalVersionAttribute("%version3%+%branch%.%githash% %haschanges% %utcnow% %now:yyMMdd%")] diff --git a/Fody/ModuleWeaver.cs b/Fody/ModuleWeaver.cs index 7b91efd..c5b3211 100644 --- a/Fody/ModuleWeaver.cs +++ b/Fody/ModuleWeaver.cs @@ -29,7 +29,7 @@ public class ModuleWeaver Configuration _config; const string INFO_ATTRIBUTE = "AssemblyInformationalVersionAttribute"; - const string FILE_ATTRIBUTE = "AssemblyFileAttribute"; + const string FILE_ATTRIBUTE = "AssemblyFileVersionAttribute"; public ModuleWeaver() { diff --git a/Tests/TaskTests.cs b/Tests/TaskTests.cs index 3bf8bb3..85ca38f 100644 --- a/Tests/TaskTests.cs +++ b/Tests/TaskTests.cs @@ -4,6 +4,7 @@ using System.Reflection; using Mono.Cecil; using NUnit.Framework; +using System.Xml.Linq; [TestFixture] public class TaskTests @@ -12,8 +13,10 @@ public class TaskTests // ReSharper disable once PrivateFieldCanBeConvertedToLocalVariable string beforeAssemblyPath; string afterAssemblyPath; + protected XElement config = null; - public TaskTests() + [OneTimeSetUp] + public void Setup() { beforeAssemblyPath = Path.GetFullPath(Path.Combine(TestContext.CurrentContext.TestDirectory, @"..\..\..\AssemblyToProcess\bin\Debug\AssemblyToProcess.dll")); #if (!DEBUG) @@ -26,12 +29,13 @@ public TaskTests() var moduleDefinition = ModuleDefinition.ReadModule(afterAssemblyPath); var currentDirectory = AssemblyLocation.CurrentDirectory(); var weavingTask = new ModuleWeaver - { - ModuleDefinition = moduleDefinition, - AddinDirectoryPath = currentDirectory, - SolutionDirectoryPath = currentDirectory, - AssemblyFilePath = afterAssemblyPath, - }; + { + ModuleDefinition = moduleDefinition, + AddinDirectoryPath = currentDirectory, + SolutionDirectoryPath = currentDirectory, + AssemblyFilePath = afterAssemblyPath, + Config = config + }; weavingTask.Execute(); moduleDefinition.Write(afterAssemblyPath); @@ -40,7 +44,6 @@ public TaskTests() assembly = Assembly.LoadFile(afterAssemblyPath); } - [Test] public void EnsureAttributeExists() { diff --git a/Tests/TokenResolverTests.cs b/Tests/TokenResolverTests.cs index 0512bb1..d29695a 100644 --- a/Tests/TokenResolverTests.cs +++ b/Tests/TokenResolverTests.cs @@ -8,18 +8,19 @@ [TestFixture] public class TokenResolverTests { - ModuleDefinition moduleDefinition; + System.Version version; FormatStringTokenResolver resolver; string beforeAssemblyPath; - [TestFixtureSetUp] + [OneTimeSetUp] public void FixtureSetUp() { beforeAssemblyPath = Path.GetFullPath(Path.Combine(TestContext.CurrentContext.TestDirectory, @"..\..\..\AssemblyToProcess\bin\Debug\AssemblyToProcess.dll")); #if (!DEBUG) beforeAssemblyPath = beforeAssemblyPath.Replace("Debug", "Release"); #endif - moduleDefinition = ModuleDefinition.ReadModule(beforeAssemblyPath); + var moduleDefinition = ModuleDefinition.ReadModule(beforeAssemblyPath); + version = moduleDefinition.Assembly.Name.Version; resolver = new FormatStringTokenResolver(); } @@ -37,7 +38,7 @@ public void Replace_version() { DoWithCurrentRepo(repo => { - var result = resolver.ReplaceTokens("%version%", moduleDefinition, repo, ""); + var result = resolver.ReplaceTokens("%version%", version, repo, ""); Assert.AreEqual("1.0.0.0", result); }); @@ -48,7 +49,7 @@ public void Replace_version1() { DoWithCurrentRepo(repo => { - var result = resolver.ReplaceTokens("%version1%", moduleDefinition, repo, ""); + var result = resolver.ReplaceTokens("%version1%", version, repo, ""); Assert.AreEqual("1", result); }); @@ -59,7 +60,7 @@ public void Replace_version2() { DoWithCurrentRepo(repo => { - var result = resolver.ReplaceTokens("%version2%", moduleDefinition, repo, ""); + var result = resolver.ReplaceTokens("%version2%", version, repo, ""); Assert.AreEqual("1.0", result); }); @@ -70,7 +71,7 @@ public void Replace_version3() { DoWithCurrentRepo(repo => { - var result = resolver.ReplaceTokens("%version3%", moduleDefinition, repo, ""); + var result = resolver.ReplaceTokens("%version3%", version, repo, ""); Assert.AreEqual("1.0.0", result); }); @@ -81,7 +82,7 @@ public void Replace_version4() { DoWithCurrentRepo(repo => { - var result = resolver.ReplaceTokens("%version4%", moduleDefinition, repo, ""); + var result = resolver.ReplaceTokens("%version4%", version, repo, ""); Assert.AreEqual("1.0.0.0", result); }); @@ -94,7 +95,7 @@ public void Replace_branch() { var branchName = repo.Head.Name; - var result = resolver.ReplaceTokens("%branch%", moduleDefinition, repo, ""); + var result = resolver.ReplaceTokens("%branch%", version, repo, ""); Assert.AreEqual(branchName, result); }); @@ -107,7 +108,7 @@ public void Replace_githash() { var sha = repo.Head.Tip.Sha; - var result = resolver.ReplaceTokens("%githash%", moduleDefinition, repo, ""); + var result = resolver.ReplaceTokens("%githash%", version, repo, ""); Assert.AreEqual(sha, result); }); @@ -118,7 +119,7 @@ public void Replace_haschanges() { DoWithCurrentRepo(repo => { - var result = resolver.ReplaceTokens("%haschanges%", moduleDefinition, repo, "HasChanges"); + var result = resolver.ReplaceTokens("%haschanges%", version, repo, "HasChanges"); if (repo.IsClean()) { @@ -138,7 +139,7 @@ public void Replace_user() { var currentUser = Environment.UserName; - var result = resolver.ReplaceTokens("%user%", moduleDefinition, repo, ""); + var result = resolver.ReplaceTokens("%user%", version, repo, ""); Assert.IsTrue(result.EndsWith(currentUser)); }); @@ -151,7 +152,7 @@ public void Replace_machine() { var machineName = Environment.MachineName; - var result = resolver.ReplaceTokens("%machine%", moduleDefinition, repo, ""); + var result = resolver.ReplaceTokens("%machine%", version, repo, ""); Assert.AreEqual(machineName, result); }); @@ -165,8 +166,8 @@ public void Replace_time() var now = DateTime.Now; var utcNow = DateTime.UtcNow; - Assert.AreEqual(now.ToString("yyMMddmm"), resolver.ReplaceTokens("%now:yyMMddmm%", moduleDefinition, repo, "")); - Assert.AreEqual(utcNow.ToShortDateString(), resolver.ReplaceTokens("%utcnow%", moduleDefinition, repo, "")); + Assert.AreEqual(now.ToString("yyMMddmm"), resolver.ReplaceTokens("%now:yyMMddmm%", version, repo, "")); + Assert.AreEqual(utcNow.ToShortDateString(), resolver.ReplaceTokens("%utcnow%", version, repo, "")); }); } @@ -181,7 +182,7 @@ public void Replace_environment_variables() var replacementTokens = string.Join("--", environmentVariables.Keys.Cast() .Select(key => "%env[" + key + "]%") .ToArray()); - var result = resolver.ReplaceTokens(replacementTokens, moduleDefinition, repo, ""); + var result = resolver.ReplaceTokens(replacementTokens, version, repo, ""); Assert.AreEqual(expected, result); }); From 0f51ff72a05a77ddce5b2260da34ceeec31a3555 Mon Sep 17 00:00:00 2001 From: Aaron Sherber Date: Sat, 24 Dec 2016 11:43:35 -0500 Subject: [PATCH 7/8] Add tests for UseFileVersion and OverwriteFileVersion --- Tests/TaskTests.cs | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/Tests/TaskTests.cs b/Tests/TaskTests.cs index 85ca38f..bb24545 100644 --- a/Tests/TaskTests.cs +++ b/Tests/TaskTests.cs @@ -5,6 +5,7 @@ using Mono.Cecil; using NUnit.Framework; using System.Xml.Linq; +using System; [TestFixture] public class TaskTests @@ -23,10 +24,15 @@ public void Setup() beforeAssemblyPath = beforeAssemblyPath.Replace("Debug", "Release"); #endif - afterAssemblyPath = beforeAssemblyPath.Replace(".dll", "2.dll"); + afterAssemblyPath = beforeAssemblyPath.Replace(".dll", $"{Guid.NewGuid().ToString()}.dll"); File.Copy(beforeAssemblyPath, afterAssemblyPath, true); var moduleDefinition = ModuleDefinition.ReadModule(afterAssemblyPath); + + var versionInfo = FileVersionInfo.GetVersionInfo(afterAssemblyPath); + Trace.WriteLine(String.Format("Before: AssemblyVersion={0}, FileVersion={1}, Config={2}", + moduleDefinition.Assembly.Name.Version, versionInfo.FileVersion, config)); + var currentDirectory = AssemblyLocation.CurrentDirectory(); var weavingTask = new ModuleWeaver { @@ -52,7 +58,7 @@ public void EnsureAttributeExists() .First(); Assert.IsNotNull(customAttributes.InformationalVersion); Assert.IsNotEmpty(customAttributes.InformationalVersion); - Trace.WriteLine(customAttributes.InformationalVersion); + Trace.WriteLine($"InfoVersion: {customAttributes.InformationalVersion}"); } [Test] @@ -63,8 +69,8 @@ public void Win32Resource() Assert.IsNotEmpty(versionInfo.ProductVersion); Assert.IsNotNull(versionInfo.FileVersion); Assert.IsNotEmpty(versionInfo.FileVersion); - Trace.WriteLine(versionInfo.ProductVersion); - Trace.WriteLine(versionInfo.FileVersion); + Trace.WriteLine($"ProductVersion: {versionInfo.ProductVersion}"); + Trace.WriteLine($"FileVersion: {versionInfo.FileVersion}"); } @@ -76,4 +82,22 @@ public void PeVerify() } #endif +} + +[TestFixture] +class UseFileVersionTests : TaskTests +{ + public UseFileVersionTests() + { + config = XElement.Parse(""); + } +} + +[TestFixture] +class OverwriteFileVersionTests : TaskTests +{ + public OverwriteFileVersionTests() + { + config = XElement.Parse(""); + } } \ No newline at end of file From ac637e3865a91d6cea4065316fb2c9b36298bdc2 Mon Sep 17 00:00:00 2001 From: Aaron Sherber Date: Sat, 24 Dec 2016 11:53:24 -0500 Subject: [PATCH 8/8] Update README --- README.md | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index cdf7630..67fa736 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ The tokens are: ## Configuration -All config options are attributes of Stamp in FodyWeavers.xml +All config options are attributes of the `Stamp` node in `FodyWeavers.xml` ### ChangeString @@ -52,15 +52,31 @@ Define the string used to indicate that the code was built from a non clean repo *Default is `HasChanges`* - + ### UseProjectGit -Define if you want to start Stamp to start searching for the Git repository in the ProjectDir (true) or the SolutionDir (false). +Define if you want to start Stamp to start searching for the Git repository in the ProjectDir (`true`) or the SolutionDir (`false`). *Default is `false`* - + + +### OverwriteFileVersion + +By default, Stamp will overwrite the `AssemblyFileVersion` with the `AssemblyVersion`. Setting this to `false` will preserve the existing `AssemblyFileVersion`. + +*Default is `true`* + + + +### UseFileVersion + +By default, Stamp uses the value from `AssemblyVersion` to construct the `AssemblyInformationalVersion`. Set this to `true` to use the `AssemblyFileVersion` instead. **Note:** If this is set to `true`, `OverwriteFileVersion` will be `false` and will ignore any value explicitly set. + +*Default is `false`* + + ## Icon