Skip to content

Commit f0e05d1

Browse files
committed
Allow sharing version computation with a user-owned project
Quickbuild requires that every P2P be to a git-controlled project. This makes it incompatible with NB.GV's running the MSBuild task to one of its own .targets files. To workaround this, a quickbuild user will have to define their own project that does nothing but compute the version using NB.GV, and then set `NBGV_CachingProjectReference` to the full path to this custom project. Note that no actual *caching* occurs at this stage when under quickbuild because quickbuild uses a new top-level msbuild call for every project. We'll need to combine this with caching version info to a file on disk for this to be a perf improvement in quickbuild.
1 parent bc15604 commit f0e05d1

File tree

8 files changed

+80
-9
lines changed

8 files changed

+80
-9
lines changed

doc/quickbuild.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Microsoft's (internal) quickbuild
2+
3+
Nerdbank.GitVersioning supports the Microsoft-internal quickbuild/cloudbuild tool.
4+
5+
It works out of the box, but each project will recompute the version, which may accumulate to a significant increase in overall build time.
6+
7+
🚧 A future version of Nerdbank.GitVersioning will cache version information as a file so that the following instructions will be effective. 🚧
8+
9+
To calculate the version just once for an entire build, a few manual steps are required.
10+
11+
1. Create this project in your repo. The suggested location is `VersionGeneration/VersionGeneration.msbuildproj`.
12+
13+
```xml
14+
<Project Sdk="Microsoft.Build.NoTargets">
15+
<PropertyGroup>
16+
<TargetFramework>net5.0</TargetFramework>
17+
<IsPackable>false</IsPackable>
18+
<SkipCopyBuildProduct>true</SkipCopyBuildProduct>
19+
<NBGV_CacheMode>VersionGenerationTarget</NBGV_CacheMode>
20+
</PropertyGroup>
21+
</Project>
22+
```
23+
24+
The `TargetFramework` property value is not important as no assemblies are built by this project,
25+
but a value is nonetheless required for NuGet to be willing to consume the Nerdbank.GitVersioning package reference
26+
(which is referenced in Directory.Build.props as described later).
27+
28+
1. Add the SDK version to your repo-root level `global.json` file, if it is not already present.
29+
The [latest available version from nuget.org](https://www.nuget.org/packages/microsoft.build.notargets) is recommended.
30+
31+
```json
32+
{
33+
"msbuild-sdks": {
34+
"Microsoft.Build.NoTargets": "3.1.0"
35+
}
36+
}
37+
```
38+
39+
1. Modify your repo-root level `Directory.Build.props` file to contain these elements:
40+
41+
```xml
42+
<PropertyGroup>
43+
<!-- This entire repo has just one version.json file, so compute the version once and share with all projects in a large build. -->
44+
<GitVersionBaseDirectory>$(MSBuildThisFileDirectory)</GitVersionBaseDirectory>
45+
</PropertyGroup>
46+
47+
<PropertyGroup Condition=" '$(QBuild)' == '1' ">
48+
<NBGV_CacheMode>MSBuildTargetCaching</NBGV_CacheMode>
49+
<NBGV_CachingProjectReference>$(MSBuildThisFileDirectory)VersionGeneration\VersionGeneration.msbuildproj</NBGV_CachingProjectReference>
50+
</PropertyGroup>
51+
52+
<ItemGroup>
53+
<PackageReference Include="Nerdbank.GitVersioning" Version="3.5.*" PrivateAssets="all" />
54+
</ItemGroup>
55+
```

src/NerdBank.GitVersioning.Tests/NerdBank.GitVersioning.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<Compile Include="..\Shared\**\*.cs" LinkBase="Shared" />
1313
</ItemGroup>
1414
<ItemGroup>
15-
<EmbeddedResource Include="..\Nerdbank.GitVersioning.Tasks\build\*.targets;..\Nerdbank.GitVersioning.Tasks\build\*.props">
15+
<EmbeddedResource Include="..\Nerdbank.GitVersioning.Tasks\build\*.targets;..\Nerdbank.GitVersioning.Tasks\build\*.props;..\Nerdbank.GitVersioning.Tasks\build\*.proj">
1616
<Visible>false</Visible>
1717
<Link>Targets\%(FileName)%(Extension)</Link>
1818
</EmbeddedResource>

src/Nerdbank.GitVersioning.Tasks/Nerdbank.GitVersioning.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ IMPORTANT: The 3.x release may produce a different version height than prior maj
5252
<file src="build\MSBuildTargetCaching.targets" target="build\MSBuildTargetCaching.targets" />
5353
<file src="build\Nerdbank.GitVersioning.Common.targets" target="build\Nerdbank.GitVersioning.Common.targets" />
5454
<file src="build\Nerdbank.GitVersioning.Inner.targets" target="build\Nerdbank.GitVersioning.Inner.targets" />
55-
<file src="build\Nerdbank.GitVersioning.Inner.Empty.targets" target="build\Nerdbank.GitVersioning.Inner.Empty.targets" />
5655
<file src="build\Nerdbank.GitVersioning.props" target="build\Nerdbank.GitVersioning$LKGSuffix$.props" />
5756
<file src="build\Nerdbank.GitVersioning.targets" target="build\Nerdbank.GitVersioning$LKGSuffix$.targets" />
57+
<file src="build\PrivateP2PCaching.proj" target="build\PrivateP2PCaching.proj" />
5858
<file src="buildCrossTargeting\Nerdbank.GitVersioning.targets" target="buildCrossTargeting\Nerdbank.GitVersioning$LKGSuffix$.targets" />
5959
<file src="readme.txt" target="readme.txt" />
6060
</files>

src/Nerdbank.GitVersioning.Tasks/build/MSBuildTargetCaching.targets

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,30 @@
66
<NBGV_InnerGlobalProperties Condition=" '$(ProjectPathRelativeToGitRepoRoot)' != '' ">$(NBGV_InnerGlobalProperties)ProjectPathRelativeToGitRepoRoot=$(ProjectPathRelativeToGitRepoRoot);</NBGV_InnerGlobalProperties>
77
<NBGV_InnerGlobalProperties Condition=" '$(GitVersionBaseDirectory)' != '' ">$(NBGV_InnerGlobalProperties)GitVersionBaseDirectory=$(GitVersionBaseDirectory);</NBGV_InnerGlobalProperties>
88
<NBGV_InnerGlobalProperties Condition=" '$(OverrideBuildNumberOffset)' != '' ">$(NBGV_InnerGlobalProperties)OverrideBuildNumberOffset=$(OverrideBuildNumberOffset);</NBGV_InnerGlobalProperties>
9-
<NBGV_InnerGlobalProperties Condition=" '$(NBGV_CacheMode)' != '' ">$(NBGV_InnerGlobalProperties)NBGV_CacheMode=$(NBGV_CacheMode);</NBGV_InnerGlobalProperties>
109
</PropertyGroup>
1110

1211
<!-- Compile a list of global properties that may vary when a project builds but that would never influence the result of the GetBuildVersion task. -->
1312
<ItemGroup>
1413
<NBGV_GlobalPropertiesToRemove Include="TargetFramework" />
1514
<NBGV_GlobalPropertiesToRemove Include="RuntimeIdentifier" />
16-
<NBGV_GlobalPropertiesToRemove Include="Configuration" />
17-
<NBGV_GlobalPropertiesToRemove Include="Platform" />
1815

1916
<_BuildMetadataSnapped Include="@(BuildMetadata)" />
2017
</ItemGroup>
2118

19+
<!-- We generally prefer to clear config|platform properties because they do not impact the version.
20+
But quickbuild doesn't like a p2p that removes these, so set them to defensible constant values in that situation. -->
21+
<PropertyGroup Condition=" '$(QBuild)' == '1' ">
22+
<NBGV_InnerGlobalProperties>$(NBGV_InnerGlobalProperties)Configuration=Release;</NBGV_InnerGlobalProperties>
23+
<NBGV_InnerGlobalProperties>$(NBGV_InnerGlobalProperties)Platform=AnyCPU;</NBGV_InnerGlobalProperties>
24+
</PropertyGroup>
25+
<ItemGroup Condition=" '$(QBuild)' != '1' ">
26+
<NBGV_GlobalPropertiesToRemove Include="Configuration" />
27+
<NBGV_GlobalPropertiesToRemove Include="Platform" />
28+
</ItemGroup>
29+
2230
<ItemGroup>
2331
<!-- Declare a P2P so that "msbuild -graph -isolate" doesn't complain when we use the MSBuild task to invoke our inner shared project. -->
24-
<ProjectReference Include="$(MSBuildThisFileDirectory)Nerdbank.GitVersioning.Inner.targets">
32+
<ProjectReference Include="$(NBGV_CachingProjectReference)">
2533
<Targets>GetBuildVersion_Properties;GetBuildVersion_CloudBuildVersionVars</Targets>
2634
<Properties>$(NBGV_InnerGlobalProperties)BuildMetadata=@(BuildMetadata, ',');</Properties>
2735
<GlobalPropertiesToRemove>@(NBGV_GlobalPropertiesToRemove)</GlobalPropertiesToRemove>
@@ -32,6 +40,7 @@
3240
<SkipGetTargetFrameworkProperties>true</SkipGetTargetFrameworkProperties>
3341
<Visible>false</Visible>
3442
<NBGV_InnerProject>true</NBGV_InnerProject>
43+
<PrivateAssets>all</PrivateAssets>
3544
</ProjectReference>
3645
</ItemGroup>
3746

src/Nerdbank.GitVersioning.Tasks/build/Nerdbank.GitVersioning.Inner.targets

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,4 @@
4343
<Message Importance="low" Text="NuGetPackageVersion: $(NuGetPackageVersion)" />
4444
</Target>
4545

46-
<Import Project="Nerdbank.GitVersioning.Inner.Empty.targets" Condition=" '$(NBGV_CacheMode)' == 'MSBuildTargetCaching' " />
47-
4846
</Project>

src/Nerdbank.GitVersioning.Tasks/build/Nerdbank.GitVersioning.props

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,10 @@
1111
-->
1212
<NBGV_CacheMode Condition=" '$(NBGV_CacheMode)' == '' and '$(QBuild)' == '1' ">None</NBGV_CacheMode>
1313
<NBGV_CacheMode Condition=" '$(NBGV_CacheMode)' == '' ">MSBuildTargetCaching</NBGV_CacheMode>
14+
15+
<!-- This property may be overridden in environments where all P2P references must be a project actually defined within the git repo directory.
16+
Learn more at :/doc/quickbuild.md.
17+
-->
18+
<NBGV_CachingProjectReference Condition=" '$(NBGV_CachingProjectReference)' == '' ">$(MSBuildThisFileDirectory)PrivateP2PCaching.proj</NBGV_CachingProjectReference>
1419
</PropertyGroup>
1520
</Project>

src/Nerdbank.GitVersioning.Tasks/build/Nerdbank.GitVersioning.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
<!-- $(TargetExt) isn't set at evaluation time for us when built in wpftmp.csproj with manual imports -->
3939
<!-- Suppress assembly version info generation if not obviously compiling an assembly. -->
4040
<GenerateAssemblyVersionInfo Condition=" '$(GenerateAssemblyVersionInfo)' == '' and '$(TargetExt)' != '.dll' and '$(TargetExt)' != '.exe' ">false</GenerateAssemblyVersionInfo>
41+
<GenerateAssemblyVersionInfo Condition=" '$(GenerateAssemblyVersionInfo)' == '' and '$(NBGV_CacheMode)' == 'VersionGenerationTarget' ">false</GenerateAssemblyVersionInfo>
4142

4243
<!-- Workaround the property stomping that msbuild does (see https://github.com/microsoft/msbuild/pull/4922) with manual imports. -->
4344
<PrepareForBuildDependsOn>

src/Nerdbank.GitVersioning.Tasks/build/Nerdbank.GitVersioning.Inner.Empty.targets renamed to src/Nerdbank.GitVersioning.Tasks/build/PrivateP2PCaching.proj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
4+
<Import Project="Nerdbank.GitVersioning.Inner.targets" />
25

36
<!-- These targets are called by MSBuild static graph because we declare a ProjectReference item to this file. -->
47
<Target Name="GetTargetFrameworks" />

0 commit comments

Comments
 (0)