|
| 1 | +// Package uru provides a migration provider for uru (multi-platform Ruby version manager). |
| 2 | +package uru |
| 3 | + |
| 4 | +import ( |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "regexp" |
| 10 | + goruntime "runtime" |
| 11 | + |
| 12 | + "github.com/dtvem/dtvem/src/internal/migration" |
| 13 | +) |
| 14 | + |
| 15 | +// rubyEntry represents a Ruby installation in uru's rubies.json. |
| 16 | +type rubyEntry struct { |
| 17 | + ID string `json:"ID"` |
| 18 | + TagLabel string `json:"TagLabel"` |
| 19 | + Exe string `json:"Exe"` |
| 20 | + Home string `json:"Home"` |
| 21 | + GemHome string `json:"GemHome"` |
| 22 | + Description string `json:"Description"` |
| 23 | +} |
| 24 | + |
| 25 | +// rubiesJSON represents the structure of uru's rubies.json file. |
| 26 | +type rubiesJSON struct { |
| 27 | + Version string `json:"Version"` |
| 28 | + Rubies map[string]rubyEntry `json:"Rubies"` |
| 29 | +} |
| 30 | + |
| 31 | +// Provider implements the migration.Provider interface for uru. |
| 32 | +type Provider struct{} |
| 33 | + |
| 34 | +// NewProvider creates a new uru migration provider. |
| 35 | +func NewProvider() *Provider { |
| 36 | + return &Provider{} |
| 37 | +} |
| 38 | + |
| 39 | +// Name returns the identifier for this version manager. |
| 40 | +func (p *Provider) Name() string { |
| 41 | + return "uru" |
| 42 | +} |
| 43 | + |
| 44 | +// DisplayName returns the human-readable name. |
| 45 | +func (p *Provider) DisplayName() string { |
| 46 | + return "uru" |
| 47 | +} |
| 48 | + |
| 49 | +// Runtime returns the runtime this provider manages. |
| 50 | +func (p *Provider) Runtime() string { |
| 51 | + return "ruby" |
| 52 | +} |
| 53 | + |
| 54 | +// getUruHome returns the uru home directory. |
| 55 | +// It checks URU_HOME environment variable first, then falls back to ~/.uru. |
| 56 | +func (p *Provider) getUruHome() string { |
| 57 | + if uruHome := os.Getenv("URU_HOME"); uruHome != "" { |
| 58 | + return uruHome |
| 59 | + } |
| 60 | + |
| 61 | + home, err := os.UserHomeDir() |
| 62 | + if err != nil { |
| 63 | + return "" |
| 64 | + } |
| 65 | + |
| 66 | + return filepath.Join(home, ".uru") |
| 67 | +} |
| 68 | + |
| 69 | +// IsPresent checks if uru is installed on the system. |
| 70 | +func (p *Provider) IsPresent() bool { |
| 71 | + uruHome := p.getUruHome() |
| 72 | + if uruHome == "" { |
| 73 | + return false |
| 74 | + } |
| 75 | + |
| 76 | + rubiesPath := filepath.Join(uruHome, "rubies.json") |
| 77 | + if _, err := os.Stat(rubiesPath); err == nil { |
| 78 | + return true |
| 79 | + } |
| 80 | + |
| 81 | + return false |
| 82 | +} |
| 83 | + |
| 84 | +// DetectVersions finds all versions registered with uru. |
| 85 | +func (p *Provider) DetectVersions() ([]migration.DetectedVersion, error) { |
| 86 | + detected := make([]migration.DetectedVersion, 0) |
| 87 | + |
| 88 | + uruHome := p.getUruHome() |
| 89 | + if uruHome == "" { |
| 90 | + return detected, nil |
| 91 | + } |
| 92 | + |
| 93 | + rubiesPath := filepath.Join(uruHome, "rubies.json") |
| 94 | + data, err := os.ReadFile(rubiesPath) |
| 95 | + if err != nil { |
| 96 | + // If we can't read the file, just return empty list |
| 97 | + return detected, nil //nolint:nilerr // Expected: no rubies.json means no uru rubies |
| 98 | + } |
| 99 | + |
| 100 | + var rubies rubiesJSON |
| 101 | + if err := json.Unmarshal(data, &rubies); err != nil { |
| 102 | + // Invalid JSON, return empty list |
| 103 | + return detected, nil //nolint:nilerr // Expected: invalid JSON means no usable uru data |
| 104 | + } |
| 105 | + |
| 106 | + // Version pattern: major.minor.patch (e.g., "3.2.0") |
| 107 | + versionRegex := regexp.MustCompile(`(\d+\.\d+\.\d+)`) |
| 108 | + |
| 109 | + for tag, entry := range rubies.Rubies { |
| 110 | + if entry.Home == "" { |
| 111 | + continue |
| 112 | + } |
| 113 | + |
| 114 | + // Extract version from ID field (e.g., "3.2.0-p0" -> "3.2.0") |
| 115 | + version := "" |
| 116 | + if matches := versionRegex.FindStringSubmatch(entry.ID); len(matches) >= 2 { |
| 117 | + version = matches[1] |
| 118 | + } |
| 119 | + |
| 120 | + if version == "" { |
| 121 | + continue |
| 122 | + } |
| 123 | + |
| 124 | + // Build path to ruby executable |
| 125 | + rubyExe := "ruby" |
| 126 | + if goruntime.GOOS == "windows" { |
| 127 | + rubyExe = "ruby.exe" |
| 128 | + } |
| 129 | + rubyPath := filepath.Join(entry.Home, rubyExe) |
| 130 | + |
| 131 | + // Verify the executable exists |
| 132 | + if _, err := os.Stat(rubyPath); err != nil { |
| 133 | + continue |
| 134 | + } |
| 135 | + |
| 136 | + detected = append(detected, migration.DetectedVersion{ |
| 137 | + Version: version, |
| 138 | + Path: rubyPath, |
| 139 | + Source: fmt.Sprintf("uru (%s)", tag), |
| 140 | + Validated: false, |
| 141 | + }) |
| 142 | + } |
| 143 | + |
| 144 | + return detected, nil |
| 145 | +} |
| 146 | + |
| 147 | +// CanAutoUninstall returns true because uru supports removing registered rubies. |
| 148 | +func (p *Provider) CanAutoUninstall() bool { |
| 149 | + return true |
| 150 | +} |
| 151 | + |
| 152 | +// UninstallCommand returns the command to remove a Ruby from uru's registry. |
| 153 | +func (p *Provider) UninstallCommand(version string) string { |
| 154 | + return fmt.Sprintf("uru admin rm %s", version) |
| 155 | +} |
| 156 | + |
| 157 | +// ManualInstructions returns instructions for manual removal. |
| 158 | +func (p *Provider) ManualInstructions() string { |
| 159 | + return "To remove a Ruby from uru's registry:\n" + |
| 160 | + " 1. Run: uru admin rm <tag>\n" + |
| 161 | + " 2. This only removes uru's reference, not the Ruby installation itself\n" + |
| 162 | + " 3. To fully uninstall, also remove the Ruby directory manually" |
| 163 | +} |
| 164 | + |
| 165 | +// init registers the uru provider on package load. |
| 166 | +func init() { |
| 167 | + if err := migration.Register(NewProvider()); err != nil { |
| 168 | + panic(fmt.Sprintf("failed to register uru migration provider: %v", err)) |
| 169 | + } |
| 170 | +} |
0 commit comments