-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.go
85 lines (75 loc) · 2.29 KB
/
build.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
// +build linux windows darwin
package v2v
import (
"github.com/fsnotify/fsnotify"
"github.com/gobuffalo/envy"
"github.com/pubgo/g/xcmds"
"github.com/pubgo/g/xerror"
"github.com/pubgo/vue2vecty/vue2vecty"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
func Build() {
var templateHome = "templates"
var isMonitor = false
var transpiler = func(path string) (err error) {
defer xerror.RespErr(&err)
_dir := filepath.Dir(path)
_compo := filepath.Base(_dir)
names := strings.Split(filepath.Base(path), ".")
name := names[0]
f, err := os.Open(path)
xerror.PanicM(err, "file open error")
_c := vue2vecty.NewTranspiler(f,
xerror.PanicErr(envy.CurrentModule()).(string)+"/"+templateHome,
"_"+strings.ReplaceAll(strings.Title(name), "-", ""),
_compo)
xerror.Panic(ioutil.WriteFile(filepath.Join(_dir, name+"-render.go"), []byte(_c.Code()), 0644))
return
}
xcmds.AddCommand(func(cmd *xcmds.Command) *xcmds.Command {
cmd.Flags().StringVar(&templateHome, "dir", templateHome, "templates directory")
cmd.Flags().BoolVarP(&isMonitor, "monitor", "m", isMonitor, "is monitor mode")
return cmd
}(&xcmds.Command{
Use: "build",
Aliases: []string{"b"},
Short: "build template",
RunE: func(cmd *xcmds.Command, args []string) (err error) {
defer xerror.RespErr(&err)
watcher := xerror.PanicErr(fsnotify.NewWatcher()).(*fsnotify.Watcher)
defer watcher.Close()
xerror.Panic(filepath.Walk(templateHome, func(path string, info os.FileInfo, err error) error {
if err != nil || info.IsDir() || !strings.Contains(info.Name(), ".") {
return xerror.Wrap(err, "file walk failed")
}
names := strings.Split(info.Name(), ".")
// 检查后缀
if !strings.Contains(names[len(names)-1], "html") {
return nil
}
xerror.Panic(watcher.Add(path))
xerror.Panic(transpiler(path))
return nil
}))
for isMonitor {
select {
case event, ok := <-watcher.Events:
if ok && (event.Op == fsnotify.Write || event.Op == fsnotify.Rename) {
_file := event.Name
logger.Info().Str("file", _file).Str("operation", event.Op.String()).Msg("file change")
xerror.Panic(transpiler(_file))
xerror.Panic(watcher.Add(_file))
}
case err, ok := <-watcher.Errors:
if ok {
xerror.Panic(err)
}
}
}
return
}},
))
}