| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- // Copyright 2017 Frédéric Guillot. All rights reserved.
- // Use of this source code is governed by the Apache 2.0
- // license that can be found in the LICENSE file.
- // +build ignore
- package main
- import (
- "crypto/sha256"
- "fmt"
- "os"
- "path"
- "path/filepath"
- "strings"
- "text/template"
- )
- const tpl = `// Code generated by go generate; DO NOT EDIT.
- package {{ .Package }} // import "miniflux.app/{{ .ImportPath }}"
- var {{ .Map }} = map[string]string{
- {{ range $constant, $content := .Files }}` + "\t" + `"{{ $constant }}": ` + "`{{ $content }}`" + `,
- {{ end }}}
- var {{ .Map }}Checksums = map[string]string{
- {{ range $constant, $content := .Checksums }}` + "\t" + `"{{ $constant }}": "{{ $content }}",
- {{ end }}}
- `
- var bundleTpl = template.Must(template.New("").Parse(tpl))
- type Bundle struct {
- Package string
- Map string
- ImportPath string
- Files map[string]string
- Checksums map[string]string
- }
- func (b *Bundle) Write(filename string) {
- f, err := os.Create(filename)
- if err != nil {
- panic(err)
- }
- defer f.Close()
- bundleTpl.Execute(f, b)
- }
- func NewBundle(pkg, mapName, importPath string) *Bundle {
- return &Bundle{
- Package: pkg,
- Map: mapName,
- ImportPath: importPath,
- Files: make(map[string]string),
- Checksums: make(map[string]string),
- }
- }
- func readFile(filename string) []byte {
- data, err := os.ReadFile(filename)
- if err != nil {
- panic(err)
- }
- return data
- }
- func checksum(data []byte) string {
- return fmt.Sprintf("%x", sha256.Sum256(data))
- }
- func basename(filename string) string {
- return path.Base(filename)
- }
- func stripExtension(filename string) string {
- filename = strings.TrimSuffix(filename, path.Ext(filename))
- return strings.Replace(filename, " ", "_", -1)
- }
- func glob(pattern string) []string {
- // There is no Glob function in path package, so we have to use filepath and replace in case of Windows
- files, _ := filepath.Glob(pattern)
- for i := range files {
- if strings.Contains(files[i], "\\") {
- files[i] = strings.Replace(files[i], "\\", "/", -1)
- }
- }
- return files
- }
- func generateBundle(bundleFile, pkg, mapName string, srcFiles []string) {
- bundle := NewBundle(pkg, mapName, pkg)
- for _, srcFile := range srcFiles {
- data := readFile(srcFile)
- filename := stripExtension(basename(srcFile))
- bundle.Files[filename] = string(data)
- bundle.Checksums[filename] = checksum(data)
- }
- bundle.Write(bundleFile)
- }
- func main() {
- generateBundle("template/views.go", "template", "templateViewsMap", glob("template/html/*.html"))
- generateBundle("template/common.go", "template", "templateCommonMap", glob("template/html/common/*.html"))
- }
|