2014-11-16 13:13:20 -07:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 12:43:32 -07:00
|
|
|
//
|
2015-03-07 13:36:35 -07: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-08 23:52:18 -07:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-06-01 13:50:14 -07:00
|
|
|
|
2014-07-31 03:30:19 -07:00
|
|
|
// +build ignore
|
|
|
|
|
2014-03-17 14:56:07 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"compress/gzip"
|
|
|
|
"flag"
|
2018-04-11 14:02:14 -07:00
|
|
|
"fmt"
|
2014-03-17 14:56:07 -07:00
|
|
|
"go/format"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2014-12-06 05:46:02 -07:00
|
|
|
"strings"
|
2014-03-17 14:56:07 -07:00
|
|
|
"text/template"
|
|
|
|
)
|
|
|
|
|
|
|
|
var tpl = template.Must(template.New("assets").Parse(`package auto
|
|
|
|
|
2014-09-02 04:07:33 -07:00
|
|
|
func Assets() map[string][]byte {
|
2015-04-04 10:06:20 -07:00
|
|
|
var assets = make(map[string][]byte, {{.Assets | len}})
|
|
|
|
{{range $asset := .Assets}}
|
2018-04-11 14:02:14 -07:00
|
|
|
assets["{{$asset.Name}}"] = {{$asset.Data}}{{end}}
|
2014-09-02 04:07:33 -07:00
|
|
|
return assets
|
2014-03-17 14:56:07 -07:00
|
|
|
}
|
2015-04-04 10:06:20 -07:00
|
|
|
|
2014-03-17 14:56:07 -07:00
|
|
|
`))
|
|
|
|
|
|
|
|
type asset struct {
|
2014-09-01 09:19:19 -07:00
|
|
|
Name string
|
|
|
|
Data string
|
2014-03-17 14:56:07 -07: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 05:46:02 -07:00
|
|
|
if strings.HasPrefix(filepath.Base(name), ".") {
|
|
|
|
// Skip dotfiles
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-03-17 14:56:07 -07: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 09:19:19 -07:00
|
|
|
Name: filepath.ToSlash(name),
|
2018-04-11 14:02:14 -07:00
|
|
|
Data: fmt.Sprintf("%#v", buf.Bytes()), // "[]byte{0x00, 0x01, ...}"
|
2014-03-17 14:56:07 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-04 10:06:20 -07:00
|
|
|
type templateVars struct {
|
2017-02-05 10:49:57 -07:00
|
|
|
Assets []asset
|
2015-04-04 10:06:20 -07:00
|
|
|
}
|
|
|
|
|
2014-03-17 14:56:07 -07:00
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
filepath.Walk(flag.Arg(0), walkerFor(flag.Arg(0)))
|
|
|
|
var buf bytes.Buffer
|
2015-04-04 10:06:20 -07:00
|
|
|
tpl.Execute(&buf, templateVars{
|
2017-02-05 10:49:57 -07:00
|
|
|
Assets: assets,
|
2015-04-04 10:06:20 -07:00
|
|
|
})
|
2014-03-17 14:56:07 -07:00
|
|
|
bs, err := format.Source(buf.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
os.Stdout.Write(bs)
|
|
|
|
}
|