|
1 | 1 | <Project> |
2 | 2 |
|
3 | | - <!-- Get the analyzer from the CommunityToolkit.Mvvm NuGet package --> |
| 3 | + <!-- Get the analyzer from the 'CommunityToolkit.Mvvm' NuGet package --> |
4 | 4 | <Target Name="MVVMToolkit_GatherAnalyzers"> |
5 | 5 | <ItemGroup> |
6 | 6 | <MVVMToolkit_Analyzer Include="@(Analyzer)" Condition="'%(Analyzer.NuGetPackageId)' == 'CommunityToolkit.Mvvm'" /> |
|
14 | 14 | DependsOnTargets="MVVMToolkit_GatherAnalyzers"> |
15 | 15 |
|
16 | 16 | <!-- |
17 | | - Use the CSharpCoreTargetsPath property to find the version of the compiler we are using. This is the same mechanism |
| 17 | + Use the 'CSharpCoreTargetsPath' property to find the version of the compiler we are using. This is the same mechanism |
18 | 18 | MSBuild uses to find the compiler. We could check the assembly version for any compiler assembly (since they all have |
19 | | - the same version) but Microsoft.Build.Tasks.CodeAnalysis.dll is where MSBuild loads the compiler tasks from so if |
| 19 | + the same version) but 'Microsoft.Build.Tasks.CodeAnalysis.dll' is where MSBuild loads the compiler tasks from so if |
20 | 20 | someone is getting creative with msbuild tasks/targets this is the "most correct" assembly to check. |
21 | 21 | --> |
22 | 22 | <GetAssemblyIdentity AssemblyFiles="$([System.IO.Path]::Combine($([System.IO.Path]::GetDirectoryName($(CSharpCoreTargetsPath))), 'Microsoft.Build.Tasks.CodeAnalysis.dll'))"> |
23 | 23 | <Output TaskParameter="Assemblies" ItemName="MVVMToolkit_CurrentCompilerAssemblyIdentity"/> |
24 | 24 | </GetAssemblyIdentity> |
25 | 25 |
|
26 | 26 | <PropertyGroup> |
27 | | - <!-- Transform the resulting item from GetAssemblyIdentity into a property representing its assembly version --> |
| 27 | + <!-- Transform the resulting item from 'GetAssemblyIdentity' task into a property representing its assembly version --> |
28 | 28 | <MVVMToolkit_CurrentCompilerVersion>@(MVVMToolkit_CurrentCompilerAssemblyIdentity->%(Version))</MVVMToolkit_CurrentCompilerVersion> |
29 | 29 |
|
30 | | - <!-- The CurrentCompilerIsNotNewEnough property can now be defined based on the Roslyn assembly version --> |
| 30 | + <!-- The 'MVVMToolkit_CurrentCompilerIsNotNewEnough' property can now be defined based on the Roslyn assembly version --> |
31 | 31 | <MVVMToolkit_CurrentCompilerIsNotNewEnough Condition="$([MSBuild]::VersionLessThan($(MVVMToolkit_CurrentCompilerVersion), 4.0))">true</MVVMToolkit_CurrentCompilerIsNotNewEnough> |
32 | 32 | </PropertyGroup> |
33 | 33 |
|
|
45 | 45 | </Target> |
46 | 46 |
|
47 | 47 | <!-- |
48 | | - Manually remove additional analyzers if Roslyn component versioning is not supported (ie. if a legacy .csproj project is used). |
| 48 | + Manually remove additional analyzers if Roslyn component versioning is not supported (i.e., if a legacy .csproj project is used). |
49 | 49 | This target is only run if Roslyn 4.0 or greater is present, as otherwise all analyzers would have already been removed anyway. |
50 | 50 | --> |
51 | 51 | <Target Name="MVVMToolkit_RemoveAdditionalAnalyzers_WhenRoslynComponentVersioningIsNotSupported" |
|
63 | 63 | </PropertyGroup> |
64 | 64 |
|
65 | 65 | <!-- |
66 | | - This condition is a bit convoluted, but it's essentially just selecting all analyzers from the NuGet package that don't have the target Roslyn directory name in their full path. |
67 | | - For instance, if Roslyn 4.3 is the highest supported version, the target directory name will be "roslyn 4.3", and this condition will filter out all analyzers with a path such |
68 | | - as: "C:\...\.nuget\...\CommunityToolkit.Mvvm\analyzers\roslyn4.0\cs\CommunityToolkit.Mvvm". The [System.String]::Copy trick is used to achieve two things: we can't directly |
69 | | - invoke a property function (ie. Contains in this case) on a metadata item, so we need an intermediate string to invoke it on. We could also use [System.String]::new, but using |
70 | | - Copy is more efficient (on .NET Framework runtime) as it'll just skip the allocation entirely. |
| 66 | + The following line is just removing all the analyzers present in the NuGet package from the `Analyzer` item list |
| 67 | + that doesn't have the target Roslyn identifier as the folder name in their full path. |
| 68 | +
|
| 69 | + For Example: |
| 70 | + If Roslyn 4.3 is the highest supported version, the Roslyn identifier (aka folder name) will be `roslyn4.3`, and |
| 71 | + this condition will remove all analyzers with a path such as: "[CommunityToolkit.Mvvm]\analyzers\roslyn4.0\cs\*.dll". |
| 72 | +
|
| 73 | + Here, We can't directly invoke a property function (ie. `Contains` in this case) on a metadata item, |
| 74 | + so we need an intermediate string to invoke it on. We could have used `[System.String]::new()` method, |
| 75 | + but it allocates in the .NET Framework runtime. Instead, we use `[System.String]::Copy` which doesn't allocate. |
71 | 76 | --> |
72 | 77 | <ItemGroup> |
73 | 78 | <Analyzer Remove="@(MVVMToolkit_Analyzer)" Condition="!$([System.String]::Copy(%(MVVMToolkit_Analyzer.FullPath)).Contains($(MVVMToolkit_SelectedRoslynAnalyzerTarget)))"/> |
74 | 79 | </ItemGroup> |
75 | 80 | </Target> |
76 | 81 |
|
77 | | - <!-- Remove the analyzer if Roslyn is missing --> |
| 82 | + <!-- Remove the analyzer if Roslyn is missing or the project is not using C# --> |
78 | 83 | <Target Name="MVVMToolkit_RemoveAnalyzers_WhenRoslynIsNotFound" |
79 | 84 | Condition="'$(CSharpCoreTargetsPath)' == ''" |
80 | 85 | AfterTargets="ResolvePackageDependenciesForBuild;ResolveNuGetPackageAssets" |
|
0 commit comments