Skip to content

Commit 8da5f17

Browse files
committed
read file version info to use in TeamCity
1 parent 8b0c5d9 commit 8da5f17

4 files changed

+136
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{93C850A3-C878-4A6D-9B31-215ECF31C6E1}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>FileVersionExtractor</RootNamespace>
11+
<AssemblyName>FileVersionExtractor</AssemblyName>
12+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<DebugSymbols>true</DebugSymbols>
17+
<DebugType>full</DebugType>
18+
<Optimize>false</Optimize>
19+
<OutputPath>bin\</OutputPath>
20+
<DefineConstants>DEBUG;TRACE</DefineConstants>
21+
<ErrorReport>prompt</ErrorReport>
22+
<WarningLevel>4</WarningLevel>
23+
</PropertyGroup>
24+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
25+
<DebugType>pdbonly</DebugType>
26+
<Optimize>true</Optimize>
27+
<OutputPath>bin\</OutputPath>
28+
<DefineConstants>TRACE</DefineConstants>
29+
<ErrorReport>prompt</ErrorReport>
30+
<WarningLevel>4</WarningLevel>
31+
</PropertyGroup>
32+
<ItemGroup>
33+
<Reference Include="Microsoft.Build.Framework" />
34+
<Reference Include="System" />
35+
<Reference Include="System.Core" />
36+
<Reference Include="System.Xml.Linq" />
37+
<Reference Include="System.Data.DataSetExtensions" />
38+
<Reference Include="Microsoft.CSharp" />
39+
<Reference Include="System.Data" />
40+
<Reference Include="System.Xml" />
41+
</ItemGroup>
42+
<ItemGroup>
43+
<Compile Include="GetAssemblyFileVersion.cs" />
44+
</ItemGroup>
45+
<ItemGroup>
46+
<Folder Include="Properties\" />
47+
</ItemGroup>
48+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
49+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
50+
Other similar extension points exist, see Microsoft.Common.targets.
51+
<Target Name="BeforeBuild">
52+
</Target>
53+
<Target Name="AfterBuild">
54+
</Target>
55+
-->
56+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
using System.IO;
3+
using System.Text.RegularExpressions;
4+
using Microsoft.Build.Framework;
5+
6+
namespace FileVersionExtractor
7+
{
8+
public class GetAssemblyFileVersion : ITask
9+
{
10+
private const string Pattern = @"(?:AssemblyFileVersion\("")(?<ver>(\d*)\.(\d*)(\.(\d*)(\.(\d*))?)?)(?:""\))";
11+
12+
[Required]
13+
public string FilePathAssemblyInfo { get; set; }
14+
15+
[Output]
16+
public string AssemblyFileVersion { get; set; }
17+
18+
public IBuildEngine BuildEngine { get; set; }
19+
20+
public ITaskHost HostObject { get; set; }
21+
22+
public bool Execute()
23+
{
24+
StreamReader streamreaderAssemblyInfo = null;
25+
AssemblyFileVersion = String.Empty;
26+
try
27+
{
28+
streamreaderAssemblyInfo = new StreamReader(FilePathAssemblyInfo);
29+
string strLine;
30+
while ((strLine = streamreaderAssemblyInfo.ReadLine()) != null)
31+
{
32+
Match matchVersion = Regex.Match(
33+
strLine,
34+
Pattern,
35+
RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline | RegexOptions.ExplicitCapture);
36+
if (!matchVersion.Success)
37+
{
38+
continue;
39+
}
40+
41+
var groupVersion = matchVersion.Groups["ver"];
42+
if ((!groupVersion.Success) || (String.IsNullOrEmpty(groupVersion.Value)))
43+
{
44+
continue;
45+
}
46+
47+
AssemblyFileVersion = groupVersion.Value;
48+
break;
49+
}
50+
}
51+
catch (Exception e)
52+
{
53+
var args = new BuildMessageEventArgs(e.Message, string.Empty, "GetAssemblyFileVersion", MessageImportance.High);
54+
BuildEngine.LogMessageEvent(args);
55+
}
56+
finally
57+
{
58+
if (streamreaderAssemblyInfo != null)
59+
{
60+
streamreaderAssemblyInfo.Close();
61+
}
62+
}
63+
64+
return (true);
65+
}
66+
}
67+
}

RomanticWeb.sln

+6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".build", ".build", "{5510EA
4343
NuGetBuild\NugetBuild.targets = NuGetBuild\NugetBuild.targets
4444
EndProjectSection
4545
EndProject
46+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FileVersionExtractor", "FileVersionExtractor\FileVersionExtractor.csproj", "{93C850A3-C878-4A6D-9B31-215ECF31C6E1}"
47+
EndProject
4648
Global
4749
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4850
Debug|Any CPU = Debug|Any CPU
@@ -81,6 +83,10 @@ Global
8183
{BB89FC1F-FA9C-4D6D-BD12-0A0297692863}.Debug|Any CPU.Build.0 = Debug|Any CPU
8284
{BB89FC1F-FA9C-4D6D-BD12-0A0297692863}.Release|Any CPU.ActiveCfg = Release|Any CPU
8385
{BB89FC1F-FA9C-4D6D-BD12-0A0297692863}.Release|Any CPU.Build.0 = Release|Any CPU
86+
{93C850A3-C878-4A6D-9B31-215ECF31C6E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
87+
{93C850A3-C878-4A6D-9B31-215ECF31C6E1}.Debug|Any CPU.Build.0 = Debug|Any CPU
88+
{93C850A3-C878-4A6D-9B31-215ECF31C6E1}.Release|Any CPU.ActiveCfg = Release|Any CPU
89+
{93C850A3-C878-4A6D-9B31-215ECF31C6E1}.Release|Any CPU.Build.0 = Release|Any CPU
8490
EndGlobalSection
8591
GlobalSection(SolutionProperties) = preSolution
8692
HideSolutionNode = FALSE

after.RomanticWeb.sln.targets

+7-4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
<PropertyGroup Condition=" '$(TEAMCITY_BUILD_PROPERTIES_FILE)' != ''">
44
<TeamCityBuild>true</TeamCityBuild>
55
</PropertyGroup>
6+
7+
<UsingTask AssemblyFile="$(MSBuildProjectDirectory)\FileVersionExtractor\bin\FileVersionExtractor.dll"
8+
TaskName="GetAssemblyFileVersion"/>
69

710
<Target Name="Update TeamCity Version" AfterTargets="Build">
8-
<GetAssemblyIdentity AssemblyFiles="$(MSBuildProjectDirectory)\RomanticWeb\bin\Release\RomanticWeb.dll">
9-
<Output TaskParameter="Assemblies" ItemName="AssemblyIdentities"/>
10-
</GetAssemblyIdentity>
11-
<Message Condition="$(TeamCityBuild) == 'true'" Text="##teamcity[buildNumber '%(AssemblyIdentities.Version)']" />
11+
<GetAssemblyFileVersion FilePathAssemblyInfo="$(MSBuildProjectDirectory)\RomanticWeb\Properties\VersionAssemblyInfo.cs">
12+
<Output TaskParameter="AssemblyFileVersion" PropertyName="AssemblyFileVersion" />
13+
</GetAssemblyFileVersion>
14+
<Message Condition="$(TeamCityBuild) == 'true'" Text="##teamcity[buildNumber '$(AssemblyFileVersion)']" />
1215
</Target>
1316

1417
<Target Name="GitHubLink" AfterTargets="Build">

0 commit comments

Comments
 (0)