-
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add ppds plugins command group for plugin registration management #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4e8d392
feat: add ppds plugins command group for plugin registration management
joshsmithxrm 0e20e33
fix: use connection pool for plugin commands
joshsmithxrm 1f78da4
feat: add --package filter to ppds plugins list
joshsmithxrm ca58e98
feat: add full step registration fields and package hierarchy to plug…
joshsmithxrm c184147
feat: add merge support and solution option to extract command
joshsmithxrm 7890f21
docs: update PPDS.Cli changelog for v1.0.0-beta.2
joshsmithxrm 8bd8d6b
feat(plugins)!: add Description/AsyncAutoDelete, remove SecureConfigu…
joshsmithxrm ed648e0
fix: address PR review feedback for plugins command group
joshsmithxrm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,319 @@ | ||
| using System.CommandLine; | ||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using PPDS.Cli.Infrastructure; | ||
| using PPDS.Cli.Plugins.Models; | ||
| using PPDS.Cli.Plugins.Registration; | ||
| using PPDS.Dataverse.Pooling; | ||
|
|
||
| namespace PPDS.Cli.Commands.Plugins; | ||
|
|
||
| /// <summary> | ||
| /// Remove orphaned plugin registrations not in configuration. | ||
| /// </summary> | ||
| public static class CleanCommand | ||
| { | ||
| private static readonly JsonSerializerOptions JsonReadOptions = new() | ||
| { | ||
| PropertyNameCaseInsensitive = true | ||
| }; | ||
|
|
||
| private static readonly JsonSerializerOptions JsonWriteOptions = new() | ||
| { | ||
| WriteIndented = true, | ||
| DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull | ||
| }; | ||
|
|
||
| public static Command Create() | ||
| { | ||
| var configOption = new Option<FileInfo>("--config", "-c") | ||
| { | ||
| Description = "Path to registrations.json", | ||
| Required = true | ||
| }.AcceptExistingOnly(); | ||
|
|
||
| var whatIfOption = new Option<bool>("--what-if") | ||
| { | ||
| Description = "Preview deletions without applying", | ||
| DefaultValueFactory = _ => false | ||
| }; | ||
|
|
||
| var command = new Command("clean", "Remove orphaned registrations not in configuration") | ||
| { | ||
| configOption, | ||
| PluginsCommandGroup.ProfileOption, | ||
| PluginsCommandGroup.EnvironmentOption, | ||
| whatIfOption, | ||
| PluginsCommandGroup.JsonOption | ||
| }; | ||
|
|
||
| command.SetAction(async (parseResult, cancellationToken) => | ||
| { | ||
| var config = parseResult.GetValue(configOption)!; | ||
| var profile = parseResult.GetValue(PluginsCommandGroup.ProfileOption); | ||
| var environment = parseResult.GetValue(PluginsCommandGroup.EnvironmentOption); | ||
| var whatIf = parseResult.GetValue(whatIfOption); | ||
| var json = parseResult.GetValue(PluginsCommandGroup.JsonOption); | ||
|
|
||
| return await ExecuteAsync(config, profile, environment, whatIf, json, cancellationToken); | ||
| }); | ||
|
|
||
| return command; | ||
| } | ||
|
|
||
| private static async Task<int> ExecuteAsync( | ||
| FileInfo configFile, | ||
| string? profile, | ||
| string? environment, | ||
| bool whatIf, | ||
| bool json, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| try | ||
| { | ||
| // Load configuration | ||
| var configJson = await File.ReadAllTextAsync(configFile.FullName, cancellationToken); | ||
| var config = JsonSerializer.Deserialize<PluginRegistrationConfig>(configJson, JsonReadOptions); | ||
|
|
||
| if (config?.Assemblies == null || config.Assemblies.Count == 0) | ||
| { | ||
| Console.Error.WriteLine("No assemblies found in configuration file."); | ||
| return ExitCodes.Failure; | ||
| } | ||
|
|
||
| // Connect to Dataverse | ||
| await using var serviceProvider = await ProfileServiceFactory.CreateFromProfilesAsync( | ||
| profile, | ||
| environment, | ||
| verbose: false, | ||
| debug: false, | ||
| ProfileServiceFactory.DefaultDeviceCodeCallback, | ||
| cancellationToken); | ||
|
|
||
| var pool = serviceProvider.GetRequiredService<IDataverseConnectionPool>(); | ||
| await using var client = await pool.GetClientAsync(cancellationToken: cancellationToken); | ||
| var registrationService = new PluginRegistrationService(client); | ||
|
|
||
| if (!json) | ||
| { | ||
| var connectionInfo = serviceProvider.GetRequiredService<ResolvedConnectionInfo>(); | ||
| ConsoleHeader.WriteConnectedAs(connectionInfo); | ||
| Console.WriteLine(); | ||
|
|
||
| if (whatIf) | ||
| { | ||
| Console.WriteLine("[What-If Mode] No changes will be applied."); | ||
| Console.WriteLine(); | ||
| } | ||
| } | ||
|
|
||
| var results = new List<CleanResult>(); | ||
|
|
||
| foreach (var assemblyConfig in config.Assemblies) | ||
| { | ||
| cancellationToken.ThrowIfCancellationRequested(); | ||
|
|
||
| var result = await CleanAssemblyAsync( | ||
| registrationService, | ||
| assemblyConfig, | ||
| whatIf, | ||
| json, | ||
| cancellationToken); | ||
|
|
||
| results.Add(result); | ||
| } | ||
|
|
||
| if (json) | ||
| { | ||
| Console.WriteLine(JsonSerializer.Serialize(results, JsonWriteOptions)); | ||
| } | ||
| else | ||
| { | ||
| Console.WriteLine(); | ||
| var totalOrphans = results.Sum(r => r.OrphanedSteps.Count); | ||
| var totalDeleted = results.Sum(r => r.StepsDeleted); | ||
| var totalTypesDeleted = results.Sum(r => r.TypesDeleted); | ||
|
|
||
| if (totalOrphans == 0) | ||
| { | ||
| Console.WriteLine("No orphaned registrations found."); | ||
| } | ||
| else if (whatIf) | ||
| { | ||
| Console.WriteLine($"Would delete: {totalOrphans} step(s), {totalTypesDeleted} orphaned type(s)"); | ||
| } | ||
| else | ||
| { | ||
| Console.WriteLine($"Deleted: {totalDeleted} step(s), {totalTypesDeleted} orphaned type(s)"); | ||
| } | ||
| } | ||
|
|
||
| return ExitCodes.Success; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| Console.Error.WriteLine($"Error cleaning plugins: {ex.Message}"); | ||
| return ExitCodes.Failure; | ||
| } | ||
Check noticeCode scanning / CodeQL Generic catch clause Note
Generic catch clause.
|
||
| } | ||
|
|
||
| private static async Task<CleanResult> CleanAssemblyAsync( | ||
| PluginRegistrationService service, | ||
| PluginAssemblyConfig assemblyConfig, | ||
| bool whatIf, | ||
| bool json, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| var result = new CleanResult | ||
| { | ||
| AssemblyName = assemblyConfig.Name | ||
| }; | ||
|
|
||
| // Check if assembly exists | ||
| var assembly = await service.GetAssemblyByNameAsync(assemblyConfig.Name); | ||
| if (assembly == null) | ||
| { | ||
| if (!json) | ||
| Console.WriteLine($"Assembly not found: {assemblyConfig.Name}"); | ||
| return result; | ||
| } | ||
|
|
||
| if (!json) | ||
| Console.WriteLine($"Checking assembly: {assemblyConfig.Name}"); | ||
|
|
||
| // Build set of configured step names | ||
| var configuredStepNames = new HashSet<string>(); | ||
| foreach (var typeConfig in assemblyConfig.Types) | ||
| { | ||
| foreach (var stepConfig in typeConfig.Steps) | ||
| { | ||
| var stepName = stepConfig.Name ?? $"{typeConfig.TypeName}: {stepConfig.Message} of {stepConfig.Entity}"; | ||
| configuredStepNames.Add(stepName); | ||
| } | ||
| } | ||
|
|
||
| // Get existing types and steps | ||
| var existingTypes = await service.ListTypesForAssemblyAsync(assembly.Id); | ||
|
|
||
| foreach (var existingType in existingTypes) | ||
| { | ||
| cancellationToken.ThrowIfCancellationRequested(); | ||
|
|
||
| var steps = await service.ListStepsForTypeAsync(existingType.Id); | ||
|
|
||
| // Find orphaned steps and track count for type cleanup | ||
| var orphanedStepsInType = steps.Where(s => !configuredStepNames.Contains(s.Name)).ToList(); | ||
|
|
||
| foreach (var step in orphanedStepsInType) | ||
| { | ||
| result.OrphanedSteps.Add(new OrphanedStep | ||
| { | ||
| TypeName = existingType.TypeName, | ||
| StepName = step.Name, | ||
| StepId = step.Id | ||
| }); | ||
|
|
||
| if (whatIf) | ||
| { | ||
| if (!json) | ||
| Console.WriteLine($" [What-If] Would delete step: {step.Name}"); | ||
| } | ||
| else | ||
| { | ||
| await service.DeleteStepAsync(step.Id); | ||
| result.StepsDeleted++; | ||
| if (!json) | ||
| Console.WriteLine($" Deleted step: {step.Name}"); | ||
| } | ||
| } | ||
|
|
||
| // Check if type is orphaned (no configured steps and not in allTypeNames) | ||
| var typeHasConfiguredSteps = assemblyConfig.Types | ||
| .Any(t => t.TypeName == existingType.TypeName && t.Steps.Count > 0); | ||
|
|
||
| var typeInAllTypeNames = assemblyConfig.AllTypeNames.Contains(existingType.TypeName); | ||
|
|
||
| if (!typeHasConfiguredSteps && !typeInAllTypeNames) | ||
| { | ||
| // Calculate remaining steps in memory instead of re-querying | ||
| var remainingStepsCount = steps.Count - orphanedStepsInType.Count; | ||
|
|
||
| if (remainingStepsCount == 0) | ||
| { | ||
| result.OrphanedTypes.Add(new OrphanedType | ||
| { | ||
| TypeName = existingType.TypeName, | ||
| TypeId = existingType.Id | ||
| }); | ||
|
|
||
| if (whatIf) | ||
| { | ||
| if (!json) | ||
| Console.WriteLine($" [What-If] Would delete orphaned type: {existingType.TypeName}"); | ||
| } | ||
| else | ||
| { | ||
| try | ||
| { | ||
| await service.DeletePluginTypeAsync(existingType.Id); | ||
| result.TypesDeleted++; | ||
| if (!json) | ||
| Console.WriteLine($" Deleted orphaned type: {existingType.TypeName}"); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| if (!json) | ||
| Console.WriteLine($" Warning: Could not delete type {existingType.TypeName}: {ex.Message}"); | ||
| } | ||
Check noticeCode scanning / CodeQL Generic catch clause Note
Generic catch clause.
|
||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| #region Result Models | ||
|
|
||
| private sealed class CleanResult | ||
| { | ||
| [JsonPropertyName("assemblyName")] | ||
| public string AssemblyName { get; set; } = string.Empty; | ||
|
|
||
| [JsonPropertyName("orphanedSteps")] | ||
| public List<OrphanedStep> OrphanedSteps { get; set; } = []; | ||
|
|
||
| [JsonPropertyName("orphanedTypes")] | ||
| public List<OrphanedType> OrphanedTypes { get; set; } = []; | ||
|
|
||
| [JsonPropertyName("stepsDeleted")] | ||
| public int StepsDeleted { get; set; } | ||
|
|
||
| [JsonPropertyName("typesDeleted")] | ||
| public int TypesDeleted { get; set; } | ||
| } | ||
|
|
||
| private sealed class OrphanedStep | ||
| { | ||
| [JsonPropertyName("typeName")] | ||
| public string TypeName { get; set; } = string.Empty; | ||
|
|
||
| [JsonPropertyName("stepName")] | ||
| public string StepName { get; set; } = string.Empty; | ||
|
|
||
| [JsonPropertyName("stepId")] | ||
| public Guid StepId { get; set; } | ||
| } | ||
|
|
||
| private sealed class OrphanedType | ||
| { | ||
| [JsonPropertyName("typeName")] | ||
| public string TypeName { get; set; } = string.Empty; | ||
|
|
||
| [JsonPropertyName("typeId")] | ||
| public Guid TypeId { get; set; } | ||
| } | ||
|
|
||
| #endregion | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.