Skip to content

Commit 5be269e

Browse files
authored
Merge pull request #428 from CommunityToolkit/dev/roslyn-multi-targeting
Multi-target Roslyn dependency in MVVM Toolkit generators
2 parents 4fae4dd + f577ec0 commit 5be269e

File tree

6 files changed

+71
-13
lines changed

6 files changed

+71
-13
lines changed

CommunityToolkit.Mvvm.SourceGenerators/CommunityToolkit.Mvvm.SourceGenerators.csproj

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,29 @@
55
<IsPackable>false</IsPackable>
66
</PropertyGroup>
77

8+
<!--
9+
The MVVM Toolkit source generators rely on Roslyn multi-targeting to support multiple versions of the Microsoft.CodeAnalysis.CSharp package.
10+
This package reference actually needs a matching version of Roslyn to be available for consumers of the source generator, so if we always
11+
used the latest version, the MVVM Toolkit would just fail to load for users of eg. an older version of Visual Studio. Thankfully, Roslyn
12+
supports analyzers that bundle multiple versions in the same NuGet package, each in a subfolder with a name matching the Roslyn version.
13+
To leverage this, this project receives the MvvmToolkitSourceGeneratorRoslynVersion property as input, so that the MVVM Toolkit can build
14+
it multiple times with multiple versions during packing, to then extract each .dll and copy it to the right NuGet package folder.
15+
-->
16+
<PropertyGroup>
17+
18+
<!-- When the property is not set (which will not happen when creating a NuGet package anyway), just default to 4.0.1 -->
19+
<MvvmToolkitSourceGeneratorRoslynVersion Condition="'$(MvvmToolkitSourceGeneratorRoslynVersion)' == ''">4.0.1</MvvmToolkitSourceGeneratorRoslynVersion>
20+
21+
<!--
22+
The output path will end up being eg. "bin\Debug\roslyn4.0\netstandard2.0" (the trailing "netstandard2.0" is added automatically).
23+
This is only set when $(MvvmToolkitIsGeneratingNuGetPackage) is set (when packing CommunityToolkit.Mvvm). This avoids unit test issues.
24+
-->
25+
<OutputPath Condition="'$(MvvmToolkitIsGeneratingNuGetPackage)' == 'true'">bin\$(Configuration)\roslyn$(MvvmToolkitSourceGeneratorRoslynVersion.Substring(0, 3))\</OutputPath>
26+
27+
<!-- Also define a "ROSLYN_<MAJOR>_<MINOR>" build constant, so the generator code can multi-target whenever needed and add any required polyfills -->
28+
<DefineConstants>$(DefineConstants);ROSLYN_$(MvvmToolkitSourceGeneratorRoslynVersion.Substring(0, 3).Replace('.', '_'))</DefineConstants>
29+
</PropertyGroup>
30+
831
<ItemGroup>
932
<Compile Remove="EmbeddedResources\INotifyPropertyChanged.cs" />
1033
<Compile Remove="EmbeddedResources\NotNullAttribute.cs" />
@@ -32,7 +55,7 @@
3255
</ItemGroup>
3356

3457
<ItemGroup>
35-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.0.1" PrivateAssets="all" Pack="false" />
58+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="$(MvvmToolkitSourceGeneratorRoslynVersion)" PrivateAssets="all" Pack="false" />
3659
</ItemGroup>
3760

3861
<ItemGroup>

CommunityToolkit.Mvvm/CommunityToolkit.Mvvm.csproj

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,14 @@
4040
<IsTrimmable>true</IsTrimmable>
4141
</PropertyGroup>
4242

43-
<!-- Source generator project reference for packing -->
43+
<!-- Reference the various multi-targeted versions of the source generator project (one per Roslyn version) -->
4444
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
45-
<ProjectReference Include="..\CommunityToolkit.Mvvm.SourceGenerators\CommunityToolkit.Mvvm.SourceGenerators.csproj" ReferenceOutputAssembly="false" />
45+
<ProjectReference Include="..\CommunityToolkit.Mvvm.SourceGenerators\CommunityToolkit.Mvvm.SourceGenerators.csproj" ReferenceOutputAssembly="false">
46+
<Properties>MvvmToolkitSourceGeneratorRoslynVersion=4.0.1;MvvmToolkitIsGeneratingNuGetPackage=true</Properties>
47+
</ProjectReference>
48+
<ProjectReference Include="..\CommunityToolkit.Mvvm.SourceGenerators\CommunityToolkit.Mvvm.SourceGenerators.csproj" ReferenceOutputAssembly="false">
49+
<Properties>MvvmToolkitSourceGeneratorRoslynVersion=4.3.0;MvvmToolkitIsGeneratingNuGetPackage=true</Properties>
50+
</ProjectReference>
4651
</ItemGroup>
4752

