2017-11-20 06:10:04 +01: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"
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
|
2018-12-10 02:59:39 +01:00
|
|
|
"github.com/tdewolff/minify/v2"
|
|
|
|
"github.com/tdewolff/minify/v2/css"
|
|
|
|
"github.com/tdewolff/minify/v2/js"
|
2017-11-20 06:10:04 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const tpl = `// Code generated by go generate; DO NOT EDIT.
|
|
|
|
|
2018-08-25 06:51:50 +02:00
|
|
|
package {{ .Package }} // import "miniflux.app/{{ .ImportPath }}"
|
2017-11-20 06:10:04 +01: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 07:18:51 +02:00
|
|
|
var bundleTpl = template.Must(template.New("").Parse(tpl))
|
2017-11-20 06:10:04 +01:00
|
|
|
|
2018-07-06 07:18:51 +02:00
|
|
|
type Bundle struct {
|
2018-08-25 06:51:50 +02:00
|
|
|
Package string
|
|
|
|
Map string
|
|
|
|
ImportPath string
|
|
|
|
Files map[string]string
|
|
|
|
Checksums map[string]string
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2018-07-06 07:18:51 +02: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 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2018-08-25 06:51:50 +02:00
|
|
|
func NewBundle(pkg, mapName, importPath string) *Bundle {
|
2018-07-06 07:18:51 +02:00
|
|
|
return &Bundle{
|
2018-08-25 06:51:50 +02:00
|
|
|
Package: pkg,
|
|
|
|
Map: mapName,
|
|
|
|
ImportPath: importPath,
|
|
|
|
Files: make(map[string]string),
|
|
|
|
Checksums: make(map[string]string),
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
2018-07-06 07:18:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func readFile(filename string) []byte {
|
|
|
|
data, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|
2017-11-20 06:10:04 +01:00
|
|
|
|
2018-07-06 07:18:51 +02: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 19:22:58 +02:00
|
|
|
filename = strings.TrimSuffix(filename, path.Ext(filename))
|
2018-07-06 07:18:51 +02:00
|
|
|
return strings.Replace(filename, " ", "_", -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func glob(pattern string) []string {
|
2018-09-22 19:22:58 +02:00
|
|
|
// There is no Glob function in path package, so we have to use filepath and replace in case of Windows
|
2017-11-20 06:10:04 +01:00
|
|
|
files, _ := filepath.Glob(pattern)
|
2018-09-22 19:22:58 +02:00
|
|
|
for i := range files {
|
|
|
|
if strings.Contains(files[i], "\\") {
|
|
|
|
files[i] = strings.Replace(files[i], "\\", "/", -1)
|
|
|
|
}
|
|
|
|
}
|
2018-07-06 07:18:51 +02:00
|
|
|
return files
|
|
|
|
}
|
|
|
|
|
|
|
|
func concat(files []string) string {
|
|
|
|
var b strings.Builder
|
2017-11-20 06:10:04 +01:00
|
|
|
for _, file := range files {
|
2018-07-06 07:18:51 +02:00
|
|
|
b.Write(readFile(file))
|
|
|
|
}
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
2018-07-16 06:51:09 +02:00
|
|
|
func generateJSBundle(bundleFile string, bundleFiles map[string][]string, prefixes, suffixes map[string]string) {
|
2018-08-25 06:51:50 +02:00
|
|
|
bundle := NewBundle("static", "Javascripts", "ui/static")
|
2018-07-06 07:18:51 +02:00
|
|
|
m := minify.New()
|
|
|
|
m.AddFunc("text/javascript", js.Minify)
|
|
|
|
|
2018-07-16 06:51:09 +02:00
|
|
|
for name, srcFiles := range bundleFiles {
|
|
|
|
var b strings.Builder
|
|
|
|
|
|
|
|
if prefix, found := prefixes[name]; found {
|
|
|
|
b.WriteString(prefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.WriteString(concat(srcFiles))
|
|
|
|
|
|
|
|
if suffix, found := suffixes[name]; found {
|
|
|
|
b.WriteString(suffix)
|
|
|
|
}
|
|
|
|
|
|
|
|
minifiedData, err := m.String("text/javascript", b.String())
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bundle.Files[name] = minifiedData
|
|
|
|
bundle.Checksums[name] = checksum([]byte(minifiedData))
|
2018-07-06 07:18:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bundle.Write(bundleFile)
|
|
|
|
}
|
|
|
|
|
2018-07-07 20:00:39 +02:00
|
|
|
func generateCSSBundle(bundleFile string, themes map[string][]string) {
|
2018-08-25 06:51:50 +02:00
|
|
|
bundle := NewBundle("static", "Stylesheets", "ui/static")
|
2018-07-07 20:00:39 +02:00
|
|
|
m := minify.New()
|
|
|
|
m.AddFunc("text/css", css.Minify)
|
2018-07-06 07:18:51 +02:00
|
|
|
|
2018-07-07 20:00:39 +02:00
|
|
|
for theme, srcFiles := range themes {
|
|
|
|
data := concat(srcFiles)
|
|
|
|
minifiedData, err := m.String("text/css", data)
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2018-07-07 20:00:39 +02:00
|
|
|
bundle.Files[theme] = minifiedData
|
|
|
|
bundle.Checksums[theme] = checksum([]byte(minifiedData))
|
2018-07-06 07:18:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bundle.Write(bundleFile)
|
|
|
|
}
|
2017-11-20 06:10:04 +01:00
|
|
|
|
2018-07-06 07:18:51 +02:00
|
|
|
func generateBinaryBundle(bundleFile string, srcFiles []string) {
|
2018-08-25 06:51:50 +02:00
|
|
|
bundle := NewBundle("static", "Binaries", "ui/static")
|
2018-07-06 07:18:51 +02:00
|
|
|
|
|
|
|
for _, srcFile := range srcFiles {
|
|
|
|
data := readFile(srcFile)
|
|
|
|
filename := basename(srcFile)
|
|
|
|
encodedData := base64.StdEncoding.EncodeToString(data)
|
|
|
|
|
|
|
|
bundle.Files[filename] = string(encodedData)
|
|
|
|
bundle.Checksums[filename] = checksum(data)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2018-07-06 07:18:51 +02:00
|
|
|
bundle.Write(bundleFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateBundle(bundleFile, pkg, mapName string, srcFiles []string) {
|
2018-08-25 06:51:50 +02:00
|
|
|
bundle := NewBundle(pkg, mapName, pkg)
|
2018-07-06 07:18:51 +02: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 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2018-07-06 07:18:51 +02:00
|
|
|
bundle.Write(bundleFile)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2018-07-16 06:51:09 +02:00
|
|
|
generateJSBundle("ui/static/js.go", map[string][]string{
|
|
|
|
"app": []string{
|
|
|
|
"ui/static/js/dom_helper.js",
|
|
|
|
"ui/static/js/touch_handler.js",
|
|
|
|
"ui/static/js/keyboard_handler.js",
|
|
|
|
"ui/static/js/request_builder.js",
|
|
|
|
"ui/static/js/modal_handler.js",
|
2019-07-18 05:27:39 +02:00
|
|
|
"ui/static/js/app.js",
|
2018-07-16 06:51:09 +02:00
|
|
|
"ui/static/js/bootstrap.js",
|
|
|
|
},
|
2020-09-24 05:20:31 +02:00
|
|
|
"service-worker": []string{
|
|
|
|
"ui/static/js/service_worker.js",
|
2018-07-16 06:51:09 +02:00
|
|
|
},
|
|
|
|
}, map[string]string{
|
|
|
|
"app": "(function(){'use strict';",
|
|
|
|
"sw": "'use strict';",
|
|
|
|
}, map[string]string{
|
|
|
|
"app": "})();",
|
2018-07-06 07:18:51 +02:00
|
|
|
})
|
|
|
|
|
2018-07-07 20:00:39 +02:00
|
|
|
generateCSSBundle("ui/static/css.go", map[string][]string{
|
2019-09-22 04:10:08 +02:00
|
|
|
"light_serif": []string{"ui/static/css/light.css", "ui/static/css/serif.css", "ui/static/css/common.css"},
|
|
|
|
"light_sans_serif": []string{"ui/static/css/light.css", "ui/static/css/sans_serif.css", "ui/static/css/common.css"},
|
|
|
|
"dark_serif": []string{"ui/static/css/dark.css", "ui/static/css/serif.css", "ui/static/css/common.css"},
|
|
|
|
"dark_sans_serif": []string{"ui/static/css/dark.css", "ui/static/css/sans_serif.css", "ui/static/css/common.css"},
|
|
|
|
"system_serif": []string{"ui/static/css/system.css", "ui/static/css/serif.css", "ui/static/css/common.css"},
|
|
|
|
"system_sans_serif": []string{"ui/static/css/system.css", "ui/static/css/sans_serif.css", "ui/static/css/common.css"},
|
2018-07-07 20:00:39 +02:00
|
|
|
})
|
|
|
|
|
2018-07-06 07:18:51 +02:00
|
|
|
generateBinaryBundle("ui/static/bin.go", glob("ui/static/bin/*"))
|
|
|
|
|
2018-08-02 05:28:45 +02:00
|
|
|
generateBundle("database/sql.go", "database", "SqlMap", glob("database/sql/*.sql"))
|
2018-07-06 07:18:51 +02:00
|
|
|
generateBundle("template/views.go", "template", "templateViewsMap", glob("template/html/*.html"))
|
|
|
|
generateBundle("template/common.go", "template", "templateCommonMap", glob("template/html/common/*.html"))
|
|
|
|
generateBundle("locale/translations.go", "locale", "translations", glob("locale/translations/*.json"))
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|