Skip to content

Commit 7e1a946

Browse files
committed
Move MSBuild logic into new targets
- Move Package properties from props to targets Include only when the project is packable. - Move SourceGen common MSBuild logic to a new file. This logic will be updated to use custom 'TargetFrameworks' based multi-targeting instead of of using global properties. This move is just for making the git blame and diff helpful later. - Move the T4 MSBuild logic to a new file and generalize them so any project can use it just by including the targets file.
1 parent 40bc8a3 commit 7e1a946

File tree

6 files changed

+69
-55
lines changed

6 files changed

+69
-55
lines changed

CommunityToolkit.Diagnostics/CommunityToolkit.Diagnostics.csproj

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,6 @@
4141
</When>
4242
</Choose>
4343

44-
<!-- T4 service used by the Guard APIs -->
45-
<ItemGroup>
46-
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
47-
</ItemGroup>
48-
49-
<ItemGroup>
50-
<None Update="Generated\*.tt">
51-
<Generator>TextTemplatingFileGenerator</Generator>
52-
<LastGenOutput>%(Filename).g.cs</LastGenOutput>
53-
</None>
54-
</ItemGroup>
55-
56-
<ItemGroup>
57-
<Compile Update="Generated\*.g.cs">
58-
<DesignTime>True</DesignTime>
59-
<AutoGen>True</AutoGen>
60-
<DependentUpon>$([System.IO.Path]::ChangeExtension(%(Filename), '.tt'))</DependentUpon>
61-
</Compile>
62-
</ItemGroup>
44+
<Import Project="$(BuildToolsDirectory)Community.Toolkit.TextTemplates.targets" />
6345

6446
</Project>

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

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,6 @@
55
<TargetFramework>netstandard2.0</TargetFramework>
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-
318
<ItemGroup>
329
<Compile Remove="EmbeddedResources\*.cs" />
3310
<EmbeddedResource Include="EmbeddedResources\*.cs" CopyToOutputDirectory="PreserveNewest" />
@@ -42,4 +19,6 @@
4219
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="$(MvvmToolkitSourceGeneratorRoslynVersion)" PrivateAssets="All" Pack="false" />
4320
</ItemGroup>
4421

22+
<Import Project="$(BuildToolsDirectory)Community.Toolkit.SourceGen.targets" />
23+
4524
</Project>

build/Community.Toolkit.Common.props

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
11
<Project>
22

3-
<PropertyGroup>
4-
<Company>Microsoft</Company>
5-
<Authors>Microsoft</Authors>
6-
<Product>.NET Community Toolkit</Product>
7-
<CommonTags>.NET;Community;Toolkit;dotnet</CommonTags>
8-
<PackageLicenseExpression>MIT</PackageLicenseExpression>
9-
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
10-
<Copyright>(c) .NET Foundation and Contributors. All rights reserved.</Copyright>
11-
<PackageProjectUrl>https://github.com/CommunityToolkit/dotnet</PackageProjectUrl>
12-
<PackageReleaseNotes>https://github.com/CommunityToolkit/dotnet/releases</PackageReleaseNotes>
13-
<PackageIcon>Icon.png</PackageIcon>
14-
</PropertyGroup>
15-
163
<PropertyGroup>
174
<Nullable>Enable</Nullable>
185
<LangVersion>10.0</LangVersion>

build/Community.Toolkit.Common.targets

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
<Project>
22

3+
<PropertyGroup Condition="$(IsPackable)">
4+
<Company>Microsoft</Company>
5+
<Authors>Microsoft</Authors>
6+
<Product>.NET Community Toolkit</Product>
7+
<CommonTags>.NET;Community;Toolkit;dotnet</CommonTags>
8+
<PackageLicenseExpression>MIT</PackageLicenseExpression>
9+
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
10+
<Copyright>(c) .NET Foundation and Contributors. All rights reserved.</Copyright>
11+
<PackageProjectUrl>https://github.com/CommunityToolkit/dotnet</PackageProjectUrl>
12+
<PackageReleaseNotes>https://github.com/CommunityToolkit/dotnet/releases</PackageReleaseNotes>
13+
<PackageIcon>Icon.png</PackageIcon>
14+
</PropertyGroup>
15+
316
<PropertyGroup Condition="$(IsPackable)">
417
<!-- TODO: Dynamically generate Title if one wasn't set -->
518
<Title Condition="'$(Title)' == ''">$(Product) Asset</Title>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<Project>
2+
3+
<!--
4+
The MVVM Toolkit source generators rely on Roslyn multi-targeting to support multiple versions of the 'Microsoft.CodeAnalysis.CSharp' package.
5+
6+
This package reference actually needs a matching version of Roslyn to be available for consumers of the source generator, so if we always
7+
used the latest version, the MVVM Toolkit would just fail to load for users of e.g. an older version of Visual Studio. Thankfully, Roslyn
8+
supports analyzers that bundle multiple versions in the same NuGet package, each in a subfolder with a name matching the Roslyn version.
9+
10+
To leverage this, this project receives the 'MvvmToolkitSourceGeneratorRoslynVersion' property as input, so that the MVVM Toolkit can build
11+
it multiple times with multiple versions during packing, to then extract each .dll and copy it to the right NuGet package folder.
12+
-->
13+
14+
<PropertyGroup>
15+
<!-- When the property is not set (which will not happen when creating a NuGet package anyway), just default to 4.0.1 -->
16+
<MvvmToolkitSourceGeneratorRoslynVersion Condition="'$(MvvmToolkitSourceGeneratorRoslynVersion)' == ''">4.0.1</MvvmToolkitSourceGeneratorRoslynVersion>
17+
18+
<!--
19+
The output path will end up being eg. 'bin\Debug\roslyn4.0\netstandard2.0' (the trailing 'netstandard2.0' is added automatically).
20+
This is only set when $(MvvmToolkitIsGeneratingNuGetPackage) is set (when packing CommunityToolkit.Mvvm). This avoids unit test issues.
21+
-->
22+
<OutputPath Condition="'$(MvvmToolkitIsGeneratingNuGetPackage)' == 'true'">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+
28+
</Project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project>
2+
3+
<!-- T4 text template service used by the Visual Studio project system -->
4+
<ItemGroup>
5+
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
6+
</ItemGroup>
7+
8+
<!-- Using T4 text templates to generate sources -->
9+
<ItemGroup>
10+
<None Update="**\*.tt">
11+
<Generator>TextTemplatingFileGenerator</Generator>
12+
<LastGenOutput>%(Filename).g.cs</LastGenOutput>
13+
</None>
14+
</ItemGroup>
15+
16+
<!-- Sources generated based on the above text templates -->
17+
<ItemGroup>
18+
<Compile Update="**\*.g.cs">
19+
<AutoGen>True</AutoGen>
20+
<DesignTime>True</DesignTime>
21+
<DependentUpon>$([System.IO.Path]::ChangeExtension(%(Filename), '.tt'))</DependentUpon>
22+
</Compile>
23+
</ItemGroup>
24+
25+
</Project>

0 commit comments

Comments
 (0)