genFile generates file from the specified template.
(templText string, td *GLHeader, fout string, gosrc bool)
| 43 | |
| 44 | // genFile generates file from the specified template. |
| 45 | func genFile(templText string, td *GLHeader, fout string, gosrc bool) error { |
| 46 | |
| 47 | // Parses the template |
| 48 | tmpl := template.New("tmpl") |
| 49 | tmpl, err := tmpl.Parse(templText) |
| 50 | if err != nil { |
| 51 | return err |
| 52 | } |
| 53 | |
| 54 | // Expands template to buffer |
| 55 | var buf bytes.Buffer |
| 56 | err = tmpl.Execute(&buf, &td) |
| 57 | if err != nil { |
| 58 | return err |
| 59 | } |
| 60 | text := buf.Bytes() |
| 61 | |
| 62 | // Creates output file |
| 63 | f, err := os.Create(fout) |
| 64 | if err != nil { |
| 65 | return nil |
| 66 | } |
| 67 | |
| 68 | // If requested, formats generated text as Go source |
| 69 | if gosrc { |
| 70 | src, err := format.Source(text) |
| 71 | if err != nil { |
| 72 | return err |
| 73 | } |
| 74 | text = src |
| 75 | } |
| 76 | |
| 77 | // Writes source to output file |
| 78 | _, err = f.Write(text) |
| 79 | if err != nil { |
| 80 | return err |
| 81 | } |
| 82 | return f.Close() |
| 83 | } |
| 84 | |
| 85 | // |
| 86 | // Template for glapi C file |