From df8135659f616cd07522b491e85a661c21080776 Mon Sep 17 00:00:00 2001 From: Logan Bussell Date: Wed, 16 Apr 2025 14:55:59 -0700 Subject: [PATCH 1/7] Refactor Update Dependencies project to facilitate adding additional commands --- eng/Set-DotnetVersions.ps1 | 2 +- eng/update-dependencies/BaseCommand.cs | 45 +++++++ eng/update-dependencies/BaseUrlUpdater.cs | 10 +- .../DockerfileShaUpdater.cs | 28 ++-- eng/update-dependencies/IOptions.cs | 13 ++ eng/update-dependencies/ManifestHelper.cs | 5 +- eng/update-dependencies/NuGet.config | 9 ++ eng/update-dependencies/NuGetConfigUpdater.cs | 5 +- eng/update-dependencies/Options.cs | 125 ------------------ eng/update-dependencies/Program.cs | 26 ++++ eng/update-dependencies/ReleaseState.cs | 10 ++ ...dateDependencies.cs => SpecificCommand.cs} | 27 ++-- .../SpecificCommandOptions.cs | 125 ++++++++++++++++++ .../VariableUpdaterBase.cs | 6 +- eng/update-dependencies/VersionUpdater.cs | 8 +- .../update-dependencies.csproj | 17 +-- 16 files changed, 269 insertions(+), 192 deletions(-) create mode 100644 eng/update-dependencies/BaseCommand.cs create mode 100644 eng/update-dependencies/IOptions.cs create mode 100644 eng/update-dependencies/NuGet.config delete mode 100644 eng/update-dependencies/Options.cs create mode 100644 eng/update-dependencies/Program.cs create mode 100644 eng/update-dependencies/ReleaseState.cs rename eng/update-dependencies/{UpdateDependencies.cs => SpecificCommand.cs} (95%) create mode 100644 eng/update-dependencies/SpecificCommandOptions.cs diff --git a/eng/Set-DotnetVersions.ps1 b/eng/Set-DotnetVersions.ps1 index 5e7d6d064c..53f8ff8935 100644 --- a/eng/Set-DotnetVersions.ps1 +++ b/eng/Set-DotnetVersions.ps1 @@ -149,5 +149,5 @@ if ($AzdoVariableName) { Write-Host "##vso[task.setvariable variable=$AzdoVariableName]$updateDepsArgs" } else { - & dotnet run --project $PSScriptRoot/update-dependencies/update-dependencies.csproj @updateDepsArgs + & dotnet run --project $PSScriptRoot/update-dependencies/update-dependencies.csproj specific @updateDepsArgs } diff --git a/eng/update-dependencies/BaseCommand.cs b/eng/update-dependencies/BaseCommand.cs new file mode 100644 index 0000000000..e07ce1f86a --- /dev/null +++ b/eng/update-dependencies/BaseCommand.cs @@ -0,0 +1,45 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.CommandLine; +using System.CommandLine.NamingConventionBinder; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; + +namespace Dotnet.Docker; + +public abstract class BaseCommand() where TOptions : IOptions +{ + public abstract Task ExecuteAsync(TOptions options); + + public static void Register(IServiceCollection serviceCollection) where T : BaseCommand + { + serviceCollection.AddSingleton, T>(); + } + + public static Command Create(string name, string description) + { + var command = new Command(name, description); + + foreach (var option in TOptions.Options) + { + command.Add(option); + } + + foreach (var argument in TOptions.Arguments) + { + command.Add(argument); + } + + command.Action = Handler; + return command; + } + + protected static BindingHandler Handler => + CommandHandler.Create(async (options, host) => + { + var thisCommand = host.Services.GetRequiredService>(); + return await thisCommand.ExecuteAsync(options); + }); +} diff --git a/eng/update-dependencies/BaseUrlUpdater.cs b/eng/update-dependencies/BaseUrlUpdater.cs index 647c7f2d69..5d2c97826f 100644 --- a/eng/update-dependencies/BaseUrlUpdater.cs +++ b/eng/update-dependencies/BaseUrlUpdater.cs @@ -15,20 +15,20 @@ namespace Dotnet.Docker; internal class BaseUrlUpdater : FileRegexUpdater { private const string BaseUrlGroupName = "BaseUrlValue"; - private readonly Options _options; + private readonly SpecificCommandOptions _options; private readonly JObject _manifestVariables; - public BaseUrlUpdater(string repoRoot, Options options) + public BaseUrlUpdater(string repoRoot, SpecificCommandOptions options) { - Path = System.IO.Path.Combine(repoRoot, UpdateDependencies.VersionsFilename); + Path = System.IO.Path.Combine(repoRoot, SpecificCommand.VersionsFilename); VersionGroupName = BaseUrlGroupName; Regex = ManifestHelper.GetManifestVariableRegex( ManifestHelper.GetBaseUrlVariableName(options.DockerfileVersion, options.SourceBranch, options.VersionSourceName), $"(?<{BaseUrlGroupName}>.+)"); _options = options; - _manifestVariables = (JObject?)ManifestHelper.LoadManifest(UpdateDependencies.VersionsFilename)["variables"] ?? - throw new InvalidOperationException($"'{UpdateDependencies.VersionsFilename}' property missing in '{UpdateDependencies.VersionsFilename}'"); ; + _manifestVariables = (JObject?)ManifestHelper.LoadManifest(SpecificCommand.VersionsFilename)["variables"] ?? + throw new InvalidOperationException($"'{SpecificCommand.VersionsFilename}' property missing in '{SpecificCommand.VersionsFilename}'"); ; } protected override string TryGetDesiredValue(IEnumerable dependencyInfos, out IEnumerable usedDependencyInfos) diff --git a/eng/update-dependencies/DockerfileShaUpdater.cs b/eng/update-dependencies/DockerfileShaUpdater.cs index 986c1ba704..0e522c7f10 100644 --- a/eng/update-dependencies/DockerfileShaUpdater.cs +++ b/eng/update-dependencies/DockerfileShaUpdater.cs @@ -1,6 +1,5 @@ // 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; @@ -38,13 +37,13 @@ public class DockerfileShaUpdater : FileRegexUpdater private readonly string? _buildVersion; private readonly string _arch; private readonly string _os; - private readonly Options _options; + private readonly SpecificCommandOptions _options; private readonly string _versions; private readonly Dictionary _urls; private readonly Lazy _manifestVariables; public DockerfileShaUpdater( - string productName, string dockerfileVersion, string? buildVersion, string arch, string os, string versions, Options options) + string productName, string dockerfileVersion, string? buildVersion, string arch, string os, string versions, SpecificCommandOptions options) { _productName = productName; _dockerfileVersion = new Version(dockerfileVersion); @@ -74,10 +73,10 @@ public DockerfileShaUpdater( () => { const string VariablesProperty = "variables"; - JToken? variables = ManifestHelper.LoadManifest(UpdateDependencies.VersionsFilename)[VariablesProperty]; + JToken? variables = ManifestHelper.LoadManifest(SpecificCommand.VersionsFilename)[VariablesProperty]; if (variables is null) { - throw new InvalidOperationException($"'{VariablesProperty}' property missing in '{UpdateDependencies.VersionsFilename}'"); + throw new InvalidOperationException($"'{VariablesProperty}' property missing in '{SpecificCommand.VersionsFilename}'"); } return (JObject)variables; }); @@ -92,23 +91,14 @@ public DockerfileShaUpdater( } public static IEnumerable CreateUpdaters( - string productName, string dockerfileVersion, string repoRoot, Options options) + string productName, string dockerfileVersion, string repoRoot, SpecificCommandOptions options) { - string versionsPath = System.IO.Path.Combine(repoRoot, UpdateDependencies.VersionsFilename); + string versionsPath = System.IO.Path.Combine(repoRoot, SpecificCommand.VersionsFilename); string versions = File.ReadAllText(versionsPath); // The format of the sha variable name is '||||sha'. // The 'os' and 'arch' segments are optional. - string shaVariablePattern; - if (productName == NetStandard21TargetingPack) - { - // NetStandard targeting pack is not associated with a specific Dockerfile version - shaVariablePattern = $"\"(?<{ShaVariableGroupName}>{Regex.Escape(productName)}.*\\|sha)\":"; - } - else - { - shaVariablePattern = $"\"(?<{ShaVariableGroupName}>{Regex.Escape(productName)}\\|{Regex.Escape(dockerfileVersion)}.*\\|sha)\":"; - } + string shaVariablePattern = $"\"(?<{ShaVariableGroupName}>{Regex.Escape(productName)}\\|{Regex.Escape(dockerfileVersion)}.*\\|sha)\":"; Regex shaVariableRegex = new(shaVariablePattern); @@ -151,7 +141,7 @@ public static IEnumerable CreateUpdaters( // Remove Aspire Dashboard case once https://github.com/dotnet/aspire/issues/2035 is fixed. string archiveExt = _os.Contains("win") || _productName.Contains("aspire-dashboard") ? "zip" : "tar.gz"; string versionDir = _buildVersion ?? ""; - string versionFile = UpdateDependencies.ResolveProductVersion(versionDir, _options); + string versionFile = SpecificCommand.ResolveProductVersion(versionDir, _options); string downloadUrl = _urls[_productName] .Replace("$DOTNET_BASE_URL", baseUrl) @@ -416,7 +406,7 @@ Other times the first line is the first checksum entry. This return checksumEntries; } - private static string? GetBuildVersion(string productName, string dockerfileVersion, string variables, Options options) + private static string? GetBuildVersion(string productName, string dockerfileVersion, string variables, SpecificCommandOptions options) { string? buildVersion; if (options.ProductVersions.TryGetValue(productName, out string? version)) diff --git a/eng/update-dependencies/IOptions.cs b/eng/update-dependencies/IOptions.cs new file mode 100644 index 0000000000..cf90739422 --- /dev/null +++ b/eng/update-dependencies/IOptions.cs @@ -0,0 +1,13 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Collections.Generic; +using System.CommandLine; + +namespace Dotnet.Docker; + +public interface IOptions +{ + public static abstract List