improve embed tool

This commit is contained in:
c9s 2022-05-19 10:49:26 +08:00
parent 1054dd00e1
commit fd45f801e2
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
3 changed files with 83 additions and 14 deletions

View File

@ -230,16 +230,16 @@ frontend/out/index.html: frontend/node_modules
cd frontend && yarn export cd frontend && yarn export
pkg/server/assets.go: frontend/out/index.html pkg/server/assets.go: frontend/out/index.html
go run ./util/embed -package server -output $@ $(FRONTEND_EXPORT_DIR) go run ./util/embed -package server -tag web -output $@ $(FRONTEND_EXPORT_DIR)
$(BACKTEST_REPORT_APP_DIR)/node_modules: $(BACKTEST_REPORT_APP_DIR)/node_modules:
cd $(BACKTEST_REPORT_APP_DIR) && yarn install cd $(BACKTEST_REPORT_APP_DIR) && yarn install
$(BACKTEST_REPORT_APP_DIR)/out/index.html: $(BACKTEST_REPORT_APP_DIR)/node_modules $(BACKTEST_REPORT_APP_DIR)/out/index.html: .FORCE $(BACKTEST_REPORT_APP_DIR)/node_modules
cd $(BACKTEST_REPORT_APP_DIR) && yarn build && yarn export cd $(BACKTEST_REPORT_APP_DIR) && yarn build && yarn export
pkg/backtest/assets.go: $(BACKTEST_REPORT_APP_DIR)/out/index.html pkg/backtest/assets.go: $(BACKTEST_REPORT_APP_DIR)/out/index.html
go run ./util/embed -package backtest -output $@ $(BACKTEST_REPORT_EXPORT_DIR) go run ./util/embed -package backtest -tag web -output $@ $(BACKTEST_REPORT_EXPORT_DIR)
embed: pkg/server/assets.go pkg/backtest/assets.go embed: pkg/server/assets.go pkg/backtest/assets.go

View File

@ -0,0 +1,65 @@
//go:build !web
// +build !web
package backtest
import (
"bytes"
"errors"
"net/http"
"os"
"time"
)
var assets = map[string][]byte{}
var FS = &fs{}
type fs struct{}
func (fs *fs) Open(name string) (http.File, error) {
if name == "/" {
return fs, nil
}
b, ok := assets[name]
if !ok {
return nil, os.ErrNotExist
}
return &file{name: name, size: len(b), Reader: bytes.NewReader(b)}, nil
}
func (fs *fs) Close() error { return nil }
func (fs *fs) Read(p []byte) (int, error) { return 0, nil }
func (fs *fs) Seek(offset int64, whence int) (int64, error) { return 0, nil }
func (fs *fs) Stat() (os.FileInfo, error) { return fs, nil }
func (fs *fs) Name() string { return "/" }
func (fs *fs) Size() int64 { return 0 }
func (fs *fs) Mode() os.FileMode { return 0755 }
func (fs *fs) ModTime() time.Time { return time.Time{} }
func (fs *fs) IsDir() bool { return true }
func (fs *fs) Sys() interface{} { return nil }
func (fs *fs) Readdir(count int) ([]os.FileInfo, error) {
files := []os.FileInfo{}
for name, data := range assets {
files = append(files, &file{name: name, size: len(data), Reader: bytes.NewReader(data)})
}
return files, nil
}
type file struct {
name string
size int
*bytes.Reader
}
func (f *file) Close() error { return nil }
func (f *file) Readdir(count int) ([]os.FileInfo, error) {
return nil, errors.New("readdir is not supported")
}
func (f *file) Stat() (os.FileInfo, error) { return f, nil }
func (f *file) Name() string { return f.name }
func (f *file) Size() int64 { return int64(f.size) }
func (f *file) Mode() os.FileMode { return 0644 }
func (f *file) ModTime() time.Time { return time.Time{} }
func (f *file) IsDir() bool { return false }
func (f *file) Sys() interface{} { return nil }

View File

@ -17,9 +17,9 @@ var funcs = map[string]interface{}{
} }
var ( var (
tmpl = template.Must(template.New("").Funcs(funcs).Parse(`// +build web tmpl = template.Must(template.New("").Funcs(funcs).Parse(`{{- if .Tag -}} // +build {{ .Tag }} {{- end }}
// code is generated by embed. DO NOT EDIT. // Code generated by "embed"; DO NOT EDIT.
package {{ .Package }} package {{ .Package }}
import ( import (
@ -81,19 +81,22 @@ func (f *file) ModTime() time.Time{ return time.Time{} }
func (f *file) IsDir() bool { return false } func (f *file) IsDir() bool { return false }
func (f *file) Sys() interface{} { return nil } func (f *file) Sys() interface{} { return nil }
`))) `))
)
// Embed is a helper function that embeds assets from the given directories // Embed is a helper function that embeds assets from the given directories
// into a Go source file. It is designed to be called from some generator // into a Go source file. It is designed to be called from some generator
// script, see example project to find out how it can be used. // script, see example project to find out how it can be used.
func Embed(packageName, file string, dirs ...string) error { func Embed(file string, dirs ...string) error {
var buf bytes.Buffer var buf bytes.Buffer
// Execute template // Execute template
if err := tmpl.Execute(&buf, struct{ if err := tmpl.Execute(&buf, struct {
Package string Package string
Tag string
}{ }{
Package: packageName, Package: packageName,
Tag: tag,
}); err != nil { }); err != nil {
return err return err
} }
@ -142,16 +145,17 @@ func formatBytes(s []byte) string {
return builder.String() return builder.String()
} }
var packageName string
var outputFile string
var tag string
func main() { func main() {
var packageName string flag.StringVar(&packageName, "package", "", "package name")
var outputFile string flag.StringVar(&tag, "tag", "", "build tag in the generated file")
flag.StringVar(&outputFile, "output", "assets.go", "output filename")
flag.StringVar(&packageName,"package", "", "package name")
flag.StringVar(&outputFile,"output", "assets.go", "output filename")
flag.Parse() flag.Parse()
args := flag.Args() args := flag.Args()
if err := Embed(packageName, outputFile, args...) ; err != nil { if err := Embed(outputFile, args...); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }