Add support for a minimum required version in extension (#637)

This commit is contained in:
Taylor Southwick 2021-06-16 15:17:40 -07:00 коммит произвёл GitHub
Родитель ef31c22633
Коммит 7b4db6c125
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
7 изменённых файлов: 71 добавлений и 28 удалений

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

@ -1,13 +1,14 @@
// 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.Reflection;
namespace Microsoft.DotNet.UpgradeAssistant.Cli
{
public static class Constants
{
public static string Version
public static string FullVersion
{
get
{
@ -15,5 +16,21 @@ namespace Microsoft.DotNet.UpgradeAssistant.Cli
return attribute?.InformationalVersion ?? "0.0.0-unspecified";
}
}
public static Version Version
{
get
{
var version = FullVersion;
var idx = version.IndexOf('-', StringComparison.Ordinal);
if (idx > 0)
{
version = version.Substring(0, idx);
}
return Version.Parse(version);
}
}
}
}

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

@ -135,7 +135,7 @@ namespace Microsoft.DotNet.UpgradeAssistant.Cli
private class UpgradeState
{
public string Build { get; init; } = Constants.Version;
public string Build { get; init; } = Constants.FullVersion;
public string? CurrentProject { get; init; }

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

@ -114,7 +114,19 @@ namespace Microsoft.DotNet.UpgradeAssistant.Cli
services.AddSingleton<IUpgradeStateManager, FileUpgradeStateFactory>();
services.AddExtensions(context.Configuration, options.Extension);
services.AddExtensions()
.AddDefaultExtensions(context.Configuration)
.AddFromEnvironmentVariables(context.Configuration)
.Configure(opts =>
{
opts.CurrentVersion = Constants.Version;
foreach (var path in options.Extension)
{
opts.ExtensionPaths.Add(path);
}
});
services.AddMsBuild();
services.AddSingleton(options);
@ -173,7 +185,7 @@ namespace Microsoft.DotNet.UpgradeAssistant.Cli
private static void ShowHeader()
{
var title = $"- Microsoft .NET Upgrade Assistant v{Constants.Version} -";
var title = $"- Microsoft .NET Upgrade Assistant v{Constants.FullVersion} -";
Console.WriteLine(new string('-', title.Length));
Console.WriteLine(title);
Console.WriteLine(new string('-', title.Length));

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

@ -67,6 +67,8 @@ namespace Microsoft.DotNet.UpgradeAssistant.Extensions
public Version? Version => GetOptions<Version>("Version");
public Version? MinUpgradeAssistantVersion => GetOptions<Version>("MinRequiredVersion");
public T? GetOptions<T>(string sectionName) => Configuration.GetSection(sectionName).Get<T>();
public void Dispose()

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

@ -26,7 +26,14 @@ namespace Microsoft.DotNet.UpgradeAssistant.Extensions
{
if (LoadExtension(path) is ExtensionInstance extension)
{
list.Add(extension);
if (extension.MinUpgradeAssistantVersion is Version minVersion && minVersion < options.Value.CurrentVersion)
{
logger.LogWarning("Could not load extension from {Path}. Requires at least v{Version} of Upgrade Assistant.", path, minVersion);
}
else
{
list.Add(extension);
}
}
else
{
@ -49,11 +56,11 @@ namespace Microsoft.DotNet.UpgradeAssistant.Extensions
{
if (instance.Version is Version version)
{
logger.LogDebug("Loaded extension: {Name} v{Version} [{Location}]", instance.Name, version, instance.Location);
logger.LogDebug("Found extension '{Name}' v{Version} [{Location}]", instance.Name, version, instance.Location);
}
else
{
logger.LogDebug("Loaded extension: {Name} [{Location}]", instance.Name, instance.Location);
logger.LogDebug("Found extension '{Name}' [{Location}]", instance.Name, instance.Location);
}
return instance;

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

@ -1,6 +1,7 @@
// 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;
namespace Microsoft.DotNet.UpgradeAssistant.Extensions
@ -8,5 +9,7 @@ namespace Microsoft.DotNet.UpgradeAssistant.Extensions
public class ExtensionOptions
{
public ICollection<string> ExtensionPaths { get; } = new List<string>();
public Version CurrentVersion { get; set; } = null!;
}
}

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

@ -22,31 +22,17 @@ namespace Microsoft.DotNet.UpgradeAssistant.Extensions
/// extensions found in specified paths.
/// </summary>
/// <param name="services">The service collection to register services into.</param>
/// <param name="currentVersion">The current app version.</param>
/// <param name="configuration">The app configuration containing a setting for extension paths and the default extension service providers. These extensions will be registered before those found with the string[] argument.</param>
/// <param name="additionalExtensionPaths">Paths to probe for additional extensions. Can be paths to ExtensionManifest.json files, directories with such files, or zip files. These extensions will be registered after those found from configuration.</param>
public static void AddExtensions(this IServiceCollection services, IConfiguration configuration, IEnumerable<string> additionalExtensionPaths)
/// <returns>A builder for options</returns>
public static OptionsBuilder<ExtensionOptions> AddExtensions(this IServiceCollection services)
{
if (services is null)
{
throw new ArgumentNullException(nameof(services));
}
if (configuration is null)
{
throw new ArgumentNullException(nameof(configuration));
}
services.AddOptions<ExtensionOptions>()
.AddDefaultExtensions(configuration)
.AddFromEnvironmentVariables(configuration)
.Configure(options =>
{
foreach (var path in additionalExtensionPaths)
{
options.ExtensionPaths.Add(path);
}
});
services.AddOptions<JsonSerializerOptions>()
.Configure(o =>
{
@ -58,6 +44,8 @@ namespace Microsoft.DotNet.UpgradeAssistant.Extensions
services.AddScoped<ExtensionManager>();
services.AddTransient<IEnumerable<ExtensionInstance>>(ctx => ctx.GetRequiredService<ExtensionManager>());
services.AddExtensionLoaders();
return services.AddOptions<ExtensionOptions>();
}
private static void AddExtensionLoaders(this IServiceCollection services)
@ -67,8 +55,14 @@ namespace Microsoft.DotNet.UpgradeAssistant.Extensions
services.AddTransient<IExtensionLoader, ZipExtensionLoader>();
}
private static OptionsBuilder<ExtensionOptions> AddDefaultExtensions(this OptionsBuilder<ExtensionOptions> builder, IConfiguration configuration)
=> builder.Configure(options =>
public static OptionsBuilder<ExtensionOptions> AddDefaultExtensions(this OptionsBuilder<ExtensionOptions> builder, IConfiguration configuration)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
return builder.Configure(options =>
{
const string ExtensionDirectory = "extensions";
const string DefaultExtensionsSection = "DefaultExtensions";
@ -82,9 +76,16 @@ namespace Microsoft.DotNet.UpgradeAssistant.Extensions
options.ExtensionPaths.Add(path);
}
});
}
private static OptionsBuilder<ExtensionOptions> AddFromEnvironmentVariables(this OptionsBuilder<ExtensionOptions> builder, IConfiguration configuration)
=> builder.Configure(options =>
public static OptionsBuilder<ExtensionOptions> AddFromEnvironmentVariables(this OptionsBuilder<ExtensionOptions> builder, IConfiguration configuration)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
return builder.Configure(options =>
{
var extensionPathString = configuration[UpgradeAssistantExtensionPathsSettingName];
var pathsFromString = extensionPathString?.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries) ?? Enumerable.Empty<string>();
@ -94,5 +95,6 @@ namespace Microsoft.DotNet.UpgradeAssistant.Extensions
options.ExtensionPaths.Add(path);
}
});
}
}
}