Skip to content

Commit

Permalink
- implements plugin edit command
Browse files Browse the repository at this point in the history
  • Loading branch information
baywet committed Mar 28, 2024
1 parent 39c092a commit 74d62ea
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 1 deletion.
121 changes: 121 additions & 0 deletions src/kiota/Handlers/Plugin/EditHandler.cs
Original file line number Diff line number Diff line change
@@ -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<string> ClassOption
{
get; init;
}
public required Option<string> OutputOption
{
get; init;
}
public required Option<string> DescriptionOption
{
get; init;
}
public required Option<List<string>> IncludePatternsOption
{
get; init;
}
public required Option<List<string>> ExcludePatternsOption
{
get; init;
}
public required Option<bool> SkipGenerationOption
{
get; init;
}
public required Option<List<PluginType>> PluginTypesOption
{
get; init;
}

public override async Task<int> InvokeAsync(InvocationContext context)
{
string output = context.ParseResult.GetValueForOption(OutputOption) ?? string.Empty;
List<PluginType>? 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<string>? includePatterns = context.ParseResult.GetValueForOption(IncludePatternsOption);
List<string>? 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<KiotaBuilder>(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
}
}
}
}
12 changes: 11 additions & 1 deletion src/kiota/KiotaPluginCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 74d62ea

Please sign in to comment.