5 Star 17 Fork 4

东方星痕/harl

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
watcher.go 1.63 KB
一键复制 编辑 原始数据 按行查看 历史
东方星痕 提交于 2020-10-21 00:40 +08:00 . init
package watcher
import (
"fmt"
"github.com/radovskyb/watcher"
"harl/model"
"harl/utils"
"log"
"path"
"strings"
"time"
)
type FileWatch struct {
W *watcher.Watcher
Event chan model.Envent
config model.Config
}
func New(config model.Config) *FileWatch {
return &FileWatch{
W: watcher.New(),
config: config,
Event: make(chan model.Envent),
}
}
func (fw *FileWatch) Watcher() {
w := watcher.New()
defer w.Close()
w.FilterOps(watcher.Write, watcher.Remove)
go func() {
for {
select {
case event := <-w.Event:
fmt.Println(event) // Print the event's info.
if event.IsDir() {
continue
}
if !utils.IncludesString(fw.config.Build.Includes, path.Ext(event.Path)) {
continue
}
if strings.HasSuffix(event.Path, ".hap") {
fw.Event <- model.Envent{
Action: "reload",
Data: map[string]string{"bin": event.Path},
}
} else {
fw.Event <- model.Envent{
Action: "build",
Data: nil,
}
}
case err := <-w.Error:
log.Fatalln(err)
case <-w.Closed:
return
}
}
}()
// Watch test_folder recursively for changes.
if err := w.AddRecursive(fw.config.Build.Project); err != nil {
log.Fatalln(err)
}
for _, exclude := range fw.config.Build.Excludes {
err := w.Ignore(fmt.Sprintf("%s/%s", fw.config.Build.Project, exclude))
if err != nil {
fmt.Errorf("exclude files error: %w", err)
}
}
w.IgnoreHiddenFiles(true)
delay, _ := time.ParseDuration(fmt.Sprintf("%dms", fw.config.Build.Delay))
// Start the watching process - it'll check for changes every 100ms.
if err := w.Start(delay); err != nil {
log.Fatalln(err)
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/ystyle/harl.git
git@gitee.com:ystyle/harl.git
ystyle
harl
harl
v0.1.1

搜索帮助