-
Notifications
You must be signed in to change notification settings - Fork 442
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
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ee1dce8
Add PackageDiff tool and tasks
jtschuster e6a5e58
Update src/SourceBuild/content/eng/tools/tasks/PackageDiff/PackageDif…
jtschuster 6482507
Make task netcore, remove tool, add msbuild to target
jtschuster 8872857
Merge branch 'main' of https://github.com/dotnet/installer into Packa…
jtschuster 097a707
Merge branch 'PackageDiff' of https://github.com/jtschuster/installer…
jtschuster 445af1f
Don't run ReportPackageDiffs by default until it works correctly
jtschuster File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/SourceBuild/content/eng/tools/tasks/PackageDiff/GetClosestPackage.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
[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; | ||
} | ||
} | ||
|
18 changes: 18 additions & 0 deletions
18
src/SourceBuild/content/eng/tools/tasks/PackageDiff/GetClosestPackageBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
101 changes: 101 additions & 0 deletions
101
src/SourceBuild/content/eng/tools/tasks/PackageDiff/NugetPackageTaskBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
44
src/SourceBuild/content/eng/tools/tasks/PackageDiff/PackageDiff.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/SourceBuild/content/eng/tools/tasks/PackageDiff/PackageDiff.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)" /> | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
</ItemGroup> | ||||||||
</Project> | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?