Use vfsgen instead of go-bindata (#7080)

* use vfsgen instead of go-bindata

* fix templates

* fix fmt

* vendor vsfgen
This commit is contained in:
Lunny Xiao 2019-06-03 05:43:47 +08:00 committed by Lauris BH
parent 8eba27c792
commit 83b90e4199
36 changed files with 1224 additions and 612 deletions

23
modules/options/main.go Normal file
View file

@ -0,0 +1,23 @@
// +build ignore
package main
import (
"log"
"net/http"
"github.com/shurcooL/vfsgen"
)
func main() {
var fsTemplates http.FileSystem = http.Dir("../../options")
err := vfsgen.Generate(fsTemplates, vfsgen.Options{
PackageName: "options",
BuildTags: "bindata",
VariableName: "Assets",
Filename: "bindata.go",
})
if err != nil {
log.Fatal("%v", err)
}
}

View file

@ -4,10 +4,8 @@
package options
//go:generate go-bindata -tags "bindata" -ignore "TRANSLATORS" -pkg "options" -o "bindata.go" ../../options/...
//go:generate go run -mod=vendor main.go
//go:generate go fmt bindata.go
//go:generate sed -i.bak s/..\/..\/options\/// bindata.go
//go:generate rm -f bindata.go.bak
type directorySet map[string][]string

View file

@ -41,7 +41,7 @@ func Dir(name string) ([]string, error) {
result = append(result, files...)
}
files, err := AssetDir(path.Join("..", "..", "options", name))
files, err := AssetDir(name)
if err != nil {
return []string{}, fmt.Errorf("Failed to read embedded directory. %v", err)
@ -52,6 +52,24 @@ func Dir(name string) ([]string, error) {
return directories.AddAndGet(name, result), nil
}
func AssetDir(dirName string) ([]string, error) {
d, err := Assets.Open(dirName)
if err != nil {
return nil, err
}
defer d.Close()
files, err := d.Readdir(-1)
if err != nil {
return nil, err
}
var results = make([]string, 0, len(files))
for _, file := range files {
results = append(results, file.Name())
}
return results, nil
}
// Locale reads the content of a specific locale from bindata or custom path.
func Locale(name string) ([]byte, error) {
return fileFromDir(path.Join("locale", name))
@ -85,5 +103,11 @@ func fileFromDir(name string) ([]byte, error) {
return ioutil.ReadFile(customPath)
}
return Asset(name)
f, err := Assets.Open(name)
if err != nil {
return nil, err
}
defer f.Close()
return ioutil.ReadAll(f)
}

23
modules/public/main.go Normal file
View file

@ -0,0 +1,23 @@
// +build ignore
package main
import (
"log"
"net/http"
"github.com/shurcooL/vfsgen"
)
func main() {
var fsPublic http.FileSystem = http.Dir("../../public")
err := vfsgen.Generate(fsPublic, vfsgen.Options{
PackageName: "public",
BuildTags: "bindata",
VariableName: "Assets",
Filename: "bindata.go",
})
if err != nil {
log.Fatal("%v", err)
}
}

View file

@ -17,10 +17,8 @@ import (
"gopkg.in/macaron.v1"
)
//go:generate go-bindata -tags "bindata" -ignore "\\.go|\\.less" -pkg "public" -o "bindata.go" ../../public/...
//go:generate go run -mod=vendor main.go
//go:generate go fmt bindata.go
//go:generate sed -i.bak s/..\/..\/public\/// bindata.go
//go:generate rm -f bindata.go.bak
// Options represents the available options to configure the macaron handler.
type Options struct {

View file

@ -7,19 +7,12 @@
package public
import (
"github.com/go-macaron/bindata"
"gopkg.in/macaron.v1"
)
// Static implements the macaron static handler for serving assets.
func Static(opts *Options) macaron.Handler {
opts.FileSystem = bindata.Static(bindata.Options{
Asset: Asset,
AssetDir: AssetDir,
AssetInfo: AssetInfo,
AssetNames: AssetNames,
Prefix: "",
})
opts.FileSystem = Assets
// we don't need to pass the directory, because the directory var is only
// used when in the options there is no FileSystem.
return opts.staticHandler("")

23
modules/templates/main.go Normal file
View file

@ -0,0 +1,23 @@
// +build ignore
package main
import (
"log"
"net/http"
"github.com/shurcooL/vfsgen"
)
func main() {
var fsTemplates http.FileSystem = http.Dir("../../templates")
err := vfsgen.Generate(fsTemplates, vfsgen.Options{
PackageName: "templates",
BuildTags: "bindata",
VariableName: "Assets",
Filename: "bindata.go",
})
if err != nil {
log.Fatal("%v", err)
}
}

View file

@ -203,3 +203,21 @@ func Mailer() *template.Template {
return templates
}
func Asset(name string) ([]byte, error) {
f, err := Assets.Open("/" + name)
if err != nil {
return nil, err
}
defer f.Close()
return ioutil.ReadAll(f)
}
func AssetNames() []string {
realFS := Assets.(vfsgen۰FS)
var results = make([]string, 0, len(realFS))
for k := range realFS {
results = append(results, k[1:])
}
return results
}

View file

@ -4,7 +4,5 @@
package templates
//go:generate go-bindata -tags "bindata" -ignore "\\.go" -pkg "templates" -o "bindata.go" ../../templates/...
//go:generate go run -mod=vendor main.go
//go:generate go fmt bindata.go
//go:generate sed -i.bak s/..\/..\/templates\/// bindata.go
//go:generate rm -f bindata.go.bak