Skip to content

Commit 04c514c

Browse files
authored
Merge pull request #942 from CommunityToolkit/dev/windows-sdk-validation
Add .targets to validate the Windows SDK version
2 parents 1f8e527 + bbb4fb2 commit 04c514c

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
<!--
25+
The minimum build versions for the Windows SDK package. We keep track of up to 3 different versions:
26+
- The minimum Windows SDK package for which we don't want to emit a build error.
27+
- The recommended Windows SDK package for UWP projects using the .NET 9 SDK (with UWP XAML projections).
28+
- The recommended Windows SDK package for WindowsAppSDK projects (ie. without UWP XAML projections).
29+
-->
30+
<_MvvmToolkitWindowsSdkPackageMinBuild>38</_MvvmToolkitWindowsSdkPackageMinBuild>
31+
<_MvvmToolkitWindowsSdkPackageRecommendedUwpBuild>39</_MvvmToolkitWindowsSdkPackageRecommendedUwpBuild>
32+
<_MvvmToolkitWindowsSdkPackageRecommendedWindowsAppSDKBuild>41</_MvvmToolkitWindowsSdkPackageRecommendedWindowsAppSDKBuild>
33+
34+
<!-- Switch on the recommended one based on configuration -->
35+
<_MvvmToolkitWindowsSdkPackageRecommendedBuild>$(_MvvmToolkitWindowsSdkPackageMinBuild)</_MvvmToolkitWindowsSdkPackageRecommendedBuild>
36+
<_MvvmToolkitWindowsSdkPackageRecommendedBuild Condition="'$(UseUwp)' == 'true'">$(_MvvmToolkitWindowsSdkPackageRecommendedUwpBuild)</_MvvmToolkitWindowsSdkPackageRecommendedBuild>
37+
<_MvvmToolkitWindowsSdkPackageRecommendedBuild Condition="'$(UseUwp)' != 'true'">$(_MvvmToolkitWindowsSdkPackageRecommendedWindowsAppSDKBuild)</_MvvmToolkitWindowsSdkPackageRecommendedBuild>
38+
</PropertyGroup>
39+
<ItemGroup>
40+
41+
<!-- If 'WindowsSdkPackageVersion' is explicitly set, validate against that -->
42+
<_MvvmToolkitWindowsSdkPackage Include="$(WindowsSdkPackageVersion)" Condition="'$(WindowsSdkPackageVersion)' != ''">
43+
<Referenced>$(WindowsSdkPackageVersion)</Referenced>
44+
<Required>10.0.$([System.Version]::Parse("$(WindowsSdkPackageVersion.Split('-')[0])").Build).$(_MvvmToolkitWindowsSdkPackageMinBuild)</Required>
45+
<Recommended>10.0.$([System.Version]::Parse("$(WindowsSdkPackageVersion.Split('-')[0])").Build).$(_MvvmToolkitWindowsSdkPackageRecommendedBuild)</Recommended>
46+
</_MvvmToolkitWindowsSdkPackage>
47+
48+
<!-- Otherwise, validate against the framework reference package -->
49+
<_MvvmToolkitWindowsSdkPackage
50+
Include="@(ResolvedFrameworkReference)"
51+
Condition="'$(WindowsSdkPackageVersion)' == '' AND '@(ResolvedFrameworkReference)' != '' AND '%(Identity)' == 'Microsoft.Windows.SDK.NET.Ref'">
52+
<Referenced>%(ResolvedFrameworkReference.TargetingPackVersion)</Referenced>
53+
<Required>10.0.$([System.Version]::Parse("%(ResolvedFrameworkReference.TargetingPackVersion)").Build).$(_MvvmToolkitWindowsSdkPackageMinBuild)</Required>
54+
<Recommended>10.0.$([System.Version]::Parse("%(ResolvedFrameworkReference.TargetingPackVersion)").Build).$(_MvvmToolkitWindowsSdkPackageRecommendedBuild)</Recommended>
55+
</_MvvmToolkitWindowsSdkPackage>
56+
57+
<!-- Check whether the referenced Windows SDK package is compatible -->
58+
<_MvvmToolkitCompatibleWindowsSdkPackages
59+
Include="@(_MvvmToolkitWindowsSdkPackage)"
60+
Condition="'@(_MvvmToolkitWindowsSdkPackage)' != '' AND $([MSBuild]::VersionGreaterThanOrEquals(%(Referenced), %(Required)))" />
61+
</ItemGroup>
62+
<PropertyGroup>
63+
64+
<!-- Extract the required and recommended Windows SDK package version (to show if the one in use is not compatible) -->
65+
<_MvvmToolkitWindowsSdkPackageRequired>@(_MvvmToolkitWindowsSdkPackage->'%(Required)')</_MvvmToolkitWindowsSdkPackageRequired>
66+
<_MvvmToolkitWindowsSdkPackageRecommended>@(_MvvmToolkitWindowsSdkPackage->'%(Recommended)')</_MvvmToolkitWindowsSdkPackageRecommended>
67+
</PropertyGroup>
68+
69+
<!-- Emit an error if the Windows SDK package version isn't valid -->
70+
<Error
71+
Condition="'@(_MvvmToolkitCompatibleWindowsSdkPackages)' == ''"
72+
Code="MVVMTKCFG0003"
73+
HelpLink="https://aka.ms/mvvmtoolkit/errors/mvvmtkcfg0003"
74+
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. For your project configuration, it is recommended to set the package version to '$(_MvvmToolkitWindowsSdkPackageRecommended)'." />
75+
</Target>
76+
77+
</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)