-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.go
More file actions
477 lines (403 loc) · 13.8 KB
/
project.go
File metadata and controls
477 lines (403 loc) · 13.8 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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
package gogo
import (
"fmt"
"path/filepath"
"github.com/guillermo/gogo/fs"
)
// Project represents a Go project being modified
type Project struct {
opts Options
fs fs.FS
conflictFunc ConflictFunc
}
// modifyStructInFile modifies or creates a struct in Go source code
func (p *Project) modifyStructInFile(content []byte, s structDef, defaultPackage string) ([]byte, error) {
// This will be implemented with AST parsing
// For now, return a placeholder
if len(content) == 0 {
// Create new file
return createNewFileWithStruct(s, defaultPackage)
}
// Modify existing file
return modifyExistingFile(content, s)
}
// modifyMethodInFile modifies or creates methods in Go source code
func (p *Project) modifyMethodInFile(content []byte, opts MethodOpts, defaultPackage string) ([]byte, error) {
// This will be implemented with AST parsing
// For now, return a placeholder
if len(content) == 0 {
// Create new file
return createNewFileWithMethod(opts, defaultPackage)
}
// Modify existing file
return modifyExistingFileForMethod(content, opts)
}
// modifyFunctionInFile modifies or creates functions in Go source code
func (p *Project) modifyFunctionInFile(content []byte, opts FunctionOpts, defaultPackage string) ([]byte, error) {
// This will be implemented with AST parsing
// For now, return a placeholder
if len(content) == 0 {
// Create new file
return createNewFileWithFunction(opts, defaultPackage)
}
// Modify existing file
return modifyExistingFileForFunction(content, opts)
}
// modifyVariableInFile modifies or creates variables in Go source code
func (p *Project) modifyVariableInFile(content []byte, opts VariableOpts, defaultPackage string) ([]byte, error) {
// This will be implemented with AST parsing
// For now, return a placeholder
if len(content) == 0 {
// Create new file
return createNewFileWithVariable(opts, defaultPackage)
}
// Modify existing file
return modifyExistingFileForVariable(content, opts)
}
// modifyConstantInFile modifies or creates constants in Go source code
func (p *Project) modifyConstantInFile(content []byte, opts ConstantOpts, defaultPackage string) ([]byte, error) {
// This will be implemented with AST parsing
// For now, return a placeholder
if len(content) == 0 {
// Create new file
return createNewFileWithConstant(opts, defaultPackage)
}
// Modify existing file
return modifyExistingFileForConstant(content, opts)
}
// modifyTypeInFile modifies or creates type definitions in Go source code
func (p *Project) modifyTypeInFile(content []byte, opts TypeOpts, defaultPackage string) ([]byte, error) {
// This will be implemented with AST parsing
// For now, return a placeholder
if len(content) == 0 {
// Create new file
return createNewFileWithType(opts, defaultPackage)
}
// Modify existing file
return modifyExistingFileForType(content, opts)
}
// Struct creates or modifies a struct using the unified API
func (p *Project) Struct(opts StructOpts) error {
// Validation: Fields and Content are mutually exclusive
if len(opts.Fields) > 0 && opts.Content != "" {
return fmt.Errorf("Fields and Content are mutually exclusive - provide only one")
}
// Validation: Must provide either Fields or Content
if len(opts.Fields) == 0 && opts.Content == "" {
return fmt.Errorf("must provide either Fields or Content")
}
// Validation: Required fields
if opts.Filename == "" {
return fmt.Errorf("Filename is required")
}
if opts.Name == "" {
return fmt.Errorf("struct Name is required")
}
// Prepare the struct definition
var s structDef
if opts.Content != "" {
// Parse the content string to create StructField slice (like CreateOrSetMethodS)
parsedFields, err := parseFieldsString(opts.Content)
if err != nil {
return fmt.Errorf("failed to parse fields: %w", err)
}
s = structDef{
Name: opts.Name,
EnsureFields: parsedFields,
PreserveExisting: true,
}
} else {
// Use Fields-based approach (like CreateOrSetStruct)
s = structDef{
Name: opts.Name,
EnsureFields: opts.Fields,
DeleteFields: opts.DeleteFields,
PreserveExisting: opts.PreserveExisting,
}
}
// Read existing file content if it exists
var oldContent []byte
_, err := p.fs.Stat(opts.Filename)
fileExists := err == nil
if fileExists {
oldContent, err = p.fs.ReadFile(opts.Filename)
if err != nil {
return fmt.Errorf("failed to read existing file: %w", err)
}
}
// Parse and modify the content
newContent, err := p.modifyStructInFile(oldContent, s, p.opts.InitialPackageName)
if err != nil {
return fmt.Errorf("failed to modify struct: %w", err)
}
// Check if there are actual changes
if fileExists && string(oldContent) == string(newContent) {
// No changes needed
return nil
}
// Apply the changes
return p.applyChanges(opts.Filename, oldContent, newContent, fileExists)
}
// Method creates or modifies a method using the unified API
func (p *Project) Method(opts MethodOpts) error {
// Validation: Parameters/ReturnType/Body and Content are mutually exclusive
hasStructuredParams := len(opts.Parameters) > 0 || opts.ReturnType != "" || opts.Body != ""
if hasStructuredParams && opts.Content != "" {
return fmt.Errorf("Parameters/ReturnType/Body and Content are mutually exclusive - provide only one approach")
}
// Validation: Must provide either structured params or content
if !hasStructuredParams && opts.Content == "" {
return fmt.Errorf("must provide either Parameters/ReturnType/Body or Content")
}
// Validation: Required fields
if opts.Filename == "" {
return fmt.Errorf("Filename is required")
}
if opts.Name == "" {
return fmt.Errorf("method Name is required")
}
if opts.ReceiverType == "" {
return fmt.Errorf("ReceiverType is required for methods")
}
// Read existing file content if it exists
var oldContent []byte
_, err := p.fs.Stat(opts.Filename)
fileExists := err == nil
if fileExists {
oldContent, err = p.fs.ReadFile(opts.Filename)
if err != nil {
return fmt.Errorf("failed to read existing file: %w", err)
}
}
// Parse and modify the content
newContent, err := p.modifyMethodInFile(oldContent, opts, p.opts.InitialPackageName)
if err != nil {
return fmt.Errorf("failed to modify method: %w", err)
}
// Check if there are actual changes
if fileExists && string(oldContent) == string(newContent) {
// No changes needed
return nil
}
// Apply the changes using the same pattern as Struct
return p.applyChanges(opts.Filename, oldContent, newContent, fileExists)
}
// Function creates or modifies a function using the unified API
func (p *Project) Function(opts FunctionOpts) error {
// Validation: Parameters/ReturnType/Body and Content are mutually exclusive
hasStructuredParams := len(opts.Parameters) > 0 || opts.ReturnType != "" || opts.Body != ""
if hasStructuredParams && opts.Content != "" {
return fmt.Errorf("Parameters/ReturnType/Body and Content are mutually exclusive - provide only one approach")
}
// Validation: Must provide either structured params or content
if !hasStructuredParams && opts.Content == "" {
return fmt.Errorf("must provide either Parameters/ReturnType/Body or Content")
}
// Validation: Required fields
if opts.Filename == "" {
return fmt.Errorf("Filename is required")
}
if opts.Name == "" {
return fmt.Errorf("function Name is required")
}
// Read existing file content if it exists
var oldContent []byte
_, err := p.fs.Stat(opts.Filename)
fileExists := err == nil
if fileExists {
oldContent, err = p.fs.ReadFile(opts.Filename)
if err != nil {
return fmt.Errorf("failed to read existing file: %w", err)
}
}
// Parse and modify the content
newContent, err := p.modifyFunctionInFile(oldContent, opts, p.opts.InitialPackageName)
if err != nil {
return fmt.Errorf("failed to modify function: %w", err)
}
// Check if there are actual changes
if fileExists && string(oldContent) == string(newContent) {
// No changes needed
return nil
}
// Apply the changes
return p.applyChanges(opts.Filename, oldContent, newContent, fileExists)
}
// Variable creates or modifies variables using the unified API
func (p *Project) Variable(opts VariableOpts) error {
// Validation: Variables and Content are mutually exclusive
if len(opts.Variables) > 0 && opts.Content != "" {
return fmt.Errorf("Variables and Content are mutually exclusive - provide only one")
}
// Validation: Must provide either Variables or Content
if len(opts.Variables) == 0 && opts.Content == "" {
return fmt.Errorf("must provide either Variables or Content")
}
// Validation: Required fields
if opts.Filename == "" {
return fmt.Errorf("Filename is required")
}
// Read existing file content if it exists
var oldContent []byte
_, err := p.fs.Stat(opts.Filename)
fileExists := err == nil
if fileExists {
oldContent, err = p.fs.ReadFile(opts.Filename)
if err != nil {
return fmt.Errorf("failed to read existing file: %w", err)
}
}
// Parse and modify the content
newContent, err := p.modifyVariableInFile(oldContent, opts, p.opts.InitialPackageName)
if err != nil {
return fmt.Errorf("failed to modify variables: %w", err)
}
// Check if there are actual changes
if fileExists && string(oldContent) == string(newContent) {
// No changes needed
return nil
}
// Apply the changes
return p.applyChanges(opts.Filename, oldContent, newContent, fileExists)
}
// Constant creates or modifies constants using the unified API
func (p *Project) Constant(opts ConstantOpts) error {
// Validation: Constants and Content are mutually exclusive
if len(opts.Constants) > 0 && opts.Content != "" {
return fmt.Errorf("Constants and Content are mutually exclusive - provide only one")
}
// Validation: Must provide either Constants or Content
if len(opts.Constants) == 0 && opts.Content == "" {
return fmt.Errorf("must provide either Constants or Content")
}
// Validation: Required fields
if opts.Filename == "" {
return fmt.Errorf("Filename is required")
}
// Read existing file content if it exists
var oldContent []byte
_, err := p.fs.Stat(opts.Filename)
fileExists := err == nil
if fileExists {
oldContent, err = p.fs.ReadFile(opts.Filename)
if err != nil {
return fmt.Errorf("failed to read existing file: %w", err)
}
}
// Parse and modify the content
newContent, err := p.modifyConstantInFile(oldContent, opts, p.opts.InitialPackageName)
if err != nil {
return fmt.Errorf("failed to modify constants: %w", err)
}
// Check if there are actual changes
if fileExists && string(oldContent) == string(newContent) {
// No changes needed
return nil
}
// Apply the changes
return p.applyChanges(opts.Filename, oldContent, newContent, fileExists)
}
// Type creates or modifies type definitions using the unified API
func (p *Project) Type(opts TypeOpts) error {
// Validation: Types and Content are mutually exclusive
if len(opts.Types) > 0 && opts.Content != "" {
return fmt.Errorf("Types and Content are mutually exclusive - provide only one")
}
// Validation: Must provide either Types or Content
if len(opts.Types) == 0 && opts.Content == "" {
return fmt.Errorf("must provide either Types or Content")
}
// Validation: Required fields
if opts.Filename == "" {
return fmt.Errorf("Filename is required")
}
// Read existing file content if it exists
var oldContent []byte
_, err := p.fs.Stat(opts.Filename)
fileExists := err == nil
if fileExists {
oldContent, err = p.fs.ReadFile(opts.Filename)
if err != nil {
return fmt.Errorf("failed to read existing file: %w", err)
}
}
// Parse and modify the content
newContent, err := p.modifyTypeInFile(oldContent, opts, p.opts.InitialPackageName)
if err != nil {
return fmt.Errorf("failed to modify types: %w", err)
}
// Check if there are actual changes
if fileExists && string(oldContent) == string(newContent) {
// No changes needed
return nil
}
// Apply the changes
return p.applyChanges(opts.Filename, oldContent, newContent, fileExists)
}
// applyChanges applies the changes to a file using the common pattern
func (p *Project) applyChanges(filename string, oldContent, newContent []byte, fileExists bool) error {
// Ensure the directory exists
dir := filepath.Dir(filename)
if dir != "" && dir != "." {
if err := p.fs.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("failed to create directory %s: %w", dir, err)
}
}
// Create temp file
tempFile, err := p.fs.TempFile(dir, ".gogo-*.go")
if err != nil {
return fmt.Errorf("failed to create temp file: %w", err)
}
tempPath := tempFile.Name()
tempFile.Close()
// Write new content to temp file
if err := p.fs.WriteFile(tempPath, newContent, 0644); err != nil {
p.fs.Remove(tempPath)
return fmt.Errorf("failed to write temp file: %w", err)
}
// Prepare change info
action := "modify"
if !fileExists {
action = "create"
}
changeInfo := ChangeInfo{
Action: action,
FileName: filename,
OldContent: oldContent,
NewContent: newContent,
Diff: generateDiff(oldContent, newContent, filename),
}
// Ask for confirmation if needed
if p.conflictFunc != nil {
if !p.conflictFunc(p.fs, filename, tempPath, changeInfo) {
// User rejected changes
p.fs.Remove(tempPath)
return nil
}
}
// Apply changes by moving temp file to target
if fileExists {
// Backup existing file temporarily
backupPath := filename + ".backup"
if err := p.fs.Rename(filename, backupPath); err != nil {
p.fs.Remove(tempPath)
return fmt.Errorf("failed to backup existing file: %w", err)
}
// Move temp file to target
if err := p.fs.Rename(tempPath, filename); err != nil {
// Restore backup
p.fs.Rename(backupPath, filename)
p.fs.Remove(tempPath)
return fmt.Errorf("failed to apply changes: %w", err)
}
// Remove backup
p.fs.Remove(backupPath)
} else {
// Just move temp file to target
if err := p.fs.Rename(tempPath, filename); err != nil {
p.fs.Remove(tempPath)
return fmt.Errorf("failed to create file: %w", err)
}
}
return nil
}