/
preamble.go
144 lines (114 loc) Β· 3.27 KB
/
preamble.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
package write
import (
"path"
"github.com/mavolin/corgi/file"
"github.com/mavolin/corgi/file/fileutil"
"github.com/mavolin/corgi/internal/meta"
)
func writePackage(ctx *ctx) {
ctx.writeln("package " + ctx.destPackage)
}
func writeImports(ctx *ctx) {
processed := make(map[string]struct{})
namespaceUsed := make(map[string]struct{})
files := make([]*file.File, len(ctx._stack))
copy(files, ctx._stack)
ctx.writeln("import (")
defer ctx.writeln(")")
writeBaseImports(ctx)
for i := 0; i < len(files); i++ {
f := files[i]
if _, ok := processed[f.Module+"/"+f.PathInModule]; ok {
continue
}
processed[f.Module+"/"+f.PathInModule] = struct{}{}
for _, use := range f.Uses {
for _, useSpec := range use.Uses {
files = append(files, useSpec.Library.Files...)
for _, dep := range useSpec.Library.Dependencies {
files = append(files, dep.Library.Files...)
}
}
}
for _, imp := range f.Imports {
for _, impSpec := range imp.Imports {
var namespace string
if impSpec.Alias != nil {
namespace = impSpec.Alias.Ident
} else {
namespace = path.Base(fileutil.Unquote(impSpec.Path))
}
if _, ok := namespaceUsed[namespace]; ok {
continue
}
namespaceUsed[namespace] = struct{}{}
if impSpec.Alias != nil {
ctx.writeln(impSpec.Alias.Ident + " " + fileutil.Quote(impSpec.Path))
} else {
ctx.writeln(fileutil.Quote(impSpec.Path))
}
}
}
}
}
func writeBaseImports(ctx *ctx) {
ctx.writeln(ctx.ident("fmt") + ` "fmt"`)
ctx.writeln(ctx.ident("io") + ` "io"`)
ctx.writeln(ctx.ident("woof") + ` "github.com/mavolin/corgi/woof"`)
}
func writeCodegenComment(ctx *ctx) {
ctx.writeln("")
ctx.writeln("// Code generated by github.com/mavolin/corgi (" + meta.Version + "). DO NOT EDIT.")
ctx.writeln("")
}
func writeGlobalCode(ctx *ctx) {
for _, code := range ctx.mainFile().GlobalCode {
for _, ln := range code.Lines {
ctx.writeln(ln.Code)
}
}
}
func writeFuncCode(ctx *ctx) {
for _, f := range ctx._stack[:len(ctx._stack)-1] {
ctx.debug("func code", f.Name)
for _, itm := range f.Scope {
c, ok := itm.(file.Code)
if !ok {
continue
}
code(ctx, c)
}
}
}
func writeFunc(ctx *ctx) {
ctx.write("func " + ctx.mainFile().Func.Name.Ident + "(" + ctx.ident("w") + " " + ctx.ioQual("Writer"))
for _, param := range ctx.mainFile().Func.Params {
ctx.write(", ")
for nameI, name := range param.Names {
if nameI > 0 {
ctx.writeln(", ")
}
ctx.write(name.Ident)
}
ctx.write(" " + param.Type.Type)
}
ctx.writeln(") error {")
defer ctx.writeln("}")
ctx.writeln(ctx.ident(ctxVar) + " := " + ctx.woofFunc("NewContext", ctx.ident("w")))
ctx.writeln("var " + ctx.ident("err") + " error")
ctx.writeln("defer func() { " + ctx.ident("err") + " = " + ctx.contextFunc("Recover") + "}()")
for _, comm := range ctx.mainFile().TopLevelComments {
mcom := fileutil.ParseMachineComment(comm)
if mcom.Namespace == "corgi" && mcom.Directive == "nonce" {
ctx.debugItem(comm, comm.Lines[0].Comment+" (used to inject script nonce attr)")
ctx.writeln(ctx.contextFunc("SetScriptNonce", mcom.Args))
ctx.hasNonce = true
break
}
}
writeLibMixins(ctx)
writeFuncCode(ctx)
scope(ctx, ctx.baseFile().Scope, false)
ctx.flushGenerate()
ctx.writeln("return " + ctx.ident("err"))
}