Add task to find the single best tarball, and remove unused usings

This commit is contained in:
Jackson Schuster 2024-02-28 07:36:59 -08:00
parent f704a53482
commit 45728f7ba5
6 changed files with 90 additions and 42 deletions

View file

@ -1,18 +1,23 @@
<Project>
<UsingTask AssemblyFile="$(SdkArchiveDiffTasksAssembly)" TaskName="FindArchiveDiffs" />
<UsingTask AssemblyFile="$(SdkArchiveDiffTasksAssembly)" TaskName="GetClosestOfficialSdk" />
<UsingTask AssemblyFile="$(SdkArchiveDiffTasksAssembly)" TaskName="GetSingleTarballItem" />
<Target Name="ReportSdkArchiveDiffs"
AfterTargets="Build"
DependsOnTargets="DetermineSourceBuiltSdkVersion" >
<Message Text="No SDK archive to compare against." Importance="High" Condition="'@(SdkTarballItem)' == ''" />
<GetClosestOfficialSdk BuiltSdkPath="@(SdkTarballItem)" Condition="'@(SdkTarballItem)' != ''">
<GetSingleTarballItem SdkTarballItems="@(SdkTarballItem)">
<Output TaskParameter="BestSdkTarballItem" PropertyName="_TestSdkArchivePath"/>
</GetSingleTarballItem>
<GetClosestOfficialSdk BuiltSdkPath="$(_TestSdkArchivePath)" Condition="'@(_TestSdkArchivePath)' != ''">
<Output TaskParameter="ClosestOfficialSdkPath" PropertyName="ClosestOfficialSdkPath" />
</GetClosestOfficialSdk>
<Message Text="Failed to find closest official SDK archive." Importance="High" Condition="'@(SdkTarballItem)' != '' AND '$(ClosestOfficialSdkPath)' == ''" />
<FindArchiveDiffs BaselineArchive="@(SdkTarballItem)" TestArchive="$(ClosestOfficialSdkPath)" Condition="'@(SdkTarballItem)' != '' AND '$(ClosestOfficialSdkPath)' != ''">
<Message Text="Failed to find closest official SDK archive." Importance="High" Condition="'$(_TestSdkArchivePath)' != '' AND '$(ClosestOfficialSdkPath)' == ''" />
<FindArchiveDiffs BaselineArchive="$(_TestSdkArchivePath)" TestArchive="$(ClosestOfficialSdkPath)" Condition="'$(_TestSdkArchivePath)' != '' AND '$(ClosestOfficialSdkPath)' != ''">
<Output TaskParameter="ContentDifferences" ItemName="ContentDifferences" />
</FindArchiveDiffs>

View file

@ -2,17 +2,12 @@
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Immutable;
using System.Formats.Tar;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Reflection;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
using System.Threading;
using System.Threading.Tasks;
using static ArchiveExtensions;
public abstract class Archive : IDisposable
{
@ -134,4 +129,32 @@ public abstract class Archive : IDisposable
_archive.Dispose();
}
}
public static (string Version, string Rid, string extension) GetInfoFromArchivePath(string path)
{
string extension;
if (path.EndsWith(".tar.gz"))
{
extension = ".tar.gz";
}
else if (path.EndsWith(".zip"))
{
extension = ".zip";
}
else
{
throw new ArgumentException($"Invalid archive extension '{path}': must end with .tar.gz or .zip");
}
string filename = Path.GetFileName(path)[..^extension.Length];
var dashDelimitedParts = filename.Split('-');
var (rid, versionString) = dashDelimitedParts switch
{
["dotnet", "sdk", var first, var second, var third, var fourth] when PathWithVersions.IsVersionString(first) => (third + '-' + fourth, first + '-' + second),
["dotnet", "sdk", var first, var second, var third, var fourth] when PathWithVersions.IsVersionString(third) => (first + '-' + second, third + '-' + fourth),
_ => throw new ArgumentException($"Invalid archive file name '{filename}': file name should include full build version and rid in the format dotnet-sdk-<version>-<rid>{extension} or dotnet-sdk-<rid>-<version>{extension}")
};
return (versionString, rid, extension);
}
}

View file

@ -1,14 +1,12 @@
// 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.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Microsoft.Build.Framework;
public class GetClosestOfficialSdk : Microsoft.Build.Utilities.Task
{
[Required]
@ -24,7 +22,7 @@ public class GetClosestOfficialSdk : Microsoft.Build.Utilities.Task
public async Task<bool> ExecuteAsync()
{
var (versionString, rid, extension) = GetInfoFromArchivePath(BuiltSdkPath);
var (versionString, rid, extension) = Archive.GetInfoFromArchivePath(BuiltSdkPath);
string downloadUrl = GetLatestOfficialSdkUrl(versionString, rid, extension);
@ -63,31 +61,4 @@ public class GetClosestOfficialSdk : Microsoft.Build.Utilities.Task
return $"https://aka.ms/dotnet/{channel}/daily/dotnet-sdk-{rid}{extension}";
}
static (string Version, string Rid, string extension) GetInfoFromArchivePath(string path)
{
string extension;
if (path.EndsWith(".tar.gz"))
{
extension = ".tar.gz";
}
else if (path.EndsWith(".zip"))
{
extension = ".zip";
}
else
{
throw new ArgumentException($"Invalid archive extension '{path}': must end with .tar.gz or .zip");
}
string filename = Path.GetFileName(path)[..^extension.Length];
var dashDelimitedParts = filename.Split('-');
var (rid, versionString) = dashDelimitedParts switch
{
["dotnet", "sdk", var first, var second, var third, var fourth] when PathWithVersions.IsVersionString(first) => (third + '-' + fourth, first + '-' + second),
["dotnet", "sdk", var first, var second, var third, var fourth] when PathWithVersions.IsVersionString(third) => (first + '-' + second, third + '-' + fourth),
_ => throw new ArgumentException($"Invalid archive file name '{filename}': file name should include full build version and rid in the format dotnet-sdk-<version>-<rid>{extension} or dotnet-sdk-<rid>-<version>{extension}")
};
return (versionString, rid, extension);
}
}

View file

@ -0,0 +1,51 @@
// 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.Collections.Generic;
using Microsoft.Build.Framework;
public class GetSingleTarballItem : Microsoft.Build.Utilities.Task
{
[Required]
public required ITaskItem[] SdkTarballItems { get; init; }
[Output]
public string BestSdkTarballItem { get; set; } = "";
public override bool Execute()
{
List<string> tarballItems = new ();
foreach(var item in SdkTarballItems)
{
try
{
var (versionString, rid, extension) = Archive.GetInfoFromArchivePath(item.ItemSpec);
tarballItems.Add(item.ItemSpec);
}
catch (ArgumentException e)
{
Log.LogMessage(MessageImportance.High, e.Message);
continue;
}
}
switch (tarballItems.Count){
case 0:
Log.LogMessage(MessageImportance.High, "No valid tarball items found");
BestSdkTarballItem = "";
break;
case 1:
Log.LogMessage(MessageImportance.High, $"{tarballItems[0]} is the only valid tarball item found");
BestSdkTarballItem = tarballItems[0];
break;
default:
tarballItems.Sort((a,b) => a.Length - b.Length);
Log.LogMessage(MessageImportance.High, $"Multiple valid tarball items found: '{string.Join("', '", tarballItems)}'");
BestSdkTarballItem = tarballItems[0];
Log.LogMessage(MessageImportance.High, $"Choosing '{BestSdkTarballItem}");
break;
}
return true;
}
}

View file

@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

View file

@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;