Fix ApiCompat (#4385)

This commit is contained in:
Utkarsh Umesan Pillai 2023-04-12 14:42:15 -07:00 committed by GitHub
parent 3d0cf75442
commit 7db694157f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 7 deletions

View File

@ -12,22 +12,28 @@
</PackageReference>
</ItemGroup>
<ItemGroup Condition="'$(MinVerTagPrefix)' == 'core-' AND '$(CheckAPICompatibility)' == 'true'">
<PropertyGroup>
<RunApiCompat>true</RunApiCompat>
</PropertyGroup>
<ItemGroup Condition="'$(MinVerTagPrefix)' == 'core-' AND '$(CheckAPICompatibility)' == 'true' AND '$(RunApiCompat)' == 'true'">
<PackageReference Include="Microsoft.DotNet.ApiCompat" Version="6.0.0-beta.21308.1" PrivateAssets="All" />
<ResolvedMatchingContract Include="$(RepoRoot)\build\LastMajorVersionBinaries\$(AssemblyName)\$(OTelPreviousStableVer)\lib\$(TargetFramework)\$(AssemblyName).dll" />
</ItemGroup>
<Target Name="PreBuild" BeforeTargets="DispatchToInnerBuilds;ValidateApiCompatForSrc" Condition="'$(MinVerTagPrefix)' == 'core-' AND '$(CheckAPICompatibility)' == 'true'">
<Target Name="PreBuild" BeforeTargets="DispatchToInnerBuilds;ValidateApiCompatForSrc" Condition="'$(MinVerTagPrefix)' == 'core-' AND '$(CheckAPICompatibility)' == 'true' AND '$(RunApiCompat)' == 'true'">
<!-- Note: DispatchToInnerBuilds is called for projects with multiple
targets defined to spawn a build process for each target framework being
compiled. Executing BEFORE that step means this runs once for a project
instead of in parallel for each target framework defined. If we ever have a
project with only a single target, this will NOT run and an alternative
solution will be needed. -->
<Message Text="ApiCompat: Running the powershell script to download the package for $(MSBuildProjectName)." Importance="High"/>
<Exec Command="powershell -ExecutionPolicy Unrestricted -File &quot;$(RepoRoot)\build\PreBuild.ps1&quot; -package $(AssemblyName) -version &quot;$(OTelPreviousStableVer)&quot; -workDir &quot;$(RepoRoot)\build\LastMajorVersionBinaries&quot;" />
</Target>
<Target Name="FindContractDependencyPaths" BeforeTargets="ValidateApiCompatForSrc" AfterTargets="ResolveAssemblyReferences" Condition="'$(MinVerTagPrefix)' == 'core-' AND '$(CheckAPICompatibility)' == 'true'">
<Target Name="FindContractDependencyPaths" BeforeTargets="ValidateApiCompatForSrc" AfterTargets="ResolveAssemblyReferences" Condition="'$(MinVerTagPrefix)' == 'core-' AND '$(CheckAPICompatibility)' == 'true' AND '$(RunApiCompat)' == 'true'">
<Message Text="ApiCompat: Finding the contract dependency path for $(MSBuildProjectName)." Importance="High"/>
<ItemGroup>
<_ReferencePathDirectories Include="@(ReferencePath -> '%(RootDir)%(Directory)')" />
</ItemGroup>

View File

@ -12,20 +12,35 @@ if (-Not (Test-Path $workDir))
if (Test-Path -Path "$workDir\$package.$version.zip")
{
Write-Debug "Previous package $package@$version already downloaded for compatibility check"
Write-Host "Previous package $package@$version already downloaded for compatibility check"
}
else
{
Write-Host "Retrieving package $package@$version for compatibility check"
Invoke-WebRequest -Uri https://www.nuget.org/api/v2/package/$package/$version -Outfile "$workDir\$package.$version.zip"
try
{
$Response = Invoke-WebRequest -Uri https://www.nuget.org/api/v2/package/$package/$version -Outfile "$workDir\$package.$version.zip"
}
catch
{
$StatusCode = $_.Exception.Response.StatusCode.value__
throw "Error downloading the package $package@$version. Status code of the received response: $StatusCode"
}
}
if (Test-Path -Path "$workDir\$package\$version\lib")
{
Write-Debug "Previous package $package@$version already extracted to '$workDir\$package\$version\lib'"
Write-Host "Previous package $package@$version already extracted to '$workDir\$package\$version\lib'"
}
else
{
Write-Host "Extracting package $package@$version from '$workDir\$package.$version.zip' to '$workDir\$package\$version' for compatibility check"
Expand-Archive -LiteralPath "$workDir\$package.$version.zip" -DestinationPath "$workDir\$package\$version" -Force
try
{
Expand-Archive -LiteralPath "$workDir\$package.$version.zip" -DestinationPath "$workDir\$package\$version" -Force
}
catch
{
throw "Error extracting $package@$version.zip"
}
}