Files
syncthing-arm/script/genassets.go
T

111 lines
2.0 KiB
Go
Raw Normal View History

2014-11-16 21:13:20 +01:00
// Copyright (C) 2014 The Syncthing Authors.
2014-09-29 21:43:32 +02:00
//
2015-03-07 21:36:35 +01: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,
// You can obtain one at https://mozilla.org/MPL/2.0/.
2014-06-01 22:50:14 +02:00
// +build ignore
2014-03-17 22:56:07 +01:00
package main
import (
"bytes"
"compress/gzip"
"flag"
"fmt"
2014-03-17 22:56:07 +01:00
"go/format"
"io"
"os"
"path/filepath"
"strconv"
2014-12-06 13:46:02 +01:00
"strings"
2014-03-17 22:56:07 +01:00
"text/template"
2018-05-10 06:53:39 +01:00
"time"
2014-03-17 22:56:07 +01:00
)
var tpl = template.Must(template.New("assets").Parse(`// Code generated by genassets.go - DO NOT EDIT.
package auto
2014-03-17 22:56:07 +01:00
2018-05-10 06:53:39 +01:00
const Generated int64 = {{.Generated}}
2014-09-02 13:07:33 +02:00
func Assets() map[string][]byte {
var assets = make(map[string][]byte, {{.Assets | len}})
{{range $asset := .Assets}}
assets["{{$asset.Name}}"] = {{$asset.Data}}{{end}}
2014-09-02 13:07:33 +02:00
return assets
2014-03-17 22:56:07 +01:00
}
2014-03-17 22:56:07 +01:00
`))
type asset struct {
2014-09-01 18:19:19 +02:00
Name string
Data string
2014-03-17 22:56:07 +01: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 13:46:02 +01:00
if strings.HasPrefix(filepath.Base(name), ".") {
// Skip dotfiles
return nil
}
2014-03-17 22:56:07 +01:00
if info.Mode().IsRegular() {
fd, err := os.Open(name)
if err != nil {
return err
}
var buf bytes.Buffer
gw := gzip.NewWriter(&buf)
io.Copy(gw, fd)
fd.Close()
gw.Flush()
gw.Close()
name, _ = filepath.Rel(basePath, name)
assets = append(assets, asset{
2014-09-01 18:19:19 +02:00
Name: filepath.ToSlash(name),
Data: fmt.Sprintf("[]byte(%q)", buf.String()),
2014-03-17 22:56:07 +01:00
})
}
return nil
}
}
type templateVars struct {
2018-05-10 06:53:39 +01:00
Assets []asset
Generated int64
}
2014-03-17 22:56:07 +01:00
func main() {
flag.Parse()
filepath.Walk(flag.Arg(0), walkerFor(flag.Arg(0)))
var buf bytes.Buffer
// 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
}
tpl.Execute(&buf, templateVars{
2018-05-10 06:53:39 +01:00
Assets: assets,
Generated: generated,
})
2014-03-17 22:56:07 +01:00
bs, err := format.Source(buf.Bytes())
if err != nil {
panic(err)
}
os.Stdout.Write(bs)
}