-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput
More file actions
359 lines (301 loc) · 7.67 KB
/
Copy pathinput
File metadata and controls
359 lines (301 loc) · 7.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// cmd/pm/main.go
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/yourusername/pm/internal/config"
"github.com/yourusername/pm/internal/modules/management"
)
func main() {
homeDir, err := os.UserHomeDir()
if err != nil {
fmt.Printf("Error getting home directory: %v\n", err)
os.Exit(1)
}
configPath := filepath.Join(homeDir, ".config", "project-manager", "config.json")
cfg, err := config.Load(configPath)
if err != nil {
fmt.Printf("Error loading config: %v\n", err)
os.Exit(1)
}
manager := management.New(cfg)
if err := manager.RunCommand(os.Args[1], os.Args[2]); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
// internal/config/types.go
package config
type Component struct {
Name string `json:"name"`
Path string `json:"path"`
Commands map[string]string `json:"commands"`
Enabled bool `json:"enabled"`
}
type Project struct {
Name string `json:"name"`
Components []Component `json:"components"`
}
type Config struct {
Projects []Project `json:"projects"`
}
// internal/config/config.go
package config
import (
"encoding/json"
"os"
"path/filepath"
)
func Load(path string) (*Config, error) {
data, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
cfg := &Config{}
if err := Save(cfg, path); err != nil {
return nil, err
}
return cfg, nil
}
return nil, err
}
var cfg Config
if err := json.Unmarshal(data, &cfg); err != nil {
return nil, err
}
return &cfg, nil
}
func Save(cfg *Config, path string) error {
dir := filepath.Dir(path)
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
data, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
return err
}
return os.WriteFile(path, data, 0644)
}
// internal/modules/management/types.go
package management
import (
"time"
"github.com/yourusername/pm/internal/config"
)
type ProcessState struct {
ProjectName string `yaml:"projectName"`
ComponentID string `yaml:"componentId"`
Path string `yaml:"path"`
Command string `yaml:"command"`
CommandType string `yaml:"commandType"`
PID int `yaml:"pid"`
WindowID string `yaml:"windowId"`
TabID string `yaml:"tabId"`
StartTime time.Time `yaml:"startTime"`
LastSeen time.Time `yaml:"lastSeen"`
IsResponding bool `yaml:"isResponding"`
}
type ProcessManager struct {
StateFile string
Processes map[string]ProcessState
}
// internal/modules/management/state.go
package management
import (
"fmt"
"os"
"path/filepath"
"time"
"gopkg.in/yaml.v2"
)
func (pm *ProcessManager) LoadState() error {
data, err := os.ReadFile(pm.StateFile)
if err != nil {
if os.IsNotExist(err) {
pm.Processes = make(map[string]ProcessState)
return nil
}
return err
}
return yaml.Unmarshal(data, &pm.Processes)
}
func (pm *ProcessManager) SaveState() error {
dir := filepath.Dir(pm.StateFile)
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
data, err := yaml.Marshal(pm.Processes)
if err != nil {
return err
}
return os.WriteFile(pm.StateFile, data, 0644)
}
func (pm *ProcessManager) RegisterProcess(state ProcessState) error {
key := fmt.Sprintf("%s-%s-%s", state.ProjectName, state.ComponentID, state.CommandType)
state.StartTime = time.Now()
state.LastSeen = time.Now()
state.IsResponding = true
pm.Processes[key] = state
return pm.SaveState()
}
func (pm *ProcessManager) UpdateProcess(key string, isResponding bool) error {
if proc, exists := pm.Processes[key]; exists {
proc.LastSeen = time.Now()
proc.IsResponding = isResponding
pm.Processes[key] = proc
return pm.SaveState()
}
return fmt.Errorf("process not found: %s", key)
}
func (pm *ProcessManager) StopProcess(projectName, componentID, commandType string) error {
key := fmt.Sprintf("%s-%s-%s", projectName, componentID, commandType)
if proc, exists := pm.Processes[key]; exists {
if err := killProcess(proc.PID); err != nil {
return err
}
delete(pm.Processes, key)
return pm.SaveState()
}
return fmt.Errorf("process not found: %s", key)
}
// internal/modules/management/terminal.go
package management
import (
"fmt"
"os/exec"
"strings"
)
type TerminalGroup struct {
Path string
Components []TerminalComponent
}
type TerminalComponent struct {
Name string
Command string
CommandType string
}
func runInTerminal(group *TerminalGroup, projectName string, pm *ProcessManager) error {
script := fmt.Sprintf(`
tell application "Terminal"
set newWindow to do script "cd %s && echo '# %s - %s' && %s"
set windowId to id of window 1
return windowId
end tell
`, group.Path, projectName, group.Components[0].Name, group.Components[0].Command)
output, err := exec.Command("osascript", "-e", script).Output()
if err != nil {
return err
}
windowID := strings.TrimSpace(string(output))
state := ProcessState{
ProjectName: projectName,
ComponentID: group.Components[0].Name,
Path: group.Path,
Command: group.Components[0].Command,
CommandType: group.Components[0].CommandType,
WindowID: windowID,
TabID: "1",
}
if err := pm.RegisterProcess(state); err != nil {
return err
}
for i, comp := range group.Components[1:] {
script := fmt.Sprintf(`
tell application "Terminal"
tell window id %s
set newTab to do script "cd %s && echo '# %s - %s' && %s"
return id of tab %d
end tell
end tell
`, windowID, group.Path, projectName, comp.Name, comp.Command, i+2)
output, err := exec.Command("osascript", "-e", script).Output()
if err != nil {
return err
}
tabID := strings.TrimSpace(string(output))
state := ProcessState{
ProjectName: projectName,
ComponentID: comp.Name,
Path: group.Path,
Command: comp.Command,
CommandType: comp.CommandType,
WindowID: windowID,
TabID: tabID,
}
if err := pm.RegisterProcess(state); err != nil {
return err
}
}
return nil
}
// internal/modules/management/manager.go
package management
import (
"fmt"
"os"
"path/filepath"
"github.com/yourusername/pm/internal/config"
)
type Manager struct {
config *config.Config
procMgr *ProcessManager
}
func New(cfg *config.Config) *Manager {
homeDir, _ := os.UserHomeDir()
stateFile := filepath.Join(homeDir, ".config", "project-manager", "state.yaml")
return &Manager{
config: cfg,
procMgr: &ProcessManager{
StateFile: stateFile,
Processes: make(map[string]ProcessState),
},
}
}
func (m *Manager) RunCommand(projectName, commandType string) error {
if err := m.procMgr.LoadState(); err != nil {
return fmt.Errorf("failed to load state: %v", err)
}
var project *config.Project
for _, p := range m.config.Projects {
if p.Name == projectName {
project = &p
break
}
}
if project == nil {
return fmt.Errorf("project %s not found", projectName)
}
terminalGroups := make(map[string]*TerminalGroup)
for _, component := range project.Components {
if !component.Enabled {
continue
}
command, ok := component.Commands[commandType]
if !ok {
continue
}
absPath, err := filepath.Abs(component.Path)
if err != nil {
return fmt.Errorf("failed to resolve path for %s: %v", component.Name, err)
}
group, exists := terminalGroups[absPath]
if !exists {
group = &TerminalGroup{
Path: absPath,
Components: make([]TerminalComponent, 0),
}
terminalGroups[absPath] = group
}
group.Components = append(group.Components, TerminalComponent{
Name: component.Name,
Command: command,
CommandType: commandType,
})
}
for _, group := range terminalGroups {
if err := runInTerminal(group, projectName, m.procMgr); err != nil {
return fmt.Errorf("failed to run in Terminal.app: %v", err)
}
}
return nil
}