Skip to content

Commit f1e94dc

Browse files
committed
Fix ReplaceVersionInfo for sdk projects. Add tests
1 parent 36f6bb0 commit f1e94dc

File tree

9 files changed

+81
-10
lines changed

9 files changed

+81
-10
lines changed

MSBuildGitHash.Tests/Data/Directory.Build.props

-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,4 @@
66
</PropertyGroup>
77

88
<Import Project="$(MSBuildGitHashProps)"/>
9-
10-
<Import Project="$(MSBuildGitHashTargets)"/>
119
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<Project>
2+
<Import Project="$(MSBuildGitHashTargets)"/>
3+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
2+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
3+
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
8+
<Version>3.2.1</Version>
9+
<OutputType>Exe</OutputType>
10+
<OutputPath>bin\</OutputPath>
11+
<MSBuildGitHashReplaceInfoVersion>True</MSBuildGitHashReplaceInfoVersion>
12+
</PropertyGroup>
13+
14+
<ItemGroup>
15+
<Reference Include="System" />
16+
<Reference Include="System.Core" />
17+
<Reference Include="System.Xml.Linq" />
18+
<Reference Include="System.Data.DataSetExtensions" />
19+
<Reference Include="Microsoft.CSharp" />
20+
<Reference Include="System.Data" />
21+
<Reference Include="System.Net.Http" />
22+
<Reference Include="System.Xml" />
23+
</ItemGroup>
24+
25+
<ItemGroup>
26+
<Compile Include="../Program.cs"/>
27+
</ItemGroup>
28+
29+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
30+
</Project>
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<RepositoryUrl>https://github.com/MarkPflug/MSBuildGitHash</RepositoryUrl>
4+
<RepositoryType>git</RepositoryType>
5+
<TargetFramework>net461</TargetFramework>
6+
<Version>3.2.1</Version>
7+
<OutputType>Exe</OutputType>
8+
<MSBuildGitHashReplaceInfoVersion>True</MSBuildGitHashReplaceInfoVersion>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<Compile Include="..\Program.cs"/>
13+
</ItemGroup>
14+
</Project>

MSBuildGitHash.Tests/MSBuildGitHashTests.cs

+16
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,14 @@ string BuildProject(string projFile)
8585
}
8686
Assert.True(restored, "Failed to restore packages");
8787
var result = proj.Build(logger);
88+
LogProps(proj);
8889
var outputPath = proj.GetPropertyValue("TargetPath");
8990
Assert.True(result, "Build failed");
9091
return outputPath;
9192
}
9293

9394
const string InfoVersionPattern = @"^\d+.\d+.\d+\+[0-9a-f]{7}(-dirty)?$";
95+
const string InfoVersionShortPattern = @"^[0-9a-f]{7}(-dirty)?$";
9496

9597
[Theory]
9698
[InlineData("Data/Sdk1/Proj.csproj")]
@@ -107,6 +109,20 @@ public void SdkProjectTest(string projectFile)
107109
Assert.Equal("https://github.com/MarkPflug/MSBuildGitHash", attr.Value);
108110
}
109111

