Skip to content

Commit 861beea

Browse files
committed
Update .targets for MVVM Toolkit to fix .wapproj issues
1 parent d3d9752 commit 861beea

File tree

1 file changed

+53
-24
lines changed

1 file changed

+53
-24
lines changed

CommunityToolkit.Mvvm/CommunityToolkit.Mvvm.targets

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,62 @@
1212
AfterTargets="ResolvePackageDependenciesForBuild;ResolveNuGetPackageAssets"
1313
DependsOnTargets="_MVVMToolkitGatherAnalyzers">
1414

15-
<!-- Use the CSharpCoreTargetsPath property to find the version of the compiler we are using. This is the same mechanism
16-
MSBuild uses to find the compiler. We could check the assembly version for any compiler assembly (since they all have
17-
the same version) but Microsoft.Build.Tasks.CodeAnalysis.dll is where MSBuild loads the compiler tasks from so if
18-
someone is getting creative with msbuild tasks/targets this is the "most correct" assembly to check. -->
19-
<GetAssemblyIdentity AssemblyFiles="$([System.IO.Path]::Combine(`$([System.IO.Path]::GetDirectoryName($(CSharpCoreTargetsPath)))`,`Microsoft.Build.Tasks.CodeAnalysis.dll`))">
20-
<Output TaskParameter="Assemblies" ItemName="CurrentCompilerAssemblyIdentity"/>
21-
</GetAssemblyIdentity>
22-
2315
<PropertyGroup>
24-
25-
<!-- Transform the resulting item from GetAssemblyIdentity into a property representing its assembly version -->
26-
<CurrentCompilerVersion>@(CurrentCompilerAssemblyIdentity->'%(Version)')</CurrentCompilerVersion>
2716

28-
<!-- The CurrentCompilerVersionIsNotNewEnough property can now be defined based on the Roslyn assembly version -->
29-
<CurrentCompilerVersionIsNotNewEnough Condition="$([MSBuild]::VersionLessThan($(CurrentCompilerVersion), 4.0))">true</CurrentCompilerVersionIsNotNewEnough>
30-
<CurrentCompilerVersionIsNotNewEnough Condition="$(CurrentCompilerVersionIsNotNewEnough) == ''">false</CurrentCompilerVersionIsNotNewEnough>
17+
<!-- If CSharpCoreTargetsPath is not empty, the Roslyn path is the directory name from there -->
18+
<RoslynDirectoryName Condition="$(CSharpCoreTargetsPath) != ''">$([System.IO.Path]::GetDirectoryName($(CSharpCoreTargetsPath))</RoslynDirectoryName>
19+
20+
<!-- Otherwise, fallback to $(MSBuildBinPath)\Roslyn\ -->
21+
<RoslynDirectoryName Condition="$(RoslynDirectoryName) == ''">$([System.IO.Path]::Combine(`$(MSBuildBinPath)`,`Roslyn`))</RoslynDirectoryName>
22+
23+
<!-- Actually check that Microsoft.Build.Tasks.CodeAnalysis.dll exists at the resolved location.
24+
If it doesn't (eg. if the consuming project is a .wapproj project), remove the analyzer.
25+
In that case, the warning will also not be emitted, as there are no C# files to compile anyway. -->
26+
<RoslynAssemblyPath>$([System.IO.Path]::Combine(`$(RoslynDirectoryName)`,`Microsoft.Build.Tasks.CodeAnalysis.dll`))</RoslynAssemblyPath>
27+
<IsRoslynAssemblyPresent Condition="$([System.IO.File]::Exists(`$(RoslynAssemblyPath)`)">true</IsRoslynAssemblyPresent>
28+
<IsRoslynAssemblyPresent Condition="$(IsRoslynAssemblyPresent) == ''">false</IsRoslynAssemblyPresent>
3129
</PropertyGroup>
32-
33-
<!-- If the Roslyn version is < 4.0, disable the source generators -->
34-
<ItemGroup Condition ="$(CurrentCompilerVersionIsNotNewEnough) == 'true'">
35-
<Analyzer Remove="@(_MVVMToolkitAnalyzer)"/>
36-
</ItemGroup>
37-
38-
<!-- If the source generators are disabled, also emit a warning. This would've been produced by MSBuild itself as well, but
39-
emitting this manually lets us customize the message to inform developers as to why exactly the generators have been
40-
disabled, and that the rest of the MVVM Toolkit will still keep working as intended, just without additional features. -->
41-
<Warning Condition ="$(CurrentCompilerVersionIsNotNewEnough) == '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."/>
30+
31+
<Choose>
32+
<When Condition="$(IsRoslynAssemblyPresent) == 'true'">
33+
34+
<!-- Use the CSharpCoreTargetsPath property to find the version of the compiler we are using. This is the same mechanism
35+
MSBuild uses to find the compiler. We could check the assembly version for any compiler assembly (since they all have
36+
the same version) but Microsoft.Build.Tasks.CodeAnalysis.dll is where MSBuild loads the compiler tasks from so if
37+
someone is getting creative with msbuild tasks/targets this is the "most correct" assembly to check. -->
38+
<GetAssemblyIdentity AssemblyFiles="$(RoslynAssemblyPath)">
39+
<Output TaskParameter="Assemblies" ItemName="CurrentCompilerAssemblyIdentity"/>
40+
</GetAssemblyIdentity>
41+
42+
<PropertyGroup>
43+
44+
<!-- Transform the resulting item from GetAssemblyIdentity into a property representing its assembly version -->
45+
<CurrentCompilerVersion>@(CurrentCompilerAssemblyIdentity->'%(Version)')</CurrentCompilerVersion>
46+
47+
<!-- The CurrentCompilerVersionIsNotNewEnough property can now be defined based on the Roslyn assembly version -->
48+
<CurrentCompilerVersionIsNotNewEnough Condition="$([MSBuild]::VersionLessThan($(CurrentCompilerVersion), 4.0))">true</CurrentCompilerVersionIsNotNewEnough>
49+
<CurrentCompilerVersionIsNotNewEnough Condition="$(CurrentCompilerVersionIsNotNewEnough) == ''">false</CurrentCompilerVersionIsNotNewEnough>
50+
</PropertyGroup>
51+
52+
<!-- If the Roslyn version is < 4.0, disable the source generators -->
53+
<ItemGroup Condition ="$(CurrentCompilerVersionIsNotNewEnough) == 'true'">
54+
<Analyzer Remove="@(_MVVMToolkitAnalyzer)"/>
55+
</ItemGroup>
56+
57+
<!-- If the source generators are disabled, also emit a warning. This would've been produced by MSBuild itself as well, but
58+
emitting this manually lets us customize the message to inform developers as to why exactly the generators have been
59+
disabled, and that the rest of the MVVM Toolkit will still keep working as intended, just without additional features. -->
60+
<Warning Condition ="$(CurrentCompilerVersionIsNotNewEnough) == '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."/>
61+
</When>
62+
63+
<Otherwise>
64+
65+
<!-- If no Roslyn assembly could be found, just remove the analyzer without emitting a warning -->
66+
<ItemGroup>
67+
<Analyzer Remove="@(_MVVMToolkitAnalyzer)"/>
68+
</ItemGroup>
69+
</Otherwise>
70+
</Choose>
4271
</Target>
4372

4473
</Project>

0 commit comments

Comments
 (0)