-
Notifications
You must be signed in to change notification settings - Fork 0
/
bindatafs.go
92 lines (78 loc) · 2.3 KB
/
bindatafs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package bindatafs
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/jteeuwen/go-bindata"
"github.com/qor/admin"
)
type BindataFS struct {
Path string
ViewPaths []string
AssetFileSystem admin.AssetFSInterface
}
var AssetFS *BindataFS
func init() {
AssetFS = &BindataFS{AssetFileSystem: &admin.AssetFileSystem{}, Path: "config/admin/bindatafs"}
}
func (assetFS *BindataFS) RegisterPath(path string) error {
assetFS.ViewPaths = append(assetFS.ViewPaths, path)
return assetFS.AssetFileSystem.RegisterPath(path)
}
func (assetFS *BindataFS) Asset(name string) ([]byte, error) {
name = strings.TrimPrefix(name, "/")
if len(_bindata) > 0 {
return Asset(name)
}
return assetFS.AssetFileSystem.Asset(name)
}
func (assetFS *BindataFS) Glob(pattern string) (matches []string, err error) {
if len(_bindata) > 0 {
for key, _ := range _bindata {
if ok, err := filepath.Match(pattern, key); ok && err == nil {
matches = append(matches, key)
}
}
return matches, nil
}
return assetFS.AssetFileSystem.Glob(pattern)
}
func (assetFS *BindataFS) Compile() error {
fmt.Println("Compiling QOR templates...")
os.RemoveAll(filepath.Join(assetFS.Path, "templates"))
assetFS.copyFiles(filepath.Join(assetFS.Path, "templates"))
config := bindata.NewConfig()
config.Input = []bindata.InputConfig{
{
Path: filepath.Join(assetFS.Path, "templates"),
Recursive: true,
},
}
config.Package = "bindatafs"
config.Tags = "bindatafs"
config.Output = filepath.Join(assetFS.Path, "templates_bindatafs.go")
config.Prefix = filepath.Join(assetFS.Path, "templates")
config.NoMetadata = true
defer os.Exit(0)
return bindata.Translate(config)
}
func (assetFS *BindataFS) copyFiles(templatesPath string) {
for i := len(assetFS.ViewPaths) - 1; i >= 0; i-- {
viewPath := assetFS.ViewPaths[i]
filepath.Walk(viewPath, func(path string, info os.FileInfo, err error) error {
if err == nil {
var relativePath = strings.TrimPrefix(path, viewPath)
if info.IsDir() {
err = os.MkdirAll(filepath.Join(templatesPath, relativePath), os.ModePerm)
} else if info.Mode().IsRegular() {
if source, err := ioutil.ReadFile(path); err == nil {
err = ioutil.WriteFile(filepath.Join(templatesPath, relativePath), source, os.ModePerm)
}
}
}
return err
})
}
}