Skip to content

Commit 7bd28e4

Browse files
committed
Enable Roslyn multi-targeting in the MVVM Toolkit generator
1 parent 9d1a525 commit 7bd28e4

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,26 @@
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+
<!-- The output path will end up being eg. "bin\Debug\roslyn4.0\netstandard2.0" (the trailing "netstandard2.0" is added automatically) -->
22+
<OutputPath>bin\$(Configuration)\roslyn$(MvvmToolkitSourceGeneratorRoslynVersion.Substring(0, 3))</OutputPath>
23+
24+
<!-- Also define a "ROSLYN_<MAJOR>_<MINOR>" build constant, so the generator code can multi-target whenever needed and add any required polyfills -->
25+
<DefineConstants>$(DefineConstants);ROSLYN_$(MvvmToolkitSourceGeneratorRoslynVersion.Substring(0, 3).Replace('.', '_'))</DefineConstants>
26+
</PropertyGroup>
27+
828
<ItemGroup>
929
<Compile Remove="EmbeddedResources\INotifyPropertyChanged.cs" />
1030
<Compile Remove="EmbeddedResources\NotNullAttribute.cs" />
@@ -32,7 +52,7 @@
3252
</ItemGroup>
3353

3454
<ItemGroup>
35-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.0.1" PrivateAssets="all" Pack="false" />
55+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="$(MvvmToolkitSourceGeneratorRoslynVersion)" PrivateAssets="all" Pack="false" />
3656
</ItemGroup>
3757

3858
<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</Properties>
47+
</ProjectReference>
48+
<ProjectReference Include="..\CommunityToolkit.Mvvm.SourceGenerators\CommunityToolkit.Mvvm.SourceGenerators.csproj" ReferenceOutputAssembly="false">
49+
<Properties>MvvmToolkitSourceGeneratorRoslynVersion=4.3.0</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>

0 commit comments

Comments
 (0)