Skip to content

Commit

Permalink
GitVersion
Browse files Browse the repository at this point in the history
- Use GitVersion to stamp version numbers into assemblies.

https://github.com/GitTools/GitVersion

- Add LibraryVersionInfo class and simple test case.

GitVersion mode = ContinuousDelivery

- `Mainline` mode is not available until GitVersion v4.x [currently in pre-release]
  • Loading branch information
jthelin committed Nov 3, 2017
1 parent 34daf5b commit 99491fe
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 13 deletions.
1 change: 1 addition & 0 deletions ServerHost.sln
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Build", "Build", "{64DBC33D
Build.sh = Build.sh
Clean.cmd = Clean.cmd
documentation\DocFormatter.xsl = documentation\DocFormatter.xsl
GitVersion.yml = GitVersion.yml
nuget-restore.cmd = nuget-restore.cmd
ServerHost.nuspec = ServerHost.nuspec
Test.cmd = Test.cmd
Expand Down
79 changes: 79 additions & 0 deletions ServerHost/LibraryVersionInfo.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Version info for ServerHost library.
/// </summary>
/// <remarks>
/// Based on the <c>Orleans.Runtime.RuntimeVersion</c> class.
/// https://github.com/dotnet/orleans/blob/master/src/Orleans/Runtime/RuntimeVersion.cs
/// </remarks>
public static class LibraryVersionInfo
{
private static readonly TypeInfo libraryTypeInfo = typeof(ServerHost).GetTypeInfo();

/// <summary>
/// The full version string of the library.
/// eg: '2012.5.9.51607 Build:12345 Timestamp: 20120509-185359'
/// </summary>
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;
}
}

/// <summary>
/// The ApiVersion of the library.
/// eg: '1.0.0.0'
/// </summary>
public static string ApiVersion
{
get
{
Assembly me = libraryTypeInfo.Assembly;
AssemblyName libraryInfo = me.GetName();
return libraryInfo.Version.ToString();
}
}

/// <summary>
/// The FileVersion of the library.
/// eg: '2012.5.9.51607'
/// </summary>
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;
}
}
}
13 changes: 0 additions & 13 deletions ServerHost/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,3 @@

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("e9fff8d5-f4c0-483d-aee6-5ff82afd434f")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
3 changes: 3 additions & 0 deletions ServerHost/ServerHost.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ServerHost.cs" />
<Compile Include="LibraryVersionInfo.cs" />
<Compile Include="ServerHostHandle.cs" />
<Compile Include="ServerHostFixture.cs" />
</ItemGroup>
Expand Down Expand Up @@ -74,7 +75,9 @@
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\GitVersionTask.3.6.5\build\dotnet\GitVersionTask.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\GitVersionTask.3.6.5\build\dotnet\GitVersionTask.targets'))" />
<Error Condition="!Exists('..\packages\SourceLink.Create.CommandLine.2.5.0\build\SourceLink.Create.CommandLine.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\SourceLink.Create.CommandLine.2.5.0\build\SourceLink.Create.CommandLine.targets'))" />
</Target>
<Import Project="..\packages\GitVersionTask.3.6.5\build\dotnet\GitVersionTask.targets" Condition="Exists('..\packages\GitVersionTask.3.6.5\build\dotnet\GitVersionTask.targets')" />
<Import Project="..\packages\SourceLink.Create.CommandLine.2.5.0\build\SourceLink.Create.CommandLine.targets" Condition="Exists('..\packages\SourceLink.Create.CommandLine.2.5.0\build\SourceLink.Create.CommandLine.targets')" />
</Project>
1 change: 1 addition & 0 deletions ServerHost/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="GitVersionTask" version="3.6.5" targetFramework="net45" developmentDependency="true" />
<package id="log4net" version="2.0.5" targetFramework="net45" />
<package id="SourceLink.Create.CommandLine" version="2.5.0" targetFramework="net45" developmentDependency="true" />
</packages>
18 changes: 18 additions & 0 deletions Tests/Tests.Net46/ServerHostTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,23 @@ public void LoadServerInNewAppDomain()
serverHostHandle.Server.Should().NotBeNull("Null ServerHostHandle.Server returned.");
serverHostHandle.Server.Should().BeOfType<TestServer.Server>("Server instance type.");
}

[Fact]
[Trait("Category", "BVT")]
public void ServerHost_Version()
{
_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);

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.");
versionString.Should().NotStartWith("0.0.0.0", "Version should not be zero.");
}
}
}

0 comments on commit 99491fe

Please sign in to comment.