Skip to content

Commit 0ae955d

Browse files
committed
Add .targets file to disable source generators on Roslyn 3.x
Still a bit experimental, needs more testing and validation
1 parent 8cdc261 commit 0ae955d

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

CommunityToolkit.Mvvm/CommunityToolkit.Mvvm.csproj

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
- Ioc: a helper class to configure dependency injection service containers.
1919
</Description>
2020
<PackageTags>MVVM;Toolkit;MVVMToolkit;INotifyPropertyChanged;Observable;IOC;DI;Dependency Injection;Object Messaging;Extensions;Helpers</PackageTags>
21-
<TargetsForTfmSpecificContentInPackage>$(TargetsForTfmSpecificContentInPackage);CopyAnalyzerProjectReferencesToPackage</TargetsForTfmSpecificContentInPackage>
2221
</PropertyGroup>
2322

2423
<!-- .NET Standard 2.0 doesn't have the Span<T> and IAsyncEnumerable<T> types -->
@@ -36,22 +35,23 @@
3635
</ItemGroup>
3736

3837
<!-- Source generator project reference for packing -->
39-
<ItemGroup>
40-
<ProjectReference Include="..\CommunityToolkit.Mvvm.SourceGenerators\CommunityToolkit.Mvvm.SourceGenerators.csproj" PrivateAssets="all" />
38+
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
39+
<ProjectReference Include="..\CommunityToolkit.Mvvm.SourceGenerators\CommunityToolkit.Mvvm.SourceGenerators.csproj" ReferenceOutputAssembly="false" />
4140
</ItemGroup>
4241

43-
<!--
44-
Target to pack the source generator into the "analyzers\dotnet\cs" package folder.
45-
The condition is set to .NET Standard 2.0 so that the analyzer is only added to the package
46-
when that target is being built, instead of once for all three targets, which would fail.
47-
It will still be available for all targets anyway though, as analyzers don't have a target.
48-
-->
49-
<Target Name="CopyAnalyzerProjectReferencesToPackage" DependsOnTargets="BuildOnlySettings;ResolveReferences" Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
50-
<ItemGroup>
51-
<TfmSpecificPackageFile Include="@(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget', 'ProjectReference')->WithMetadataValue('PrivateAssets', 'All'))">
52-
<PackagePath>analyzers\dotnet\cs</PackagePath>
53-
</TfmSpecificPackageFile>
54-
</ItemGroup>
55-
</Target>
42+
<ItemGroup Label="Package">
43+
44+
<!-- Include the custom .targets file to check the source generator (.NET 6 is not needed as it guarantees Roslyn 4.x) -->
45+
<None Include="CommunityToolkit.Mvvm.targets" PackagePath="buildTransitive\netstandard2.0" Pack="true" />
46+
<None Include="CommunityToolkit.Mvvm.targets" PackagePath="buildTransitive\netstandard2.1" Pack="true" />
47+
<None Include="CommunityToolkit.Mvvm.targets" PackagePath="build\netstandard2.0" Pack="true" />
48+
<None Include="CommunityToolkit.Mvvm.targets" PackagePath="build\netstandard2.1" Pack="true" />
49+
50+
<!-- Pack the source generator to the right package folder -->
51+
<None Include="..\CommunityToolkit.Mvvm.SourceGenerators\bin\$(Configuration)\netstandard2.0\CommunityToolkit.Mvvm.SourceGenerators.dll"
52+
PackagePath="analyzers\dotnet\roslyn4.0\cs"
53+
Pack="true"
54+
Visible="false" />
55+
</ItemGroup>
5656

5757
</Project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project>
2+
3+
<!-- Get the analyzer from the CommunityToolkit.Mvvm NuGet package -->
4+
<Target Name="_MVVMToolkitGatherAnalyzers">
5+
<ItemGroup>
6+
<_MVVMToolkitAnalyzer Include="@(Analyzer)" Condition="'%(Analyzer.NuGetPackageId)' == 'CommunityToolkit.Mvvm'" />
7+
</ItemGroup>
8+
</Target>
9+
10+
<!-- Remove the analyzer if using Roslyn 3.x (incremental generators require Roslyn 4.x) -->
11+
<Target Name="_MVVMToolkitRemoveAnalyzersForRoslyn3"
12+
Condition="'$(SupportsRoslynComponentVersioning)' != 'true'"
13+
AfterTargets="ResolvePackageDependenciesForBuild;ResolveNuGetPackageAssets"
14+
DependsOnTargets="_MVVMToolkitGatherAnalyzers">
15+
<ItemGroup>
16+
<Analyzer Remove="@(_MVVMToolkitAnalyzer)"/>
17+
</ItemGroup>
18+
<Warning 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."/>
19+
</Target>
20+
21+
</Project>

0 commit comments

Comments
 (0)