From a280492aaab4f0690fab57b10caecf595755c5f3 Mon Sep 17 00:00:00 2001 From: seekwe Date: Tue, 26 Jan 2021 14:37:31 +0800 Subject: [PATCH] perf: optimized to listen to multiple types --- app/root/config.go | 21 +++++++++++---------- app/watch/start.go | 10 ++++++++++ app/watch/task.go | 14 ++++++++++---- app/watch/var.go | 1 + util/util.go | 2 +- util/util_win.go | 4 ++-- 6 files changed, 35 insertions(+), 17 deletions(-) diff --git a/app/root/config.go b/app/root/config.go index c1dbbe0..0748373 100644 --- a/app/root/config.go +++ b/app/root/config.go @@ -82,26 +82,27 @@ monitor: # 命令 command: # 开启监听的同时会后台执行的命令,可以放置一些初始化命令 - startupExec: - - go version + # startupExec: + # - go version # 监听的文件有更改会执行的命令,不支持复杂的命令,如需要请写成脚本调用 # 支持变量占位符,{{file}} {{ext}} {{changed}} # 支持不同平台执行不同命令,如 Windows 下才执行 dir:win@dir,或者 Linux 下:linux@ls -a - exec: + # exec: + # - echo "Ok" + + # 自定义不同类型文件执行命令 + # 上面的 exec 是属于全部文件通用的,如果想不同文件更新执行不同指令可以用 exec+文件后缀(首字母大写) 来设置,如: + execGo: - go build -o %s - ./%s + # execPhp: + # - echo "is php" + # 开启监听后自动执行一次上面 exec 配置的全部命令 startup: true - # 自定义不同类型文件执行命令 - # 上面的 exec 是属于全部文件通用的,如果想不同文件更新执行不同指令可以用 exec+文件后缀(首字母大写) 来设置,如: - #execGo: - # - echo "is go" - #execPhp: - # - echo "is php" - # 本地静态服务器 http: # 类型: vue-run, vue-spa, web, 留空表示不启动 diff --git a/app/watch/start.go b/app/watch/start.go index 8a295f0..feb4a55 100644 --- a/app/watch/start.go +++ b/app/watch/start.go @@ -1,9 +1,11 @@ package watch import ( + "github.com/sohaha/zlsgo/zstring" "os" "os/exec" "os/signal" + "strings" "syscall" "github.com/mitchellh/go-homedir" @@ -143,6 +145,14 @@ func start() { }() signal.Notify(signalChan, os.Interrupt, os.Kill, syscall.SIGINT, syscall.SIGTERM) + keyword := "command.exec" + for _, s := range v.AllKeys() { + if s != keyword && strings.HasPrefix(s, keyword) { + fileExt := strings.TrimPrefix(s, keyword) + execFileExt = append(execFileExt, zstring.Ucfirst(fileExt)) + } + } + if startup { task.preRun(new(changedFile)) } diff --git a/app/watch/task.go b/app/watch/task.go index 145365f..55d94c2 100644 --- a/app/watch/task.go +++ b/app/watch/task.go @@ -75,6 +75,12 @@ func (t *taskType) preRun(cf *changedFile) { go t.run(cf, extCommand, true, fileExt) } else { go t.run(cf, execCommand, true) + if cf.Path == "" { + for _, fileExt := range execFileExt { + extCommand := v.GetStringSlice("command.exec" + fileExt) + go t.run(cf, extCommand, true, fileExt) + } + } } } @@ -117,9 +123,9 @@ func (t *taskType) run(cf *changedFile, commands []string, outpuContent bool, ex } carr := cmdParse2Array(c, cf) if outpuContent { - util.Log.Printf("Command: %v\n", carr) + util.Log.Printf("Command: %v", carr) } else { - util.Log.Printf("Background command: %v\n", carr) + util.Log.Printf("Background command: %v", carr) continue } cmd := command(carr) @@ -226,7 +232,7 @@ func (t *taskType) runBackground(cf *changedFile, commands []string) []*exec.Cmd var r []*exec.Cmd for i := 0; i < l; i++ { carr := cmdParse2Array(commands[i], cf) - util.Log.Printf("Background command: %v\n", carr) + util.Log.Printf("Background command: %v", carr) cmd := command(carr) err := cmd.Start() if err != nil { @@ -249,7 +255,7 @@ func cloes(cmd *exec.Cmd) { cmd := exec.Command("TASKKILL", "/T", "/F", "/PID", ztype.ToString(cmd.Process.Pid)) _, _ = cmd.CombinedOutput() } - time.Sleep(time.Microsecond * 200) + time.Sleep(time.Second / 6) _ = cmd.Process.Kill() } } diff --git a/app/watch/var.go b/app/watch/var.go index bf27601..3b4c4fb 100644 --- a/app/watch/var.go +++ b/app/watch/var.go @@ -25,6 +25,7 @@ var ( lastPid int task *taskType execCommand []string + execFileExt []string startupExecCommand []string startup bool pushTimer sync.Map diff --git a/util/util.go b/util/util.go index 97facf1..ad95204 100644 --- a/util/util.go +++ b/util/util.go @@ -25,7 +25,7 @@ var ( once sync.Once installPath string homePath string - Version = "1.0.26" + Version = "1.0.27" BuildTime = "" BuildGoVersion = "" ) diff --git a/util/util_win.go b/util/util_win.go index e440764..d7119ff 100644 --- a/util/util_win.go +++ b/util/util_win.go @@ -20,6 +20,6 @@ func hideFile(path string) error { } } -func SetLimit(l int) { +func SetLimit(l int) { -} \ No newline at end of file +}