From 222272b75cd3936a4d984eb07f504a359377df98 Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Mon, 17 Mar 2014 22:56:07 +0100 Subject: [PATCH] Faster and more portable assets generator --- assets.sh | 27 -------------- build.sh | 2 +- cmd/assets/assets.go | 86 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 28 deletions(-) delete mode 100755 assets.sh create mode 100644 cmd/assets/assets.go diff --git a/assets.sh b/assets.sh deleted file mode 100755 index 76cfea21e..000000000 --- a/assets.sh +++ /dev/null @@ -1,27 +0,0 @@ -#!/usr/bin/env bash - -cat < auto/gui.files.go + go run cmd/assets/assets.go gui > auto/gui.files.go go get -d } diff --git a/cmd/assets/assets.go b/cmd/assets/assets.go new file mode 100644 index 000000000..081a11216 --- /dev/null +++ b/cmd/assets/assets.go @@ -0,0 +1,86 @@ +package main + +import ( + "bytes" + "compress/gzip" + "flag" + "fmt" + "go/format" + "io" + "os" + "path/filepath" + "text/template" +) + +var tpl = template.Must(template.New("assets").Parse(`package auto + +import ( + "bytes" + "compress/gzip" + "encoding/hex" + "io/ioutil" +) + +var Assets = make(map[string][]byte) + +func init() { + var bs []byte + var gr *gzip.Reader +{{range $asset := .assets}} + bs, _ = hex.DecodeString("{{$asset.HexData}}") + gr, _ = gzip.NewReader(bytes.NewBuffer(bs)) + bs, _ = ioutil.ReadAll(gr) + Assets["{{$asset.Name}}"] = bs +{{end}} +} +`)) + +type asset struct { + Name string + HexData string +} + +var assets []asset + +func walkerFor(basePath string) filepath.WalkFunc { + return func(name string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + 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{ + Name: name, + HexData: fmt.Sprintf("%x", buf.Bytes()), + }) + } + + return nil + } +} + +func main() { + flag.Parse() + + filepath.Walk(flag.Arg(0), walkerFor(flag.Arg(0))) + var buf bytes.Buffer + tpl.Execute(&buf, map[string][]asset{"assets": assets}) + bs, err := format.Source(buf.Bytes()) + if err != nil { + panic(err) + } + os.Stdout.Write(bs) +}