-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
239 lines (194 loc) · 5.86 KB
/
main.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package main
import (
"errors"
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"github.com/switchupcb/disgo/_gen/tools"
)
var (
downloadFlag = flag.Bool("d", false, "Downloads the latest copy of dasgo from Github.")
skipEndpoints = flag.Bool("xe", false, "Skips the generation of endpoint functions.")
)
const (
exeDir = "_gen"
// redirect `>` is not guaranteed to work, so files must be written.
filemodewrite = 0644
downloadURL = "https://github.com/switchupcb/dasgo/archive/main.zip"
inputDownload = "input/dasgo.zip"
unzippedFolder = "dasgo-10"
copygenFolder = "_gen/tools/_copygen/"
outputEndpoints = "../wrapper/endpoints.go"
outputDasgo = "../wrapper/dasgo.go"
endpointErr = "endpoint error: %w\nUse -xe to skip the generation of endpoint functions."
)
func main() {
if err := check(); err != nil {
fmt.Printf("%v", err)
os.Exit(1)
}
flag.Parse()
// download the latest copy of dasgo from Github.
if *downloadFlag {
if err := download(downloadURL, inputDownload); err != nil {
fmt.Printf("%v", err)
os.Exit(1)
}
}
// dasgo generation
absfilepath, err := filepath.Abs("input/" + unzippedFolder + "/dasgo")
if err != nil {
fmt.Printf("an error occurred determining the unzipped dasgo source code filepath.\n%v", err)
os.Exit(1)
}
if !*skipEndpoints {
if err = endpoints(absfilepath); err != nil {
fmt.Printf("%v", err)
os.Exit(1)
}
}
if err = convert(absfilepath); err != nil {
fmt.Printf("%v", err)
os.Exit(1)
}
// disgo generation
if err = generate(); err != nil {
fmt.Printf("%v", err)
os.Exit(1)
}
}
// check checks that the current working directory is `disgo/_gen`.
func check() error {
cwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("error getting the current working directory: %w", err)
}
if filepath.Base(cwd) != exeDir && filepath.Base(filepath.Dir(cwd)) != "disgo" {
return errors.New("This executable must be run from disgo/" + exeDir)
}
return nil
}
// download downloads and extracts (.zip) a file from a URL.
func download(url, output string) error {
curl := exec.Command("curl", "-L", url, "-o", output, "--create-dirs")
std, err := curl.CombinedOutput()
if err != nil {
return fmt.Errorf("error downloading file from url.\n%v", string(std))
}
unzip := exec.Command("unzip", "-o", output, "-d", "input")
std, err = unzip.CombinedOutput()
if err != nil {
return fmt.Errorf("error unzipping file.\n%v", string(std))
}
return nil
}
// endpoints generates endpoint functions from Dasgo endpoints.
func endpoints(path string) error {
inputEndpoints := filepath.Join(path, "endpoints.go")
data, err := os.ReadFile(inputEndpoints)
if err != nil {
return fmt.Errorf(endpointErr, err)
}
std, err := tools.Endpoints(data)
if err != nil {
return fmt.Errorf(endpointErr, err)
}
err = os.WriteFile(outputEndpoints, std, filemodewrite)
if err != nil {
return fmt.Errorf(endpointErr, err)
}
err = os.Remove(inputEndpoints)
if err != nil {
return fmt.Errorf(endpointErr, err)
}
return nil
}
// convert converts Dasgo objects to the Disgo standard.
func convert(abspath string) error {
dasgopath := abspath + "/..."
// xstruct
xstruct := exec.Command("tools/xstruct", "-d", dasgopath, "-p", "wrapper", "-g", "-f")
std, err := xstruct.CombinedOutput()
if err != nil {
return fmt.Errorf("xstruct error: %v", string(std))
}
// typefix
std, err = tools.TypeFix(std)
if err != nil {
return fmt.Errorf("typefix error: %w", err)
}
err = os.WriteFile(outputDasgo, std, filemodewrite)
if err != nil {
return fmt.Errorf("snowflake error: %w", err)
}
return nil
}
// generate generates Disgo code.
func generate() error {
// requests
if err := os.Chdir("../"); err != nil {
return fmt.Errorf("chdir error (send): %w", err)
}
// send
sendgen := exec.Command("copygen", "-yml", copygenFolder+"requests/request_send.yml", "-xm")
std, err := sendgen.CombinedOutput()
if err != nil {
return fmt.Errorf("copygen error (send): %v", string(std))
}
// send: ratelimit algorithm map
sendramgen := exec.Command("copygen", "-yml", copygenFolder+"requests/coverage_endpoint_map.yml", "-xm")
std, err = sendramgen.CombinedOutput()
if err != nil {
return fmt.Errorf("copygen error (send: ratelimit algorithm map): %v", string(std))
}
// send: coverage test map
sendctmgen := exec.Command("copygen", "-yml", copygenFolder+"requests/ratelimit_algorithm_map.yml", "-xm")
std, err = sendctmgen.CombinedOutput()
if err != nil {
return fmt.Errorf("copygen error (send: coverage test map): %v", string(std))
}
// event handling
handlegen := exec.Command("copygen", "-yml", copygenFolder+"events/setup.yml", "-xm")
std, err = handlegen.CombinedOutput()
if err != nil {
return fmt.Errorf("copygen error (handle): %v", string(std))
}
// sendevents
sendeventgen := exec.Command("copygen", "-yml", copygenFolder+"sendevents/setup.yml", "-xm")
std, err = sendeventgen.CombinedOutput()
if err != nil {
return fmt.Errorf("copygen error (sendevents): %v", string(std))
}
// sendevents (shard manager)
shardeventgen := exec.Command("copygen", "-yml", copygenFolder+"shard/setup.yml", "-xm")
std, err = shardeventgen.CombinedOutput()
if err != nil {
return fmt.Errorf("copygen error (shardevents): %v", string(std))
}
// sendevents (voice server)
voiceeventgen := exec.Command("copygen", "-yml", copygenFolder+"voice/setup.yml", "-xm")
std, err = voiceeventgen.CombinedOutput()
if err != nil {
return fmt.Errorf("copygen error (voice): %v", string(std))
}
// reset
if err := os.Chdir(exeDir); err != nil {
return fmt.Errorf("chdir error (generate): %w", err)
}
// clean
data, err := os.ReadFile(outputDasgo)
if err != nil {
return fmt.Errorf("clean error: %w", err)
}
std, err = tools.Clean(data)
if err != nil {
return fmt.Errorf("clean error: %w", err)
}
err = os.WriteFile(outputDasgo, std, filemodewrite)
if err != nil {
return fmt.Errorf("%w", err)
}
return nil
}