Skip to content

Commit

Permalink
fix: new file cannot be listened to
Browse files Browse the repository at this point in the history
  • Loading branch information
sohaha committed Mar 22, 2021
1 parent ef26239 commit b44ef4e
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 73 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ zzz-stress.toml
/vendor/
.DS_Store
/zzz_*
/tmpApp
tmp
/tmpApp
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ zzz help
**Windows**

手动下载 [zzz_Windows_x86_64.tar.gz
](https://github.com/sohaha/zzz/releases)[国内点这里下载](https://github.73zls.com/https://github.com/sohaha/zzz/releases)
](https://github.com/sohaha/zzz/releases)[国内点这里下载](https://github.73zls.com/sohaha/zzz/releases)

然后打开 cmd 执行 `zzz.exe more install` 或者 设置自行环境变量。

Expand Down
31 changes: 18 additions & 13 deletions app/watch/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,31 @@ import (

func eventDispatcher(event fsnotify.Event) {
ext := path.Ext(event.Name)
types := v.GetStringSlice("monitor.types")
if len(types) > 0 && types[0] != ".*" && !inStringArray(ext, types) {
if zfile.DirExist(event.Name) {
if event.Op == fsnotify.Create {
addNewWatcher(event.Name)
} else if event.Op == fsnotify.Remove {
removeWatcher(event.Name)
} else {
otherWatcher(event.Name, event.Op)
}
event.Name = zfile.RealPath(event.Name)
isDir := zfile.DirExist(event.Name)
switch event.Op {
case fsnotify.Create:
if isDir {
addNewWatcher(event.Name)
}
case fsnotify.Remove:
removeWatcher(event.Name)
return
case fsnotify.Rename:
case fsnotify.Write:
if len(types) > 0 && types[0] != ".*" && !inStringArray(ext, types) {
return
}
fileChange(event)
default:
otherWatcher(event.Name, event.Op)
}
fileChange(event)
}

func fileChange(event fsnotify.Event) {
switch event.Op {
case fsnotify.Write, fsnotify.Remove, fsnotify.Rename:
if strings.HasSuffix(event.Name, "____tmp.go") {
if strings.HasSuffix(event.Name, "_static_resources.go") {
// ignore zzz build temporary files
return
}
Expand Down Expand Up @@ -63,6 +68,6 @@ func fileChange(event fsnotify.Event) {
push()
}))

zlog.Printf("Change: %v (%v)\n", relativeFilePath, opType)
zlog.Printf("Change: %v (%v)", relativeFilePath, opType)
}
}
1 change: 1 addition & 0 deletions app/watch/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func httpRun() {
service.Log.SetPrefix("")
service.NotFoundHandler(func(c *znet.Context) {
httpEntrance(c)
_ = c.PrevContent()
})

service.GET("/", func(c *znet.Context) {
Expand Down
10 changes: 9 additions & 1 deletion app/watch/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ func StartCmd(watchCmd *cobra.Command) (app *cobra.Command) {
if cfgPath != "" {
v.SetConfigFile(cfgPath)
}

util.SetLimit(999999)

// cfgPath = zfile.RealPath(cfgPath)
run(cmd)
},
}
util.SetLimit(999999)
watchCmd.AddCommand(app)
return
}
Expand Down Expand Up @@ -89,6 +91,12 @@ func start() {
cmds []*exec.Cmd
)
poll := v.GetBool("monitor.poll")
types = v.GetStringSlice("monitor.types")
includeDirs = v.GetStringSlice("monitor.includeDirs")
exceptDirs = v.GetStringSlice("monitor.ExceptDirs")
for i := range exceptDirs {
exceptDirs[i] = zfile.RealPath(exceptDirs[i], false)
}
// watcher, err = fsnotify.NewWatcher()
if poll {
watcher = NewPollingWatcher()
Expand Down
10 changes: 8 additions & 2 deletions app/watch/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package watch

import (
"errors"
"github.com/sohaha/zlsgo/zfile"
"github.com/sohaha/zlsgo/zlog"
"io/ioutil"
"net"
"os/exec"
Expand Down Expand Up @@ -37,14 +39,18 @@ func isIgnoreDirectory(folder string) bool {
}

func listFile(folder string, fun func(string)) {
folder = zfile.RealPath(folder)
if isIgnoreDirectory(folder) {
zlog.Error("Ignore", folder)
return
}

files, _ := ioutil.ReadDir(folder)
for _, file := range files {
if file.IsDir() {
d := folder + "/" + file.Name()
d := zfile.RealPath(folder + "/" + file.Name())
if isIgnoreDirectory(d) {
continue
}
fun(d)
listFile(d, fun)
}
Expand Down
5 changes: 5 additions & 0 deletions app/watch/var.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package watch

import (
"github.com/sohaha/zlsgo/zfile"
"os"
"path/filepath"
"sync"
Expand All @@ -19,6 +20,9 @@ var (
projectFolder = "."
watcher FileWatcher
watchDirs = make([]string, 0)
exceptDirs = make([]string, 0)
types = make([]string, 0)
includeDirs = make([]string, 0)
globalErr error
done = make(chan bool, 1)
signalChan = make(chan os.Signal, 1)
Expand Down Expand Up @@ -47,6 +51,7 @@ func init() {
if projectFolder, globalErr = os.Getwd(); globalErr != nil {
util.Log.Fatal(globalErr)
}
projectFolder = zfile.RealPath(projectFolder)
projectFolder = filepath.ToSlash(projectFolder)
projectFolder, _ = filepath.Abs(projectFolder)
}
76 changes: 27 additions & 49 deletions app/watch/watch.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package watch

import (
"github.com/sohaha/zlsgo/zfile"
"path/filepath"
"strings"

Expand All @@ -26,20 +27,18 @@ func addWatcher() {
}

func addNewWatcher(dir string) {
exceptDirs := v.GetStringSlice("monitor.ExceptDirs")
fullDir := filepath.ToSlash(dir)
for i := 0; i < len(exceptDirs); i++ {
if dir == exceptDirs[i] {
return
}
}

if !inStringArray(fullDir, watchDirs) {
watchDirs = append(watchDirs, fullDir)
isExceptDirs := arrExceptDirs()
if isExceptDirs {
return
}
//isExceptDirs := arrExceptDirs()
//if isExceptDirs {
// return
//}
zlog.Println("Watcher: ", fullDir)
err := watcher.Add(fullDir)
if err != nil {
Expand All @@ -49,93 +48,72 @@ func addNewWatcher(dir string) {
}

func removeWatcher(dir string) {
fullDir := filepath.ToSlash(dir)
err := watcher.Remove(fullDir)
if err == nil && inStringArray(fullDir, watchDirs) {
if inStringArray(dir, watchDirs) {
if len(watchDirs) > 0 {
for i, v := range watchDirs {
if v == fullDir {
if v == dir {
watchDirs = append(watchDirs[:i], watchDirs[i+1:]...)
break
}
}
}

}
zlog.Println("RemoveWatcher: ", fullDir)
}

func otherWatcher(name string, event fsnotify.Op) {
// zlog.Debug("otherWatcher", name, event)
}

func arrIncludeDirs() {
includeDirs := v.GetStringSlice("monitor.includeDirs")
for i := 0; i < len(includeDirs); i++ {
darr := dirParse2Array(includeDirs[i])

isD := strings.Index(darr[0], ".") == 0

if len(darr) < 1 || len(darr) > 2 {
arr := dirParse2Array(includeDirs[i])
isD := strings.Index(arr[0], ".") == 0
if len(arr) < 1 || len(arr) > 2 {
zlog.Fatal("Error listening for file path: ", includeDirs[i])
}
if strings.HasPrefix(darr[0], "/") {
if strings.HasPrefix(arr[0], "/") {
zlog.Fatal("watchDirs must be relative paths: ", includeDirs[i])
}
isAll := len(darr) == 2 && darr[1] == "*"
isAll := len(arr) == 2 && arr[1] == "*"
addFiles := func(dir string) {
dir = zfile.RealPath(dir)
if isAll {
watchDirs = append(watchDirs, dir)
listFile(dir, func(d string) {
path, _ := filepath.Abs(d)
watchDirs = arrayUniqueAdd(watchDirs, path)
//path, _ := filepath.Abs(d)
watchDirs = arrayUniqueAdd(watchDirs, d)
})
} else {
path, _ := filepath.Abs(dir)
watchDirs = arrayUniqueAdd(watchDirs, path)
} else if !isIgnoreDirectory(dir) {
//path, _ := filepath.Abs(dir)
watchDirs = arrayUniqueAdd(watchDirs, dir)
}
}

if darr[0] == "." {
if arr[0] == "." {
addFiles(projectFolder)
} else if isD {
path, _ := filepath.Abs(darr[0])
addFiles(path)
addFiles(arr[0])
} else {
md := darr[0]
if !filepath.IsAbs(md) {
md = projectFolder + "/" + darr[0]
}
if len(darr) == 2 && darr[1] == "*" {
path, _ := filepath.Abs(md)
watchDirs = arrayUniqueAdd(watchDirs, path)
md := arr[0]
md = zfile.RealPath(md)
if len(arr) == 2 && arr[1] == "*" {
watchDirs = arrayUniqueAdd(watchDirs, md)
listFile(md, func(d string) {
path, _ := filepath.Abs(d)
watchDirs = arrayUniqueAdd(watchDirs, path)
})
} else {
path, _ := filepath.Abs(md)
watchDirs = arrayUniqueAdd(watchDirs, path)
watchDirs = arrayUniqueAdd(watchDirs, md)
}
}
}

}

func arrExceptDirs() (update bool) {
exceptDirs := v.GetStringSlice("monitor.ExceptDirs")
for i := 0; i < len(exceptDirs); i++ {
p := exceptDirs[i]
if !filepath.IsAbs(p) {
p = projectFolder + "/" + exceptDirs[i]
}
path, _ := filepath.Abs(p)
update = true
watchDirs = arrayRemoveElement(watchDirs, path)
listFile(p, func(d string) {
path, _ := filepath.Abs(d)
watchDirs = arrayRemoveElement(watchDirs, path)
})
watchDirs = arrayRemoveElement(watchDirs, p)
}

return update
}
2 changes: 1 addition & 1 deletion cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ var buildCmd = &cobra.Command{
for _, referencedAsset := range referencedAssets {
packfileData, err := build.GeneratePackFileString(referencedAsset, buildIgnore)
util.CheckIfError(err)
targetFile := filepath.Join(referencedAsset.BaseDir, referencedAsset.PackageName+"____tmp.go")
targetFile := filepath.Join(referencedAsset.BaseDir, referencedAsset.PackageName+"_static_resources.go")
targetFiles = append(targetFiles, targetFile)
err = ioutil.WriteFile(targetFile, []byte(packfileData), 0644)
util.CheckIfError(err)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/manifoldco/promptui v0.8.0
github.com/mitchellh/go-homedir v1.1.0
github.com/sohaha/gconf v0.0.10
github.com/sohaha/zlsgo v1.1.3
github.com/sohaha/zlsgo v1.1.8
github.com/sohaha/zstatic v0.0.2
github.com/spf13/cobra v1.1.1
github.com/spf13/viper v1.7.1
Expand Down
Loading

0 comments on commit b44ef4e

Please sign in to comment.