From ed6865b4139dfbbacba80a61aa79b2f5a242f2b6 Mon Sep 17 00:00:00 2001 From: Marc Paine Date: Tue, 4 Oct 2022 16:28:11 -0700 Subject: [PATCH 1/7] Updated various versions to fix some transitive CG issues --- eng/Versions.props | 2 +- global.json | 3 +++ src/MSBuildBinLogQuery/MSBuildBinLogQuery.csproj | 2 +- src/core-sdk-tasks/AzurePublisher.cs | 4 +--- src/core-sdk-tasks/core-sdk-tasks.csproj | 4 ++-- src/dotnet-core-uninstall/dotnet-core-uninstall.csproj | 4 ++-- 6 files changed, 10 insertions(+), 9 deletions(-) diff --git a/eng/Versions.props b/eng/Versions.props index ac52d1f1..c924acdc 100644 --- a/eng/Versions.props +++ b/eng/Versions.props @@ -15,6 +15,6 @@ 13.0.1 3.2.2146 - 9.3.3 + 12.13.1 diff --git a/global.json b/global.json index ddc6167e..ba40af30 100644 --- a/global.json +++ b/global.json @@ -1,4 +1,7 @@ { + "sdk": { + "version": "7.0.100-rc.1.22431.12" + }, "tools": { "dotnet": "7.0.100-rc.1.22431.12" }, diff --git a/src/MSBuildBinLogQuery/MSBuildBinLogQuery.csproj b/src/MSBuildBinLogQuery/MSBuildBinLogQuery.csproj index 4ffb13f3..d04254b8 100644 --- a/src/MSBuildBinLogQuery/MSBuildBinLogQuery.csproj +++ b/src/MSBuildBinLogQuery/MSBuildBinLogQuery.csproj @@ -7,7 +7,7 @@ - + diff --git a/src/core-sdk-tasks/AzurePublisher.cs b/src/core-sdk-tasks/AzurePublisher.cs index c537331b..45ea123e 100644 --- a/src/core-sdk-tasks/AzurePublisher.cs +++ b/src/core-sdk-tasks/AzurePublisher.cs @@ -10,9 +10,7 @@ using System.Net.Http; using System.Threading; using System.Threading.Tasks; -using Microsoft.WindowsAzure.Storage; -using Microsoft.WindowsAzure.Storage.Auth; -using Microsoft.WindowsAzure.Storage.Blob; +using Azure.Storage.Blobs; namespace Microsoft.DotNet.Cli.Build { diff --git a/src/core-sdk-tasks/core-sdk-tasks.csproj b/src/core-sdk-tasks/core-sdk-tasks.csproj index 9cffaa32..cf4c5bd0 100644 --- a/src/core-sdk-tasks/core-sdk-tasks.csproj +++ b/src/core-sdk-tasks/core-sdk-tasks.csproj @@ -10,9 +10,9 @@ - + - + diff --git a/src/dotnet-core-uninstall/dotnet-core-uninstall.csproj b/src/dotnet-core-uninstall/dotnet-core-uninstall.csproj index 46578c8f..62a7863a 100644 --- a/src/dotnet-core-uninstall/dotnet-core-uninstall.csproj +++ b/src/dotnet-core-uninstall/dotnet-core-uninstall.csproj @@ -16,8 +16,8 @@ - - + + From a6890a59110d99dc80df436aff9e4ae746dac4ac Mon Sep 17 00:00:00 2001 From: Marc Paine Date: Tue, 4 Oct 2022 16:33:19 -0700 Subject: [PATCH 2/7] Update a few more package versions --- eng/Versions.props | 2 +- src/core-sdk-tasks/core-sdk-tasks.csproj | 8 ++++---- src/dotnet-core-uninstall/dotnet-core-uninstall.csproj | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/eng/Versions.props b/eng/Versions.props index c924acdc..21e291a3 100644 --- a/eng/Versions.props +++ b/eng/Versions.props @@ -14,7 +14,7 @@ release 13.0.1 - 3.2.2146 + 3.3.2180 12.13.1 diff --git a/src/core-sdk-tasks/core-sdk-tasks.csproj b/src/core-sdk-tasks/core-sdk-tasks.csproj index cf4c5bd0..ab8bb7f0 100644 --- a/src/core-sdk-tasks/core-sdk-tasks.csproj +++ b/src/core-sdk-tasks/core-sdk-tasks.csproj @@ -6,12 +6,12 @@ - - - + + + - + diff --git a/src/dotnet-core-uninstall/dotnet-core-uninstall.csproj b/src/dotnet-core-uninstall/dotnet-core-uninstall.csproj index 62a7863a..5402ed6a 100644 --- a/src/dotnet-core-uninstall/dotnet-core-uninstall.csproj +++ b/src/dotnet-core-uninstall/dotnet-core-uninstall.csproj @@ -14,11 +14,11 @@ - - + + - + From bc2b56ff7a05de82996d5ca9254b0e7814c99fa1 Mon Sep 17 00:00:00 2001 From: Chet Husk Date: Tue, 4 Oct 2022 22:02:39 -0500 Subject: [PATCH 3/7] initial conversion --- .../Shared/Commands/UninstallHelpBuilder.cs | 8 +- .../Shared/Configs/CommandLineConfigs.cs | 102 +++++++----------- .../Shared/Filterers/Filterer.cs | 2 +- .../Shared/Configs/CommandLineConfigsTests.cs | 27 +++-- 4 files changed, 62 insertions(+), 77 deletions(-) diff --git a/src/dotnet-core-uninstall/Shared/Commands/UninstallHelpBuilder.cs b/src/dotnet-core-uninstall/Shared/Commands/UninstallHelpBuilder.cs index 6309ebc3..7d3a602c 100644 --- a/src/dotnet-core-uninstall/Shared/Commands/UninstallHelpBuilder.cs +++ b/src/dotnet-core-uninstall/Shared/Commands/UninstallHelpBuilder.cs @@ -9,12 +9,12 @@ namespace Microsoft.DotNet.Tools.Uninstall.Shared.Commands { public class UninstallHelpBuilder : HelpBuilder { - public UninstallHelpBuilder(IConsole console) : base(console) { } + public UninstallHelpBuilder(LocalizationResources localizationResources, int maxWidth = int.MaxValue) : base(localizationResources, maxWidth) { } - public override void Write(ICommand command) + public override void Write(HelpContext context) { - base.Write(command); - if (command.Name.Equals("dry-run") || command.Name.Equals("remove")) + base.Write(context); + if (context.Command.Name.Equals("dry-run") || context.Command.Name.Equals("remove")) { Console.Out.Write(RuntimeInfo.RunningOnWindows ? LocalizableStrings.HelpExplainationParagraphWindows : LocalizableStrings.HelpExplainationParagraphMac); diff --git a/src/dotnet-core-uninstall/Shared/Configs/CommandLineConfigs.cs b/src/dotnet-core-uninstall/Shared/Configs/CommandLineConfigs.cs index fb941947..7d137e27 100644 --- a/src/dotnet-core-uninstall/Shared/Configs/CommandLineConfigs.cs +++ b/src/dotnet-core-uninstall/Shared/Configs/CommandLineConfigs.cs @@ -51,84 +51,60 @@ internal static class CommandLineConfigs public static readonly string X64OptionName = "x64"; public static readonly string X86OptionName = "x86"; - public static readonly Option UninstallAllOption = new Option( + public static readonly Option UninstallAllOption = new Option( "--all", LocalizableStrings.UninstallAllOptionDescription); - public static readonly Option UninstallAllLowerPatchesOption = new Option( + public static readonly Option UninstallAllLowerPatchesOption = new Option( "--all-lower-patches", LocalizableStrings.UninstallAllLowerPatchesOptionDescription); - public static readonly Option UninstallAllButLatestOption = new Option( + public static readonly Option UninstallAllButLatestOption = new Option( "--all-but-latest", LocalizableStrings.UninstallAllButLatestOptionDescription); - public static readonly Option UninstallAllButOption = new Option( + public static readonly Option> UninstallAllButOption = new Option>( "--all-but", - LocalizableStrings.UninstallAllButOptionDescription) - { - Argument = new Argument> - { - Name = LocalizableStrings.UninstallAllButOptionArgumentName - } - }; + LocalizableStrings.UninstallAllButOptionDescription); - public static readonly Option UninstallAllBelowOption = new Option( + public static readonly Option UninstallAllBelowOption = new Option( "--all-below", - LocalizableStrings.UninstallAllBelowOptionDescription) - { - Argument = new Argument - { - Name = LocalizableStrings.UninstallAllBelowOptionArgumentName - } - }; + LocalizableStrings.UninstallAllBelowOptionDescription); - public static readonly Option UninstallAllPreviewsOption = new Option( + public static readonly Option UninstallAllPreviewsOption = new Option( "--all-previews", LocalizableStrings.UninstallAllPreviewsOptionDescription); - public static readonly Option UninstallAllPreviewsButLatestOption = new Option( + public static readonly Option UninstallAllPreviewsButLatestOption = new Option( "--all-previews-but-latest", LocalizableStrings.UninstallAllPreviewsButLatestOptionDescription); - public static readonly Option UninstallMajorMinorOption = new Option( + public static readonly Option UninstallMajorMinorOption = new Option( "--major-minor", - LocalizableStrings.UninstallMajorMinorOptionDescription) - { - Argument = new Argument - { - Name = LocalizableStrings.UninstallMajorMinorOptionArgumentName - } - }; + LocalizableStrings.UninstallMajorMinorOptionDescription); - public static readonly Option VerbosityOption = new Option( + public static readonly Option VerbosityOption = new Option( new[] { "--verbosity", "-v" }, - LocalizableStrings.VerbosityOptionDescription) - { - Argument = new Argument - { - Name = LocalizableStrings.VerbosityOptionArgumentName - } - }; + LocalizableStrings.VerbosityOptionDescription); - public static readonly Option ListX64Option = new Option( + public static readonly Option ListX64Option = new Option( $"--{X64OptionName}", LocalizableStrings.ListX64OptionDescription); - public static readonly Option ListX86Option = new Option( + public static readonly Option ListX86Option = new Option( $"--{X86OptionName}", LocalizableStrings.ListX86OptionDescription); - public static readonly Option VersionOption = new Option("--version") + public static readonly Option VersionOption = new Option("--version") { IsHidden = true }; - public static readonly Option YesOption = new Option( + public static readonly Option YesOption = new Option( new[] { "--yes", "-y" }, LocalizableStrings.YesOptionDescription); - public static readonly Option ForceOption = new Option( + public static readonly Option ForceOption = new Option( "--force", RuntimeInfo.RunningOnWindows ? LocalizableStrings.ForceOptionDescriptionWindows : LocalizableStrings.ForceOptionDescriptionMac); @@ -145,26 +121,26 @@ internal static class CommandLineConfigs UninstallMajorMinorOption }; - public static readonly Option[] ListBundleTypeOptions = new Option[] + public static readonly Option[] ListBundleTypeOptions = new Option[] { - new Option($"--{SdkOptionName}", LocalizableStrings.ListSdkOptionDescription), - new Option($"--{RuntimeOptionName}", LocalizableStrings.ListRuntimeOptionDescription), - new Option($"--{AspNetRuntimeOptionName}", LocalizableStrings.ListAspNetRuntimeOptionDescription), - new Option($"--{HostingBundleOptionName}", LocalizableStrings.ListHostingBundleOptionDescription) + new Option($"--{SdkOptionName}", LocalizableStrings.ListSdkOptionDescription), + new Option($"--{RuntimeOptionName}", LocalizableStrings.ListRuntimeOptionDescription), + new Option($"--{AspNetRuntimeOptionName}", LocalizableStrings.ListAspNetRuntimeOptionDescription), + new Option($"--{HostingBundleOptionName}", LocalizableStrings.ListHostingBundleOptionDescription) }; - public static readonly Option[] UninstallBundleTypeOptions = new Option[] + public static readonly Option[] UninstallBundleTypeOptions = new Option[] { - new Option($"--{SdkOptionName}", LocalizableStrings.UninstallSdkOptionDescription), - new Option($"--{RuntimeOptionName}", LocalizableStrings.UninstallRuntimeOptionDescription), - new Option($"--{AspNetRuntimeOptionName}", LocalizableStrings.UninstallAspNetRuntimeOptionDescription), - new Option($"--{HostingBundleOptionName}", LocalizableStrings.UninstallHostingBundleOptionDescription) + new Option($"--{SdkOptionName}", LocalizableStrings.UninstallSdkOptionDescription), + new Option($"--{RuntimeOptionName}", LocalizableStrings.UninstallRuntimeOptionDescription), + new Option($"--{AspNetRuntimeOptionName}", LocalizableStrings.UninstallAspNetRuntimeOptionDescription), + new Option($"--{HostingBundleOptionName}", LocalizableStrings.UninstallHostingBundleOptionDescription) }; - public static readonly Option[] ArchUninstallOptions = new Option[] + public static readonly Option[] ArchUninstallOptions = new Option[] { - new Option($"--{X64OptionName}", LocalizableStrings.UninstallX64OptionDescription), - new Option($"--{X86OptionName}", LocalizableStrings.UninstallX86OptionDescription) + new Option($"--{X64OptionName}", LocalizableStrings.UninstallX64OptionDescription), + new Option($"--{X86OptionName}", LocalizableStrings.UninstallX86OptionDescription) }; public static readonly Option[] AdditionalUninstallOptions = new Option[] @@ -232,13 +208,13 @@ static CommandLineConfigs() AssignOptionsToCommand(ListCommand, ListAuxOptions); var bundleCollector = RuntimeInfo.RunningOnWindows ? new RegistryQuery() as IBundleCollector : new FileSystemExplorer() as IBundleCollector; - ListCommand.Handler = CommandHandler.Create(ExceptionHandler.HandleException(() => ListCommandExec.Execute(bundleCollector))); - DryRunCommand.Handler = CommandHandler.Create(ExceptionHandler.HandleException(() => DryRunCommandExec.Execute(bundleCollector))); - RemoveCommand.Handler = CommandHandler.Create(ExceptionHandler.HandleException(() => UninstallCommandExec.Execute(bundleCollector))); + ListCommand.SetHandler(_ => ExceptionHandler.HandleException(() => ListCommandExec.Execute(bundleCollector))); + DryRunCommand.SetHandler(_ => ExceptionHandler.HandleException(() => DryRunCommandExec.Execute(bundleCollector))); + RemoveCommand.SetHandler(_ => ExceptionHandler.HandleException(() => UninstallCommandExec.Execute(bundleCollector))); UninstallCommandParser = new CommandLineBuilder(UninstallRootCommand) .UseDefaults() - .UseHelpBuilder(context => new UninstallHelpBuilder(context.Console)) + .UseHelpBuilder(context => new UninstallHelpBuilder(LocalizationResources.Instance)) .Build(); CommandLineParseResult = UninstallCommandParser.Parse(Environment.GetCommandLineArgs()); } @@ -281,7 +257,7 @@ public static BundleType GetTypeSelection(this ParseResult parseResult) var supportedBundleTypes = SupportedBundleTypeConfigs.GetSupportedBundleTypes(); var typeSelection = supportedBundleTypes - .Where(type => parseResult.ValueForOption($"--{type.OptionName}")) + .Where(type => parseResult.CommandResult.Children.FirstOrDefault(c => c is OptionResult o && o.Option.Name == $"--{type.OptionName}" && o.GetValueForOption(o.Option) is bool b && b) is not null) .Select(type => type.Type) .Aggregate((BundleType)0, (orSum, next) => orSum | next); @@ -294,10 +270,10 @@ public static BundleArch GetArchSelection(this ParseResult parseResult) { var archSelection = new[] { - (OptionName: X64OptionName, Arch: BundleArch.X64), - (OptionName: X86OptionName, Arch: BundleArch.X86) + (Option: ListX64Option, Arch: BundleArch.X64), + (Option: ListX86Option, Arch: BundleArch.X86) } - .Where(tuple => parseResult.ValueForOption($"--{tuple.OptionName}")) + .Where(tuple => parseResult.GetValueForOption(tuple.Option)) .Select(tuple => tuple.Arch) .Aggregate((BundleArch)0, (orSum, next) => orSum | next); diff --git a/src/dotnet-core-uninstall/Shared/Filterers/Filterer.cs b/src/dotnet-core-uninstall/Shared/Filterers/Filterer.cs index b41549a0..0da68a75 100644 --- a/src/dotnet-core-uninstall/Shared/Filterers/Filterer.cs +++ b/src/dotnet-core-uninstall/Shared/Filterers/Filterer.cs @@ -48,7 +48,7 @@ internal abstract class ArgFilterer : Filterer { public override IEnumerable Filter(ParseResult parseResult, Option option, IEnumerable bundles, BundleType typeSelection, BundleArch archSelection) { - var argValue = parseResult.ValueForOption(option); + var argValue = parseResult.GetValueForOption(option as Option); return Filter(argValue, bundles, typeSelection, archSelection); } diff --git a/test/dotnet-core-uninstall.Tests/Shared/Configs/CommandLineConfigsTests.cs b/test/dotnet-core-uninstall.Tests/Shared/Configs/CommandLineConfigsTests.cs index b4dc31f0..e17b1ed8 100644 --- a/test/dotnet-core-uninstall.Tests/Shared/Configs/CommandLineConfigsTests.cs +++ b/test/dotnet-core-uninstall.Tests/Shared/Configs/CommandLineConfigsTests.cs @@ -114,8 +114,9 @@ internal void TestRemoveCommandAccept(string option, string argValue = "", objec if (!option.Equals(string.Empty)) { - parseResult.CommandResult.OptionResult(option).Should().NotBeNull(); - parseResult.ValueForOption(option).Should().BeEquivalentTo(expected); + var optionResult = parseResult.CommandResult.Children.First(c => c is OptionResult o && o.Option.Name == option) as OptionResult; + optionResult.Should().NotBeNull(); + parseResult.GetValueForOption(optionResult.Option).Should().BeEquivalentTo(expected); } else { @@ -148,8 +149,9 @@ internal void TestDryRunCommandAccept(string option, string argValue = "", objec if (!option.Equals(string.Empty)) { - parseResult.CommandResult.OptionResult(option).Should().NotBeNull(); - parseResult.ValueForOption(option).Should().BeEquivalentTo(expected); + var optionResult = parseResult.CommandResult.Children.First(c => c is OptionResult o && o.Option.Name == option) as OptionResult; + optionResult.Should().NotBeNull(); + parseResult.GetValueForOption(optionResult.Option).Should().BeEquivalentTo(expected); } else { @@ -182,8 +184,9 @@ internal void TestWhatIfCommandAccept(string option, string argValue = "", objec if (!option.Equals(string.Empty)) { - parseResult.CommandResult.OptionResult(option).Should().NotBeNull(); - parseResult.ValueForOption(option).Should().BeEquivalentTo(expected); + var optionResult = parseResult.CommandResult.Children.First(c => c is OptionResult o && o.Option.Name == option) as OptionResult; + optionResult.Should().NotBeNull(); + parseResult.GetValueForOption(optionResult.Option).Should().BeEquivalentTo(expected); } else { @@ -422,8 +425,10 @@ internal void TestGetUninstallRemoveOptionAccept(string option, string argValue } else { + var optionResult = commandResult.Children.First(c => c is OptionResult o) as OptionResult; + optionResult.Should().NotBeNull(); commandResult.GetUninstallMainOption().Name - .Should().Be(commandResult.OptionResult(option).Option.Name); + .Should().Be(optionResult.Option.Name); } } @@ -451,8 +456,10 @@ internal void TestGetUninstallDryRunOptionAccept(string option, string argValue } else { + var optionResult = commandResult.Children.First(c => c is OptionResult o) as OptionResult; + optionResult.Should().NotBeNull(); commandResult.GetUninstallMainOption().Name - .Should().Be(commandResult.OptionResult(option).Option.Name); + .Should().Be(optionResult.Option.Name); } } @@ -480,8 +487,10 @@ internal void TestGetUninstallWhatIfOptionAccept(string option, string argValue } else { + var optionResult = commandResult.Children.First(c => c is OptionResult o) as OptionResult; + optionResult.Should().NotBeNull(); commandResult.GetUninstallMainOption().Name - .Should().Be(commandResult.OptionResult(option).Option.Name); + .Should().Be(optionResult.Option.Name); } } From fd1db3578114053f79848dbb7fcb943513e6ffab Mon Sep 17 00:00:00 2001 From: Chet Husk Date: Wed, 5 Oct 2022 10:38:36 -0500 Subject: [PATCH 4/7] make validators more strict and fix test baselines --- .../Shared/Configs/CommandLineConfigs.cs | 62 +++++++++++++++++-- .../Shared/Configs/CommandLineConfigsTests.cs | 44 ++++++------- 2 files changed, 78 insertions(+), 28 deletions(-) diff --git a/src/dotnet-core-uninstall/Shared/Configs/CommandLineConfigs.cs b/src/dotnet-core-uninstall/Shared/Configs/CommandLineConfigs.cs index 7d137e27..c6a076c0 100644 --- a/src/dotnet-core-uninstall/Shared/Configs/CommandLineConfigs.cs +++ b/src/dotnet-core-uninstall/Shared/Configs/CommandLineConfigs.cs @@ -8,6 +8,7 @@ using System.CommandLine.Invocation; using System.CommandLine.Parsing; using System.Linq; +using System.Text.RegularExpressions; using Microsoft.DotNet.Tools.Uninstall.MacOs; using Microsoft.DotNet.Tools.Uninstall.Shared.BundleInfo; using Microsoft.DotNet.Tools.Uninstall.Shared.Commands; @@ -63,13 +64,50 @@ internal static class CommandLineConfigs "--all-but-latest", LocalizableStrings.UninstallAllButLatestOptionDescription); - public static readonly Option> UninstallAllButOption = new Option>( + public static Regex SemVer = new Regex(@"^([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+)?$"); + public static string CheckTokenIsVersion(ArgumentResult r) { + // ideally we'd parse and check a semver here, but not sure we want to take + // that dependency + var input = r.Tokens[0].Value; + if(SemVer.IsMatch(input) || Version.TryParse(input, out _)) { + r.OnlyTake(1); + return input; + } else { + r.OnlyTake(0); + r.ErrorMessage = $"Version '{input}' is not a valid SemVer version or .NET Version"; + return null; + } + } + + public static string[] CheckTokensAreVersion(ArgumentResult r) { + // ideally we'd parse and check a semver here, but not sure we want to take + // that dependency + var parsed = new List(); + foreach(var token in r.Tokens) { + var input = token.Value; + if(SemVer.IsMatch(input) || Version.TryParse(input, out _)) { + parsed.Add(input); + } else { + r.ErrorMessage = $"Version '{input}' is not a valid SemVer version or .NET Version"; + return null; + } + } + r.OnlyTake(parsed.Count); + return parsed.ToArray(); + } + + public static readonly Option UninstallAllButOption = new Option( "--all-but", - LocalizableStrings.UninstallAllButOptionDescription); + parseArgument: CheckTokensAreVersion, + description: LocalizableStrings.UninstallAllButOptionDescription) { + AllowMultipleArgumentsPerToken = true + }; public static readonly Option UninstallAllBelowOption = new Option( "--all-below", - LocalizableStrings.UninstallAllBelowOptionDescription); + parseArgument: CheckTokenIsVersion, + description: LocalizableStrings.UninstallAllBelowOptionDescription + ); public static readonly Option UninstallAllPreviewsOption = new Option( "--all-previews", @@ -78,10 +116,22 @@ internal static class CommandLineConfigs public static readonly Option UninstallAllPreviewsButLatestOption = new Option( "--all-previews-but-latest", LocalizableStrings.UninstallAllPreviewsButLatestOptionDescription); + + private static Regex MajorMinor = new Regex(@"^\d+\.\d+$"); + public static string ParseMajorMinor(ArgumentResult r) { + var input = r.Tokens[0].Value; + if (MajorMinor.IsMatch(input)) { + return input; + } else { + r.ErrorMessage = $"The version '{input}' is not in the format Major.Minor."; + return null; + } + } public static readonly Option UninstallMajorMinorOption = new Option( "--major-minor", - LocalizableStrings.UninstallMajorMinorOptionDescription); + parseArgument: ParseMajorMinor, + description: LocalizableStrings.UninstallMajorMinorOptionDescription); public static readonly Option VerbosityOption = new Option( new[] { "--verbosity", "-v" }, @@ -95,7 +145,7 @@ internal static class CommandLineConfigs $"--{X86OptionName}", LocalizableStrings.ListX86OptionDescription); - public static readonly Option VersionOption = new Option("--version") + public static readonly Option VersionOption = new Option("--version") { IsHidden = true }; @@ -257,7 +307,7 @@ public static BundleType GetTypeSelection(this ParseResult parseResult) var supportedBundleTypes = SupportedBundleTypeConfigs.GetSupportedBundleTypes(); var typeSelection = supportedBundleTypes - .Where(type => parseResult.CommandResult.Children.FirstOrDefault(c => c is OptionResult o && o.Option.Name == $"--{type.OptionName}" && o.GetValueForOption(o.Option) is bool b && b) is not null) + .Where(type => parseResult.CommandResult.Children.FirstOrDefault(c => c is OptionResult o && o.Option.Name == type.OptionName && o.GetValueForOption(o.Option) is bool b && b) is not null) .Select(type => type.Type) .Aggregate((BundleType)0, (orSum, next) => orSum | next); diff --git a/test/dotnet-core-uninstall.Tests/Shared/Configs/CommandLineConfigsTests.cs b/test/dotnet-core-uninstall.Tests/Shared/Configs/CommandLineConfigsTests.cs index e17b1ed8..08bf05c3 100644 --- a/test/dotnet-core-uninstall.Tests/Shared/Configs/CommandLineConfigsTests.cs +++ b/test/dotnet-core-uninstall.Tests/Shared/Configs/CommandLineConfigsTests.cs @@ -95,14 +95,14 @@ internal void TestListCommandReject(string command) } [Theory] - [InlineData("--all")] - [InlineData("--all-lower-patches")] - [InlineData("--all-but-latest")] + [InlineData("--all", null, true)] + [InlineData("--all-lower-patches", null, true)] + [InlineData("--all-but-latest", null, true)] [InlineData("--all-but", "2.2.300", new[] { "2.2.300" })] [InlineData("--all-but", "2.2.300 3.0.100", new[] { "2.2.300", "3.0.100" })] [InlineData("--all-below", "2.2.300", "2.2.300")] - [InlineData("--all-previews")] - [InlineData("--all-previews-but-latest")] + [InlineData("--all-previews", null, true)] + [InlineData("--all-previews-but-latest", null, true)] [InlineData("--major-minor", "2.2", "2.2")] [InlineData("", "2.2.300", new[] { "2.2.300" })] [InlineData("", "2.2.300 3.0.100", new[] { "2.2.300", "3.0.100" })] @@ -114,7 +114,7 @@ internal void TestRemoveCommandAccept(string option, string argValue = "", objec if (!option.Equals(string.Empty)) { - var optionResult = parseResult.CommandResult.Children.First(c => c is OptionResult o && o.Option.Name == option) as OptionResult; + var optionResult = parseResult.CommandResult.Children.First(c => c is OptionResult o && o.Option.Name == option.TrimStart('-')) as OptionResult; optionResult.Should().NotBeNull(); parseResult.GetValueForOption(optionResult.Option).Should().BeEquivalentTo(expected); } @@ -130,26 +130,26 @@ internal void TestRemoveCommandAccept(string option, string argValue = "", objec } [Theory] - [InlineData("--all")] - [InlineData("--all-lower-patches")] - [InlineData("--all-but-latest")] + [InlineData("--all", null, true)] + [InlineData("--all-lower-patches", null, true)] + [InlineData("--all-but-latest", null, true)] [InlineData("--all-but", "2.2.300", new[] { "2.2.300" })] [InlineData("--all-but", "2.2.300 3.0.100", new[] { "2.2.300", "3.0.100" })] [InlineData("--all-below", "2.2.300", "2.2.300")] - [InlineData("--all-previews")] - [InlineData("--all-previews-but-latest")] + [InlineData("--all-previews", null, true)] + [InlineData("--all-previews-but-latest", null, true)] [InlineData("--major-minor", "2.2", "2.2")] [InlineData("", "2.2.300", new[] { "2.2.300" })] [InlineData("", "2.2.300 3.0.100", new[] { "2.2.300", "3.0.100" })] [InlineData("", "--unknown-option", new[] { "--unknown-option" })] [InlineData("", "--unknown-option argument", new[] { "--unknown-option", "argument" })] - internal void TestDryRunCommandAccept(string option, string argValue = "", object expected = null) + internal void TestDryRunCommandAccept(string option, string argValue, object expected) { var parseResult = CommandLineConfigs.UninstallRootCommand.Parse($"dry-run {option} {argValue}"); if (!option.Equals(string.Empty)) { - var optionResult = parseResult.CommandResult.Children.First(c => c is OptionResult o && o.Option.Name == option) as OptionResult; + var optionResult = parseResult.CommandResult.Children.First(c => c is OptionResult o && o.Option.Name == option.TrimStart('-')) as OptionResult; optionResult.Should().NotBeNull(); parseResult.GetValueForOption(optionResult.Option).Should().BeEquivalentTo(expected); } @@ -165,14 +165,14 @@ internal void TestDryRunCommandAccept(string option, string argValue = "", objec } [Theory] - [InlineData("--all")] - [InlineData("--all-lower-patches")] - [InlineData("--all-but-latest")] + [InlineData("--all", null, true)] + [InlineData("--all-lower-patches", null, true)] + [InlineData("--all-but-latest", null, true)] [InlineData("--all-but", "2.2.300", new[] { "2.2.300" })] [InlineData("--all-but", "2.2.300 3.0.100", new[] { "2.2.300", "3.0.100" })] [InlineData("--all-below", "2.2.300", "2.2.300")] - [InlineData("--all-previews")] - [InlineData("--all-previews-but-latest")] + [InlineData("--all-previews", null, true)] + [InlineData("--all-previews-but-latest", null, true)] [InlineData("--major-minor", "2.2", "2.2")] [InlineData("", "2.2.300", new[] { "2.2.300" })] [InlineData("", "2.2.300 3.0.100", new[] { "2.2.300", "3.0.100" })] @@ -184,7 +184,7 @@ internal void TestWhatIfCommandAccept(string option, string argValue = "", objec if (!option.Equals(string.Empty)) { - var optionResult = parseResult.CommandResult.Children.First(c => c is OptionResult o && o.Option.Name == option) as OptionResult; + var optionResult = parseResult.CommandResult.Children.First(c => c is OptionResult o && o.Option.Name == option.TrimStart('-')) as OptionResult; optionResult.Should().NotBeNull(); parseResult.GetValueForOption(optionResult.Option).Should().BeEquivalentTo(expected); } @@ -425,7 +425,7 @@ internal void TestGetUninstallRemoveOptionAccept(string option, string argValue } else { - var optionResult = commandResult.Children.First(c => c is OptionResult o) as OptionResult; + var optionResult = commandResult.Children.First(c => c is OptionResult o && o.Option.Name == option.TrimStart('-')) as OptionResult; optionResult.Should().NotBeNull(); commandResult.GetUninstallMainOption().Name .Should().Be(optionResult.Option.Name); @@ -456,7 +456,7 @@ internal void TestGetUninstallDryRunOptionAccept(string option, string argValue } else { - var optionResult = commandResult.Children.First(c => c is OptionResult o) as OptionResult; + var optionResult = commandResult.Children.First(c => c is OptionResult o && o.Option.Name == option.TrimStart('-')) as OptionResult; optionResult.Should().NotBeNull(); commandResult.GetUninstallMainOption().Name .Should().Be(optionResult.Option.Name); @@ -487,7 +487,7 @@ internal void TestGetUninstallWhatIfOptionAccept(string option, string argValue } else { - var optionResult = commandResult.Children.First(c => c is OptionResult o) as OptionResult; + var optionResult = commandResult.Children.First(c => c is OptionResult o && o.Option.Name == option.TrimStart('-')) as OptionResult; optionResult.Should().NotBeNull(); commandResult.GetUninstallMainOption().Name .Should().Be(optionResult.Option.Name); From 92ce955a790e3d2263946e49fb9da32b69f8f0a8 Mon Sep 17 00:00:00 2001 From: Chet Husk Date: Wed, 5 Oct 2022 12:44:30 -0500 Subject: [PATCH 5/7] add missing help paragraph --- .../Shared/Commands/UninstallHelpBuilder.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/dotnet-core-uninstall/Shared/Commands/UninstallHelpBuilder.cs b/src/dotnet-core-uninstall/Shared/Commands/UninstallHelpBuilder.cs index 7d3a602c..3f4604a5 100644 --- a/src/dotnet-core-uninstall/Shared/Commands/UninstallHelpBuilder.cs +++ b/src/dotnet-core-uninstall/Shared/Commands/UninstallHelpBuilder.cs @@ -16,9 +16,9 @@ public override void Write(HelpContext context) base.Write(context); if (context.Command.Name.Equals("dry-run") || context.Command.Name.Equals("remove")) { - Console.Out.Write(RuntimeInfo.RunningOnWindows ? LocalizableStrings.HelpExplainationParagraphWindows : + context.Output.Write(RuntimeInfo.RunningOnWindows ? LocalizableStrings.HelpExplainationParagraphWindows : LocalizableStrings.HelpExplainationParagraphMac); - Console.Out.Write(Environment.NewLine); + context.Output.Write(Environment.NewLine); } } } From 107249cad68c1c61e2a3d18406ce75ccf857f962 Mon Sep 17 00:00:00 2001 From: Marc Paine Date: Fri, 7 Oct 2022 16:19:13 -0700 Subject: [PATCH 6/7] Remove the Azure Publisher as it doesn't appear to be used and had an old reference. --- eng/Versions.props | 1 - src/core-sdk-tasks/AzurePublisher.cs | 218 ----------------------- src/core-sdk-tasks/CopyBlobsToLatest.cs | 132 -------------- src/core-sdk-tasks/core-sdk-tasks.csproj | 1 - 4 files changed, 352 deletions(-) delete mode 100644 src/core-sdk-tasks/AzurePublisher.cs delete mode 100644 src/core-sdk-tasks/CopyBlobsToLatest.cs diff --git a/eng/Versions.props b/eng/Versions.props index 21e291a3..ec047b38 100644 --- a/eng/Versions.props +++ b/eng/Versions.props @@ -15,6 +15,5 @@ 13.0.1 3.3.2180 - 12.13.1 diff --git a/src/core-sdk-tasks/AzurePublisher.cs b/src/core-sdk-tasks/AzurePublisher.cs deleted file mode 100644 index 45ea123e..00000000 --- a/src/core-sdk-tasks/AzurePublisher.cs +++ /dev/null @@ -1,218 +0,0 @@ -// Copyright (c) .NET Foundation and contributors. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -#if !SOURCE_BUILD -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.IO; -using System.Linq; -using System.Net.Http; -using System.Threading; -using System.Threading.Tasks; -using Azure.Storage.Blobs; - -namespace Microsoft.DotNet.Cli.Build -{ - public class AzurePublisher - { - public enum Product - { - SharedFramework, - Host, - HostFxr, - Sdk, - } - - private const string s_dotnetBlobContainerName = "dotnet"; - - private string _connectionString { get; set; } - private string _containerName { get; set; } - private CloudBlobContainer _blobContainer { get; set; } - - public AzurePublisher(string accountName, string accountKey, string containerName = s_dotnetBlobContainerName) - { - _containerName = containerName; - _blobContainer = GetDotnetBlobContainer(accountName, accountKey, containerName); - } - - private CloudBlobContainer GetDotnetBlobContainer(string accountName, string accountKey, string containerName) - { - var storageCredentials = new StorageCredentials(accountName, accountKey); - var storageAccount = new CloudStorageAccount(storageCredentials, true); - return GetDotnetBlobContainer(storageAccount, containerName); - } - - private CloudBlobContainer GetDotnetBlobContainer(CloudStorageAccount storageAccount, string containerName) - { - CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); - - return blobClient.GetContainerReference(containerName); - } - - public string UploadFile(string file, Product product, string version) - { - string url = CalculateRelativePathForFile(file, product, version); - CloudBlockBlob blob = _blobContainer.GetBlockBlobReference(url); - blob.UploadFromFileAsync(file).Wait(); - SetBlobPropertiesBasedOnFileType(blob); - return url; - } - - public void PublishStringToBlob(string blob, string content) - { - CloudBlockBlob blockBlob = _blobContainer.GetBlockBlobReference(blob); - blockBlob.UploadTextAsync(content).Wait(); - - SetBlobPropertiesBasedOnFileType(blockBlob); - } - - public void CopyBlob(string sourceBlob, string targetBlob) - { - CloudBlockBlob source = _blobContainer.GetBlockBlobReference(sourceBlob); - CloudBlockBlob target = _blobContainer.GetBlockBlobReference(targetBlob); - - // Create the empty blob - using (MemoryStream ms = new MemoryStream()) - { - target.UploadFromStreamAsync(ms).Wait(); - } - - // Copy actual blob data - target.StartCopyAsync(source).Wait(); - } - - public void SetBlobPropertiesBasedOnFileType(string path) - { - CloudBlockBlob blob = _blobContainer.GetBlockBlobReference(path); - SetBlobPropertiesBasedOnFileType(blob); - } - - private void SetBlobPropertiesBasedOnFileType(CloudBlockBlob blockBlob) - { - if (Path.GetExtension(blockBlob.Uri.AbsolutePath.ToLower()) == ".svg") - { - blockBlob.Properties.ContentType = "image/svg+xml"; - blockBlob.Properties.CacheControl = "no-cache"; - blockBlob.SetPropertiesAsync().Wait(); - } - else if (Path.GetExtension(blockBlob.Uri.AbsolutePath.ToLower()) == ".version") - { - blockBlob.Properties.ContentType = "text/plain"; - blockBlob.Properties.CacheControl = "no-cache"; - blockBlob.SetPropertiesAsync().Wait(); - } - } - - public IEnumerable ListBlobs(Product product, string version) - { - string virtualDirectory = $"{product}/{version}"; - return ListBlobs(virtualDirectory); - } - - public IEnumerable ListBlobs(string virtualDirectory) - { - CloudBlobDirectory blobDir = _blobContainer.GetDirectoryReference(virtualDirectory); - BlobContinuationToken continuationToken = new BlobContinuationToken(); - - var blobFiles = blobDir.ListBlobsSegmentedAsync(continuationToken).Result; - return blobFiles.Results.Select(bf => bf.Uri.PathAndQuery.Replace($"/{_containerName}/", string.Empty)); - } - - public string AcquireLeaseOnBlob( - string blob, - TimeSpan? maxWaitDefault = null, - TimeSpan? delayDefault = null) - { - TimeSpan maxWait = maxWaitDefault ?? TimeSpan.FromSeconds(120); - TimeSpan delay = delayDefault ?? TimeSpan.FromMilliseconds(500); - - Stopwatch stopWatch = new Stopwatch(); - stopWatch.Start(); - - // This will throw an exception with HTTP code 409 when we cannot acquire the lease - // But we should block until we can get this lease, with a timeout (maxWaitSeconds) - while (stopWatch.ElapsedMilliseconds < maxWait.TotalMilliseconds) - { - try - { - CloudBlockBlob cloudBlob = _blobContainer.GetBlockBlobReference(blob); - Task task = cloudBlob.AcquireLeaseAsync(TimeSpan.FromMinutes(1), null); - task.Wait(); - return task.Result; - } - catch (Exception e) - { - Console.WriteLine($"Retrying lease acquisition on {blob}, {e.Message}"); - Thread.Sleep(delay); - } - } - - throw new Exception($"Unable to acquire lease on {blob}"); - } - - public void ReleaseLeaseOnBlob(string blob, string leaseId) - { - CloudBlockBlob cloudBlob = _blobContainer.GetBlockBlobReference(blob); - AccessCondition ac = new AccessCondition() { LeaseId = leaseId }; - cloudBlob.ReleaseLeaseAsync(ac).Wait(); - } - - public bool IsLatestSpecifiedVersion(string version) - { - Task task = _blobContainer.GetBlockBlobReference(version).ExistsAsync(); - task.Wait(); - return task.Result; - } - - public void DropLatestSpecifiedVersion(string version) - { - CloudBlockBlob blob = _blobContainer.GetBlockBlobReference(version); - using (MemoryStream ms = new MemoryStream()) - { - blob.UploadFromStreamAsync(ms).Wait(); - } - } - - public void CreateBlobIfNotExists(string path) - { - Task task = _blobContainer.GetBlockBlobReference(path).ExistsAsync(); - task.Wait(); - if (!task.Result) - { - CloudBlockBlob blob = _blobContainer.GetBlockBlobReference(path); - using (MemoryStream ms = new MemoryStream()) - { - blob.UploadFromStreamAsync(ms).Wait(); - } - } - } - - public bool TryDeleteBlob(string path) - { - try - { - DeleteBlob(path); - - return true; - } - catch (Exception e) - { - Console.WriteLine($"Deleting blob {path} failed with \r\n{e.Message}"); - - return false; - } - } - - private void DeleteBlob(string path) - { - _blobContainer.GetBlockBlobReference(path).DeleteAsync().Wait(); - } - - private static string CalculateRelativePathForFile(string file, Product product, string version) - { - return $"{product}/{version}/{Path.GetFileName(file)}"; - } - } -} -#endif \ No newline at end of file diff --git a/src/core-sdk-tasks/CopyBlobsToLatest.cs b/src/core-sdk-tasks/CopyBlobsToLatest.cs deleted file mode 100644 index 734898a7..00000000 --- a/src/core-sdk-tasks/CopyBlobsToLatest.cs +++ /dev/null @@ -1,132 +0,0 @@ -// Copyright (c) .NET Foundation and contributors. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -#if !SOURCE_BUILD -using Microsoft.Build.Framework; -using Microsoft.Build.Utilities; -using System; -using System.IO; -using System.Linq; -using System.Text.RegularExpressions; - -namespace Microsoft.DotNet.Cli.Build -{ - public class CopyBlobsToLatest : Task - { - private const string feedRegex = @"(?https:\/\/(?[^\.-]+)(?[^\/]*)\/((?[a-zA-Z0-9+\/]*?\/\d{4}-\d{2}-\d{2})\/)?(?[^\/]+)\/(?.*\/)?)index\.json"; - - private AzurePublisher _azurePublisher; - - [Required] - public string FeedUrl { get; set; } - - [Required] - public string AccountKey { get; set; } - - [Required] - public string Channel { get; set; } - - [Required] - public string CommitHash { get; set; } - - [Required] - public string NugetVersion { get; set; } - - private string ContainerName { get; set; } - - private AzurePublisher AzurePublisherTool - { - get - { - if (_azurePublisher == null) - { - Match m = Regex.Match(FeedUrl, feedRegex); - if (m.Success) - { - string accountName = m.Groups["accountname"].Value; - string ContainerName = m.Groups["containername"].Value; - - _azurePublisher = new AzurePublisher( - accountName, - AccountKey, - ContainerName); - } - else - { - throw new Exception( - "Unable to parse expected feed. Please check ExpectedFeedUrl."); - } - } - - return _azurePublisher; - } - } - - public override bool Execute() - { - string targetFolder = $"{AzurePublisher.Product.Sdk}/{Channel}"; - - string targetVersionFile = $"{targetFolder}/{CommitHash}"; - string semaphoreBlob = $"{targetFolder}/publishSemaphore"; - AzurePublisherTool.CreateBlobIfNotExists(semaphoreBlob); - string leaseId = AzurePublisherTool.AcquireLeaseOnBlob(semaphoreBlob); - - // Prevent race conditions by dropping a version hint of what version this is. If we see this file - // and it is the same as our version then we know that a race happened where two+ builds finished - // at the same time and someone already took care of publishing and we have no work to do. - if (AzurePublisherTool.IsLatestSpecifiedVersion(targetVersionFile)) - { - AzurePublisherTool.ReleaseLeaseOnBlob(semaphoreBlob, leaseId); - return true; - } - else - { - Regex versionFileRegex = new Regex(@"(?[\w\d]{40})"); - - // Delete old version files - AzurePublisherTool.ListBlobs(targetFolder) - .Where(s => versionFileRegex.IsMatch(s)) - .ToList() - .ForEach(f => AzurePublisherTool.TryDeleteBlob(f)); - - // Drop the version file signaling such for any race-condition builds (see above comment). - AzurePublisherTool.DropLatestSpecifiedVersion(targetVersionFile); - } - - try - { - CopyBlobs(targetFolder); - - string cliVersion = GetVersionFileContent(CommitHash, NugetVersion); - AzurePublisherTool.PublishStringToBlob($"{targetFolder}/latest.version", cliVersion); - } - finally - { - AzurePublisherTool.ReleaseLeaseOnBlob(semaphoreBlob, leaseId); - } - - return true; - } - - private void CopyBlobs(string destinationFolder) - { - Log.LogMessage("Copying blobs to {0}/{1}", ContainerName, destinationFolder); - - foreach (string blob in AzurePublisherTool.ListBlobs(AzurePublisher.Product.Sdk, NugetVersion)) - { - string targetName = Path.GetFileName(blob) - .Replace(NugetVersion, "latest"); - - string target = $"{destinationFolder}/{targetName}"; - - AzurePublisherTool.CopyBlob(blob, target); - } - } - - private string GetVersionFileContent(string commitHash, string version) - { - return $@"{commitHash}{Environment.NewLine}{version}{Environment.NewLine}"; - } - } -} -#endif diff --git a/src/core-sdk-tasks/core-sdk-tasks.csproj b/src/core-sdk-tasks/core-sdk-tasks.csproj index ab8bb7f0..6ccb0e80 100644 --- a/src/core-sdk-tasks/core-sdk-tasks.csproj +++ b/src/core-sdk-tasks/core-sdk-tasks.csproj @@ -12,7 +12,6 @@ - From d543408106620f92d5f678c637dc1ec2da02fbef Mon Sep 17 00:00:00 2001 From: Marc Paine Date: Fri, 7 Oct 2022 16:20:15 -0700 Subject: [PATCH 7/7] The environment has arguments includes the call to the dll itself which is causing the parser to error. --- src/dotnet-core-uninstall/Shared/Commands/ListCommandExec.cs | 2 +- src/dotnet-core-uninstall/Shared/Configs/CommandLineConfigs.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/dotnet-core-uninstall/Shared/Commands/ListCommandExec.cs b/src/dotnet-core-uninstall/Shared/Commands/ListCommandExec.cs index f2d9480c..3c1c81ef 100644 --- a/src/dotnet-core-uninstall/Shared/Commands/ListCommandExec.cs +++ b/src/dotnet-core-uninstall/Shared/Commands/ListCommandExec.cs @@ -33,7 +33,7 @@ private static void Execute( { Console.WriteLine(RuntimeInfo.RunningOnWindows ? LocalizableStrings.WindowsListCommandOutput : LocalizableStrings.MacListCommandOutput); - var listCommandParseResult = CommandLineConfigs.ListCommand.Parse(Environment.GetCommandLineArgs()); + var listCommandParseResult = CommandLineConfigs.ListCommand.Parse(Environment.GetCommandLineArgs().Skip(1).ToList()); var verbose = listCommandParseResult.CommandResult.GetVerbosityLevel().Equals(VerbosityLevel.Detailed) || listCommandParseResult.CommandResult.GetVerbosityLevel().Equals(VerbosityLevel.Diagnostic); diff --git a/src/dotnet-core-uninstall/Shared/Configs/CommandLineConfigs.cs b/src/dotnet-core-uninstall/Shared/Configs/CommandLineConfigs.cs index c6a076c0..48dd1c5d 100644 --- a/src/dotnet-core-uninstall/Shared/Configs/CommandLineConfigs.cs +++ b/src/dotnet-core-uninstall/Shared/Configs/CommandLineConfigs.cs @@ -266,7 +266,7 @@ static CommandLineConfigs() .UseDefaults() .UseHelpBuilder(context => new UninstallHelpBuilder(LocalizationResources.Instance)) .Build(); - CommandLineParseResult = UninstallCommandParser.Parse(Environment.GetCommandLineArgs()); + CommandLineParseResult = UninstallCommandParser.Parse(Environment.GetCommandLineArgs().Skip(1).ToList()); } public static Option GetUninstallMainOption(this CommandResult commandResult)