Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PackageDiff tool and task #18580

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/SourceBuild/content/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@
<PropertyGroup>
<XPlatSourceBuildTasksAssembly>$([MSBuild]::NormalizePath('$(ArtifactsBinDir)', 'Microsoft.DotNet.SourceBuild.Tasks.XPlat', '$(Configuration)', 'Microsoft.DotNet.SourceBuild.Tasks.XPlat.dll'))</XPlatSourceBuildTasksAssembly>
<LeakDetectionTasksAssembly>$([MSBuild]::NormalizePath('$(ArtifactsBinDir)', 'Microsoft.DotNet.SourceBuild.Tasks.LeakDetection', '$(Configuration)', 'Microsoft.DotNet.SourceBuild.Tasks.LeakDetection.dll'))</LeakDetectionTasksAssembly>
<PackageDiffTasksAssembly>$([MSBuild]::NormalizePath('$(ArtifactsBinDir)', 'PackageDiff', '$(Configuration)', 'PackageDiff.dll'))</PackageDiffTasksAssembly>
</PropertyGroup>

<PropertyGroup Condition="'$(EnablePoison)' == 'true'">
Expand Down
1 change: 1 addition & 0 deletions src/SourceBuild/content/build.proj
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
</Target>

<Import Project="$(RepositoryEngineeringDir)build.sourcebuild.targets" Condition="'$(DotNetBuildSourceOnly)' == 'true'" />
<Import Project="$(RepositoryEngineeringDir)build.targets" />

</Project>
19 changes: 19 additions & 0 deletions src/SourceBuild/content/eng/build.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project>

<UsingTask AssemblyFile="$(PackageDiffTasksAssembly)" TaskName="PackageDiff" />
<UsingTask AssemblyFile="$(PackageDiffTasksAssembly)" TaskName="PackagesDiff" />
<UsingTask AssemblyFile="$(PackageDiffTasksAssembly)" TaskName="GetClosestPackage" />
<UsingTask AssemblyFile="$(PackageDiffTasksAssembly)" TaskName="GetClosestPackages" />
<Target Name="ReportPackageDiffs"
AfterTargets="Build"
Condition="'$(ReportPackageDiffs)' == 'true'">
<ItemGroup>
<PackagesToDiff Include="$(ArtifactsShippingPackagesDir)**/*.nupkg" />
</ItemGroup>
<GetClosestPackages Packages="@(PackagesToDiff)" >
<Output TaskParameter="ClosestPackagePaths" ItemName="ClosestPackages" />
</GetClosestPackages>
<PackagesDiff BaselinePackages="@(ClosestPackages)" TestPackages="@(PackagesToDiff)" />
</Target>

</Project>
9 changes: 9 additions & 0 deletions src/SourceBuild/content/eng/tools/init-build.proj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
UnpackTarballs;
BuildXPlatTasks;
BuildMSBuildSdkResolver;
BuildPackageDiff;
BuildLeakDetection;
ExtractToolPackage;
GenerateRootFs;
Expand Down Expand Up @@ -116,6 +117,14 @@
</Touch>
</Target>

<Target Name="BuildPackageDiff" >
<MSBuild Projects="tasks\PackageDiff\PackageDiff.csproj"
Targets="Restore"
Properties="MSBuildRestoreSessionId=$([System.Guid]::NewGuid())" />
<MSBuild Projects="tasks\PackageDiff\PackageDiff.csproj"
Targets="Build" />
</Target>

<Target Name="GenerateRootFs"
Condition="'$(BuildOS)' != 'windows' and '$(CrossBuild)' == 'true' and '$(ROOTFS_DIR)' == ''">
<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Threading.Tasks;
using Microsoft.Build.Framework;

public class GetClosestPackage: GetClosestPackageBase
{
[Required]
public string PackagePath { get; set; } = "";

[Output]
public string ClosestPackagePath { get; set; } = "";

public override async Task<bool> ExecuteAsync()
{
ClosestPackagePath = await GetClosestPackage(PackagePath);
return true;
}
}

public class GetClosestPackages: GetClosestPackageBase
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just have the task that accepts multiple packages and remove the singular one?

