Skip to content

Commit

Permalink
Simplify target to enable config binding src generator from NuGet pac…
Browse files Browse the repository at this point in the history
…kage, ensure it runs for all TFMs, and rename enabling property (#84379)

* Simplify target to enable config binding src generator and rename sentinel property

* Further simplify target

* Address feedback

* Ensure generator removal target is for all TFMs

* Add logic to emit correct recommended minimum TFM
  • Loading branch information
layomia authored Apr 9, 2023
1 parent 86b48d7 commit e039add
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 53 deletions.
17 changes: 0 additions & 17 deletions eng/OffByDefaultRoslynComponent.targets.template

This file was deleted.

35 changes: 0 additions & 35 deletions eng/packaging.targets
Original file line number Diff line number Diff line change
Expand Up @@ -193,41 +193,6 @@
Overwrite="true" />
</Target>

<PropertyGroup>
<_OffByDefaultRoslynComponentTargetsTemplate>$(MSBuildThisFileDirectory)OffByDefaultRoslynComponent.targets.template</_OffByDefaultRoslynComponentTargetsTemplate>
<OffByDefaultRoslynComponentTargetsFileIntermediatePath>$(IntermediateOutputPath)OffByDefaultRoslynComponent.targets</OffByDefaultRoslynComponentTargetsFileIntermediatePath>
<RoslynComponentsAreOffByDefault Condition="'$(RoslynComponentsAreOffByDefault)' == ''">false</RoslynComponentsAreOffByDefault>
</PropertyGroup>

<!-- In packages that contain Analyzers that are off by default, include a .targets file that will exclude the analyzer. -->
<Target Name="ExcludeOffByDefaultRoslynComponentTargetsInPackage"
AfterTargets="IncludeAnalyzersInPackage"
Condition="'@(ProjectReference)' != '' and
@(ProjectReference->AnyHaveMetadataValue('PackAsAnalyzer', 'true')) and
'$(RoslynComponentsAreOffByDefault)' == 'true'"
DependsOnTargets="GenerateOffByDefaultRoslynComponentTargetsFile">
<ItemGroup>
<Content Include="$(OffByDefaultRoslynComponentTargetsFileIntermediatePath)" PackagePath="buildTransitive\netstandard2.0\$(PackageId).targets" />
<Content Include="$(OffByDefaultRoslynComponentTargetsFileIntermediatePath)" PackagePath="buildTransitive\%(NETStandardCompatError.Supported)\$(PackageId).targets" Condition="'@(NETStandardCompatError)' != ''" />
</ItemGroup>
</Target>

<Target Name="GenerateOffByDefaultRoslynComponentTargetsFile"
Inputs="$(MSBuildProjectFullPath);$(_OffByDefaultRoslynComponentTargetsTemplate)"
Outputs="$(OffByDefaultRoslynComponentTargetsFileIntermediatePath)">
<PropertyGroup>
<_OffByDefaultRoslynComponentTargetPrefix>$(PackageId.Replace('.', '_'))</_OffByDefaultRoslynComponentTargetPrefix>
<EnableSourceGeneratorPropertyName Condition="'$(EnableSourceGeneratorPropertyName)' == ''">Enable$(PackageId.Replace('.', ''))SourceGenerator</EnableSourceGeneratorPropertyName>
</PropertyGroup>

<WriteLinesToFile File="$(OffByDefaultRoslynComponentTargetsFileIntermediatePath)"
Lines="$([System.IO.File]::ReadAllText('$(_OffByDefaultRoslynComponentTargetsTemplate)')
.Replace('{TargetPrefix}', '$(_OffByDefaultRoslynComponentTargetPrefix)')
.Replace('{NuGetPackageId}', '$(PackageId)')
.Replace('{EnableSourceGeneratorPropertyName}', '$(EnableSourceGeneratorPropertyName)'))"
Overwrite="true" />
</Target>

<!-- Add targets file that marks a .NETStandard applicable tfm as unsupported. -->
<Target Name="AddNETStandardCompatErrorFileForPackaging"
Condition="'@(NETStandardCompatError)' != '' and '$(DisableNETStandardCompatErrors)' != 'true'"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@

<PropertyGroup>
<IncludeMultiTargetRoslynComponentTargets>false</IncludeMultiTargetRoslynComponentTargets>
<RoslynComponentsAreOffByDefault>true</RoslynComponentsAreOffByDefault>
<DisableNETStandardCompatErrors>true</DisableNETStandardCompatErrors>
</PropertyGroup>

<ItemGroup>
<Content Include="buildTransitive\$(PackageId).targets" PackagePath="buildTransitive\netstandard2.0\" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project InitialTargets="NETStandardCompatError_Microsoft_Extensions_Configuration_Binder">
<Target Name="_Microsoft_Extensions_Configuration_BinderRemoveAnalyzer"
Condition="'$(EnableConfigurationBindingGenerator)' != 'true'"
AfterTargets="ResolvePackageDependenciesForBuild;ResolveNuGetPackageAssets">

<ItemGroup>
<Analyzer Remove="@(Analyzer->WithMetadataValue('NuGetPackageId', 'Microsoft.Extensions.Configuration.Binder'))" />
</ItemGroup>
</Target>

<Target Name="NETStandardCompatError_Microsoft_Extensions_Configuration_Binder"
Condition="'$(SuppressTfmSupportBuildWarnings)' == ''">
<PropertyGroup>
<_Microsoft_Extensions_Configuration_Binder_Compatible_TargetFramework
Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'netcoreapp2.0')) AND
!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))"
>net6.0</_Microsoft_Extensions_Configuration_Binder_Compatible_TargetFramework>
<_Microsoft_Extensions_Configuration_Binder_Compatible_TargetFramework
Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net461')) AND
!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net462'))"
>net462</_Microsoft_Extensions_Configuration_Binder_Compatible_TargetFramework>
</PropertyGroup>
<Warning Condition="'$(_Microsoft_Extensions_Configuration_Binder_Compatible_TargetFramework)' != ''"
Text="Microsoft.Extensions.Configuration.Binder doesn't support $(TargetFramework) and has not been tested with it. Consider upgrading your TargetFramework to $(_Microsoft_Extensions_Configuration_Binder_Compatible_TargetFramework) or later. You may also set &lt;SuppressTfmSupportBuildWarnings&gt;true&lt;/SuppressTfmSupportBuildWarnings&gt; in the project file to ignore this warning and attempt to run in this unsupported configuration at your own risk." />
</Target>
</Project>

0 comments on commit e039add

Please sign in to comment.