| 1 |
package xmlgen |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"context" |
| 6 |
"embed" |
| 7 |
"fmt" |
| 8 |
"go/format" |
| 9 |
"log" |
| 10 |
"os" |
| 11 |
"path/filepath" |
| 12 |
"strings" |
| 13 |
"text/template" |
| 14 |
|
| 15 |
"github.com/Masterminds/goutils" |
| 16 |
"github.com/Masterminds/sprig" |
| 17 |
"github.com/pkg/errors" |
| 18 |
"golang.org/x/tools/imports" |
| 19 |
) |
| 20 |
|
| 21 |
//go:embed template/*.tmpl template/**/*.tmpl |
| 22 |
var templateFS embed.FS |
| 23 |
|
| 24 |
func MustParseTemplates() *template.Template { |
| 25 |
//ext := pick(sprig.TxtFuncMap(), "ternary", "title") |
| 26 |
tpl := template.Must( |
| 27 |
template.New("gqlgen"). |
| 28 |
//Funcs(gen.Funcs). |
| 29 |
//Funcs(ext). |
| 30 |
Funcs(sprig.TxtFuncMap()). |
| 31 |
Funcs(map[string]interface{}{ |
| 32 |
"ParamName": ParamName, |
| 33 |
}). |
| 34 |
//Funcs(Funcs). |
| 35 |
ParseFS(templateFS, "template/*.tmpl", "template/**/*.tmpl"), |
| 36 |
) |
| 37 |
//return &gen.Template{ |
| 38 |
// Template: tpl, |
| 39 |
// FuncMap: gen.Funcs, |
| 40 |
//} |
| 41 |
return tpl |
| 42 |
} |
| 43 |
|
| 44 |
func NewAMIGenerator() *Generator { |
| 45 |
return &Generator{ |
| 46 |
Template: MustParseTemplates(), |
| 47 |
Formatter: GoFormatter, |
| 48 |
Templates: []TemplateGenerate{ |
| 49 |
&GenerateTemplate{ |
| 50 |
Name: "ami/actions", |
| 51 |
FormatName: "actions.go", |
| 52 |
}, |
| 53 |
&GenerateTemplate{ |
| 54 |
Name: "ami/events", |
| 55 |
FormatName: "events.go", |
| 56 |
}, |
| 57 |
}, |
| 58 |
} |
| 59 |
} |
| 60 |
func NewAGIGenerator() *Generator { |
| 61 |
return &Generator{ |
| 62 |
Template: MustParseTemplates(), |
| 63 |
Formatter: GoFormatter, |
| 64 |
Templates: []TemplateGenerate{ |
| 65 |
//&GenerateTemplate{ |
| 66 |
// Name: "agi/commands", |
| 67 |
// FormatName: "commands.go", |
| 68 |
//}, |
| 69 |
&GenerateTemplate{ |
| 70 |
Name: "ami/actions", |
| 71 |
FormatName: "ami/amimodels/actions.go", |
| 72 |
}, |
| 73 |
&GenerateTemplate{ |
| 74 |
Name: "ami/events", |
| 75 |
FormatName: "ami/amimodels/events.go", |
| 76 |
}, |
| 77 |
}, |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
type Generator struct { |
| 82 |
Files []File |
| 83 |
Template *template.Template |
| 84 |
Templates []TemplateGenerate |
| 85 |
Debug bool |
| 86 |
Formatter func(file *File) error |
| 87 |
Loader func() error |
| 88 |
} |
| 89 |
type File struct { |
| 90 |
Name string |
| 91 |
Content []byte |
| 92 |
} |
| 93 |
type WriteConfig struct { |
| 94 |
Target string |
| 95 |
DryRun bool |
| 96 |
} |
| 97 |
|
| 98 |
func (ge *Generator) Write(s WriteConfig) error { |
| 99 |
for _, v := range ge.Files { |
| 100 |
o := filepath.Join(s.Target, v.Name) |
| 101 |
// log.Println("write ", v.Name, " to ", o) |
| 102 |
if !s.DryRun { |
| 103 |
if err := os.WriteFile(o, v.Content, 0644); err != nil { |
| 104 |
return err |
| 105 |
} |
| 106 |
} |
| 107 |
if s.DryRun || ge.Debug { |
| 108 |
log.Println(o, "\n", strings.TrimSpace(string(v.Content))) |
| 109 |
} |
| 110 |
} |
| 111 |
return nil |
| 112 |
} |
| 113 |
|
| 114 |
func (ge *Generator) Generate(ctx context.Context, source interface{}) (err error) { |
| 115 |
if err = ge.init(); err != nil { |
| 116 |
return |
| 117 |
} |
| 118 |
var all []File |
| 119 |
for _, tmpl := range ge.Templates { |
| 120 |
var files []File |
| 121 |
files, err = tmpl.Generate(ctx, ge, source) |
| 122 |
if err != nil { |
| 123 |
return err |
| 124 |
} |
| 125 |
if ge.Formatter != nil { |
| 126 |
for i := range files { |
| 127 |
f := files[i] |
| 128 |
file := &files[i] |
| 129 |
if err = ge.Formatter(file); err != nil { |
| 130 |
if ge.Debug { |
| 131 |
log.Println(f.Name, err.Error(), "\n", lineNumber(string(f.Content))) |
| 132 |
} |
| 133 |
return errors.Wrapf(err, "format %v", file.Name) |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
all = append(all, files...) |
| 138 |
} |
| 139 |
ge.Files = append(ge.Files, all...) |
| 140 |
return |
| 141 |
} |
| 142 |
|
| 143 |
func (ge *Generator) init() error { |
| 144 |
if ge.Debug { |
| 145 |
log.Println("generating") |
| 146 |
} |
| 147 |
if ge.Loader != nil { |
| 148 |
if ge.Debug { |
| 149 |
log.Println("generator loading") |
| 150 |
} |
| 151 |
if err := ge.Loader(); err != nil { |
| 152 |
return err |
| 153 |
} |
| 154 |
} |
| 155 |
return nil |
| 156 |
} |
| 157 |
|
| 158 |
type TemplateGenerate interface { |
| 159 |
Generate(ctx context.Context, ge *Generator, source interface{}) ([]File, error) |
| 160 |
} |
| 161 |
type GenerateTemplate struct { |
| 162 |
Name string // template name. |
| 163 |
DeriveSource func(ctx context.Context, rootSource interface{}) interface{} |
| 164 |
Skip func(ctx context.Context, source interface{}) bool // skip condition (storage constraints or gated by a feature-flag). |
| 165 |
Format func(ctx context.Context, source interface{}) string // file name format. |
| 166 |
FormatName string // for single |
| 167 |
} |
| 168 |
|
| 169 |
func (tmpl *GenerateTemplate) Generate(ctx context.Context, ge *Generator, source interface{}) (files []File, err error) { |
| 170 |
if tmpl.DeriveSource != nil { |
| 171 |
source = tmpl.DeriveSource(ctx, source) |
| 172 |
} |
| 173 |
all, ok := source.([]interface{}) |
| 174 |
if !ok { |
| 175 |
all = []interface{}{source} |
| 176 |
} |
| 177 |
|
| 178 |
for _, source := range all { |
| 179 |
f, err := tmpl.one(ctx, ge, source) |
| 180 |
if err != nil { |
| 181 |
return nil, err |
| 182 |
} |
| 183 |
if f.Name != "" { |
| 184 |
files = append(files, f) |
| 185 |
} |
| 186 |
} |
| 187 |
return |
| 188 |
} |
| 189 |
func (tmpl *GenerateTemplate) one(ctx context.Context, ge *Generator, source interface{}) (file File, err error) { |
| 190 |
if tmpl.Skip != nil { |
| 191 |
if tmpl.Skip(ctx, source) { |
| 192 |
return |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
b := bytes.NewBuffer(nil) |
| 197 |
if err = ge.Template.ExecuteTemplate(b, tmpl.Name, source); err != nil { |
| 198 |
return file, fmt.Errorf("execute template %q: %w", tmpl.Name, err) |
| 199 |
} |
| 200 |
|
| 201 |
fn := tmpl.FormatName |
| 202 |
if tmpl.Format != nil { |
| 203 |
fn = tmpl.Format(ctx, source) |
| 204 |
} |
| 205 |
file.Name = fn |
| 206 |
file.Content = b.Bytes() |
| 207 |
if ge.Debug { |
| 208 |
log.Printf("generate %s with %T to %s", tmpl.Name, source, fn) |
| 209 |
} |
| 210 |
return |
| 211 |
} |
| 212 |
|
| 213 |
func GoFormatter(f *File) (err error) { |
| 214 |
f.Content, err = format.Source(f.Content) |
| 215 |
if err == nil { |
| 216 |
f.Content, err = imports.Process(f.Name, f.Content, nil) |
| 217 |
} |
| 218 |
return |
| 219 |
} |
| 220 |
func ReportFile(f *File) { |
| 221 |
log.Println(f.Name, "\n", lineNumber(string(f.Content))) |
| 222 |
} |
| 223 |
func lineNumber(s string) string { |
| 224 |
lines := strings.Split(s, "\n") |
| 225 |
c := strings.Builder{} |
| 226 |
for i, v := range lines { |
| 227 |
c.WriteString(fmt.Sprintf("%v: ", i)) |
| 228 |
c.WriteString(v) |
| 229 |
c.WriteRune('\n') |
| 230 |
} |
| 231 |
return c.String() |
| 232 |
} |
| 233 |
|
| 234 |
func ParamName(s string) string { |
| 235 |
s = goutils.Uncapitalize(s) |
| 236 |
switch s { |
| 237 |
case "interface": |
| 238 |
s = "iface" |
| 239 |
} |
| 240 |
return s |
| 241 |
} |