|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "os" |
| 6 | + "os/user" |
| 7 | + "path/filepath" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +// Config represents the application configuration |
| 12 | +type Config struct { |
| 13 | + Version int `json:"version"` |
| 14 | + BaseDir string `json:"base_dir"` |
| 15 | + OriginDir string `json:"origin_dir"` |
| 16 | + WorktreesDir string `json:"worktrees_dir"` |
| 17 | + DefaultRemoteBranch string `json:"default_remote_branch"` |
| 18 | + Engines []Engine `json:"engines"` |
| 19 | + CustomEngineRoots []string `json:"custom_engine_roots"` |
| 20 | + LastRunUTC string `json:"last_run_utc"` |
| 21 | +} |
| 22 | + |
| 23 | +// Engine represents a managed Unreal Engine installation |
| 24 | +type Engine struct { |
| 25 | + EnginePath string `json:"engine_path"` |
| 26 | + EngineVersion string `json:"engine_version"` |
| 27 | + WorktreeSubdir string `json:"worktree_subdir"` |
| 28 | + Branch string `json:"branch"` |
| 29 | + PluginLinkPath string `json:"plugin_link_path"` |
| 30 | + StockPluginDisabledByTool bool `json:"stock_plugin_disabled_by_tool"` |
| 31 | +} |
| 32 | + |
| 33 | +// Manager handles configuration operations |
| 34 | +type Manager struct { |
| 35 | + exeDir string |
| 36 | + baseDir string |
| 37 | + configPath string |
| 38 | +} |
| 39 | + |
| 40 | +// New creates a new configuration manager |
| 41 | +func New(exeDir string) *Manager { |
| 42 | + baseDir := getUserConfigDir() |
| 43 | + return &Manager{ |
| 44 | + exeDir: exeDir, |
| 45 | + baseDir: baseDir, |
| 46 | + configPath: filepath.Join(baseDir, "config.json"), |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// getUserConfigDir returns the user's config directory for the application |
| 51 | +func getUserConfigDir() string { |
| 52 | + // Get the current user |
| 53 | + usr, err := user.Current() |
| 54 | + if err != nil { |
| 55 | + // Fallback to executable directory if we can't get user info |
| 56 | + exePath, _ := os.Executable() |
| 57 | + return filepath.Dir(exePath) |
| 58 | + } |
| 59 | + |
| 60 | + // Use the user's config directory |
| 61 | + // On Windows: %APPDATA%\Pi\unreal_source_control |
| 62 | + // On Linux/macOS: ~/.config/Pi/unreal_source_control |
| 63 | + configDir := filepath.Join(usr.HomeDir, "AppData", "Roaming", "Pi", "unreal_source_control") |
| 64 | + |
| 65 | + // Create the directory if it doesn't exist |
| 66 | + os.MkdirAll(configDir, 0755) |
| 67 | + |
| 68 | + return configDir |
| 69 | +} |
| 70 | + |
| 71 | +// GetExeDir returns the executable directory |
| 72 | +func (m *Manager) GetExeDir() string { |
| 73 | + return m.exeDir |
| 74 | +} |
| 75 | + |
| 76 | +// GetBaseDir returns the base directory for the application data |
| 77 | +func (m *Manager) GetBaseDir() string { |
| 78 | + return m.baseDir |
| 79 | +} |
| 80 | + |
| 81 | +// Exists checks if the configuration file exists |
| 82 | +func (m *Manager) Exists() bool { |
| 83 | + _, err := os.Stat(m.configPath) |
| 84 | + return !os.IsNotExist(err) |
| 85 | +} |
| 86 | + |
| 87 | +// Load loads the configuration from file |
| 88 | +func (m *Manager) Load() (*Config, error) { |
| 89 | + data, err := os.ReadFile(m.configPath) |
| 90 | + if err != nil { |
| 91 | + return nil, err |
| 92 | + } |
| 93 | + |
| 94 | + var config Config |
| 95 | + if err := json.Unmarshal(data, &config); err != nil { |
| 96 | + return nil, err |
| 97 | + } |
| 98 | + |
| 99 | + // Resolve relative paths |
| 100 | + config.BaseDir = m.resolvePath(config.BaseDir) |
| 101 | + config.OriginDir = m.resolvePath(config.OriginDir) |
| 102 | + config.WorktreesDir = m.resolvePath(config.WorktreesDir) |
| 103 | + |
| 104 | + return &config, nil |
| 105 | +} |
| 106 | + |
| 107 | +// Save saves the configuration to file |
| 108 | +func (m *Manager) Save(config *Config) error { |
| 109 | + // Make a copy to avoid modifying the original |
| 110 | + saveConfig := *config |
| 111 | + |
| 112 | + // Convert absolute paths to relative where possible |
| 113 | + saveConfig.BaseDir = m.makeRelative(saveConfig.BaseDir) |
| 114 | + saveConfig.OriginDir = m.makeRelative(saveConfig.OriginDir) |
| 115 | + saveConfig.WorktreesDir = m.makeRelative(saveConfig.WorktreesDir) |
| 116 | + |
| 117 | + // Update last run time |
| 118 | + saveConfig.LastRunUTC = time.Now().UTC().Format(time.RFC3339) |
| 119 | + |
| 120 | + data, err := json.MarshalIndent(saveConfig, "", " ") |
| 121 | + if err != nil { |
| 122 | + return err |
| 123 | + } |
| 124 | + |
| 125 | + return os.WriteFile(m.configPath, data, 0644) |
| 126 | +} |
| 127 | + |
| 128 | +// CreateDefault creates a default configuration |
| 129 | +func (m *Manager) CreateDefault() *Config { |
| 130 | + return &Config{ |
| 131 | + Version: 1, |
| 132 | + BaseDir: m.baseDir, |
| 133 | + OriginDir: "repo-origin", |
| 134 | + WorktreesDir: "worktrees", |
| 135 | + DefaultRemoteBranch: "dev", |
| 136 | + Engines: []Engine{}, |
| 137 | + CustomEngineRoots: []string{}, |
| 138 | + LastRunUTC: time.Now().UTC().Format(time.RFC3339), |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +// AddEngine adds an engine to the configuration |
| 143 | +func (m *Manager) AddEngine(config *Config, eng Engine) { |
| 144 | + config.Engines = append(config.Engines, eng) |
| 145 | +} |
| 146 | + |
| 147 | +// RemoveEngine removes an engine from the configuration |
| 148 | +func (m *Manager) RemoveEngine(config *Config, enginePath string) { |
| 149 | + for i, eng := range config.Engines { |
| 150 | + if eng.EnginePath == enginePath { |
| 151 | + config.Engines = append(config.Engines[:i], config.Engines[i+1:]...) |
| 152 | + break |
| 153 | + } |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +// GetEngineByPath gets an engine by its path |
| 158 | +func (m *Manager) GetEngineByPath(config *Config, enginePath string) *Engine { |
| 159 | + for i, eng := range config.Engines { |
| 160 | + if eng.EnginePath == enginePath { |
| 161 | + return &config.Engines[i] |
| 162 | + } |
| 163 | + } |
| 164 | + return nil |
| 165 | +} |
| 166 | + |
| 167 | +// resolvePath resolves a path relative to the base directory |
| 168 | +func (m *Manager) resolvePath(path string) string { |
| 169 | + if filepath.IsAbs(path) { |
| 170 | + return path |
| 171 | + } |
| 172 | + return filepath.Join(m.baseDir, path) |
| 173 | +} |
| 174 | + |
| 175 | +// makeRelative makes a path relative to the base directory if possible |
| 176 | +func (m *Manager) makeRelative(path string) string { |
| 177 | + rel, err := filepath.Rel(m.baseDir, path) |
| 178 | + if err != nil || len(rel) >= len(path) { |
| 179 | + return path // Return original if can't make relative or if relative is longer |
| 180 | + } |
| 181 | + return rel |
| 182 | +} |
0 commit comments