-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
132 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters