From 353f1a3064d256f2d31465ab86259edd78c59cb6 Mon Sep 17 00:00:00 2001 From: Matthew Leibowitz Date: Wed, 13 Nov 2024 14:47:08 +0800 Subject: [PATCH] Download using new APIs (#25800) * Download using new APIs mono is no longer found on machines * fix --- eng/cake/dotnet.cake | 16 +++++++-------- eng/cake/helpers.cake | 44 +++++++++++++++++++++++++++++++++++++++- eng/devices/windows.cake | 2 +- 3 files changed, 51 insertions(+), 11 deletions(-) diff --git a/eng/cake/dotnet.cake b/eng/cake/dotnet.cake index 7d348b6f826f..03de4f1c7dda 100644 --- a/eng/cake/dotnet.cake +++ b/eng/cake/dotnet.cake @@ -345,7 +345,7 @@ Task("dotnet-pack-maui") Task("dotnet-pack-additional") .WithCriteria(RunPackTarget()) - .Does(() => + .Does(async () => { // Download some additional symbols that need to be archived along with the maui symbols: // - _NativeAssets.windows @@ -353,16 +353,14 @@ Task("dotnet-pack-additional") // - libHarfBuzzSharp.pdb var assetsDir = $"./artifacts/additional-assets"; var nativeAssetsVersion = XmlPeek("./eng/Versions.props", "/Project/PropertyGroup/_SkiaSharpNativeAssetsVersion"); - NuGetInstall("_NativeAssets.windows", new NuGetInstallSettings - { - Version = nativeAssetsVersion, - ExcludeVersion = true, - OutputDirectory = assetsDir, - Source = new[] { "https://pkgs.dev.azure.com/xamarin/public/_packaging/SkiaSharp/nuget/v3/index.json" }, - }); + await DownloadNuGetPackageAsync( + "_NativeAssets.windows", + nativeAssetsVersion, + assetsDir, + "https://pkgs.dev.azure.com/xamarin/public/_packaging/SkiaSharp/nuget/v3/index.json"); + Zip(assetsDir, $"{assetsDir}.zip"); foreach (var nupkg in GetFiles($"{assetsDir}/**/*.nupkg")) DeleteFile(nupkg); - Zip(assetsDir, $"{assetsDir}.zip"); }); Task("dotnet-pack-library-packs") diff --git a/eng/cake/helpers.cake b/eng/cake/helpers.cake index d299c3d7421c..5b3c1c7b4070 100644 --- a/eng/cake/helpers.cake +++ b/eng/cake/helpers.cake @@ -1,3 +1,13 @@ +#addin "nuget:?package=NuGet.Packaging&version=6.7.0" +#addin "nuget:?package=NuGet.Protocol&version=6.7.0" + +using System.Threading.Tasks; +using NuGet.Common; +using NuGet.Configuration; +using NuGet.Protocol; +using NuGet.Protocol.Core.Types; +using NuGet.Versioning; + bool isCleanSet = HasArgument("clean") || IsTarget("clean"); Task("Clean") @@ -108,4 +118,36 @@ bool IsTarget(string target) => Argument("target", "Default").Equals(target, StringComparison.InvariantCultureIgnoreCase); bool TargetStartsWith(string target) => - Argument("target", "Default").StartsWith(target, StringComparison.InvariantCultureIgnoreCase); \ No newline at end of file + Argument("target", "Default").StartsWith(target, StringComparison.InvariantCultureIgnoreCase); + +public async Task DownloadNuGetPackageAsync(string packageId, string version, string outputDirectory, string feedUri) +{ + // Create a source repository + var repository = Repository.Factory.GetCoreV3(feedUri); + + // Find the package + var resource = await repository.GetResourceAsync(); + var packageVersion = NuGetVersion.Parse(version); + var cacheContext = new SourceCacheContext(); + + // Set up logging (optional, use NullLogger if you don't need logging) + ILogger logger = NullLogger.Instance; + + // Download the package to the output directory + EnsureDirectoryExists(outputDirectory); + var packagePath = System.IO.Path.Combine(outputDirectory, $"{packageId}.{version}.nupkg"); + + using (var fileStream = new FileStream(packagePath, FileMode.Create, FileAccess.Write, FileShare.None)) + { + // Download package + var success = await resource.CopyNupkgToStreamAsync( + packageId, packageVersion, fileStream, cacheContext, logger, default); + + if (!success) + { + throw new Exception("Failed to download the package."); + } + + Information("Package '{0} v{1}' downloaded successfully to {2}", packageId, version, packagePath); + } +} diff --git a/eng/devices/windows.cake b/eng/devices/windows.cake index 12591722040b..a6db0f33c91a 100644 --- a/eng/devices/windows.cake +++ b/eng/devices/windows.cake @@ -96,7 +96,7 @@ Task("GenerateMsixCert") { Information("Generating cert"); var rsa = RSA.Create(); - var req = new CertificateRequest("CN=" + certCN, rsa, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); + var req = new CertificateRequest("CN=" + certCN, rsa, System.Security.Cryptography.HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); req.CertificateExtensions.Add(new X509EnhancedKeyUsageExtension(new OidCollection {