-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathtscodegen.go
147 lines (131 loc) · 3.83 KB
/
tscodegen.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
package main
import (
"strings"
)
func (gc *GenerousContext) GenerateTsCode(outPath string) error {
gc.Task("Generating typescript bindings")
doc := gc.NewPathDoc(outPath)
doc.Line("")
doc.Line("// These bindings were generated by generous")
doc.Line("// See <https://docs.itch.ovh/butlerd/master/> for a human-friendly documentation")
doc.Line("")
doc.Line("import { createRequest, createNotification } from %#v;", "butlerd")
scope := newScope(gc)
must(scope.Assimilate("github.com/itchio/butler/butlerd", "types.go"))
must(scope.Assimilate("github.com/itchio/go-itchio", "types.go"))
must(scope.Assimilate("github.com/itchio/dash", "types.go"))
must(scope.Assimilate("github.com/itchio/ox", "runtime.go"))
must(scope.Assimilate("github.com/itchio/butler/installer/bfs", "receipt.go"))
bindType := func(entry *Entry) {
doc.Line("")
doc.Line("/**")
switch entry.kind {
case EntryKindParams:
doc.Line(" * Params for %s", entry.name)
case EntryKindResult:
params := scope.FindEntry(strings.TrimSuffix(entry.typeName, "Result") + "Params")
doc.Line(" * Result for %s", params.name)
case EntryKindNotification:
doc.Line(" * Payload for %s", entry.name)
default:
if len(entry.doc) == 0 {
doc.Line(" * undocumented")
} else {
for _, line := range entry.doc {
doc.Line(" * %s", line)
}
}
}
doc.Line(" */")
switch entry.typeKind {
case EntryTypeKindStruct:
doc.Line("export interface %s {", entry.typeName)
if len(entry.structFields) == 0 {
doc.Line(" // no fields")
} else {
for _, sf := range entry.structFields {
if len(sf.doc) == 0 {
doc.Line(" /** undocumented */")
} else if len(sf.doc) == 1 {
doc.Line(" /** %s */", sf.doc[0])
} else {
doc.Line(" /**")
for _, line := range sf.doc {
doc.Line(" * %s", line)
}
doc.Line(" */")
}
var optionalMarker = ""
if sf.optional {
optionalMarker = "?"
}
doc.Line(" %s%s: %s;", sf.name, optionalMarker, sf.typeString)
}
}
doc.Line("}")
case EntryTypeKindEnum:
doc.Line("export enum %s {", entry.typeName)
for _, val := range entry.enumValues {
for _, line := range val.doc {
doc.Line(" // %s", line)
}
// special case for "386", woo
name := val.name
if strings.ContainsAny(name[0:1], "0123456789") {
name = "_" + name
}
doc.Line(" %s = %s,", name, val.value)
}
doc.Line("}")
case EntryTypeKindAlias:
doc.Line("export type %s = %s;", entry.typeName, typeToString(entry.typeSpec.Type))
}
}
for _, category := range scope.categoryList {
cat := scope.categories[category]
for _, entry := range cat.entries {
bindType(entry)
switch entry.kind {
case EntryKindResult:
paramsTypeName := strings.TrimSuffix(entry.typeName, "Result") + "Params"
resultTypeName := entry.typeName
params := scope.FindEntry(paramsTypeName)
method := params.name
symbolName := strings.Replace(method, ".", "", -1)
doc.Line("")
doc.Line("/**")
if len(params.doc) == 0 {
doc.Line(" * undocumented")
} else {
for _, line := range params.doc {
doc.Line(" * %s", line)
}
}
doc.Line(" */")
doc.Line("export const %s = createRequest<", symbolName)
doc.Line(" %s,", paramsTypeName)
doc.Line(" %s", resultTypeName)
doc.Line(">(%#v);", method)
case EntryKindNotification:
method := entry.name
symbolName := strings.Replace(method, ".", "", -1)
doc.Line("")
doc.Line("")
doc.Line("/**")
if len(entry.doc) == 0 {
doc.Line(" * undocumented")
} else {
for _, line := range entry.doc {
doc.Line(" * %s", line)
}
}
doc.Line(" */")
doc.Line("export const %s = ", symbolName)
doc.Line(" createNotification<%s>(%#v);", entry.typeName, method)
}
}
}
doc.Commit("")
doc.Write()
return nil
}