Skip to content

Commit

Permalink
perf: optimized to listen to multiple types
Browse files Browse the repository at this point in the history
  • Loading branch information
sohaha committed Jan 26, 2021
1 parent 4091091 commit a280492
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 17 deletions.
21 changes: 11 additions & 10 deletions app/root/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, 留空表示不启动
Expand Down
10 changes: 10 additions & 0 deletions app/watch/start.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package watch

import (
"github.com/sohaha/zlsgo/zstring"
"os"
"os/exec"
"os/signal"
"strings"
"syscall"

"github.com/mitchellh/go-homedir"
Expand Down Expand Up @@ -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))
}
Expand Down
14 changes: 10 additions & 4 deletions app/watch/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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()
}
}
Expand Down
1 change: 1 addition & 0 deletions app/watch/var.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var (
lastPid int
task *taskType
execCommand []string
execFileExt []string
startupExecCommand []string
startup bool
pushTimer sync.Map
Expand Down
2 changes: 1 addition & 1 deletion util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var (
once sync.Once
installPath string
homePath string
Version = "1.0.26"
Version = "1.0.27"
BuildTime = ""
BuildGoVersion = ""
)
Expand Down
4 changes: 2 additions & 2 deletions util/util_win.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ func hideFile(path string) error {
}
}

func SetLimit(l int) {
func SetLimit(l int) {

}
}

0 comments on commit a280492

Please sign in to comment.