-
Notifications
You must be signed in to change notification settings - Fork 4
/
ast.go
205 lines (187 loc) · 5.08 KB
/
ast.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
package utility
import (
"bytes"
"embed"
"fmt"
"go/ast"
"go/format"
"go/parser"
"go/token"
"path/filepath"
"strings"
"github.com/gookit/color"
)
func ImportPackage(node *ast.File, projectModule, packageName string) {
path := filepath.Join(projectModule, "domain", packageName)
importSpec := &ast.ImportSpec{
Path: &ast.BasicLit{
Kind: token.STRING,
Value: fmt.Sprintf(`"%v"`, path),
},
}
importDecl := &ast.GenDecl{
Tok: token.IMPORT,
Lparen: token.Pos(1), // for grouping
Specs: []ast.Spec{importSpec},
}
// Check if there are existing imports, and if so, add to them
found := false
for _, decl := range node.Decls {
genDecl, ok := decl.(*ast.GenDecl)
if ok && genDecl.Tok == token.IMPORT {
genDecl.Specs = append(genDecl.Specs, importSpec)
found = true
break
}
}
// If no import declaration exists, add the new one to Decls
if !found {
node.Decls = append([]ast.Decl{importDecl}, node.Decls...)
}
}
func AddAnotherFxOptionsInModule(path, module, projectModule string) string {
fset := token.NewFileSet()
node, err := parser.ParseFile(fset, path, nil, parser.ParseComments)
if err != nil {
fmt.Println(err)
}
ImportPackage(node, projectModule, module)
// Traverse the AST and find the fx.Options call
ast.Inspect(node, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.CallExpr:
if sel, ok := x.Fun.(*ast.SelectorExpr); ok {
if sel.Sel.Name == "Options" {
x.Args = append(x.Args, []ast.Expr{
ast.NewIdent(module + ".Module"),
}...)
}
}
}
return true
})
// Add the source code in buffer
var buf bytes.Buffer
if err := format.Node(&buf, fset, node); err != nil {
fmt.Println(err)
}
formattedCode := buf.String()
providerToInsert := fmt.Sprintf("%v.Module,", module)
formattedCode = strings.Replace(formattedCode, providerToInsert, "\n\t"+providerToInsert, 1)
return formattedCode
}
func GetFunctionDeclarations(path string, templatesFS embed.FS) []string {
fset := token.NewFileSet()
f, err := templatesFS.Open(path)
if err != nil {
color.Redln(err)
}
node, err := parser.ParseFile(fset, "", f, parser.ParseComments)
if err != nil {
fmt.Println(err)
}
var functions []string
ast.Inspect(node, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.FuncDecl:
functions = append(functions, x.Name.Name)
}
return true
})
return functions
}
func AddAnotherFxProviderInModule(path, provider string) string {
fset := token.NewFileSet()
node, err := parser.ParseFile(fset, path, nil, parser.ParseComments)
if err != nil {
fmt.Println(err)
}
// Traverse the AST and find the fx.Options call
ast.Inspect(node, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.CallExpr:
if sel, ok := x.Fun.(*ast.SelectorExpr); ok {
if sel.Sel.Name == "Options" {
x.Args = append(x.Args, &ast.CallExpr{
Fun: &ast.SelectorExpr{
X: ast.NewIdent("fx"),
Sel: ast.NewIdent("Provide"),
},
Args: []ast.Expr{
ast.NewIdent(provider),
},
Rparen: token.Pos(1),
})
}
}
}
return true
})
// Add the source code in buffer
var buf bytes.Buffer
if err := format.Node(&buf, fset, node); err != nil {
fmt.Println(err)
}
formattedCode := buf.String()
formattedCode = strings.Replace(formattedCode, provider, "\n\t"+provider, 1)
return formattedCode
}
func AddListOfProvideInFxOptions(path string, providerList []string) string {
fset := token.NewFileSet()
node, err := parser.ParseFile(fset, path, nil, parser.ParseComments)
if err != nil {
fmt.Println(err)
}
// Track existing providers
existingProviders := make(map[string]any)
// Traverse the AST and find the fx.Options call
ast.Inspect(node, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.CallExpr:
if sel, ok := x.Fun.(*ast.SelectorExpr); ok && sel.Sel.Name == "Options" {
// Check existing arguments in fx.Options
for _, arg := range x.Args {
if callExpr, ok := arg.(*ast.CallExpr); ok {
if selExpr, ok := callExpr.Fun.(*ast.SelectorExpr); ok && selExpr.Sel.Name == "Provide" {
if len(callExpr.Args) > 0 {
if ident, ok := callExpr.Args[0].(*ast.Ident); ok {
existingProviders[ident.Name] = struct{}{}
}
}
}
}
}
// Add new providers
for _, provider := range providerList {
if _, exists := existingProviders[provider]; !exists {
x.Args = append(x.Args, &ast.CallExpr{
Fun: &ast.SelectorExpr{
X: ast.NewIdent("fx"),
Sel: ast.NewIdent("Provide"),
},
Args: []ast.Expr{
ast.NewIdent(provider),
},
Rparen: token.Pos(1),
})
}
}
}
}
return true
})
// Add the source code in buffer
var buf bytes.Buffer
if err := format.Node(&buf, fset, node); err != nil {
fmt.Println(err)
}
formattedCode := buf.String()
for _, provider := range providerList {
if _, exists := existingProviders[provider]; exists {
continue
}
providerToInsert := fmt.Sprintf("fx.Provide(%v)", provider)
formattedCode = strings.Replace(formattedCode, providerToInsert, "\n\t\t"+providerToInsert, -1)
}
return formattedCode
}