2017-11-20 00:10:04 -05:00
|
|
|
// 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.
|
|
|
|
|
2018-08-25 00:51:50 -04:00
|
|
|
package {{ .Package }} // import "miniflux.app/{{ .ImportPath }}"
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
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 }}}
|
|
|
|
`
|
|
|
|
|
2018-07-06 01:18:51 -04:00
|
|
|
var bundleTpl = template.Must(template.New("").Parse(tpl))
|
2017-11-20 00:10:04 -05:00
|
|
|
|
2018-07-06 01:18:51 -04:00
|
|
|
type Bundle struct {
|
2018-08-25 00:51:50 -04:00
|
|
|
Package string
|
|
|
|
Map string
|
|
|
|
ImportPath string
|
|
|
|
Files map[string]string
|
|
|
|
Checksums map[string]string
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2018-07-06 01:18:51 -04:00
|
|
|
func (b *Bundle) Write(filename string) {
|
|
|
|
f, err := os.Create(filename)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
bundleTpl.Execute(f, b)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2018-08-25 00:51:50 -04:00
|
|
|
func NewBundle(pkg, mapName, importPath string) *Bundle {
|
2018-07-06 01:18:51 -04:00
|
|
|
return &Bundle{
|
2018-08-25 00:51:50 -04:00
|
|
|
Package: pkg,
|
|
|
|
Map: mapName,
|
|
|
|
ImportPath: importPath,
|
|
|
|
Files: make(map[string]string),
|
|
|
|
Checksums: make(map[string]string),
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
2018-07-06 01:18:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func readFile(filename string) []byte {
|
2021-02-17 00:19:03 -05:00
|
|
|
data, err := os.ReadFile(filename)
|
2018-07-06 01:18:51 -04:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|
2017-11-20 00:10:04 -05:00
|
|
|
|
2018-07-06 01:18:51 -04:00
|
|
|
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 {
|
2018-09-22 13:22:58 -04:00
|
|
|
filename = strings.TrimSuffix(filename, path.Ext(filename))
|
2018-07-06 01:18:51 -04:00
|
|
|
return strings.Replace(filename, " ", "_", -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func glob(pattern string) []string {
|
2018-09-22 13:22:58 -04:00
|
|
|
// There is no Glob function in path package, so we have to use filepath and replace in case of Windows
|
2017-11-20 00:10:04 -05:00
|
|
|
files, _ := filepath.Glob(pattern)
|
2018-09-22 13:22:58 -04:00
|
|
|
for i := range files {
|
|
|
|
if strings.Contains(files[i], "\\") {
|
|
|
|
files[i] = strings.Replace(files[i], "\\", "/", -1)
|
|
|
|
}
|
|
|
|
}
|
2018-07-06 01:18:51 -04:00
|
|
|
return files
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateBundle(bundleFile, pkg, mapName string, srcFiles []string) {
|
2018-08-25 00:51:50 -04:00
|
|
|
bundle := NewBundle(pkg, mapName, pkg)
|
2018-07-06 01:18:51 -04:00
|
|
|
|
|
|
|
for _, srcFile := range srcFiles {
|
|
|
|
data := readFile(srcFile)
|
|
|
|
filename := stripExtension(basename(srcFile))
|
|
|
|
|
|
|
|
bundle.Files[filename] = string(data)
|
|
|
|
bundle.Checksums[filename] = checksum(data)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2018-07-06 01:18:51 -04:00
|
|
|
bundle.Write(bundleFile)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2018-07-06 01:18:51 -04:00
|
|
|
generateBundle("template/views.go", "template", "templateViewsMap", glob("template/html/*.html"))
|
|
|
|
generateBundle("template/common.go", "template", "templateCommonMap", glob("template/html/common/*.html"))
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|