| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package templates | 5 package templates |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "bytes" | 8 "bytes" |
| 9 "io/ioutil" |
| 9 "text/template" | 10 "text/template" |
| 10 | 11 |
| 11 "mojom/generators/go/gofmt" | 12 "mojom/generators/go/gofmt" |
| 12 "mojom/generators/go/translator" | 13 "mojom/generators/go/translator" |
| 13 ) | 14 ) |
| 14 | 15 |
| 15 // goFileTmpl is the template object for a go file. | 16 // goFileTmpl is the template object for a go file. |
| 16 var goFileTmpl *template.Template | 17 var goFileTmpl *template.Template |
| 17 | 18 |
| 18 // ExecuteTemplates accepts a translator.TmplFile and returns the formatted | 19 // ExecuteTemplates accepts a translator.TmplFile and returns the formatted |
| 19 // source code for the go bindings. | 20 // source code for the go bindings. |
| 20 func ExecuteTemplates(tmplFile *translator.TmplFile) string { | 21 func ExecuteTemplates(tmplFile *translator.TmplFile) string { |
| 21 buffer := &bytes.Buffer{} | 22 buffer := &bytes.Buffer{} |
| 22 if err := goFileTmpl.ExecuteTemplate(buffer, "FileTemplate", tmplFile);
err != nil { | 23 if err := goFileTmpl.ExecuteTemplate(buffer, "FileTemplate", tmplFile);
err != nil { |
| 23 panic(err) | 24 panic(err) |
| 24 } | 25 } |
| 25 | 26 |
| 26 » src, err := gofmt.FormatGoFile(buffer.String()) | 27 » unformatted := buffer.String() |
| 28 » src, err := gofmt.FormatGoFile(unformatted) |
| 27 | 29 |
| 28 if err != nil { | 30 if err != nil { |
| 31 ioutil.WriteFile("/tmp/unformatted.go", buffer.Bytes(), 0666) |
| 29 panic(err) | 32 panic(err) |
| 30 } | 33 } |
| 31 return src | 34 return src |
| 32 } | 35 } |
| 33 | 36 |
| 34 func init() { | 37 func init() { |
| 35 // We parse the subtemplates only once. | 38 // We parse the subtemplates only once. |
| 36 goFileTmpl = template.New("GoFileTemplate") | 39 goFileTmpl = template.New("GoFileTemplate") |
| 37 | 40 |
| 38 template.Must(goFileTmpl.Parse(goFileTemplate)) | 41 template.Must(goFileTmpl.Parse(goFileTemplate)) |
| (...skipping 27 matching lines...) Expand all Loading... |
| 66 | 69 |
| 67 {{- range $enum := $fileTmpl.Enums}} | 70 {{- range $enum := $fileTmpl.Enums}} |
| 68 {{ template "Enum" $enum }} | 71 {{ template "Enum" $enum }} |
| 69 {{- end}} | 72 {{- end}} |
| 70 | 73 |
| 71 {{- range $interface := $fileTmpl.Interfaces}} | 74 {{- range $interface := $fileTmpl.Interfaces}} |
| 72 {{ template "Interface" $interface }} | 75 {{ template "Interface" $interface }} |
| 73 {{- end}} | 76 {{- end}} |
| 74 {{- end -}} | 77 {{- end -}} |
| 75 ` | 78 ` |
| OLD | NEW |