Add optional command line to pass options through (#651)

Create an ad hoc extension from the current directory that will pass options through.
This commit is contained in:
Taylor Southwick 2021-06-21 13:56:02 -07:00 коммит произвёл GitHub
Родитель e9d2cc7edf
Коммит c86075b5a6
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
7 изменённых файлов: 54 добавлений и 5 удалений

Просмотреть файл

@ -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).

Просмотреть файл

@ -64,10 +64,10 @@ namespace Microsoft.DotNet.UpgradeAssistant.Cli
}
public static Task<int> RunAnalysisAsync(UpgradeOptions options, Func<IHostBuilder, IHostBuilder> configure, CancellationToken token)
=> RunCommandAsync(options, host => configure(host).ConfigureServices(services =>
{
services.AddScoped<IAppCommand, ConsoleAnalyze>();
}), token);
=> RunCommandAsync(options, host => configure(host).ConfigureServices(services =>
{
services.AddScoped<IAppCommand, ConsoleAnalyze>();
}), token);
public static Task<int> RunUpgradeAsync(UpgradeOptions options, Func<IHostBuilder, IHostBuilder> 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<FileInfo>("project") { Arity = ArgumentArity.ExactlyOne }.ExistingOnly());
command.AddOption(new Option<bool>(new[] { "--verbose", "-v" }, "Enable verbose diagnostics"));
command.AddOption(new Option<IReadOnlyCollection<string>>(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<IReadOnlyCollection<string>>(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<IReadOnlyCollection<string>>(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<UpgradeTarget>(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."));
}

Просмотреть файл

@ -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<string> EntryPoint { get; set; } = Array.Empty<string>();
public IReadOnlyCollection<string> Option { get; set; } = Array.Empty<string>();
public bool Verbose { get; set; }
public bool NonInteractive { get; set; }

Просмотреть файл

@ -24,10 +24,15 @@ namespace Microsoft.DotNet.UpgradeAssistant.Extensions
private readonly Lazy<AssemblyLoadContext>? _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<string[]>(ExtensionServiceProvidersSectionName);

Просмотреть файл

@ -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<KeyValuePair<string, string>> values)
{
var config = new ConfigurationBuilder()
.AddInMemoryCollection(values)
.Build();
return new ExtensionInstance(new PhysicalFileProvider(Environment.CurrentDirectory), Environment.CurrentDirectory, config);
}
public IEnumerator<ExtensionInstance> GetEnumerator() => _extensions.Value.GetEnumerator();
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => GetEnumerator();

Просмотреть файл

@ -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<string> ExtensionPaths { get; } = new List<string>();
public IEnumerable<KeyValuePair<string, string>> RetainedProperties { get; set; } = Enumerable.Empty<KeyValuePair<string, string>>();
public Version CurrentVersion { get; set; } = null!;
}
}

Просмотреть файл

@ -48,6 +48,24 @@ namespace Microsoft.DotNet.UpgradeAssistant.Extensions
return services.AddOptions<ExtensionOptions>();
}
public static IEnumerable<KeyValuePair<string, string>> ParseOptions(this IEnumerable<string> 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<string, string>(split[0], split[1]);
}
}
}
private static void AddExtensionLoaders(this IServiceCollection services)
{
services.AddTransient<IExtensionLoader, DirectoryExtensionLoader>();