From 74d62eae7c28b29991523dd563ddf527e35a1487 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Thu, 28 Mar 2024 08:28:06 -0400 Subject: [PATCH] - implements plugin edit command --- src/kiota/Handlers/Plugin/EditHandler.cs | 121 +++++++++++++++++++++++ src/kiota/KiotaPluginCommands.cs | 12 ++- 2 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 src/kiota/Handlers/Plugin/EditHandler.cs diff --git a/src/kiota/Handlers/Plugin/EditHandler.cs b/src/kiota/Handlers/Plugin/EditHandler.cs new file mode 100644 index 0000000000..ef78b19c29 --- /dev/null +++ b/src/kiota/Handlers/Plugin/EditHandler.cs @@ -0,0 +1,121 @@ +using System.CommandLine; +using System.CommandLine.Invocation; +using System.Text.Json; +using Kiota.Builder; +using Kiota.Builder.Configuration; +using Kiota.Builder.Extensions; +using Kiota.Builder.WorkspaceManagement; +using Microsoft.Extensions.Logging; + +namespace kiota.Handlers.Plugin; + +internal class EditHandler : BaseKiotaCommandHandler +{ + public required Option ClassOption + { + get; init; + } + public required Option OutputOption + { + get; init; + } + public required Option DescriptionOption + { + get; init; + } + public required Option> IncludePatternsOption + { + get; init; + } + public required Option> ExcludePatternsOption + { + get; init; + } + public required Option SkipGenerationOption + { + get; init; + } + public required Option> PluginTypesOption + { + get; init; + } + + public override async Task InvokeAsync(InvocationContext context) + { + string output = context.ParseResult.GetValueForOption(OutputOption) ?? string.Empty; + List? pluginTypes = context.ParseResult.GetValueForOption(PluginTypesOption); + string openapi = context.ParseResult.GetValueForOption(DescriptionOption) ?? string.Empty; + bool skipGeneration = context.ParseResult.GetValueForOption(SkipGenerationOption); + string className = context.ParseResult.GetValueForOption(ClassOption) ?? string.Empty; + List? includePatterns = context.ParseResult.GetValueForOption(IncludePatternsOption); + List? excludePatterns = context.ParseResult.GetValueForOption(ExcludePatternsOption); + CancellationToken cancellationToken = context.BindingContext.GetService(typeof(CancellationToken)) is CancellationToken token ? token : CancellationToken.None; + + Configuration.Generation.SkipGeneration = skipGeneration; + Configuration.Generation.Operation = ConsumerOperation.Edit; + + var (loggerFactory, logger) = GetLoggerAndFactory(context, Configuration.Generation.OutputPath); + using (loggerFactory) + { + await CheckForNewVersionAsync(logger, cancellationToken).ConfigureAwait(false); + logger.AppendInternalTracing(); + logger.LogTrace("configuration: {configuration}", JsonSerializer.Serialize(Configuration, KiotaConfigurationJsonContext.Default.KiotaConfiguration)); + + try + { + var workspaceStorageService = new WorkspaceConfigurationStorageService(Directory.GetCurrentDirectory()); + var (config, _) = await workspaceStorageService.GetWorkspaceConfigurationAsync(cancellationToken).ConfigureAwait(false); + if (config == null) + { + DisplayError("The workspace configuration is missing, please run the init command first."); + return 1; + } + if (!config.Plugins.TryGetValue(className, out var pluginConfiguration)) + { + DisplayError($"No plugin found with the provided name {className}"); + return 1; + } + pluginConfiguration.UpdateGenerationConfigurationFromApiPluginConfiguration(Configuration.Generation, className); + AssignIfNotNullOrEmpty(output, (c, s) => c.OutputPath = s); + AssignIfNotNullOrEmpty(openapi, (c, s) => c.OpenAPIFilePath = s); + AssignIfNotNullOrEmpty(className, (c, s) => c.ClientClassName = s); + if (includePatterns is { Count: > 0 }) + Configuration.Generation.IncludePatterns = includePatterns.Select(static x => x.TrimQuotes()).ToHashSet(StringComparer.OrdinalIgnoreCase); + if (excludePatterns is { Count: > 0 }) + Configuration.Generation.ExcludePatterns = excludePatterns.Select(static x => x.TrimQuotes()).ToHashSet(StringComparer.OrdinalIgnoreCase); + if (pluginTypes is { Count: > 0 }) + Configuration.Generation.PluginTypes = pluginTypes.ToHashSet(); + + DefaultSerializersAndDeserializers(Configuration.Generation); + var builder = new KiotaBuilder(logger, Configuration.Generation, httpClient, true); + var result = await builder.GeneratePluginAsync(cancellationToken).ConfigureAwait(false); + if (result) + DisplaySuccess("Generation completed successfully"); + else if (skipGeneration) + { + DisplaySuccess("Generation skipped as --skip-generation was passed"); + DisplayGenerateCommandHint(); + } + else + { + DisplayWarning("Generation skipped as no changes were detected"); + DisplayCleanHint("plugin generate", "--refresh"); + } + var manifestPath = $"{GetAbsolutePath(Path.Combine(WorkspaceConfigurationStorageService.KiotaDirectorySegment, WorkspaceConfigurationStorageService.ManifestFileName))}#{Configuration.Generation.ClientClassName}"; + DisplayInfoHint(Configuration.Generation.Language, string.Empty, manifestPath); + DisplayGenerateAdvancedHint(includePatterns ?? [], excludePatterns ?? [], string.Empty, manifestPath, "plugin edit"); + return 0; + } + catch (Exception ex) + { +#if DEBUG + logger.LogCritical(ex, "error adding the plugin: {exceptionMessage}", ex.Message); + throw; // so debug tools go straight to the source of the exception when attached +#else + logger.LogCritical("error adding the plugin: {exceptionMessage}", ex.Message); + return 1; +#endif + } + } + } +} diff --git a/src/kiota/KiotaPluginCommands.cs b/src/kiota/KiotaPluginCommands.cs index a8124129ba..2b15c34af9 100644 --- a/src/kiota/KiotaPluginCommands.cs +++ b/src/kiota/KiotaPluginCommands.cs @@ -91,7 +91,17 @@ public static Command GetEditCommand() pluginTypes, //TODO overlay when we have support for it in OAI.net }; - //TODO map handler + command.Handler = new EditHandler + { + ClassOption = pluginNameOption, + OutputOption = outputOption, + PluginTypesOption = pluginTypes, + DescriptionOption = descriptionOption, + IncludePatternsOption = includePatterns, + ExcludePatternsOption = excludePatterns, + SkipGenerationOption = skipGenerationOption, + LogLevelOption = logLevelOption, + }; return command; } public static Command GetRemoveCommand()