|
| 1 | +using DotMake.CommandLine; |
| 2 | + |
| 3 | +namespace TALXIS.CLI.Component; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Base class for component commands that provides common functionality for create, delete, list, and explain operations. |
| 7 | +/// </summary> |
| 8 | +public abstract class BaseComponentCommand |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Gets the name of the component type (e.g., "app", "entity", "form"). |
| 12 | + /// </summary> |
| 13 | + protected abstract string ComponentType { get; } |
| 14 | + |
| 15 | + /// <summary> |
| 16 | + /// Gets a description of what this component represents. |
| 17 | + /// </summary> |
| 18 | + protected abstract string ComponentDescription { get; } |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Creates a new component instance. |
| 22 | + /// </summary> |
| 23 | + /// <param name="name">The name of the component to create.</param> |
| 24 | + /// <param name="options">Additional options for component creation.</param> |
| 25 | + public virtual async Task CreateAsync(string name, ComponentOptions? options = null) |
| 26 | + { |
| 27 | + Console.WriteLine($"Creating {ComponentType}: {name}"); |
| 28 | + |
| 29 | + if (options?.Verbose == true) |
| 30 | + { |
| 31 | + Console.WriteLine($"Component type: {ComponentType}"); |
| 32 | + Console.WriteLine($"Description: {ComponentDescription}"); |
| 33 | + } |
| 34 | + |
| 35 | + await PerformCreateAsync(name, options); |
| 36 | + |
| 37 | + Console.WriteLine($"✅ Successfully created {ComponentType}: {name}"); |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Deletes an existing component instance. |
| 42 | + /// </summary> |
| 43 | + /// <param name="name">The name of the component to delete.</param> |
| 44 | + /// <param name="options">Additional options for component deletion.</param> |
| 45 | + public virtual async Task DeleteAsync(string name, ComponentOptions? options = null) |
| 46 | + { |
| 47 | + Console.WriteLine($"Deleting {ComponentType}: {name}"); |
| 48 | + |
| 49 | + if (options?.Force != true) |
| 50 | + { |
| 51 | + Console.Write($"Are you sure you want to delete {ComponentType} '{name}'? (y/N): "); |
| 52 | + var confirmation = Console.ReadLine(); |
| 53 | + if (!string.Equals(confirmation?.Trim(), "y", StringComparison.OrdinalIgnoreCase)) |
| 54 | + { |
| 55 | + Console.WriteLine("Operation cancelled."); |
| 56 | + return; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + await PerformDeleteAsync(name, options); |
| 61 | + |
| 62 | + Console.WriteLine($"✅ Successfully deleted {ComponentType}: {name}"); |
| 63 | + } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Lists all existing component instances. |
| 67 | + /// </summary> |
| 68 | + /// <param name="options">Additional options for listing components.</param> |
| 69 | + public virtual async Task ListAsync(ComponentOptions? options = null) |
| 70 | + { |
| 71 | + Console.WriteLine($"Listing all {ComponentType} components:"); |
| 72 | + |
| 73 | + var components = await GetComponentListAsync(options); |
| 74 | + |
| 75 | + if (!components.Any()) |
| 76 | + { |
| 77 | + Console.WriteLine($"No {ComponentType} components found."); |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + foreach (var component in components) |
| 82 | + { |
| 83 | + if (options?.Verbose == true) |
| 84 | + { |
| 85 | + Console.WriteLine($" 📦 {component.Name} - {component.Description}"); |
| 86 | + if (!string.IsNullOrEmpty(component.Path)) |
| 87 | + { |
| 88 | + Console.WriteLine($" 📁 Path: {component.Path}"); |
| 89 | + } |
| 90 | + if (component.LastModified.HasValue) |
| 91 | + { |
| 92 | + Console.WriteLine($" 📅 Modified: {component.LastModified:yyyy-MM-dd HH:mm:ss}"); |
| 93 | + } |
| 94 | + } |
| 95 | + else |
| 96 | + { |
| 97 | + Console.WriteLine($" 📦 {component.Name}"); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + Console.WriteLine($"\nTotal: {components.Count()} {ComponentType} component(s)"); |
| 102 | + } |
| 103 | + |
| 104 | + /// <summary> |
| 105 | + /// Explains what this component type is and how to use it. |
| 106 | + /// </summary> |
| 107 | + /// <param name="options">Additional options for explanation.</param> |
| 108 | + public virtual Task ExplainAsync(ComponentOptions? options = null) |
| 109 | + { |
| 110 | + Console.WriteLine($"📋 {ComponentType.ToUpperInvariant()} Component"); |
| 111 | + Console.WriteLine($"Description: {ComponentDescription}"); |
| 112 | + Console.WriteLine(); |
| 113 | + Console.WriteLine("Available operations:"); |
| 114 | + Console.WriteLine($" • create - Create a new {ComponentType} component"); |
| 115 | + Console.WriteLine($" • delete - Delete an existing {ComponentType} component"); |
| 116 | + Console.WriteLine($" • list - List all {ComponentType} components"); |
| 117 | + Console.WriteLine($" • explain - Show this explanation"); |
| 118 | + Console.WriteLine(); |
| 119 | + |
| 120 | + ProvideAdditionalExplanation(); |
| 121 | + |
| 122 | + return Task.CompletedTask; |
| 123 | + } |
| 124 | + |
| 125 | + /// <summary> |
| 126 | + /// Performs the actual component creation logic. Override in derived classes. |
| 127 | + /// </summary> |
| 128 | + /// <param name="name">The name of the component to create.</param> |
| 129 | + /// <param name="options">Additional options for component creation.</param> |
| 130 | + protected abstract Task PerformCreateAsync(string name, ComponentOptions? options); |
| 131 | + |
| 132 | + /// <summary> |
| 133 | + /// Performs the actual component deletion logic. Override in derived classes. |
| 134 | + /// </summary> |
| 135 | + /// <param name="name">The name of the component to delete.</param> |
| 136 | + /// <param name="options">Additional options for component deletion.</param> |
| 137 | + protected abstract Task PerformDeleteAsync(string name, ComponentOptions? options); |
| 138 | + |
| 139 | + /// <summary> |
| 140 | + /// Gets the list of existing components. Override in derived classes. |
| 141 | + /// </summary> |
| 142 | + /// <param name="options">Additional options for listing components.</param> |
| 143 | + protected abstract Task<IEnumerable<ComponentInfo>> GetComponentListAsync(ComponentOptions? options); |
| 144 | + |
| 145 | + /// <summary> |
| 146 | + /// Provides additional component-specific explanation. Override in derived classes if needed. |
| 147 | + /// </summary> |
| 148 | + protected virtual void ProvideAdditionalExplanation() |
| 149 | + { |
| 150 | + // Default implementation - can be overridden |
| 151 | + } |
| 152 | +} |
0 commit comments