-
Notifications
You must be signed in to change notification settings - Fork 5.5k
[workload-testing] Misc improvements #99392
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f079bfe
[workload-testing] Use `PackageDownload` instead of `PackageReference`
radical 2a665d4
[workload-testing] Add property for output of workload-install command
radical fff35b8
Add support for emitting packageSourceMappings in nuget.config, which…
radical d07c3f6
cleanup
radical 0ff9c23
[workload-testing] Add PackageSourceNameForBuiltPackages property tha…
radical 512f1ab
Extract the nuget.config patch method into a separate task
radical 589b37c
fix trimmer tests build
radical 8d12e97
Update src/mono/nuget/Microsoft.NET.Runtime.WorkloadTesting.Internal/…
radical ad245c4
address review feedback from @ ilonatommy
radical 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,140 @@ | ||
| // 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.CodeAnalysis; | ||
| using System.Globalization; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Xml.Linq; | ||
| using System.Xml.XPath; | ||
| using System.Xml; | ||
| using Microsoft.Build.Framework; | ||
| using Microsoft.Build.Utilities; | ||
|
|
||
| #nullable enable | ||
|
|
||
| namespace Microsoft.Workload.Build.Tasks; | ||
|
|
||
| /* | ||
| * Used for patching a nuget.config to: | ||
| * | ||
| * 1. Add a new package source to the nuget.config | ||
| * 2. Add a new package source mapping to the nuget.config | ||
| * | ||
| * This is useful specifically the case of workload testing | ||
| */ | ||
| public class PatchNuGetConfig : Task | ||
| { | ||
| [Required, NotNull] | ||
| public string? TemplateNuGetConfigPath { get; set; } | ||
|
|
||
| [Required, NotNull] | ||
| public string? LocalNuGetsPath { get; set; } | ||
|
|
||
| public string? OutputPath { get; set; } | ||
|
|
||
| /* | ||
| * Value: ["*Aspire*", "Foo*"] | ||
| * This will be translated to: | ||
| * <packageSourceMapping> | ||
| * <packageSource key="nuget-local"> | ||
| * <package pattern="*Aspire*" /> | ||
| * <package pattern="Foo*" /> | ||
| * </packageSource> | ||
| * | ||
| * This is useful when using Central Package Management (https://learn.microsoft.com/en-us/nuget/consume-packages/central-package-management) | ||
| */ | ||
| public string[] NuGetConfigPackageSourceMappings { get; set; } = Array.Empty<string>(); | ||
|
|
||
| public string PackageSourceNameForBuiltPackages { get; set; } = "nuget-local"; | ||
|
|
||
| public override bool Execute() | ||
| { | ||
| try | ||
| { | ||
| Validate(TemplateNuGetConfigPath, PackageSourceNameForBuiltPackages, OutputPath); | ||
| GetNuGetConfig(TemplateNuGetConfigPath, LocalNuGetsPath, PackageSourceNameForBuiltPackages, NuGetConfigPackageSourceMappings, OutputPath!); | ||
| Log.LogMessage(MessageImportance.Low, $"Generated patched nuget.config at {OutputPath}"); | ||
| return true; | ||
| } | ||
| catch (LogAsErrorException laee) | ||
| { | ||
| Log.LogError(laee.Message); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| private static void Validate(string? templateNuGetConfigPath, string? packageSourceNameForBuiltPackages, string? outputPath) | ||
| { | ||
| if (string.IsNullOrEmpty(templateNuGetConfigPath)) | ||
| throw new LogAsErrorException($"{nameof(templateNuGetConfigPath)} is required"); | ||
|
|
||
| if (!File.Exists(templateNuGetConfigPath)) | ||
| throw new LogAsErrorException($"Cannot find {nameof(templateNuGetConfigPath)}={templateNuGetConfigPath}"); | ||
|
|
||
| if (string.IsNullOrEmpty(packageSourceNameForBuiltPackages)) | ||
| throw new LogAsErrorException($"{nameof(packageSourceNameForBuiltPackages)} is required"); | ||
|
|
||
| if (string.IsNullOrEmpty(outputPath)) | ||
| throw new LogAsErrorException($"{nameof(outputPath)} is required"); | ||
|
|
||
| if (Directory.Exists(outputPath)) | ||
| throw new LogAsErrorException($"{nameof(outputPath)}={outputPath} is a directory, it should be a file"); | ||
| } | ||
|
|
||
| public static void GetNuGetConfig(string templateNuGetConfigPath, string localNuGetsPath, string packageSourceNameForBuiltPackages, string[] nuGetConfigPackageSourceMappings, string outputPath) | ||
| { | ||
| Validate(templateNuGetConfigPath, packageSourceNameForBuiltPackages, outputPath); | ||
|
|
||
| XDocument doc = XDocument.Load(templateNuGetConfigPath); | ||
| string xpath = "/configuration/packageSources"; | ||
| XElement? packageSources = doc.XPathSelectElement(xpath); | ||
| if (packageSources is null) | ||
| throw new LogAsErrorException($"Could not find {xpath} in {templateNuGetConfigPath}"); | ||
|
|
||
| var newPackageSourceElement = new XElement("add", | ||
| new XAttribute("key", packageSourceNameForBuiltPackages), | ||
| new XAttribute("value", $"file://{localNuGetsPath}")); | ||
| if (packageSources.LastNode is not null) | ||
| { | ||
| packageSources.LastNode.AddAfterSelf(newPackageSourceElement); | ||
| } | ||
| else | ||
| { | ||
| packageSources.Add(newPackageSourceElement); | ||
| } | ||
|
|
||
| if (nuGetConfigPackageSourceMappings.Length > 0) | ||
| { | ||
| string mappingXpath = "/configuration/packageSourceMapping"; | ||
| XElement? packageSourceMapping = doc.XPathSelectElement(mappingXpath); | ||
| if (packageSourceMapping is null) | ||
| { | ||
| if (doc.Root is null) | ||
| throw new LogAsErrorException($"Could not find root element in {templateNuGetConfigPath}"); | ||
|
|
||
| packageSourceMapping = new XElement("packageSourceMapping"); | ||
| doc.Root.Add(packageSourceMapping); | ||
| } | ||
|
|
||
| var newPackageSourceMappingElement = new XElement("packageSource", | ||
| new XAttribute("key", packageSourceNameForBuiltPackages), | ||
| nuGetConfigPackageSourceMappings.Select | ||
| (pattern => new XElement("package", new XAttribute("pattern", pattern)))); | ||
| if (packageSourceMapping.FirstNode is not null) | ||
| { | ||
| packageSourceMapping.FirstNode?.AddBeforeSelf(newPackageSourceMappingElement); | ||
| } | ||
| else | ||
| { | ||
| packageSourceMapping.Add(newPackageSourceMappingElement); | ||
| } | ||
| } | ||
|
|
||
| using var xw = XmlWriter.Create(outputPath, new XmlWriterSettings { Indent = true, NewLineHandling = NewLineHandling.None, Encoding = Encoding.UTF8 }); | ||
| doc.WriteTo(xw); | ||
| xw.Close(); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.