{
[Required]
public ITaskItem[] Packages { get; set; } = [];

[Output]
public ITaskItem[] ClosestPackagePaths { get; set; } = [];

public override async Task<bool> ExecuteAsync()
{
ClosestPackagePaths = new ITaskItem[Packages.Length];
for (int i = 0; i < Packages.Length; i++)
{
var packagePath = Packages[i].ItemSpec;
ClosestPackagePaths[i] = new Microsoft.Build.Utilities.TaskItem(await GetClosestPackage(packagePath));
}

return true;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Threading.Tasks;

public abstract class GetClosestPackageBase : NugetPackageTaskBase
{
public async Task<string> GetClosestPackage(string packagePath)
{
if(await GetPackageFromFile(packagePath) is not {} pack)
return "";

// TODO: Find the correct closest package version from the official build feeds.
var (name, version) = pack.GetPackageNameAndVersion();
return "https://nuget.org/api/v2/package/" + name + "/" + version;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.IO;
using System.IO.Compression;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Build.Framework;

public abstract class NugetPackageTaskBase : Microsoft.Build.Utilities.Task, ICancelableTask
{
private CancellationTokenSource _cts = new CancellationTokenSource();
protected CancellationToken _cancellationToken => _cts.Token;
public abstract Task<bool> ExecuteAsync();

public override bool Execute()
{
return Task.Run(ExecuteAsync).Result;
}

public async Task<(ZipArchive, ZipArchive)?> GetPackages(string packagePath1, string packagePath2)
{
_cancellationToken.ThrowIfCancellationRequested();
var package1 = await GetPackageFromPathOrUri(packagePath1);
var package2 = await GetPackageFromPathOrUri(packagePath2);
if (package1 is null || package2 is null)
return null;
return (package1, package2);
}

public async Task<ZipArchive?> GetPackageFromPathOrUri(string pathOrUrl)
{
_cancellationToken.ThrowIfCancellationRequested();

if (File.Exists(pathOrUrl))
{
return await GetPackageFromFile(pathOrUrl);
}
else if (Uri.TryCreate(pathOrUrl, UriKind.RelativeOrAbsolute, out _))
{
return await GetPackageFromUrlAsync(pathOrUrl);
}
else
{
return null;
}
}

public async Task<ZipArchive?> GetPackageFromUrlAsync(string url)
{
_cancellationToken.ThrowIfCancellationRequested();

if (Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out var uri))
{
try
{
var webClient = new HttpClient();
var packageStream = await webClient.GetStreamAsync(uri, _cancellationToken);
return new ZipArchive(packageStream);
}
catch (HttpRequestException ex)
{
if (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
{
Log.LogWarning($"Package not found (404): {url}");
return null;
}
else
{
Log.LogWarning($"Failed to download package from {url}: {ex.Message}");
return null;
}
}
}
else
{
Log.LogWarning($"Invalid URL: {url}");
return null;
}
}

public async Task<ZipArchive?> GetPackageFromFile(string pathToPackage)
{
_cancellationToken.ThrowIfCancellationRequested();
if (File.Exists(pathToPackage))
{
var packageStream = await File.ReadAllBytesAsync(pathToPackage);
return new ZipArchive(new MemoryStream(packageStream));
}

Log.LogWarning($"Package not found: {pathToPackage}");
return null;
}

public void Cancel()
{
_cts.Cancel();
}
}
44 changes: 44 additions & 0 deletions src/SourceBuild/content/eng/tools/tasks/PackageDiff/PackageDiff.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Threading.Tasks;
using Microsoft.Build.Framework;

public partial class PackageDiff : PackageDiffBase
{
[Required]
public string BaselinePackage {get; set;} = "";

[Required]
public string TestPackage {get; set;} = "";

public override async Task<bool> ExecuteAsync()
{
return await DiffPackages(BaselinePackage, TestPackage);
}
}

public partial class PackagesDiff: PackageDiffBase
{
[Required]
public ITaskItem[] BaselinePackages {get; set;} = [];

[Required]
public ITaskItem[] TestPackages {get; set;} = [];

public override async Task<bool> ExecuteAsync()
{
if (TestPackages.Length != BaselinePackages.Length)
{
Log.LogError("BaselinePackages and TestPackages must have the same length");
return false;
}

for(int i = 0; i < BaselinePackages.Length; i++)
{
Log.LogMessage(MessageImportance.High, $"Comparing {BaselinePackages[i].ItemSpec} and {TestPackages[i].ItemSpec}");
_ = await DiffPackages(BaselinePackages[i].ItemSpec, TestPackages[i].ItemSpec);
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>$(NetCurrent)</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Build.Utilities.Core" PrivateAssets="all" ExcludeAssets="Runtime" Version="$(MicrosoftBuildVersion)" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<PackageReference Include="Microsoft.Build.Utilities.Core" PrivateAssets="all" ExcludeAssets="Runtime" Version="$(MicrosoftBuildVersion)" />
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="$(MicrosoftBuildVersion)" />

</ItemGroup>
</Project>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
</Project>
</Project>

Loading