4853
<ItemGroup Label="Package">
@@ -53,11 +58,12 @@
5358
<None Include="CommunityToolkit.Mvvm.targets" PackagePath="build\netstandard2.0" Pack="true" />
5459
<None Include="CommunityToolkit.Mvvm.targets" PackagePath="build\netstandard2.1" Pack="true" />
5560

56-
<!-- Pack the source generator to the right package folder -->
57-
<None Include="..\CommunityToolkit.Mvvm.SourceGenerators\bin\$(Configuration)\netstandard2.0\CommunityToolkit.Mvvm.SourceGenerators.dll"
58-
PackagePath="analyzers\dotnet\roslyn4.0\cs"
59-
Pack="true"
60-
Visible="false" />
61+
<!--
62+
Pack the source generator to the right package folders (each matching the target Roslyn version).
63+
Roslyn will automatically load the highest version compatible with Roslyn's version in the SDK.
64+
-->
65+
<None Include="..\CommunityToolkit.Mvvm.SourceGenerators\bin\$(Configuration)\roslyn4.0\netstandard2.0\CommunityToolkit.Mvvm.SourceGenerators.dll" PackagePath="analyzers\dotnet\roslyn4.0\cs" Pack="true" Visible="false" />
66+
<None Include="..\CommunityToolkit.Mvvm.SourceGenerators\bin\$(Configuration)\roslyn4.3\netstandard2.0\CommunityToolkit.Mvvm.SourceGenerators.dll" PackagePath="analyzers\dotnet\roslyn4.3\cs" Pack="true" Visible="false" />
6167
</ItemGroup>
6268

6369
</Project>

azure-pipelines.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ jobs:
4646
- script: dotnet test -c Release -f net6.0 -l "trx;LogFileName=VSTestResults_net6.0.trx"
4747
displayName: Run .NET 6 unit tests
4848

49+
# Run the .NET 6 MVVM Toolkit tests targeting Roslyn 4.0.1
50+
- script: dotnet test tests\CommunityToolkit.Mvvm.UnitTests\CommunityToolkit.Mvvm.UnitTests.csproj -c Release -f net6.0 -l "trx;LogFileName=VSTestResults_net6.0_mvvmtoolkit_roslyn401.trx"
51+
displayName: Run CommunityToolkit.Mvvm.UnitTests unit tests with Roslyn 4.0.1
52+
53+
# Run the .NET 6 MVVM Toolkit source generator tests targeting Roslyn 4.0.1
54+
- script: dotnet test tests\CommunityToolkit.Mvvm.SourceGenerators.UnitTests\CommunityToolkit.Mvvm.SourceGenerators.UnitTests.csproj -c Release -f net6.0 -l "trx;LogFileName=VSTestResults_net6.0_mvvmtoolkit_generators_roslyn401.trx"
55+
displayName: Run CommunityToolkit.Mvvm.UnitTests unit tests with Roslyn 4.0.1
56+
4957
# Run .NET Core 3.1 tests
5058
- script: dotnet test -c Release -f netcoreapp3.1 -l "trx;LogFileName=VSTestResults_netcoreapp3.1.trx"
5159
displayName: Run .NET Core 3.1 unit tests

tests/CommunityToolkit.Mvvm.ExternalAssembly/CommunityToolkit.Mvvm.ExternalAssembly.csproj

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@
1212
<NoWarn>$(NoWarn);CS8002;SA0001</NoWarn>
1313
</PropertyGroup>
1414

