-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate_components_set.go
191 lines (161 loc) · 4.86 KB
/
generate_components_set.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
package main
import (
"bytes"
"fmt"
"go/format"
"log"
"os"
"strings"
"github.com/snowmerak/lux/v3/parser"
"github.com/urfave/cli/v2"
"golang.org/x/tools/imports"
)
const (
ComponentsSetDirectory = "./gen/components"
ComponentsSetPackage = "components"
)
func generateComponentsSetCommand(ctx *cli.Context) error {
if err := os.MkdirAll(ComponentsSetDirectory, os.ModePerm); err != nil {
return fmt.Errorf("failed to create components set directory: %w", err)
}
ps := parser.New()
if err := ps.ParseFromRoot(); err != nil {
return fmt.Errorf("failed to parse module: %w", err)
}
keys := map[string]struct{}{}
for key := range ps.Constructors {
keys[key] = struct{}{}
}
for key := range ps.Updaters {
keys[key] = struct{}{}
}
writer := bytes.Buffer{}
for name := range keys {
log.Printf("Generating %s\n", name)
writer.Reset()
cons := ps.Constructors[name]
if cons == nil {
cons = []parser.Component{}
}
upds := ps.Updaters[name]
if upds == nil {
upds = []parser.Component{}
}
if err := buildComponentSet(&writer, name, cons, upds); err != nil {
return fmt.Errorf("failed to build component set: %w", err)
}
file, err := prettyFormat(writer.Bytes())
if err != nil {
return fmt.Errorf("failed to format source: %w", err)
}
filePath := fmt.Sprintf("%s/%s%s", ComponentsSetDirectory, name, GeneratedFileSuffix)
if err := os.WriteFile(filePath, file, os.ModePerm); err != nil {
return fmt.Errorf("failed to write file: %w", err)
}
log.Printf("Generated %s\n", filePath)
}
return nil
}
func buildComponentSet(writer *bytes.Buffer, setName string, cons []parser.Component, upds []parser.Component) error {
pkgName := map[string]string{}
pkgPathMap := map[string]string{}
for i, comp := range cons {
if _, ok := pkgName[comp.PackageName]; !ok {
pkgName[comp.PackageName] = comp.PackagePath
pkgPathMap[comp.PackagePath] = comp.PackageName
continue
}
if pkgName[comp.PackageName] == comp.PackagePath {
continue
}
comp.PackageName = fmt.Sprintf("%s%d", comp.PackageName, i)
pkgName[comp.PackageName] = comp.PackagePath
pkgPathMap[comp.PackagePath] = comp.PackageName
}
for i, comp := range upds {
if _, ok := pkgName[comp.PackageName]; !ok {
pkgName[comp.PackageName] = comp.PackagePath
pkgPathMap[comp.PackagePath] = comp.PackageName
continue
}
if pkgName[comp.PackageName] == comp.PackagePath {
continue
}
comp.PackageName = fmt.Sprintf("%s%d", comp.PackageName, i)
pkgName[comp.PackageName] = comp.PackagePath
pkgPathMap[comp.PackagePath] = comp.PackageName
}
writer.WriteString("package ")
writer.WriteString(ComponentsSetPackage)
writer.WriteString("\n\n")
writer.WriteString("import (\n")
for name, path := range pkgName {
writer.WriteString("\t")
writer.WriteString(name)
writer.WriteString(" \"")
writer.WriteString(path)
writer.WriteString("\"\n")
}
writer.WriteString(")\n\n")
structName := strings.ToUpper(setName[:1]) + setName[1:]
writer.WriteString("type ")
writer.WriteString(structName)
writer.WriteString(" struct {\n")
writer.WriteString("\tcons []any\n")
writer.WriteString("\tupds []any\n")
writer.WriteString("\n}\n\n")
writer.WriteString("func New")
writer.WriteString(structName)
writer.WriteString("() *")
writer.WriteString(structName)
writer.WriteString(" {\n")
writer.WriteString("\treturn &")
writer.WriteString(structName)
writer.WriteString("{\n")
writer.WriteString("\t\tcons: []any{\n")
for _, comp := range cons {
writer.WriteString("\t\t\t")
writer.WriteString(comp.PackageName)
writer.WriteString(".")
writer.WriteString(comp.FunctionName)
writer.WriteString(",\n")
}
writer.WriteString("\t\t},\n")
writer.WriteString("\t\tupds: []any{\n")
for _, comp := range upds {
writer.WriteString("\t\t\t")
writer.WriteString(comp.PackageName)
writer.WriteString(".")
writer.WriteString(comp.FunctionName)
writer.WriteString(",\n")
}
writer.WriteString("\t\t},\n")
writer.WriteString("\t}\n")
writer.WriteString("}\n\n")
writer.WriteString("func (s *")
writer.WriteString(structName)
writer.WriteString(") Constructors() []any {\n")
writer.WriteString("\tclone := make([]any, len(s.cons))\n")
writer.WriteString("\tcopy(clone, s.cons)\n")
writer.WriteString("\treturn clone\n")
writer.WriteString("}\n\n")
writer.WriteString("func (s *")
writer.WriteString(structName)
writer.WriteString(") Updaters() []any {\n")
writer.WriteString("\tclone := make([]any, len(s.upds))\n")
writer.WriteString("\tcopy(clone, s.upds)\n")
writer.WriteString("\treturn clone\n")
writer.WriteString("}\n\n")
return nil
}
func prettyFormat(file []byte) ([]byte, error) {
file, err := format.Source(file)
if err != nil {
return nil, fmt.Errorf("failed to format source: %w", err)
}
file, err = imports.Process("", file, nil)
if err != nil {
return nil, fmt.Errorf("failed to format imports: %w", err)
}
return file, nil
}