|
| 1 | +package migration |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/josephgoksu/TaskWing/internal/bootstrap" |
| 11 | + "github.com/josephgoksu/TaskWing/internal/mcpcfg" |
| 12 | +) |
| 13 | + |
| 14 | +// CheckAndMigrate runs a post-upgrade migration if the CLI version has changed |
| 15 | +// since the last run in this project. It silently regenerates local configs and |
| 16 | +// returns warnings for issues that require manual intervention (e.g., global MCP). |
| 17 | +// |
| 18 | +// This is designed to be called from PersistentPreRunE and must be: |
| 19 | +// - Sub-millisecond on the happy path (version matches) |
| 20 | +// - Non-fatal on all error paths (never blocks user commands) |
| 21 | +func CheckAndMigrate(projectDir, currentVersion string) (warnings []string, err error) { |
| 22 | + taskwingDir := filepath.Join(projectDir, ".taskwing") |
| 23 | + versionFile := filepath.Join(taskwingDir, "version") |
| 24 | + |
| 25 | + // Not bootstrapped or inaccessible — nothing to migrate |
| 26 | + if _, err := os.Stat(taskwingDir); err != nil { |
| 27 | + return nil, nil |
| 28 | + } |
| 29 | + |
| 30 | + stored, err := os.ReadFile(versionFile) |
| 31 | + if err != nil { |
| 32 | + // Version file missing (pre-migration bootstrap). Write current and return. |
| 33 | + if werr := os.WriteFile(versionFile, []byte(currentVersion), 0644); werr != nil { |
| 34 | + fmt.Fprintf(os.Stderr, "⚠️ taskwing: could not write version stamp (%v); migration will re-run next time\n", werr) |
| 35 | + } |
| 36 | + return nil, nil |
| 37 | + } |
| 38 | + |
| 39 | + storedVersion := strings.TrimSpace(string(stored)) |
| 40 | + |
| 41 | + // Happy path: version matches — no-op |
| 42 | + if storedVersion == currentVersion { |
| 43 | + return nil, nil |
| 44 | + } |
| 45 | + |
| 46 | + // Skip dev builds to avoid constant re-runs during development |
| 47 | + if currentVersion == "dev" { |
| 48 | + return nil, nil |
| 49 | + } |
| 50 | + |
| 51 | + // --- Version mismatch: run migration --- |
| 52 | + |
| 53 | + // 1. Silent local migration: regenerate slash commands for managed AIs |
| 54 | + migrateLocalConfigs(projectDir) |
| 55 | + |
| 56 | + // 2. Global MCP check: warn about legacy server names |
| 57 | + warnings = checkGlobalMCPLegacy() |
| 58 | + |
| 59 | + // 3. Write current version |
| 60 | + if werr := os.WriteFile(versionFile, []byte(currentVersion), 0644); werr != nil { |
| 61 | + fmt.Fprintf(os.Stderr, "⚠️ taskwing: could not write version stamp (%v); migration will re-run next time\n", werr) |
| 62 | + } |
| 63 | + |
| 64 | + return warnings, nil |
| 65 | +} |
| 66 | + |
| 67 | +// migrateLocalConfigs detects which AIs have managed markers and regenerates |
| 68 | +// their slash commands (which internally prunes stale tw-* files). |
| 69 | +func migrateLocalConfigs(projectDir string) { |
| 70 | + for _, aiName := range bootstrap.ValidAINames() { |
| 71 | + cfg, ok := bootstrap.AIHelperByName(aiName) |
| 72 | + if !ok { |
| 73 | + continue |
| 74 | + } |
| 75 | + |
| 76 | + // Check if this AI has a managed marker |
| 77 | + if cfg.SingleFile { |
| 78 | + // Single-file AIs (e.g., Copilot) embed the marker in file content. |
| 79 | + // Check for the embedded marker before regenerating. |
| 80 | + filePath := filepath.Join(projectDir, cfg.CommandsDir, cfg.SingleFileName) |
| 81 | + content, err := os.ReadFile(filePath) |
| 82 | + if err != nil || !strings.Contains(string(content), "<!-- TASKWING_MANAGED -->") { |
| 83 | + continue |
| 84 | + } |
| 85 | + } else { |
| 86 | + markerPath := filepath.Join(projectDir, cfg.CommandsDir, bootstrap.TaskWingManagedFile) |
| 87 | + if _, err := os.Stat(markerPath); err != nil { |
| 88 | + continue |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // Regenerate (this prunes stale files and creates new ones) |
| 93 | + initializer := bootstrap.NewInitializer(projectDir) |
| 94 | + if err := initializer.CreateSlashCommands(aiName, false); err != nil { |
| 95 | + fmt.Fprintf(os.Stderr, "⚠️ taskwing: could not regenerate %s commands: %v\n", aiName, err) |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +// checkGlobalMCPLegacy reads Claude's global MCP config and warns if legacy |
| 101 | +// server names are present. |
| 102 | +func checkGlobalMCPLegacy() []string { |
| 103 | + home, err := os.UserHomeDir() |
| 104 | + if err != nil { |
| 105 | + return nil |
| 106 | + } |
| 107 | + configPath := filepath.Join(home, ".claude", "claude_desktop_config.json") |
| 108 | + return checkGlobalMCPLegacyAt(configPath) |
| 109 | +} |
| 110 | + |
| 111 | +// checkGlobalMCPLegacyAt checks a specific config file path for legacy server names. |
| 112 | +func checkGlobalMCPLegacyAt(configPath string) []string { |
| 113 | + content, err := os.ReadFile(configPath) |
| 114 | + if err != nil { |
| 115 | + return nil |
| 116 | + } |
| 117 | + |
| 118 | + var config struct { |
| 119 | + MCPServers map[string]json.RawMessage `json:"mcpServers"` |
| 120 | + } |
| 121 | + if err := json.Unmarshal(content, &config); err != nil { |
| 122 | + return nil |
| 123 | + } |
| 124 | + |
| 125 | + var warnings []string |
| 126 | + for name := range config.MCPServers { |
| 127 | + if mcpcfg.IsLegacyServerName(name) { |
| 128 | + warnings = append(warnings, fmt.Sprintf("Global MCP config has legacy server name %q. Run: taskwing doctor --fix --yes", name)) |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + return warnings |
| 133 | +} |
0 commit comments