Skip to content

Commit

Permalink
Include a .targets file in NuGet packages which will select the corre…
Browse files Browse the repository at this point in the history
…ct analyzer assembly depending on which Roslyn version will be used to compile.
  • Loading branch information
eerhardt committed Sep 8, 2021
1 parent 91b38de commit 8111fcf
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
41 changes: 41 additions & 0 deletions eng/MultiTargetRoslynComponent.targets.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<Project>
<!-- Reads the version of the compiler APIs that are currently being used in order to pick the correct Roslyn components. -->
<Target Name="_{TargetPrefix}ResolveRoslynApiMajorVersion"
Condition="'$(_RoslynApiMajorVersion)' == ''">
<GetAssemblyIdentity AssemblyFiles="$(RoslynTargetsPath)\Microsoft.Build.Tasks.CodeAnalysis.dll">
<Output TaskParameter="Assemblies" ItemName="_CodeAnalysisIdentity" />
</GetAssemblyIdentity>

<PropertyGroup>
<_RoslynApiMajorVersion>$([System.Version]::Parse(%(_CodeAnalysisIdentity.Version)).Major)</_RoslynApiMajorVersion>
</PropertyGroup>
</Target>

<Target Name="_{TargetPrefix}RoslynComponentMultiTargeting"
Condition="'$(SupportsRoslynComponentVersioning)' != 'true'"
AfterTargets="ResolveAssemblyReferences"
DependsOnTargets="_{TargetPrefix}ResolveRoslynApiMajorVersion">
<ItemGroup>
<!-- clear _CurrentAnalyzer, in case some other target is using it. -->
<_CurrentAnalyzer Remove="@(_CurrentAnalyzer)" />
<_CurrentAnalyzer Include="@(Analyzer)" Condition="'%(Analyzer.NuGetPackageId)' == '{NuGetPackageId}'" />
</ItemGroup>

<ItemGroup Condition="$([System.Int32]::Parse($(_RoslynApiMajorVersion))) &lt; 4">
<!-- Remove our analyzers targeting roslyn4.x -->
<Analyzer Remove="@(_CurrentAnalyzer)"
Condition="$([System.String]::Copy('%(_CurrentAnalyzer.Identity)').IndexOf('roslyn4')) &gt;= 0"/>
</ItemGroup>

<ItemGroup Condition="$([System.Int32]::Parse($(_RoslynApiMajorVersion))) &gt;= 4">
<!-- Remove our analyzers targeting roslyn3.x -->
<Analyzer Remove="@(_CurrentAnalyzer)"
Condition="$([System.String]::Copy('%(_CurrentAnalyzer.Identity)').IndexOf('roslyn3')) &gt;= 0"/>
</ItemGroup>

<ItemGroup Condition="'$({DisableSourceGeneratorPropertyName})' == 'true'">
<!-- Remove all our analyzers -->
<Analyzer Remove="@(_CurrentAnalyzer)" />
</ItemGroup>
</Target>
</Project>
36 changes: 36 additions & 0 deletions eng/packaging.targets
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,42 @@
</ItemGroup>
</Target>

<PropertyGroup>
<MultiTargetRoslynComponentTargetsFileIntermediatePath>$(IntermediateOutputPath)MultiTargetRoslynComponent.targets</MultiTargetRoslynComponentTargetsFileIntermediatePath>
<IncludeMultiTargetRoslynComponentTargets Condition="'$(IncludeMultiTargetRoslynComponentTargets)' == ''">true</IncludeMultiTargetRoslynComponentTargets>
</PropertyGroup>

<!-- In packages that contain Analyzers, include a .targets file that will select the correct
analyzer given the version of Roslyn being used to compile the project. -->
<Target Name="IncludeMultiTargetRoslynComponentTargetsInPackage"
AfterTargets="IncludeAnalyzersInPackage"
Condition="'@(AnalyzerReference)' != '' and '$(IncludeMultiTargetRoslynComponentTargets)' == 'true'"
DependsOnTargets="GenerateMultiTargetRoslynComponentTargetsFile">
<ItemGroup>
<Content Include="$(MultiTargetRoslynComponentTargetsFileIntermediatePath)"
PackagePath="build\$(PackageId).targets"
Pack="True" />
</ItemGroup>
</Target>

<Target Name="GenerateMultiTargetRoslynComponentTargetsFile"
Inputs="$(MSBuildProjectFullPath)"
Outputs="$(MultiTargetRoslynComponentTargetsFileIntermediatePath)">
<PropertyGroup>
<_MultiTargetRoslynComponentTargetsTemplate>$(MSBuildThisFileDirectory)MultiTargetRoslynComponent.targets.template</_MultiTargetRoslynComponentTargetsTemplate>
<_MultiTargetRoslynComponentTargetPrefix>$(PackageId.Replace('.', '_'))</_MultiTargetRoslynComponentTargetPrefix>
<_MultiTargetRoslynComponentDisableSourceGeneratorPropertyName>Disable$(PackageId.Replace('.', ''))SourceGenerator</_MultiTargetRoslynComponentDisableSourceGeneratorPropertyName>
<_MultiTargetRoslynComponentDisableSourceGeneratorPropertyName>$(_MultiTargetRoslynComponentDisableSourceGeneratorPropertyName.Replace('Abstractions', ''))</_MultiTargetRoslynComponentDisableSourceGeneratorPropertyName>
</PropertyGroup>

<WriteLinesToFile File="$(MultiTargetRoslynComponentTargetsFileIntermediatePath)"
Lines="$([System.IO.File]::ReadAllText('$(_MultiTargetRoslynComponentTargetsTemplate)')
.Replace('{TargetPrefix}', '$(_MultiTargetRoslynComponentTargetPrefix)')
.Replace('{NuGetPackageId}', '$(PackageId)')
.Replace('{DisableSourceGeneratorPropertyName}', '$(_MultiTargetRoslynComponentDisableSourceGeneratorPropertyName)'))"
Overwrite="true" />
</Target>

<!-- Include a netstandard compat error if the project targets both .NETStandard and
.NETCoreApp. This prohibits users to consume packages on an older .NETCoreApp version
than the minimum supported one. -->
Expand Down

0 comments on commit 8111fcf

Please sign in to comment.