-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathGitAssemblyVersion.targets
53 lines (51 loc) · 2.36 KB
/
GitAssemblyVersion.targets
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<MSBuildCommunityTasksPath Condition=" '$(MSBuildCommunityTasksPath)' == '' ">$(SolutionDir)\.build</MSBuildCommunityTasksPath>
<AssemblyInfoFile Condition=" '$(AssemblyInfoFile)' == '' ">$(MsBuildProjectDirectory)\Properties\AssemblyInfo.cs</AssemblyInfoFile>
<GeneratedAssemblyInfoFile Condition=" '$(GeneratedAssemblyInfoFile)' == '' ">$(MsBuildProjectDirectory)\Properties\GeneratedAssemblyInfo.cs</GeneratedAssemblyInfoFile>
<BuildDependsOn>
ReadAssemblyVersion;
SetAssemblyVersion;
$(BuildDependsOn)
</BuildDependsOn>
<CleanDependsOn>
$(CleanDependsOn);
SetAssemblyVersionClean
</CleanDependsOn>
</PropertyGroup>
<Import Project="$(MSBuildCommunityTasksPath)\MSBuild.Community.Tasks.targets" />
<Target Name="ReadAssemblyVersion">
<ReadLinesFromFile File="$(AssemblyInfoFile)">
<Output TaskParameter="Lines" ItemName="ItemsFromFile"/>
</ReadLinesFromFile>
<PropertyGroup>
<Pattern>;\s*\[assembly\s*:\s*AssemblyVersion.*?"(.*?)"</Pattern>
<In>@(ItemsFromFile)</In>
<AssemblyVersion>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern)).Groups.get_Item(1))</AssemblyVersion>
</PropertyGroup>
</Target>
<Target Name="SetAssemblyVersion">
<GitVersion LocalPath="$(SolutionDir)">
<Output TaskParameter="CommitHash" PropertyName="CommitHash" />
</GitVersion>
<GitBranch LocalPath="$(SolutionDir)">
<Output TaskParameter="Branch" PropertyName="Branch" />
</GitBranch>
<GitPendingChanges LocalPath="$(MSBuildProjectDirectory)">
<Output TaskParameter="HasPendingChanges" PropertyName="IsDirty" />
</GitPendingChanges>
<PropertyGroup Condition="'$(IsDirty)'==True">
<Dirty>-dirty</Dirty>
</PropertyGroup>
<PropertyGroup Condition="'$(IsDirty)'==False">
<Dirty />
</PropertyGroup>
<PropertyGroup Condition="'$(Branch)'!=''">
<BranchInfo> ($(Branch))</BranchInfo>
</PropertyGroup>
<AssemblyInfo CodeLanguage="CS" OutputFile="$(GeneratedAssemblyInfoFile)" AssemblyInformationalVersion="$(AssemblyVersion)-$(CommitHash)$(Dirty)$(BranchInfo)" />
</Target>
<Target Name="SetAssemblyVersionClean" Condition="Exists($(GeneratedAssemblyInfoFile))">
<Delete Files="$(GeneratedAssemblyInfoFile)" />
</Target>
</Project>