Skip to content

Commit f3e045c

Browse files
committed
Add .targets to validate the Windows SDK version
1 parent af626a6 commit f3e045c

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<Project>
2+
3+
<PropertyGroup>
4+
5+
<!-- Verification of the Windows SDK package version is enabled by default-->
6+
<MvvmToolkitEnableWindowsSdkPackageVersionValidation Condition="'$(MvvmToolkitEnableWindowsSdkPackageVersionValidation)' == ''">true</MvvmToolkitEnableWindowsSdkPackageVersionValidation>
7+
8+
<!--
9+
Wired up the Windows SDK package version verification target, if enabled.
10+
This only applies to TFMs targeting Windows 10 or later.
11+
-->
12+
<ResolveReferencesDependsOn Condition="'$(MSBuildProjectExtension)' == '.csproj' AND
13+
'$(MvvmToolkitVerifyWindowsSdkPackageVersion)' != 'false' AND
14+
'$(MvvmToolkitEnableWindowsSdkPackageVersionValidation)' != 'false' AND
15+
$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0-windows10.0.17763.0'))">
16+
$(ResolveReferencesDependsOn);MvvmToolkitVerifyWindowsSdkPackageVersion
17+
</ResolveReferencesDependsOn>
18+
</PropertyGroup>
19+
20+
<!-- Custom target to verify the Windows SDK package version -->
21+
<Target Name="MvvmToolkitVerifyWindowsSdkPackageVersion" DependsOnTargets="ResolveAssemblyReferences">
22+
<PropertyGroup>
23+
24+
<!-- The minimum build version for the Windows SDK package -->
25+
<_MvvmToolkitWindowsSdkPackageMinBuild>41</_MvvmToolkitWindowsSdkPackageMinBuild>
26+
</PropertyGroup>
27+
<ItemGroup>
28+
29+
<!-- If 'WindowsSdkPackageVersion' is explicitly set, validate against that -->
30+
<_MvvmToolkitWindowsSdkPackage Include="$(WindowsSdkPackageVersion)" Condition="'$(WindowsSdkPackageVersion)' != ''">
31+
<Referenced>$(WindowsSdkPackageVersion)</Referenced>
32+
<Required>10.0.$([System.Version]::Parse("$(WindowsSdkPackageVersion.Split('-')[0])").Build).$(_MvvmToolkitWindowsSdkPackageMinBuild)</Required>
33+
</_MvvmToolkitWindowsSdkPackage>
34+
35+
<!-- Otherwise, validate against the framework reference package -->
36+
<_MvvmToolkitWindowsSdkPackage
37+
Include="@(ResolvedFrameworkReference)"
38+
Condition="'$(WindowsSdkPackageVersion)' == '' AND '@(ResolvedFrameworkReference)' != '' AND '%(Identity)' == 'Microsoft.Windows.SDK.NET.Ref'">
39+
<Referenced>%(ResolvedFrameworkReference.TargetingPackVersion)</Referenced>
40+
<Required>10.0.$([System.Version]::Parse("%(ResolvedFrameworkReference.TargetingPackVersion)").Build).$(_MvvmToolkitWindowsSdkPackageMinBuild)</Required>
41+
</_MvvmToolkitWindowsSdkPackage>
42+
43+
<!-- Check whether the referenced Windows SDK package is compatible -->
44+
<_MvvmToolkitCompatibleWindowsSdkPackages
45+
Include="@(_MvvmToolkitWindowsSdkPackage)"
46+
Condition="'@(_MvvmToolkitWindowsSdkPackage)' != '' AND $([MSBuild]::VersionGreaterThanOrEquals(%(Referenced), %(Required)))" />
47+
</ItemGroup>
48+
<PropertyGroup>
49+
50+
<!-- Extract the required Windows SDK package version (to show if the one in use is not compatible) -->
51+
<_MvvmToolkitWindowsSdkPackageRequired>@(_MvvmToolkitWindowsSdkPackage->'%(Required)')</_MvvmToolkitWindowsSdkPackageRequired>
52+
</PropertyGroup>
53+
54+
<!-- Emit an error if the Windows SDK package version isn't valid -->
55+
<Error
56+
Condition="'@(_MvvmToolkitCompatibleWindowsSdkPackages)' == ''"
57+
Code="MVVMTKCFG0003"
58+
HelpLink="https://aka.ms/mvvmtoolkit/errors/mvvmtkcfg0003"
59+
Text="This version of the MVVM Toolkit requires 'Microsoft.Windows.SDK.NET.Ref' version '$(_MvvmToolkitWindowsSdkPackageRequired)' or later. Please update to .NET SDK 8.0.109, 8.0.305 or 8.0.402 (or later). Alternatively, use a temporary 'Microsoft.Windows.SDK.NET.Ref' reference, which can be done by setting the 'WindowsSdkPackageVersion' property in your .csproj file to '$(_MvvmToolkitWindowsSdkPackageRequired)'." />
60+
</Target>
61+
62+
</Project>

src/CommunityToolkit.Mvvm/CommunityToolkit.Mvvm.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,11 @@
9999
<!-- Include the custom .targets files (shared across TFMs) -->
100100
<None Include="CommunityToolkit.Mvvm.FeatureSwitches.targets" PackagePath="build" Pack="true" />
101101
<None Include="CommunityToolkit.Mvvm.SourceGenerators.targets" PackagePath="build" Pack="true" />
102+
<None Include="CommunityToolkit.Mvvm.WindowsSdk.targets" PackagePath="build" Pack="true" />
102103
<None Include="CommunityToolkit.Mvvm.targets" PackagePath="build" Pack="true" />
103104
<None Include="CommunityToolkit.Mvvm.FeatureSwitches.targets" PackagePath="buildTransitive" Pack="true" />
104105
<None Include="CommunityToolkit.Mvvm.SourceGenerators.targets" PackagePath="buildTransitive" Pack="true" />
106+
<None Include="CommunityToolkit.Mvvm.WindowsSdk.targets" PackagePath="buildTransitive" Pack="true" />
105107
<None Include="CommunityToolkit.Mvvm.targets" PackagePath="buildTransitive" Pack="true" />
106108

107109
<!--

src/CommunityToolkit.Mvvm/CommunityToolkit.Mvvm.targets

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
<PropertyGroup>
55
<_CommunityToolkitMvvmFeatureSwitchesTargets>$(MSBuildThisFileDirectory)CommunityToolkit.Mvvm.FeatureSwitches.targets</_CommunityToolkitMvvmFeatureSwitchesTargets>
66
<_CommunityToolkitMvvmSourceGeneratorsTargets>$(MSBuildThisFileDirectory)CommunityToolkit.Mvvm.SourceGenerators.targets</_CommunityToolkitMvvmSourceGeneratorsTargets>
7+
<_CommunityToolkitMvvmWindowsSdkTargets>$(MSBuildThisFileDirectory)CommunityToolkit.Mvvm.WindowsSdk.targets</_CommunityToolkitMvvmWindowsSdkTargets>
78
</PropertyGroup>
89

910
<!-- Import all available .targets -->
1011
<Import Project="$(_CommunityToolkitMvvmFeatureSwitchesTargets)" />
1112
<Import Project="$(_CommunityToolkitMvvmSourceGeneratorsTargets)" />
13+
<Import Project="$(_CommunityToolkitMvvmWindowsSdkTargets)" />
1214

1315
</Project>

0 commit comments

Comments
 (0)