Skip to content

Commit 2a47127

Browse files
authored
Merge pull request #541 from CommunityToolkit/dev/multi-target-legacy-support
Add target to fix analyzers on legacy .csproj projects
2 parents 333265f + 6cf0dc0 commit 2a47127

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/CommunityToolkit.Mvvm/CommunityToolkit.Mvvm.targets

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,36 @@
4141
<Warning Condition ="'$(MVVMToolkitCurrentCompilerVersionIsNotNewEnough)' == '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."/>
4242
</Target>
4343

44+
<!--
45+
Manually remove duplicate analyzers if Roslyn component versioning is not supported (ie. if a legacy .csproj project is used).
46+
This target is only run if Roslyn 4.0 or greater is present, as otherwise all analyzers would have already been removed anyway.
47+
-->
48+
<Target Name="MVVMToolkitRemoveDuplicateAnalyzersWhenRoslynComponentVersioningIsNotSupported"
49+
Condition="'$(MVVMToolkitCurrentCompilerVersionIsNotNewEnough)' != 'true' AND '$(SupportsRoslynComponentVersioning)' != 'true'"
50+
AfterTargets="ResolvePackageDependenciesForBuild;ResolveNuGetPackageAssets"
51+
DependsOnTargets="MVVMToolkitRemoveAnalyzersForRoslyn3">
52+
53+
<!--
54+
This switch manually implements Roslyn component versioning. That is, it checks the current version of Roslyn and
55+
removes and removes all analyzers except the highest version that is supported. The fallback is just Roslyn 4.0.
56+
-->
57+
<PropertyGroup>
58+
<MVVMToolkitSelectedRoslynAnalyzerDirectoryName Condition="$([MSBuild]::VersionGreaterThanOrEquals($(MVVMToolkitCurrentCompilerVersion), 4.3))">roslyn4.3</MVVMToolkitSelectedRoslynAnalyzerDirectoryName>
59+
<MVVMToolkitSelectedRoslynAnalyzerDirectoryName Condition="'$(MVVMToolkitSelectedRoslynAnalyzerDirectoryName)' == ''">roslyn4.0</MVVMToolkitSelectedRoslynAnalyzerDirectoryName>
60+
</PropertyGroup>
61+
<ItemGroup>
62+
63+
<!--
64+
This condition is a bit convoluted, but it's essentially just selecting all analyzers from the NuGet package that don't have the target Roslyn directory name in their full path.
65+
For instance, if Roslyn 4.3 is the highest supported version, the target directory name will be "roslyn 4.3", and this condition will filter out all analyzers with a path such
66+
as: "C:\...\.nuget\...\CommunityToolkit.Mvvm\analyzers\roslyn4.0\cs\CommunityToolkit.Mvvm". The [System.String]::Concat trick is used to achieve two things: we can't directly
67+
invoke a property function (ie. Contains in this case) on a metadata item, so we need an intermediate string to invoke it on. We could also use [System.String]::new, but using
68+
Concat is more efficient as it'll just skip the allocation entirely if one of the two inputs is an empty string, which is the case here.
69+
-->
70+
<Analyzer Remove="@(MVVMToolkitAnalyzer)" Condition="!$([System.String]::Concat('', '%(MVVMToolkitAnalyzer.FullPath)').Contains('$(MVVMToolkitSelectedRoslynAnalyzerDirectoryName)'))"/>
71+
</ItemGroup>
72+
</Target>
73+
4474
<!-- Remove the analyzer if Roslyn is missing -->
4575
<Target Name="MVVMToolkitRemoveAnalyzersForRosynNotFound"
4676
Condition="'$(CSharpCoreTargetsPath)' == ''"

0 commit comments

Comments
 (0)