15+
<!-- See comments in CommunityToolkit.MvvmUnitTests -->
16+
<PropertyGroup>
17+
<MvvmToolkitSourceGeneratorRoslynVersion Condition="'$(MvvmToolkitSourceGeneratorRoslynVersion)' == ''">4.3.0</MvvmToolkitSourceGeneratorRoslynVersion>
18+
</PropertyGroup>
1519
<ItemGroup>
1620
<ProjectReference Include="..\..\CommunityToolkit.Mvvm\CommunityToolkit.Mvvm.csproj" />
17-
<ProjectReference Include="..\..\CommunityToolkit.Mvvm.SourceGenerators\CommunityToolkit.Mvvm.SourceGenerators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" PrivateAssets="contentfiles;build" />
21+
<ProjectReference Include="..\..\CommunityToolkit.Mvvm.SourceGenerators\CommunityToolkit.Mvvm.SourceGenerators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" PrivateAssets="contentfiles;build">
22+
<Properties>MvvmToolkitSourceGeneratorRoslynVersion=$(MvvmToolkitSourceGeneratorRoslynVersion)</Properties>
23+
</ProjectReference>
1824
</ItemGroup>
1925

2026
</Project>

tests/CommunityToolkit.Mvvm.SourceGenerators.UnitTests/CommunityToolkit.Mvvm.SourceGenerators.UnitTests.csproj

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFrameworks>net472;netcoreapp3.1;net6.0</TargetFrameworks>
@@ -11,9 +11,15 @@
1111
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
1212
</ItemGroup>
1313

14+
<!-- See comments in CommunityToolkit.MvvmUnitTests -->
15+
<PropertyGroup>
16+
<MvvmToolkitSourceGeneratorRoslynVersion Condition="'$(MvvmToolkitSourceGeneratorRoslynVersion)' == ''">4.3.0</MvvmToolkitSourceGeneratorRoslynVersion>
17+
</PropertyGroup>
1418
<ItemGroup>
15-
<ProjectReference Include="..\..\CommunityToolkit.Mvvm.SourceGenerators\CommunityToolkit.Mvvm.SourceGenerators.csproj" />
1619
<ProjectReference Include="..\..\CommunityToolkit.Mvvm\CommunityToolkit.Mvvm.csproj" />
20+
<ProjectReference Include="..\..\CommunityToolkit.Mvvm.SourceGenerators\CommunityToolkit.Mvvm.SourceGenerators.csproj">
21+
<Properties>MvvmToolkitSourceGeneratorRoslynVersion=$(MvvmToolkitSourceGeneratorRoslynVersion)</Properties>
22+
</ProjectReference>
1723
</ItemGroup>
1824

1925
</Project>

tests/CommunityToolkit.Mvvm.UnitTests/CommunityToolkit.Mvvm.UnitTests.csproj

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,18 @@
1212
</ItemGroup>
1313

1414
<ItemGroup>
15-
<ProjectReference Include="..\..\CommunityToolkit.Mvvm\CommunityToolkit.Mvvm.csproj" />
16-
<ProjectReference Include="..\..\CommunityToolkit.Mvvm.SourceGenerators\CommunityToolkit.Mvvm.SourceGenerators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" PrivateAssets="contentfiles;build" />
1715
<ProjectReference Include="..\CommunityToolkit.Mvvm.ExternalAssembly\CommunityToolkit.Mvvm.ExternalAssembly.csproj" />
1816
</ItemGroup>
1917

18+
<!-- Since the MVVM Toolkit generator uses multi-targeting for Roslyn versions, we also want to test all of them here -->
19+
<PropertyGroup>
20+
<MvvmToolkitSourceGeneratorRoslynVersion Condition="'$(MvvmToolkitSourceGeneratorRoslynVersion)' == ''">4.3.0</MvvmToolkitSourceGeneratorRoslynVersion>
21+
</PropertyGroup>
22+
<ItemGroup>
23+
<ProjectReference Include="..\..\CommunityToolkit.Mvvm\CommunityToolkit.Mvvm.csproj" />
24+
<ProjectReference Include="..\..\CommunityToolkit.Mvvm.SourceGenerators\CommunityToolkit.Mvvm.SourceGenerators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" PrivateAssets="contentfiles;build">
25+
<Properties>MvvmToolkitSourceGeneratorRoslynVersion=$(MvvmToolkitSourceGeneratorRoslynVersion)</Properties>
26+
</ProjectReference>
27+
</ItemGroup>
28+
2029
</Project>

0 commit comments

Comments
 (0)