Refactor conditional logic into separate target

This works around <Choose> not supporting <Target> parents
This commit is contained in:
Sergio Pedri 2022-01-14 13:34:22 +01:00
Родитель 0d894ea7c9
Коммит 9b1596d453
1 изменённых файлов: 33 добавлений и 34 удалений

Просмотреть файл

@ -9,12 +9,10 @@
<!-- Remove the analyzer if using Roslyn 3.x (incremental generators require Roslyn 4.x) -->
<Target Name="_MVVMToolkitRemoveAnalyzersForRoslyn3"
Condition="'$(CSharpCoreTargetsPath)' != ''"
AfterTargets="ResolvePackageDependenciesForBuild;ResolveNuGetPackageAssets"
DependsOnTargets="_MVVMToolkitGatherAnalyzers">
<Choose>
<When Condition="'$(CSharpCoreTargetsPath)' != ''">
<!-- Use the CSharpCoreTargetsPath property to find the version of the compiler we are using. This is the same mechanism
MSBuild uses to find the compiler. We could check the assembly version for any compiler assembly (since they all have
the same version) but Microsoft.Build.Tasks.CodeAnalysis.dll is where MSBuild loads the compiler tasks from so if
@ -30,7 +28,6 @@
<!-- The CurrentCompilerVersionIsNotNewEnough property can now be defined based on the Roslyn assembly version -->
<CurrentCompilerVersionIsNotNewEnough Condition="$([MSBuild]::VersionLessThan($(CurrentCompilerVersion), 4.0))">true</CurrentCompilerVersionIsNotNewEnough>
<CurrentCompilerVersionIsNotNewEnough Condition="'$(CurrentCompilerVersionIsNotNewEnough)' == ''">false</CurrentCompilerVersionIsNotNewEnough>
</PropertyGroup>
<!-- If the Roslyn version is < 4.0, disable the source generators -->
@ -42,16 +39,18 @@
emitting this manually lets us customize the message to inform developers as to why exactly the generators have been
disabled, and that the rest of the MVVM Toolkit will still keep working as intended, just without additional features. -->
<Warning Condition ="'$(CurrentCompilerVersionIsNotNewEnough)' == 'true'" Text="The MVVM Toolkit source generators have been disabled on the current configuration, as they need Roslyn 4.x in order to work. The MVVM Toolkit will work just fine, but features relying on the source generators will not be available."/>
</When>
</Target>
<Otherwise>
<!-- Remove the analyzer if Roslyn is missing -->
<Target Name="_MVVMToolkitRemoveAnalyzersForRosynNotFound"
Condition="'$(CSharpCoreTargetsPath)' == ''"
AfterTargets="ResolvePackageDependenciesForBuild;ResolveNuGetPackageAssets"
DependsOnTargets="_MVVMToolkitGatherAnalyzers">
<!-- If no Roslyn assembly could be found, just remove the analyzer without emitting a warning -->
<ItemGroup>
<Analyzer Remove="@(_MVVMToolkitAnalyzer)"/>
</ItemGroup>
</Otherwise>
</Choose>
</Target>
</Project>