-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy-file.go
217 lines (165 loc) · 8.23 KB
/
copy-file.go
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
package main
import (
"fmt"
"os"
"strings"
)
// modifyFile modifies the destination file content to include additional code
func modifyFile(filePath string, entity Entity) error {
if strings.Contains(filePath, "repository.go") {
return ToUpdateRepositoriesFile(filePath, entity)
}
if strings.Contains(filePath, "service.go") {
return ToUpdateServicesFile(filePath, entity)
}
if strings.Contains(filePath, "handler.go") {
return ToUpdateHandlersFile(filePath, entity)
}
// if strings.Contains(filePath, "app_module.go") {
// return ToUpdateAppModuleFile(filePath, entityName)
// }
if strings.Contains(filePath, "route.go") {
return ToUpdateRouterFile(filePath, entity)
}
if strings.Contains(filePath, "response_messages.go") {
return ToUpdateCommonResponseMessage(filePath, entity)
}
if strings.Contains(filePath, "entities.go") {
return ToUpdateEntityFile(filePath, entity)
}
return nil
}
func ToUpdateRouterFile(filePath string, entity Entity) error {
var newLine = `
// TemplateEntity CRUD API'S
templateEntityHandler := AllHandlers.TemplateEntityHandler
templateEntityV1Routes := api.Group("/v1/templateEntity")
templateEntityV1Routes.Post("/", templateEntityHandler.CreateTemplateEntity)
templateEntityV1Routes.Get("/:id", templateEntityHandler.GetTemplateEntityByID)
templateEntityV1Routes.Post("/filter", templateEntityHandler.FindTemplateEntityWithFilter)
templateEntityV1Routes.Put("/:id", templateEntityHandler.UpdateTemplateEntityById)
templateEntityV1Routes.Delete("/:id", templateEntityHandler.DeleteTemplateEntityById)
`
newLine = replaceEntityName(newLine, entity)
// Read the existing file content
data, err := os.ReadFile(filePath)
if err != nil {
return fmt.Errorf("error reading file %s: %v", filePath, err)
}
content := string(data)
handlerLine := "templateEntityHandler := AllHandlers.TemplateEntityHandler"
if !strings.Contains(content, replaceEntityName(handlerLine, entity)) {
startKeyword := "func InitializeRoutes(fiberApp *fiber.App) {"
endKeyword := "}"
content = AddNewLineToEnd(newLine, content, startKeyword, endKeyword, "", "")
}
return WriteFileInPath(filePath, content)
}
func ToUpdateCommonResponseMessage(filePath string, entity Entity) error {
var newLine = `TemplateEntityNotFoundError = "templateEntity not found"`
newLine = replaceEntityName(newLine, entity)
// Read the existing file content
data, err := os.ReadFile(filePath)
if err != nil {
return fmt.Errorf("error reading file %s: %v", filePath, err)
}
content := string(data)
startKeyword := "const ("
endKeyword := ")"
content = AddNewLineToEnd(newLine, content, startKeyword, endKeyword, fmt.Sprintf("\n //%s \n", ToUpperFirst(entity.EntityName)), "")
return WriteFileInPath(filePath, content)
}
func ToUpdateAppModuleFile(filePath string, entity Entity) error {
// Read the existing file content
data, err := os.ReadFile(filePath)
if err != nil {
return fmt.Errorf("error reading file %s: %v", filePath, err)
}
content := string(data)
entityNameUpperFirst := ToUpperFirst(entity.EntityName)
lineToAddInRepoType := fmt.Sprintf("%sRepository aggregate.%sRepository", entityNameUpperFirst, entityNameUpperFirst)
repoStartKeyword := "type Repository struct {"
endKeyword := "}"
content = AddNewLineToStart(lineToAddInRepoType, content, repoStartKeyword, endKeyword, "", "")
lineToAddInRepositories := fmt.Sprintf("%sRepository: repository.New%sRepository()", entityNameUpperFirst, entityNameUpperFirst)
lineToAddInRepositoriesStartKeyword := "var AllRepositories = Repository{"
content = AddNewLineToStart(lineToAddInRepositories, content, lineToAddInRepositoriesStartKeyword, endKeyword, "", ",")
lineToAddInServiceType := fmt.Sprintf("%sService service.%sService", entityNameUpperFirst, entityNameUpperFirst)
serviceStartKeyword := "type Service struct {"
content = AddNewLineToStart(lineToAddInServiceType, content, serviceStartKeyword, endKeyword, "", "")
lineToAddInServices := fmt.Sprintf("%sService: service.New%sService(Allrepository.%sRepository)", entityNameUpperFirst, entityNameUpperFirst, entityNameUpperFirst)
lineToAddInServicesStartKeyword := "var AllServices = Service{"
content = AddNewLineToStart(lineToAddInServices, content, lineToAddInServicesStartKeyword, endKeyword, "", ",")
lineToAddInHandlerType := fmt.Sprintf("%sHandler handler.%sHandler", entityNameUpperFirst, entityNameUpperFirst)
handlerStartKeyword := "type Handler struct {"
content = AddNewLineToStart(lineToAddInHandlerType, content, handlerStartKeyword, endKeyword, "", "")
lineToAddInHanlders := fmt.Sprintf("%sHandler: handler.New%sHandler(Allservice.%sService)", entityNameUpperFirst, entityNameUpperFirst, entityNameUpperFirst)
lineToAddInHanldersStartKeyword := "var AllHandlers = Handler{"
content = AddNewLineToStart(lineToAddInHanlders, content, lineToAddInHanldersStartKeyword, endKeyword, "", ",")
return WriteFileInPath(filePath, content)
}
func ToUpdateEntityFile(filePath string, entity Entity) error {
// Read the existing file content
data, err := os.ReadFile(filePath)
if err != nil {
return fmt.Errorf("error reading file %s: %v", filePath, err)
}
content := string(data)
entityNameUpperFirst := ToUpperFirst(entity.EntityName)
lineToAddInRepoType := fmt.Sprintf("&%s{},", entityNameUpperFirst)
repoStartKeyword := "var Entities = []interface{}{"
endKeyword := "}"
content = AddNewLineToStart(lineToAddInRepoType, content, repoStartKeyword, endKeyword, "", "")
return WriteFileInPath(filePath, content)
}
func ToUpdateRepositoriesFile(filePath string, entity Entity) error {
// Read the existing file content
data, err := os.ReadFile(filePath)
if err != nil {
return fmt.Errorf("error reading file %s: %v", filePath, err)
}
content := string(data)
entityNameUpperFirst := ToUpperFirst(entity.EntityName)
lineToAddInRepoType := fmt.Sprintf("%sRepository %sRepository", entityNameUpperFirst, entityNameUpperFirst)
repoStartKeyword := "type Repositories struct {"
endKeyword := "}"
content = AddNewLineToStart(lineToAddInRepoType, content, repoStartKeyword, endKeyword, "", "")
lineToAddInRepositories := fmt.Sprintf("%sRepository: New%sRepository(databases)", entityNameUpperFirst, entityNameUpperFirst)
lineToAddInRepositoriesStartKeyword := "allRepositories = &Repositories{"
content = AddNewLineToStart(lineToAddInRepositories, content, lineToAddInRepositoriesStartKeyword, endKeyword, "", ",")
return WriteFileInPath(filePath, content)
}
func ToUpdateServicesFile(filePath string, entity Entity) error {
// Read the existing file content
data, err := os.ReadFile(filePath)
if err != nil {
return fmt.Errorf("error reading file %s: %v", filePath, err)
}
content := string(data)
entityNameUpperFirst := ToUpperFirst(entity.EntityName)
endKeyword := "}"
lineToAddInServiceType := fmt.Sprintf("%sService %sService", entityNameUpperFirst, entityNameUpperFirst)
serviceStartKeyword := "type Services struct {"
content = AddNewLineToStart(lineToAddInServiceType, content, serviceStartKeyword, endKeyword, "", "")
lineToAddInServices := fmt.Sprintf("%sService: New%sService(AllRepository.%sRepository)", entityNameUpperFirst, entityNameUpperFirst, entityNameUpperFirst)
lineToAddInServicesStartKeyword := "allServices = &Services{"
content = AddNewLineToStart(lineToAddInServices, content, lineToAddInServicesStartKeyword, endKeyword, "", ",")
return WriteFileInPath(filePath, content)
}
func ToUpdateHandlersFile(filePath string, entity Entity) error {
// Read the existing file content
data, err := os.ReadFile(filePath)
if err != nil {
return fmt.Errorf("error reading file %s: %v", filePath, err)
}
content := string(data)
entityNameUpperFirst := ToUpperFirst(entity.EntityName)
endKeyword := "}"
lineToAddInHandlerType := fmt.Sprintf("%sHandler %sHandler", entityNameUpperFirst, entityNameUpperFirst)
handlerStartKeyword := "type Handlers struct {"
content = AddNewLineToStart(lineToAddInHandlerType, content, handlerStartKeyword, endKeyword, "", "")
lineToAddInHanlders := fmt.Sprintf("%sHandler: New%sHandler(AllServices.%sService)", entityNameUpperFirst, entityNameUpperFirst, entityNameUpperFirst)
lineToAddInHanldersStartKeyword := "allHandlers = &Handlers{"
content = AddNewLineToStart(lineToAddInHanlders, content, lineToAddInHanldersStartKeyword, endKeyword, "", ",")
return WriteFileInPath(filePath, content)
}