112+
[Theory]
113+
[InlineData("Data/Legacy2/Proj.csproj")]
114+
[InlineData("Data/Sdk2/Proj.csproj")]
115+
public void ProjectInfoVersionTest(string projectFile)
116+
{
117+
var exepath = BuildProject(projectFile);
118+
var v = FileVersionInfo.GetVersionInfo(exepath).ProductVersion;
119+
Assert.Matches(InfoVersionShortPattern, v);
120+
var asm = Assembly.LoadFile(exepath);
121+
var attr = asm.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
122+
Assert.NotNull(attr);
123+
Assert.NotNull(attr.InformationalVersion);
124+
}
125+
110126
[Fact]
111127
public void LegacyProjectTest()
112128
{

MSBuildGitHash/MsBuildGitHash.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<!-- we don't compile anything, this is just to satisfy the VS project system / MSBuild -->
44
<TargetFrameworks>netstandard1.0</TargetFrameworks>
55
<PackageId>MSBuildGitHash</PackageId>
6-
<VersionPrefix>0.5.1</VersionPrefix>
6+
<VersionPrefix>0.5.2</VersionPrefix>
77
<PackageProjectUrl>https://github.com/MarkPflug/MSBuildGitHash</PackageProjectUrl>
88
<PackageIconUrl>https://markpflug.github.io/MarkPflug.png</PackageIconUrl>
99
<PackageTags>MSBuild Git</PackageTags>

MSBuildGitHash/build/MSBuildGitHash.props

-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@
44
<MSBuildGitHashValidate>True</MSBuildGitHashValidate>
55
<MSBuildGitHashValidateLength>32</MSBuildGitHashValidateLength>
66
<MSBuildGitHashValidateRegex>^[0-9A-Fa-f]{4,32}(-dirty|-broken)%3F%24</MSBuildGitHashValidateRegex>
7-
<MSBuildGitHashReplaceInfoVersion Condition="'$(MSBuildGitHashReplaceInfoVersion)' == ''">False</MSBuildGitHashReplaceInfoVersion>
87
</PropertyGroup>
98
</Project>

MSBuildGitHash/build/MSBuildGitHash.targets

+14-6
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,25 @@
3535
<MSBuildGitHashValueStart Condition="$(MSBuildGitHashValue.Length) &lt;= 32">$(MSBuildGitHashValue)</MSBuildGitHashValueStart>
3636
<MSBuildGitHashValueStart Condition="$(MSBuildGitHashValue.Length) &gt; 32">$(MSBuildGitHashValue.Substring(0,32))</MSBuildGitHashValueStart>
3737
<MSBuildGitHashRegexMatch>$([System.Text.RegularExpressions.Regex]::IsMatch($(MSBuildGitHashValue), $(MSBuildGitHashValidateRegex)))</MSBuildGitHashRegexMatch>
38+
</PropertyGroup>
3839

40+
<PropertyGroup Condition="'$(UsingMicrosoftNETSdk)' == 'true'">
3941
<!--
4042
SourceRevisionId is used by the Microsoft.NET.GenerateAssemblyInfo to populate
4143
the AssemblyInformationalVersion, this only happens in SDK builds.
4244
-->
43-
<SourceRevisionId
44-
Condition="'$(SourceRevisionId)' == '' And '$(UsingMicrosoftNETSdk)' == 'true' And '$(IncludeMSBuildGitHashInfoVersion)' == 'true'"
45+
<SourceRevisionId
46+
Condition="'$(SourceRevisionId)' == '' And '$(IncludeMSBuildGitHashInfoVersion)' == 'true'"
4547
>$(MSBuildGitHashValue)</SourceRevisionId>
4648
</PropertyGroup>
4749

48-
<Error
50+
<PropertyGroup Condition="'$(UsingMicrosoftNETSdk)' == 'true' And '$(MSBuildGitHashReplaceInfoVersion)' == 'True'">
51+
<!-- suppress the sdk handling of informational version when MSBuildGitHashReplaceInfoVersion is set -->
52+
<IncludeSourceRevisionInInformationalVersion>false</IncludeSourceRevisionInInformationalVersion>
53+
<InformationalVersion>$(MSBuildGitHashValue)</InformationalVersion>
54+
</PropertyGroup>
55+
56+
<Error
4957
Condition="'$(MSBuildGitHashValidate)' == 'True' And ($(MSBuildGitHashValueLength) &gt; $(MSBuildGitHashValidateLength) Or '$(MSBuildGitHashRegexMatch)' != 'True')"
5058
Text="MSBuildGitHash hash value starting with '$(MSBuildGitHashValueStart)' was invalid."
5159
/>
@@ -54,12 +62,12 @@
5462

5563
<Target
5664
Name="GenerateAssemblyVersionAttributes"
57-
Condition="'$(UsingMicrosoftNETSdk)' != 'true'"
65+
Condition="'$(UsingMicrosoftNETSdk)' != 'True'"
5866
DependsOnTargets="GetGitHash"
5967
>
6068
<PropertyGroup>
6169
<MSBuildGitHashVersionAttribute>$(Version)+$(MSBuildGitHashValue)</MSBuildGitHashVersionAttribute>
62-
<MSBuildGitHashVersionAttribute Condition="$(MSBuildGitHashReplaceInfoVersion) == 'True'">$(MSBuildGitHashValue)</MSBuildGitHashVersionAttribute>
70+
<MSBuildGitHashVersionAttribute Condition="'$(MSBuildGitHashReplaceInfoVersion)' == 'True'">$(MSBuildGitHashValue)</MSBuildGitHashVersionAttribute>
6371
</PropertyGroup>
6472

6573
<ItemGroup>
@@ -70,7 +78,7 @@
7078
</AssemblyAttributes>
7179
</ItemGroup>
7280
</Target>
73-
81+
7482
<Target
7583
Name="GenerateAssemblyAttributes"
7684
DependsOnTargets="GetGitHash"

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ Alternatively, the git hash can replace the Informational Version attribute valu
3737

3838
## Version History
3939

40+
_0.5.2_
41+
- Adds ability to replace assembly InformationalVersion via `MSBuildGitHashReplaceInfoVersion`. Thanks @Br3nt
42+
4043
_0.5.1_
4144
- Fix for #15, to allow supression of AssemblyInformationalVersionAttribute.
4245

0 commit comments

Comments
 (0)