diff --git a/ServerHost/LibraryVersionInfo.cs b/ServerHost/LibraryVersionInfo.cs new file mode 100644 index 0000000..fbdee49 --- /dev/null +++ b/ServerHost/LibraryVersionInfo.cs @@ -0,0 +1,79 @@ +// Copyright (c) Jorgen Thelin. All rights reserved. +// Licensed with Apache 2.0 https://github.com/jthelin/ServerHost/blob/master/LICENSE + +using System.Diagnostics; +using System.Reflection; + +namespace Server.Host +{ + /// + /// Version info for ServerHost library. + /// + /// + /// Based on the Orleans.Runtime.RuntimeVersion class. + /// https://github.com/dotnet/orleans/blob/master/src/Orleans/Runtime/RuntimeVersion.cs + /// + public static class LibraryVersionInfo + { + private static readonly TypeInfo libraryTypeInfo = typeof(ServerHost).GetTypeInfo(); + + /// + /// The full version string of the library. + /// eg: '2012.5.9.51607 Build:12345 Timestamp: 20120509-185359' + /// + public static string Current + { + get + { + Assembly me = libraryTypeInfo.Assembly; + FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(me.Location); + // progVersionInfo.IsDebug; does not work + bool isDebug = IsAssemblyDebugBuild(me); + string productVersion = versionInfo.ProductVersion + (isDebug ? " (Debug)." : " (Release)."); + return string.IsNullOrEmpty(productVersion) ? ApiVersion : productVersion; + } + } + + /// + /// The ApiVersion of the library. + /// eg: '1.0.0.0' + /// + public static string ApiVersion + { + get + { + Assembly me = libraryTypeInfo.Assembly; + AssemblyName libraryInfo = me.GetName(); + return libraryInfo.Version.ToString(); + } + } + + /// + /// The FileVersion of the library. + /// eg: '2012.5.9.51607' + /// + public static string FileVersion + { + get + { + Assembly me = libraryTypeInfo.Assembly; + FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(me.Location); + string fileVersion = versionInfo.FileVersion; + return string.IsNullOrEmpty(fileVersion) ? ApiVersion : fileVersion; + } + } + + private static bool IsAssemblyDebugBuild(Assembly assembly) + { + foreach (object attribute in assembly.GetCustomAttributes(false)) + { + DebuggableAttribute debuggableAttribute = attribute as DebuggableAttribute; + if (debuggableAttribute != null) + { + return debuggableAttribute.IsJITTrackingEnabled; + } + } + return false; + } + } +} diff --git a/ServerHost/ServerHost.csproj b/ServerHost/ServerHost.csproj index 61f719c..2fd7af7 100644 --- a/ServerHost/ServerHost.csproj +++ b/ServerHost/ServerHost.csproj @@ -46,6 +46,7 @@ + diff --git a/Tests/ServerHostTests.cs b/Tests/ServerHostTests.cs index 62dda23..438bee8 100644 --- a/Tests/ServerHostTests.cs +++ b/Tests/ServerHostTests.cs @@ -30,6 +30,19 @@ public void LoadServerInNewAppDomain() serverHostHandle.Server.Should().BeOfType("Server instance type."); } + [Fact] + [Trait("Category", "BVT")] + public void ServerHost_Version() + { + string versionString = LibraryVersionInfo.FileVersion; + output.WriteLine("ServerHost library version = {0}", versionString); + + versionString.Should().NotBeNullOrEmpty("Version value should be returned"); + + versionString.Should().Contain(".", "Version format = Major.Minor"); + versionString.Should().NotStartWith("1.0.0.0", "Version should be specific."); + } + #region Test Initialization / Cleanup methods // TestInitialize @@ -42,6 +55,10 @@ public ServerHostTests(ITestOutputHelper output, ServerHostTestFixture fixture) output.WriteLine("{0} Fixture = {1}", className, fixture); output.WriteLine("{0} Current directory = {1}", className, Environment.CurrentDirectory); + + output.WriteLine("ServerHost library API version = {0}", LibraryVersionInfo.ApiVersion); + output.WriteLine("ServerHost library file version = {0}", LibraryVersionInfo.FileVersion); + output.WriteLine("ServerHost library full version info string = {0}", LibraryVersionInfo.Current); } // TestCleanup