Browse Source

热更新

修改bug 优化增加回显
main
songzhibin97 4 years ago
parent
commit
2bca2e3151
  1. 44
      server/utils/cmd_Task.go
  2. 53
      server/utils/cmd_monitor.go

44
server/utils/cmd_Task.go

@ -1,7 +1,9 @@
package utils
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
@ -85,7 +87,7 @@ func (t *T) DefaultF(ch chan struct{}) error {
// 判断是否有makefile
_, err := os.Stat(filepath.Join("Makefile"))
if runtime.GOOS != "windows" && err != nil {
if runtime.GOOS != "windows" && err == nil {
_, err := exec.LookPath("make")
if err == nil {
cmd = exec.Command("makefile")
@ -105,12 +107,12 @@ func (t *T) DefaultF(ch chan struct{}) error {
default:
buildCmd = exec.Command("go", "build", "-o", "gva", "main.go")
}
err = buildCmd.Run()
fmt.Println("build 执行完成")
if err != nil {
return err
}
fmt.Println("build 执行完成")
// 执行
switch runtime.GOOS {
@ -121,15 +123,7 @@ func (t *T) DefaultF(ch chan struct{}) error {
}
makefile:
// 开始执行任务
err = cmd.Start()
if err != nil {
return err
}
t.p = cmd.Process
fmt.Println("pid", t.p.Pid)
go func() {
err = cmd.Wait()
}()
t.echo(cmd)
<-ch
// 回收资源
err = cmd.Process.Kill()
@ -138,3 +132,29 @@ makefile:
t.closeChan <- struct{}{}
return err
}
// echo: 封装回显
func (t *T) echo(cmd *exec.Cmd) error {
var stdoutBuf bytes.Buffer
stdoutIn, _ := cmd.StdoutPipe()
var errStdout, errStderr error
stdout := io.MultiWriter(os.Stdout, &stdoutBuf)
err := cmd.Start()
if err != nil {
return err
}
go func() {
_, errStdout = io.Copy(stdout, stdoutIn)
}()
t.p = cmd.Process
fmt.Println("pid", t.p.Pid)
go func() {
_ = cmd.Wait()
if errStdout != nil || errStderr != nil {
fmt.Printf("failed to capture stdout or stderr\n")
}
outStr := string(stdoutBuf.Bytes())
fmt.Printf("\nout:\n%s\n", outStr)
}()
return nil
}

53
server/utils/cmd_monitor.go

@ -55,33 +55,24 @@ func (w *Watch) Watch(path string, t *T) error {
c <- errors.New("errors close")
return
}
// 判断时间
fmt.Println("even", even)
// 判断事件
switch {
// todo 待处理
case even.Op&fsnotify.Create == fsnotify.Create:
//这里获取新创建文件的信息,如果是目录,则加入监控中
fmt.Println("创建文件 : ", even.Name)
//t.AddTask()
_ = w.Add(even.Name)
w.addTask(t, even.Name)
case even.Op&fsnotify.Write == fsnotify.Write:
fmt.Println("修改 : ", even.Name)
fmt.Println(filepath.Ext(even.Name))
if filepath.Ext(even.Name) == ".go" {
fmt.Println("send addtask:", even.Name)
t.AddTask()
}
case even.Op&fsnotify.Remove == fsnotify.Remove:
fmt.Println("删除 : ", even.Name)
//t.AddTask()
_ = w.Remove(even.Name)
case even.Op&fsnotify.Rename == fsnotify.Rename:
fmt.Println("重命名 : ", even.Name)
//t.AddTask()
fmt.Println("修改文件 : ", even.Name)
w.addTask(t, even.Name)
case even.Op&fsnotify.Remove == fsnotify.Remove || even.Op&fsnotify.Rename == fsnotify.Rename:
fmt.Println("删除或重命名文件 : ", even.Name)
_ = w.Remove(even.Name)
w.addTask(t, even.Name)
}
case err = <-w.Errors:
fmt.Println("79", err)
fmt.Println("even Error:", err)
c <- err
return
}
@ -106,10 +97,12 @@ func (w *Watch) watchDir(path string) error {
for _, f := range fileSlice {
fPath := filepath.Join(path, f.Name())
if !f.IsDir() {
// todo 这里加一些条件筛选添加监控的内容
err = w.watchFile(fPath)
if err != nil {
return err
// 判断是否可监控的文件
if chickPower(fPath) {
err = w.watchFile(fPath)
if err != nil {
return err
}
}
} else {
err := w.watchDir(fPath)
@ -123,5 +116,21 @@ func (w *Watch) watchDir(path string) error {
// watchDir: 处理监控单文件
func (w *Watch) watchFile(path string) error {
return w.Add(path)
var err error
if chickPower(path) {
err = w.Add(path)
}
return err
}
// chickPower: 判断是否在可控范围内
func chickPower(name string) bool {
return filepath.Ext(name) == ".go" || filepath.Ext(name) == ".yaml"
}
// addTask: 偏函数 简化发送任务
func (w *Watch) addTask(t *T, name string) {
if chickPower(name) {
t.AddTask()
}
}
Loading…
Cancel
Save