|
| 1 | +using System.ComponentModel; |
| 2 | +using System.Diagnostics.CodeAnalysis; |
| 3 | +using FSH.CLI.Models; |
| 4 | +using Spectre.Console; |
| 5 | +using Spectre.Console.Cli; |
| 6 | + |
| 7 | +namespace FSH.CLI.Commands; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Check for and apply FSH framework upgrades. |
| 11 | +/// </summary> |
| 12 | +[SuppressMessage("Performance", "CA1812:Avoid uninstantiated internal classes", Justification = "Instantiated by Spectre.Console.Cli via reflection")] |
| 13 | +internal sealed class UpgradeCommand : AsyncCommand<UpgradeCommand.Settings> |
| 14 | +{ |
| 15 | + [SuppressMessage("Performance", "CA1812:Avoid uninstantiated internal classes", Justification = "Instantiated by Spectre.Console.Cli via reflection")] |
| 16 | + internal sealed class Settings : CommandSettings |
| 17 | + { |
| 18 | + [CommandOption("-p|--path")] |
| 19 | + [Description("Path to the FSH project (defaults to current directory)")] |
| 20 | + [DefaultValue(".")] |
| 21 | + public string Path { get; set; } = "."; |
| 22 | + |
| 23 | + [CommandOption("--check")] |
| 24 | + [Description("Check for available upgrades without applying")] |
| 25 | + [DefaultValue(false)] |
| 26 | + public bool CheckOnly { get; set; } |
| 27 | + |
| 28 | + [CommandOption("--apply")] |
| 29 | + [Description("Apply available upgrades")] |
| 30 | + [DefaultValue(false)] |
| 31 | + public bool Apply { get; set; } |
| 32 | + |
| 33 | + [CommandOption("--skip-breaking")] |
| 34 | + [Description("Skip breaking changes during upgrade")] |
| 35 | + [DefaultValue(false)] |
| 36 | + public bool SkipBreaking { get; set; } |
| 37 | + |
| 38 | + [CommandOption("--force")] |
| 39 | + [Description("Force upgrade even if customizations detected")] |
| 40 | + [DefaultValue(false)] |
| 41 | + public bool Force { get; set; } |
| 42 | + |
| 43 | + [CommandOption("--dry-run")] |
| 44 | + [Description("Show what would be changed without making modifications")] |
| 45 | + [DefaultValue(false)] |
| 46 | + public bool DryRun { get; set; } |
| 47 | + } |
| 48 | + |
| 49 | + public override async Task<int> ExecuteAsync(CommandContext context, Settings settings, CancellationToken cancellationToken) |
| 50 | + { |
| 51 | + // Validate project has manifest |
| 52 | + var manifest = FshManifest.TryLoad(settings.Path); |
| 53 | + if (manifest == null) |
| 54 | + { |
| 55 | + AnsiConsole.MarkupLine("[red]Error:[/] No FSH project found at this location."); |
| 56 | + AnsiConsole.MarkupLine("[dim]This command requires a project created with FSH CLI 10.0.0 or later.[/]"); |
| 57 | + AnsiConsole.MarkupLine("[dim]The project must have a [yellow].fsh/manifest.json[/] file.[/]"); |
| 58 | + return 1; |
| 59 | + } |
| 60 | + |
| 61 | + // Show current status |
| 62 | + AnsiConsole.WriteLine(); |
| 63 | + AnsiConsole.MarkupLine($"[blue]FSH Upgrade[/]"); |
| 64 | + AnsiConsole.MarkupLine($"[dim]Project:[/] {Path.GetFullPath(settings.Path)}"); |
| 65 | + AnsiConsole.MarkupLine($"[dim]Current version:[/] [yellow]{manifest.FshVersion}[/]"); |
| 66 | + AnsiConsole.WriteLine(); |
| 67 | + |
| 68 | + // Determine mode |
| 69 | + if (!settings.CheckOnly && !settings.Apply) |
| 70 | + { |
| 71 | + // Default: show help |
| 72 | + ShowUsageHelp(); |
| 73 | + return 0; |
| 74 | + } |
| 75 | + |
| 76 | + if (settings.CheckOnly) |
| 77 | + { |
| 78 | + return await CheckForUpgradesAsync(manifest, settings, cancellationToken); |
| 79 | + } |
| 80 | + |
| 81 | + if (settings.Apply) |
| 82 | + { |
| 83 | + return await ApplyUpgradesAsync(manifest, settings, cancellationToken); |
| 84 | + } |
| 85 | + |
| 86 | + return 0; |
| 87 | + } |
| 88 | + |
| 89 | + private static void ShowUsageHelp() |
| 90 | + { |
| 91 | + AnsiConsole.MarkupLine("[yellow]Usage:[/]"); |
| 92 | + AnsiConsole.MarkupLine(" [green]fsh upgrade --check[/] Check for available upgrades"); |
| 93 | + AnsiConsole.MarkupLine(" [green]fsh upgrade --apply[/] Apply available upgrades"); |
| 94 | + AnsiConsole.MarkupLine(" [green]fsh upgrade --apply --skip-breaking[/] Apply safe updates only"); |
| 95 | + AnsiConsole.MarkupLine(" [green]fsh upgrade --apply --dry-run[/] Preview changes without applying"); |
| 96 | + AnsiConsole.WriteLine(); |
| 97 | + } |
| 98 | + |
| 99 | + private static Task<int> CheckForUpgradesAsync(FshManifest manifest, Settings settings, CancellationToken cancellationToken) |
| 100 | + { |
| 101 | + // TODO: Sprint 2 - Implement upgrade check |
| 102 | + // 1. Fetch latest release info from GitHub API |
| 103 | + // 2. Compare versions |
| 104 | + // 3. Show available changes |
| 105 | + |
| 106 | + AnsiConsole.MarkupLine("[yellow]⚠ Upgrade check not yet implemented[/]"); |
| 107 | + AnsiConsole.WriteLine(); |
| 108 | + AnsiConsole.MarkupLine("[dim]Coming in Sprint 2:[/]"); |
| 109 | + AnsiConsole.MarkupLine("[dim] • GitHub API integration for release fetching[/]"); |
| 110 | + AnsiConsole.MarkupLine("[dim] • Version comparison logic[/]"); |
| 111 | + AnsiConsole.MarkupLine("[dim] • Package diff detection[/]"); |
| 112 | + AnsiConsole.WriteLine(); |
| 113 | + |
| 114 | + // Placeholder output showing what it will look like |
| 115 | + AnsiConsole.MarkupLine("[blue]Preview of planned output:[/]"); |
| 116 | + AnsiConsole.WriteLine(); |
| 117 | + |
| 118 | + var panel = new Panel( |
| 119 | + """ |
| 120 | + [green]FSH Upgrade Check[/] |
| 121 | + |
| 122 | + Current: [yellow]10.0.0[/] |
| 123 | + Latest: [green]10.1.0[/] |
| 124 | + |
| 125 | + [blue]Changes available:[/] |
| 126 | + |
| 127 | + BuildingBlocks/Web: |
| 128 | + [green]+[/] Added RateLimitingMiddleware |
| 129 | + [yellow]~[/] Modified ExceptionHandler (non-breaking) |
| 130 | + |
| 131 | + Modules/Identity: |
| 132 | + [green]+[/] Added MFA support |
| 133 | + [red]![/] Breaking: IUserService signature changed |
| 134 | + |
| 135 | + Directory.Packages.props: |
| 136 | + [yellow]~[/] 12 package updates |
| 137 | + |
| 138 | + Run '[green]fsh upgrade --apply[/]' to upgrade. |
| 139 | + Run '[green]fsh upgrade --apply --skip-breaking[/]' for safe updates only. |
| 140 | + """) |
| 141 | + { |
| 142 | + Border = BoxBorder.Rounded, |
| 143 | + Padding = new Padding(2, 1) |
| 144 | + }; |
| 145 | + |
| 146 | + AnsiConsole.Write(panel); |
| 147 | + AnsiConsole.WriteLine(); |
| 148 | + |
| 149 | + return Task.FromResult(0); |
| 150 | + } |
| 151 | + |
| 152 | + private static Task<int> ApplyUpgradesAsync(FshManifest manifest, Settings settings, CancellationToken cancellationToken) |
| 153 | + { |
| 154 | + // TODO: Sprint 3 - Implement upgrade apply |
| 155 | + // 1. Fetch latest release |
| 156 | + // 2. Update Directory.Packages.props |
| 157 | + // 3. For code changes, show diff and ask confirmation |
| 158 | + // 4. Update manifest with new versions |
| 159 | + |
| 160 | + AnsiConsole.MarkupLine("[yellow]⚠ Upgrade apply not yet implemented[/]"); |
| 161 | + AnsiConsole.WriteLine(); |
| 162 | + AnsiConsole.MarkupLine("[dim]Coming in Sprint 3:[/]"); |
| 163 | + AnsiConsole.MarkupLine("[dim] • Package version updater[/]"); |
| 164 | + AnsiConsole.MarkupLine("[dim] • Safe (non-breaking) auto-apply[/]"); |
| 165 | + AnsiConsole.MarkupLine("[dim] • Interactive diff viewer[/]"); |
| 166 | + AnsiConsole.WriteLine(); |
| 167 | + |
| 168 | + if (settings.DryRun) |
| 169 | + { |
| 170 | + AnsiConsole.MarkupLine("[dim]Dry run mode - no changes would be made[/]"); |
| 171 | + } |
| 172 | + |
| 173 | + if (settings.SkipBreaking) |
| 174 | + { |
| 175 | + AnsiConsole.MarkupLine("[dim]Skip breaking mode - would skip breaking changes[/]"); |
| 176 | + } |
| 177 | + |
| 178 | + return Task.FromResult(0); |
| 179 | + } |
| 180 | +} |
0 commit comments