-
Notifications
You must be signed in to change notification settings - Fork 0
/
autobuild.go
286 lines (232 loc) · 5.79 KB
/
autobuild.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package main
import (
"bufio"
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/fsnotify/fsnotify"
)
const usage = `
Usage:
-h 显示当前帮助信息
-f 指定需要编译的main文件
-o 执行编译后的可执行文件名
-r 是否搜索子目录,默认为true
`
var defaultExtensions = []string{".go"}
var (
eventTime = make(map[string]int64)
scheduleTime time.Time
)
// 监听器
type watch struct {
appName string // 输出的程序文件
appCmd *exec.Cmd // appName的命令行包装引用,方便结束其进程。
goCmdArgs []string // 传递给go build的参数
}
func main() {
// 初始化flag
var showHelp, recursive bool
var outputName, mainFiles string
flag.BoolVar(&showHelp, "h", false, "显示帮助信息")
flag.BoolVar(&recursive, "r", true, "是否查找子目录")
flag.StringVar(&outputName, "o", "", "指定输出名称")
flag.StringVar(&mainFiles, "f", "", "指定需要编译的文件")
flag.Usage = func() {
fmt.Printf(usage)
}
flag.Parse()
if showHelp {
flag.Usage()
return
}
wd, err := os.Getwd()
if err != nil {
ColorLog("[ERRO] 获取当前工作目录时,发生错误: [%s] \n", err)
return
}
// 初始化goCmd的参数
args := []string{"build", "-o", outputName}
if len(mainFiles) > 0 {
args = append(args, mainFiles)
}
w := &watch{
appName: getAppName(outputName, wd),
goCmdArgs: args,
}
w.watcher(recursivePath(recursive, append(flag.Args(), wd)))
go w.build()
done := make(chan bool)
<-done
}
// 读取.extensions文件,用于设置需要监听的文件后缀
func watchExtensions() []string {
var exts []string
f, err := os.OpenFile(".extensions", os.O_RDONLY, 0666)
if err != nil && os.IsNotExist(err) {
return defaultExtensions
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
exts = append(exts, line)
}
return exts
}
func (w *watch) watcher(paths []string) {
ColorLog("[TRAC] 初始化文件监视器... \n")
//初始化监听器
watcher, err := fsnotify.NewWatcher()
if err != nil {
ColorLog("[ERRO] 初始化监视器失败: [%s] \n", err)
os.Exit(2)
}
go func() {
for {
select {
case event := <-watcher.Events:
build := true
if !checkIfWatch(event.Name) {
continue
}
if event.Op&fsnotify.Chmod == fsnotify.Chmod {
ColorLog("[SKIP] 跳过 [%s] \n", event)
continue
}
mt := w.getFileModTime(event.Name)
if t := eventTime[event.Name]; mt == t {
ColorLog("[SKIP] 跳过 [%s] \n", event.String())
build = false
}
eventTime[event.Name] = mt
if build {
go func() {
scheduleTime = time.Now().Add(time.Second)
for {
time.Sleep(scheduleTime.Sub(time.Now()))
if time.Now().After(scheduleTime) {
break
}
return
}
ColorLog("[INFO] 触发编译事件: <%s> \n", event)
w.build()
}()
}
case err := <-watcher.Errors:
ColorLog("[ERRO] 监控失败 [%s] \n", err)
}
}
}()
for _, path := range paths {
ColorLog("[TRAC] 监视文件夹: (%s) \n", path)
err = watcher.Add(path)
if err != nil {
ColorLog("[ERRO] 监视文件夹失败: [%s] \n", err)
os.Exit(2)
}
}
}
// 开始编译代码
func (w *watch) build() {
ColorLog("[TRAC] 编译代码... \n")
goCmd := exec.Command("go", w.goCmdArgs...)
goCmd.Stderr = os.Stderr
goCmd.Stdout = os.Stdout
if err := goCmd.Run(); err != nil {
ColorLog("[ERRO] 编译失败: [%s] \n", err)
return
}
ColorLog("[SUCC] 编译成功... \n")
w.restart()
}
func (w *watch) restart() {
defer func() {
if err := recover(); err != nil {
fmt.Println("Kill.recover -> ", err)
}
}()
if w.appCmd != nil && w.appCmd.Process != nil {
ColorLog("[TRAC] 终止旧进程... \n")
if err := w.appCmd.Process.Kill(); err != nil {
ColorLog("[ERRO] 终止进程失败 [%s] ...\n", err)
}
ColorLog("[TRAC] 旧进程被终止! \n")
}
ColorLog("[TRAC] 重新启动 [%s] \n", w.appName)
if strings.Index(w.appName, "./") == -1 {
w.appName = "./" + w.appName
}
ColorLog("[SUCC] 启动新进程... \n")
w.appCmd = exec.Command(w.appName)
w.appCmd.Stderr = os.Stderr
w.appCmd.Stdout = os.Stdout
if err := w.appCmd.Start(); err != nil {
ColorLog("[ERRO] 启动进程时出错: [%s] \n", err)
}
}
func checkIfWatch(name string) bool {
exts := watchExtensions()
if len(exts) > 0 {
for _, s := range exts {
if strings.HasSuffix(name, s) {
return true
}
}
}
return false
}
func (w *watch) getFileModTime(path string) int64 {
path = strings.Replace(path, "\\", "/", -1)
f, err := os.Open(path)
if err != nil {
ColorLog("[ERRO] 文件打开失败 [%s]\n", err)
return time.Now().Unix()
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
ColorLog("[ERRO] 获取不到文件信息 [%s]\n", err)
return time.Now().Unix()
}
return fi.ModTime().Unix()
}
func getAppName(outputName, wd string) string {
if len(outputName) == 0 {
outputName = filepath.Base(wd)
}
if runtime.GOOS == "windows" && !strings.HasSuffix(outputName, ".exe") {
outputName += ".exe"
}
if strings.IndexByte(outputName, '/') < 0 || strings.IndexByte(outputName, filepath.Separator) < 0 {
}
return outputName
}
// 根据recursive值确定是否递归查找paths每个目录下的子目录。
func recursivePath(recursive bool, paths []string) []string {
if !recursive {
return paths
}
var ret []string
walk := func(path string, fi os.FileInfo, err error) error {
if err != nil {
ColorLog("[ERRO] 遍历监视目录错误: [%s] \n", err)
}
if fi.IsDir() && strings.Index(path, "/.") < 0 {
ret = append(ret, path)
}
return nil
}
for _, path := range paths {
if err := filepath.Walk(path, walk); err != nil {
ColorLog("[ERRO] 遍历监视目录错误: [%s] \n", err)
}
}
return ret
}