From 2f7c1c419b5d61c483aa01d7694bea4ccd1340e8 Mon Sep 17 00:00:00 2001 From: Paul Baudron Date: Fri, 18 Aug 2023 10:21:19 +0200 Subject: [PATCH] Fix version displayed in Sharpmake assembly loading message --- Sharpmake.Application/Program.cs | 65 +++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 22 deletions(-) diff --git a/Sharpmake.Application/Program.cs b/Sharpmake.Application/Program.cs index 5222bde81..1b414e3a8 100644 --- a/Sharpmake.Application/Program.cs +++ b/Sharpmake.Application/Program.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.ComponentModel; +using System.Data; using System.Diagnostics; using System.IO; using System.Linq; @@ -169,17 +170,8 @@ private static int Main() { DebugEnable = CommandLine.ContainParameter("verbose") || CommandLine.ContainParameter("debug") || CommandLine.ContainParameter("diagnostics"); - var sharpmakeAssembly = Assembly.GetExecutingAssembly(); - Version version = sharpmakeAssembly.GetName().Version; - string versionString = string.Join(".", version.Major, version.Minor, version.Build); - string informationalVersion = sharpmakeAssembly.GetCustomAttribute()?.InformationalVersion; - if (version.Revision != 0 || (informationalVersion != null && informationalVersion.IndexOf("+Branch.") == -1)) - { - versionString += " (non-official)"; - if (DebugEnable && informationalVersion != null) - versionString += " " + informationalVersion; - } - + GetAssemblyInfo(Assembly.GetExecutingAssembly(), out var _, out var version, out var versionString, out var _); + LogWriteLine($"sharpmake {versionString}"); LogWriteLine(" arguments: {0}", CommandLine.GetProgramCommandLine()); LogWriteLine(" directory: {0}", Directory.GetCurrentDirectory()); @@ -399,6 +391,44 @@ private static void AppDomain_AssemblyLoad(object sender, AssemblyLoadEventArgs LogSharpmakeExtensionLoaded(args.LoadedAssembly); } + /// + /// Retrieve information from the assembly passed as argument + /// + /// The assembly to get information from + /// The name of the assembly + /// The version as read from the assembly (the 4th componant is not used by Sharpmake core, but may be used by Sharpmake extended assemblies) + /// A string of the format " [(non-official)] [()]" + /// The location of the assembly + private static void GetAssemblyInfo(Assembly assembly, out string name, out Version version, out string versionString, out string location) + { + // Name + name = assembly.GetName().Name; + + // Version + version = assembly.GetName().Version; + versionString = "(non-official)"; + string informationalVersion = assembly.GetCustomAttribute()?.InformationalVersion; + if (!string.IsNullOrEmpty(informationalVersion)) + { + var endOfVersionIndex = informationalVersion.IndexOf("+", StringComparison.Ordinal); + if (endOfVersionIndex != -1) + { + versionString = informationalVersion[..endOfVersionIndex]; + if (informationalVersion.IndexOf("-g", 0, endOfVersionIndex, StringComparison.Ordinal) != -1 + && informationalVersion.IndexOf(".Commits.0.", 0, endOfVersionIndex, StringComparison.Ordinal) == -1) + { + versionString += " (non-official)"; + } + } + + if (DebugEnable) + versionString += $" ({informationalVersion})"; + } + + // Location + location = assembly.Location; + } + private static void LogSharpmakeExtensionLoaded(Assembly extensionAssembly) { if (extensionAssembly == null) @@ -407,17 +437,8 @@ private static void LogSharpmakeExtensionLoaded(Assembly extensionAssembly) if (!ExtensionLoader.ExtensionChecker.IsSharpmakeExtension(extensionAssembly)) return; - AssemblyName extensionName = extensionAssembly.GetName(); - Version version = extensionName.Version; - string versionString = string.Join(".", version.Major, version.Minor, version.Build); - string informationalVersion = extensionAssembly.GetCustomAttribute()?.InformationalVersion; - if (version.Revision != 0 || informationalVersion == null || informationalVersion.IndexOf("+Branch.") == -1) - { - versionString += " (non-official)"; - if (DebugEnable && informationalVersion != null) - versionString += " " + informationalVersion; - } - LogWriteLine(" {0} {1} loaded from '{2}'", extensionName.Name, versionString, extensionAssembly.Location); + GetAssemblyInfo(extensionAssembly, out var extensionName, out var _, out var extensionVersion, out var extensionLocation); + LogWriteLine(" {0} {1} loaded from '{2}'", extensionName, extensionVersion, extensionLocation); } private static void CreateBuilderAndGenerate(BuildContext.BaseBuildContext buildContext, Argument parameters, bool generateDebugSolution)