generate.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. // +build ignore
  5. package main
  6. import (
  7. "crypto/sha256"
  8. "fmt"
  9. "os"
  10. "path"
  11. "path/filepath"
  12. "strings"
  13. "text/template"
  14. )
  15. const tpl = `// Code generated by go generate; DO NOT EDIT.
  16. package {{ .Package }} // import "miniflux.app/{{ .ImportPath }}"
  17. var {{ .Map }} = map[string]string{
  18. {{ range $constant, $content := .Files }}` + "\t" + `"{{ $constant }}": ` + "`{{ $content }}`" + `,
  19. {{ end }}}
  20. var {{ .Map }}Checksums = map[string]string{
  21. {{ range $constant, $content := .Checksums }}` + "\t" + `"{{ $constant }}": "{{ $content }}",
  22. {{ end }}}
  23. `
  24. var bundleTpl = template.Must(template.New("").Parse(tpl))
  25. type Bundle struct {
  26. Package string
  27. Map string
  28. ImportPath string
  29. Files map[string]string
  30. Checksums map[string]string
  31. }
  32. func (b *Bundle) Write(filename string) {
  33. f, err := os.Create(filename)
  34. if err != nil {
  35. panic(err)
  36. }
  37. defer f.Close()
  38. bundleTpl.Execute(f, b)
  39. }
  40. func NewBundle(pkg, mapName, importPath string) *Bundle {
  41. return &Bundle{
  42. Package: pkg,
  43. Map: mapName,
  44. ImportPath: importPath,
  45. Files: make(map[string]string),
  46. Checksums: make(map[string]string),
  47. }
  48. }
  49. func readFile(filename string) []byte {
  50. data, err := os.ReadFile(filename)
  51. if err != nil {
  52. panic(err)
  53. }
  54. return data
  55. }
  56. func checksum(data []byte) string {
  57. return fmt.Sprintf("%x", sha256.Sum256(data))
  58. }
  59. func basename(filename string) string {
  60. return path.Base(filename)
  61. }
  62. func stripExtension(filename string) string {
  63. filename = strings.TrimSuffix(filename, path.Ext(filename))
  64. return strings.Replace(filename, " ", "_", -1)
  65. }
  66. func glob(pattern string) []string {
  67. // There is no Glob function in path package, so we have to use filepath and replace in case of Windows
  68. files, _ := filepath.Glob(pattern)
  69. for i := range files {
  70. if strings.Contains(files[i], "\\") {
  71. files[i] = strings.Replace(files[i], "\\", "/", -1)
  72. }
  73. }
  74. return files
  75. }
  76. func generateBundle(bundleFile, pkg, mapName string, srcFiles []string) {
  77. bundle := NewBundle(pkg, mapName, pkg)
  78. for _, srcFile := range srcFiles {
  79. data := readFile(srcFile)
  80. filename := stripExtension(basename(srcFile))
  81. bundle.Files[filename] = string(data)
  82. bundle.Checksums[filename] = checksum(data)
  83. }
  84. bundle.Write(bundleFile)
  85. }
  86. func main() {
  87. generateBundle("template/views.go", "template", "templateViewsMap", glob("template/html/*.html"))
  88. generateBundle("template/common.go", "template", "templateCommonMap", glob("template/html/common/*.html"))
  89. }