2014-11-16 20:13:20 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 19:43:32 +00:00
|
|
|
//
|
2015-03-07 20:36:35 +00:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-06-01 20:50:14 +00:00
|
|
|
|
2021-08-17 08:10:41 +00:00
|
|
|
//go:build ignore
|
2014-07-31 10:30:19 +00:00
|
|
|
// +build ignore
|
|
|
|
|
2014-03-17 21:56:07 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"compress/gzip"
|
|
|
|
"flag"
|
2018-04-11 21:02:14 +00:00
|
|
|
"fmt"
|
2014-03-17 21:56:07 +00:00
|
|
|
"go/format"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-05-29 06:39:46 +00:00
|
|
|
"strconv"
|
2014-12-06 12:46:02 +00:00
|
|
|
"strings"
|
2014-03-17 21:56:07 +00:00
|
|
|
"text/template"
|
2018-05-10 05:53:39 +00:00
|
|
|
"time"
|
2014-03-17 21:56:07 +00:00
|
|
|
)
|
|
|
|
|
2018-06-13 21:43:16 +00:00
|
|
|
var tpl = template.Must(template.New("assets").Parse(`// Code generated by genassets.go - DO NOT EDIT.
|
|
|
|
|
|
|
|
package auto
|
2014-03-17 21:56:07 +00:00
|
|
|
|
2020-05-25 06:51:27 +00:00
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/assets"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Assets() map[string]assets.Asset {
|
|
|
|
var ret = make(map[string]assets.Asset, {{.Assets | len}})
|
|
|
|
t := time.Unix({{.Generated}}, 0)
|
2018-05-10 05:53:39 +00:00
|
|
|
|
2015-04-04 17:06:20 +00:00
|
|
|
{{range $asset := .Assets}}
|
2020-05-25 06:51:27 +00:00
|
|
|
ret["{{$asset.Name}}"] = assets.Asset{
|
|
|
|
Content: {{$asset.Data}},
|
|
|
|
Gzipped: {{$asset.Gzipped}},
|
|
|
|
Length: {{$asset.Length}},
|
|
|
|
Filename: {{$asset.Name | printf "%q"}},
|
|
|
|
Modified: t,
|
|
|
|
}
|
|
|
|
{{end}}
|
|
|
|
return ret
|
2014-03-17 21:56:07 +00:00
|
|
|
}
|
2015-04-04 17:06:20 +00:00
|
|
|
|
2014-03-17 21:56:07 +00:00
|
|
|
`))
|
|
|
|
|
|
|
|
type asset struct {
|
2020-05-25 06:51:27 +00:00
|
|
|
Name string
|
|
|
|
Data string
|
|
|
|
Length int
|
|
|
|
Gzipped bool
|
2014-03-17 21:56:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var assets []asset
|
|
|
|
|
|
|
|
func walkerFor(basePath string) filepath.WalkFunc {
|
|
|
|
return func(name string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-12-06 12:46:02 +00:00
|
|
|
if strings.HasPrefix(filepath.Base(name), ".") {
|
|
|
|
// Skip dotfiles
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-03-17 21:56:07 +00:00
|
|
|
if info.Mode().IsRegular() {
|
2021-11-22 07:59:47 +00:00
|
|
|
data, err := os.ReadFile(name)
|
2014-03-17 21:56:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-25 06:51:27 +00:00
|
|
|
length := len(data)
|
2014-03-17 21:56:07 +00:00
|
|
|
|
|
|
|
var buf bytes.Buffer
|
2020-05-25 06:51:27 +00:00
|
|
|
gw, _ := gzip.NewWriterLevel(&buf, gzip.BestCompression)
|
|
|
|
gw.Write(data)
|
2014-03-17 21:56:07 +00:00
|
|
|
gw.Close()
|
|
|
|
|
2020-05-25 06:51:27 +00:00
|
|
|
// Only replace asset by gzipped version if it is smaller.
|
|
|
|
// In practice, this means HTML, CSS, SVG etc. get compressed,
|
|
|
|
// while PNG and WOFF files are left uncompressed.
|
|
|
|
// lib/assets detects gzip and sets headers/decompresses.
|
|
|
|
gzipped := buf.Len() < len(data)
|
|
|
|
if gzipped {
|
|
|
|
data = buf.Bytes()
|
|
|
|
}
|
|
|
|
|
2014-03-17 21:56:07 +00:00
|
|
|
name, _ = filepath.Rel(basePath, name)
|
|
|
|
assets = append(assets, asset{
|
2020-05-25 06:51:27 +00:00
|
|
|
Name: filepath.ToSlash(name),
|
|
|
|
Data: fmt.Sprintf("%q", string(data)),
|
|
|
|
Length: length,
|
|
|
|
Gzipped: gzipped,
|
2014-03-17 21:56:07 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-04 17:06:20 +00:00
|
|
|
type templateVars struct {
|
2018-05-10 05:53:39 +00:00
|
|
|
Assets []asset
|
|
|
|
Generated int64
|
2015-04-04 17:06:20 +00:00
|
|
|
}
|
|
|
|
|
2014-03-17 21:56:07 +00:00
|
|
|
func main() {
|
2018-06-26 07:14:21 +00:00
|
|
|
outfile := flag.String("o", "", "Name of output file (default stdout)")
|
2014-03-17 21:56:07 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
filepath.Walk(flag.Arg(0), walkerFor(flag.Arg(0)))
|
|
|
|
var buf bytes.Buffer
|
2018-05-29 06:39:46 +00:00
|
|
|
|
|
|
|
// Generated time is now, except if the SOURCE_DATE_EPOCH environment
|
|
|
|
// variable is set (for reproducible builds).
|
|
|
|
generated := time.Now().Unix()
|
|
|
|
if s, _ := strconv.ParseInt(os.Getenv("SOURCE_DATE_EPOCH"), 10, 64); s > 0 {
|
|
|
|
generated = s
|
|
|
|
}
|
|
|
|
|
2015-04-04 17:06:20 +00:00
|
|
|
tpl.Execute(&buf, templateVars{
|
2018-05-10 05:53:39 +00:00
|
|
|
Assets: assets,
|
2018-05-29 06:39:46 +00:00
|
|
|
Generated: generated,
|
2015-04-04 17:06:20 +00:00
|
|
|
})
|
2014-03-17 21:56:07 +00:00
|
|
|
bs, err := format.Source(buf.Bytes())
|
|
|
|
if err != nil {
|
2018-06-26 07:14:21 +00:00
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
os.Exit(1)
|
2014-03-17 21:56:07 +00:00
|
|
|
}
|
2018-06-26 07:14:21 +00:00
|
|
|
|
|
|
|
out := io.Writer(os.Stdout)
|
|
|
|
if *outfile != "" {
|
|
|
|
out, err = os.Create(*outfile)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out.Write(bs)
|
2014-03-17 21:56:07 +00:00
|
|
|
}
|