From c86075b5a6d08b6d1be1143c85bc6796309a8f39 Mon Sep 17 00:00:00 2001 From: Taylor Southwick Date: Mon, 21 Jun 2021 13:56:02 -0700 Subject: [PATCH] Add optional command line to pass options through (#651) Create an ad hoc extension from the current directory that will pass options through. --- CHANGELOG.md | 1 + .../Program.cs | 10 ++++++---- .../UpgradeOptions.cs | 2 ++ .../ExtensionInstance.cs | 7 ++++++- .../ExtensionManager.cs | 18 ++++++++++++++++++ .../ExtensionOptions.cs | 3 +++ .../ExtensionProviderExtensions.cs | 18 ++++++++++++++++++ 7 files changed, 54 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ceeee0c..334b4f28 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). ### Added - Usage telemetry has been added to help guide product development. See [https://aka.ms/upgrade-assistant-telemetry](https://aka.ms/upgrade-assistant-telemetry) for details [#644](https://github.com/dotnet/upgrade-assistant/pull/644). +- Command line option to pass options through in the form of `--option KEY=Value` [#651](https://github.com/dotnet/upgrade-assistant/pull/651) ### Fixed - Updated `HttpContext.Current` analyzer to more correctly identify uses of `HttpContext.Current` that need replaced [#628](https://github.com/dotnet/upgrade-assistant/pull/628). diff --git a/src/cli/Microsoft.DotNet.UpgradeAssistant.Cli/Program.cs b/src/cli/Microsoft.DotNet.UpgradeAssistant.Cli/Program.cs index e7205003..289216ca 100644 --- a/src/cli/Microsoft.DotNet.UpgradeAssistant.Cli/Program.cs +++ b/src/cli/Microsoft.DotNet.UpgradeAssistant.Cli/Program.cs @@ -64,10 +64,10 @@ namespace Microsoft.DotNet.UpgradeAssistant.Cli } public static Task RunAnalysisAsync(UpgradeOptions options, Func configure, CancellationToken token) - => RunCommandAsync(options, host => configure(host).ConfigureServices(services => - { - services.AddScoped(); - }), token); + => RunCommandAsync(options, host => configure(host).ConfigureServices(services => + { + services.AddScoped(); + }), token); public static Task RunUpgradeAsync(UpgradeOptions options, Func configure, CancellationToken token) => RunCommandAsync(options, host => configure(host).ConfigureServices(services => @@ -131,6 +131,7 @@ namespace Microsoft.DotNet.UpgradeAssistant.Cli .AddFromEnvironmentVariables(context.Configuration) .Configure(opts => { + opts.RetainedProperties = options.Option.ParseOptions(); opts.CurrentVersion = UpgradeVersion.Current.Version; foreach (var path in options.Extension) @@ -243,6 +244,7 @@ namespace Microsoft.DotNet.UpgradeAssistant.Cli command.AddArgument(new Argument("project") { Arity = ArgumentArity.ExactlyOne }.ExistingOnly()); command.AddOption(new Option(new[] { "--verbose", "-v" }, "Enable verbose diagnostics")); command.AddOption(new Option>(new[] { "--extension" }, "Specifies a .NET Upgrade Assistant extension package to include. This could be an ExtensionManifest.json file, a directory containing an ExtensionManifest.json file, or a zip archive containing an extension. This option can be specified multiple times.")); + command.AddOption(new Option>(new[] { "--option" }, "Specifies a .NET Upgrade Assistant extension package to include. This could be an ExtensionManifest.json file, a directory containing an ExtensionManifest.json file, or a zip archive containing an extension. This option can be specified multiple times.")); command.AddOption(new Option>(new[] { "--entry-point", "-e" }, "Provides the entry-point project to start the upgrade process. This may include globbing patterns such as '*' for match.")); command.AddOption(new Option(new[] { "--target-tfm-support" }, "Select if you would like the Long Term Support (LTS), Current, or Preview TFM. See https://dotnet.microsoft.com/platform/support/policy/dotnet-core for details for what these mean.")); } diff --git a/src/common/Microsoft.DotNet.UpgradeAssistant.Abstractions/UpgradeOptions.cs b/src/common/Microsoft.DotNet.UpgradeAssistant.Abstractions/UpgradeOptions.cs index b4eff19e..a762c846 100644 --- a/src/common/Microsoft.DotNet.UpgradeAssistant.Abstractions/UpgradeOptions.cs +++ b/src/common/Microsoft.DotNet.UpgradeAssistant.Abstractions/UpgradeOptions.cs @@ -21,6 +21,8 @@ namespace Microsoft.DotNet.UpgradeAssistant // Name must be EntryPoint and not plural as the name of the argument that it binds to is `--entry-point` public IReadOnlyCollection EntryPoint { get; set; } = Array.Empty(); + public IReadOnlyCollection Option { get; set; } = Array.Empty(); + public bool Verbose { get; set; } public bool NonInteractive { get; set; } diff --git a/src/components/Microsoft.DotNet.UpgradeAssistant.Extensions/ExtensionInstance.cs b/src/components/Microsoft.DotNet.UpgradeAssistant.Extensions/ExtensionInstance.cs index 579dbb6d..4e296909 100644 --- a/src/components/Microsoft.DotNet.UpgradeAssistant.Extensions/ExtensionInstance.cs +++ b/src/components/Microsoft.DotNet.UpgradeAssistant.Extensions/ExtensionInstance.cs @@ -24,10 +24,15 @@ namespace Microsoft.DotNet.UpgradeAssistant.Extensions private readonly Lazy? _alc; public ExtensionInstance(IFileProvider fileProvider, string location) + : this(fileProvider, location, CreateConfiguration(fileProvider)) + { + } + + public ExtensionInstance(IFileProvider fileProvider, string location, IConfiguration configuration) { FileProvider = fileProvider; Location = location; - Configuration = CreateConfiguration(fileProvider); + Configuration = configuration; Name = GetName(Configuration, location); var serviceProviders = GetOptions(ExtensionServiceProvidersSectionName); diff --git a/src/components/Microsoft.DotNet.UpgradeAssistant.Extensions/ExtensionManager.cs b/src/components/Microsoft.DotNet.UpgradeAssistant.Extensions/ExtensionManager.cs index bdb828a4..c28a1957 100644 --- a/src/components/Microsoft.DotNet.UpgradeAssistant.Extensions/ExtensionManager.cs +++ b/src/components/Microsoft.DotNet.UpgradeAssistant.Extensions/ExtensionManager.cs @@ -3,6 +3,9 @@ using System; using System.Collections.Generic; +using System.Linq; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; @@ -43,6 +46,11 @@ namespace Microsoft.DotNet.UpgradeAssistant.Extensions logger.LogInformation("Loaded {Count} extensions", list.Count); + if (options.Value.RetainedProperties.Any()) + { + list.Add(LoadOptionsExtension(options.Value.RetainedProperties)); + } + return list; }); @@ -87,6 +95,16 @@ namespace Microsoft.DotNet.UpgradeAssistant.Extensions } } + [System.Diagnostics.CodeAnalysis.SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope", Justification = "The extension instance will dispose of this.")] + private static ExtensionInstance LoadOptionsExtension(IEnumerable> values) + { + var config = new ConfigurationBuilder() + .AddInMemoryCollection(values) + .Build(); + + return new ExtensionInstance(new PhysicalFileProvider(Environment.CurrentDirectory), Environment.CurrentDirectory, config); + } + public IEnumerator GetEnumerator() => _extensions.Value.GetEnumerator(); System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); diff --git a/src/components/Microsoft.DotNet.UpgradeAssistant.Extensions/ExtensionOptions.cs b/src/components/Microsoft.DotNet.UpgradeAssistant.Extensions/ExtensionOptions.cs index fe54da8e..517fa40e 100644 --- a/src/components/Microsoft.DotNet.UpgradeAssistant.Extensions/ExtensionOptions.cs +++ b/src/components/Microsoft.DotNet.UpgradeAssistant.Extensions/ExtensionOptions.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using System.Linq; namespace Microsoft.DotNet.UpgradeAssistant.Extensions { @@ -10,6 +11,8 @@ namespace Microsoft.DotNet.UpgradeAssistant.Extensions { public ICollection ExtensionPaths { get; } = new List(); + public IEnumerable> RetainedProperties { get; set; } = Enumerable.Empty>(); + public Version CurrentVersion { get; set; } = null!; } } diff --git a/src/components/Microsoft.DotNet.UpgradeAssistant.Extensions/ExtensionProviderExtensions.cs b/src/components/Microsoft.DotNet.UpgradeAssistant.Extensions/ExtensionProviderExtensions.cs index 8c31363f..5d2ba5ec 100644 --- a/src/components/Microsoft.DotNet.UpgradeAssistant.Extensions/ExtensionProviderExtensions.cs +++ b/src/components/Microsoft.DotNet.UpgradeAssistant.Extensions/ExtensionProviderExtensions.cs @@ -48,6 +48,24 @@ namespace Microsoft.DotNet.UpgradeAssistant.Extensions return services.AddOptions(); } + public static IEnumerable> ParseOptions(this IEnumerable options) + { + if (options is null) + { + throw new ArgumentNullException(nameof(options)); + } + + foreach (var option in options) + { + var split = option.Split('='); + + if (split.Length == 2) + { + yield return new KeyValuePair(split[0], split[1]); + } + } + } + private static void AddExtensionLoaders(this IServiceCollection services) { services.AddTransient();