main.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2017 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // encode-templates is a support tool for building text templates
  15. // directly into an application binary.
  16. //
  17. // It reads files from a "templates" directory, and generates
  18. // a Go source file containing base64-encoded representations of those
  19. // files. This allows these files to be directly compiled into the
  20. // executable.
  21. package main
  22. import (
  23. "bytes"
  24. "encoding/base64"
  25. "io/ioutil"
  26. "path/filepath"
  27. "strings"
  28. "text/template"
  29. )
  30. const TEMPLATES_GO = `// Copyright 2017 Google Inc. All Rights Reserved.
  31. //
  32. // Licensed under the Apache License, Version 2.0 (the "License");
  33. // you may not use this file except in compliance with the License.
  34. // You may obtain a copy of the License at
  35. //
  36. // http://www.apache.org/licenses/LICENSE-2.0
  37. //
  38. // Unless required by applicable law or agreed to in writing, software
  39. // distributed under the License is distributed on an "AS IS" BASIS,
  40. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  41. // See the License for the specific language governing permissions and
  42. // limitations under the License.
  43. package main
  44. func templates() map[string]string {
  45. return map[string]string{ {{range .TemplateStrings}}
  46. "{{.Name}}": "{{.Encoding}}",{{end}}
  47. }
  48. }`
  49. type NamedTemplateString struct {
  50. Name string // the name of the file to be generated by the template
  51. Encoding string // base64-encoded string
  52. }
  53. func main() {
  54. fileinfos, err := ioutil.ReadDir("templates")
  55. if err != nil {
  56. panic(err)
  57. }
  58. templateStrings := make([]*NamedTemplateString, 0)
  59. for _, fileinfo := range fileinfos {
  60. name := fileinfo.Name()
  61. if filepath.Ext(name) == ".tmpl" {
  62. data, _ := ioutil.ReadFile("templates/" + name)
  63. encoding := base64.StdEncoding.EncodeToString(data)
  64. templateStrings = append(templateStrings,
  65. &NamedTemplateString{
  66. Name: strings.TrimSuffix(name, ".tmpl"),
  67. Encoding: encoding})
  68. }
  69. }
  70. t, err := template.New("").Parse(TEMPLATES_GO)
  71. if err != nil {
  72. panic(err)
  73. }
  74. f := new(bytes.Buffer)
  75. err = t.Execute(f, struct{ TemplateStrings []*NamedTemplateString }{templateStrings})
  76. if err != nil {
  77. panic(err)
  78. }
  79. ioutil.WriteFile("templates.go", f.Bytes(), 0644)
  80. }