From 9b43ca3885890d8d449534adf78c3dbe1fda12f4 Mon Sep 17 00:00:00 2001
From: Jackson Schuster <36744439+jtschuster@users.noreply.github.com>
Date: Fri, 1 Mar 2024 11:31:35 -0800
Subject: [PATCH] Rename file to match type
---
.../GetClosestArchive.cs | 94 +++++++++++++++++++
1 file changed, 94 insertions(+)
create mode 100644 src/SourceBuild/content/eng/tools/tasks/Microsoft.DotNet.SourceBuild.Tasks.SdkArchiveDiff/GetClosestArchive.cs
diff --git a/src/SourceBuild/content/eng/tools/tasks/Microsoft.DotNet.SourceBuild.Tasks.SdkArchiveDiff/GetClosestArchive.cs b/src/SourceBuild/content/eng/tools/tasks/Microsoft.DotNet.SourceBuild.Tasks.SdkArchiveDiff/GetClosestArchive.cs
new file mode 100644
index 000000000..fc69a385a
--- /dev/null
+++ b/src/SourceBuild/content/eng/tools/tasks/Microsoft.DotNet.SourceBuild.Tasks.SdkArchiveDiff/GetClosestArchive.cs
@@ -0,0 +1,94 @@
+// 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.Net.Http;
+using System.Threading;
+using System.Threading.Tasks;
+using Microsoft.Build.Framework;
+
+public abstract class GetClosestArchive : Microsoft.Build.Utilities.Task, ICancelableTask
+{
+ [Required]
+ public required string BuiltArchivePath { get; init; }
+
+ [Output]
+ public string ClosestOfficialArchivePath { get; set; } = "";
+
+ private string? _builtVersion;
+ protected string BuiltVersion
+ {
+ get => _builtVersion ?? throw new InvalidOperationException();
+ private set => _builtVersion = value;
+ }
+
+ private string? _builtRid;
+ protected string BuiltRid
+ {
+ get => _builtRid ?? throw new InvalidOperationException();
+ private set => _builtRid = value;
+ }
+
+ private string? _archiveExtension;
+ protected string ArchiveExtension
+ {
+ get => _archiveExtension ?? throw new InvalidOperationException();
+ private set => _archiveExtension = value;
+ }
+
+ ///
+ /// The name of the package to find the closest official archive for. For example, "dotnet-sdk" or "aspnetcore-runtime".
+ ///
+ protected abstract string ArchiveName { get; }
+
+ private CancellationTokenSource _cancellationTokenSource = new();
+ protected CancellationToken CancellationToken => _cancellationTokenSource.Token;
+ public void Cancel()
+ {
+ _cancellationTokenSource.Cancel();
+ }
+
+ ///
+ /// Get the URL of the latest official archive for the given version string and RID.
+ ///
+ public abstract Task GetLatestOfficialArchiveUrl();
+
+ public abstract Task GetClosestOfficialArchiveVersion();
+
+ public override bool Execute()
+ {
+ return Task.Run(ExecuteAsync).Result;
+ }
+
+ public async Task ExecuteAsync()
+ {
+ CancellationToken.ThrowIfCancellationRequested();
+ var filename = Path.GetFileName(BuiltArchivePath);
+ (BuiltVersion, BuiltRid, ArchiveExtension) = Archive.GetInfoFromFileName(filename, ArchiveName);
+ Log.LogMessage($"Finding closest official archive for '{ArchiveName}' version '{BuiltVersion}' RID '{BuiltRid}'");
+
+ string? downloadUrl = await GetLatestOfficialArchiveUrl();
+ if (downloadUrl == null)
+ {
+ Log.LogError($"Failed to find a download URL for '{ArchiveName}' version '{BuiltVersion}' RID '{BuiltRid}'");
+ return false;
+ }
+
+ HttpClient client = new HttpClient();
+
+ Log.LogMessage(MessageImportance.High, $"Downloading {downloadUrl}");
+ HttpResponseMessage packageResponse = await client.GetAsync(downloadUrl, CancellationToken);
+
+ var packageUriPath = packageResponse.RequestMessage!.RequestUri!.LocalPath;
+
+ ClosestOfficialArchivePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + $".{ArchiveName}-{BuiltVersion}-{BuiltRid}.closest.{ArchiveExtension}");
+ Log.LogMessage($"Copying {packageUriPath} to {ClosestOfficialArchivePath}");
+ using (var file = File.Create(ClosestOfficialArchivePath))
+ {
+ await packageResponse.Content.CopyToAsync(file, CancellationToken);
+ }
+
+ return true;
+ }
+}