优化 逻辑

This commit is contained in:
dengsgo
2019-04-03 18:04:07 +08:00
parent bee37e29e6
commit 5c31d76fac
2 changed files with 27 additions and 18 deletions

View File

@@ -41,7 +41,9 @@ func parseConfig() {
cfg = new(FileGirl) cfg = new(FileGirl)
fc, err := ioutil.ReadFile(projectFolder + "/filegirl.yaml") fc, err := ioutil.ReadFile(projectFolder + "/filegirl.yaml")
if err != nil { if err != nil {
log.Panicln(PreError, "read filegirl.yaml file err: ", err) log.Println(PreError, "the filegirl.yaml file in", projectFolder, "is not exist! ", err)
fmt.Print(firstRunHelp)
log.Fatalln("fileboy unable to run.")
} }
err = yaml.Unmarshal(fc, cfg) err = yaml.Unmarshal(fc, cfg)
if err != nil { if err != nil {
@@ -50,6 +52,7 @@ func parseConfig() {
if cfg.Core.Version > Version { if cfg.Core.Version > Version {
log.Panicln(PreError, "current fileboy support max version : ", Version) log.Panicln(PreError, "current fileboy support max version : ", Version)
} }
cfg.Monitor.RootWatch = false
// init map // init map
cfg.Monitor.TypesMap = map[string]bool{} cfg.Monitor.TypesMap = map[string]bool{}
cfg.Monitor.IncludeDirsMap = map[string]bool{} cfg.Monitor.IncludeDirsMap = map[string]bool{}
@@ -68,13 +71,17 @@ func eventDispatcher(event fsnotify.Event) {
!keyInMonitorTypesMap(ext, cfg) { !keyInMonitorTypesMap(ext, cfg) {
return return
} }
fileName := relativePath(projectFolder, event.Name)
if !cfg.Monitor.RootWatch && fileName != "filegirl.yaml" {
return
}
switch event.Op { switch event.Op {
case case
fsnotify.Write, fsnotify.Write,
fsnotify.Rename: fsnotify.Rename:
log.Println("EVENT", event.Op.String(), ":", event.Name) log.Println("EVENT", event.Op.String(), ":", event.Name)
taskMan.Put(&changedFile{ taskMan.Put(&changedFile{
Name: relativePath(projectFolder, event.Name), Name: fileName,
Changed: time.Now().UnixNano(), Changed: time.Now().UnixNano(),
Ext: ext, Ext: ext,
}) })
@@ -85,7 +92,9 @@ func eventDispatcher(event fsnotify.Event) {
func addWatcher() { func addWatcher() {
log.Println("collecting directory information...") log.Println("collecting directory information...")
dirsMap := map[string]bool{} dirsMap := map[string]bool{
projectFolder: true,
}
for _, dir := range cfg.Monitor.IncludeDirs { for _, dir := range cfg.Monitor.IncludeDirs {
darr := dirParse2Array(dir) darr := dirParse2Array(dir)
if len(darr) < 1 || len(darr) > 2 { if len(darr) < 1 || len(darr) > 2 {
@@ -95,6 +104,7 @@ func addWatcher() {
log.Fatalln(PreError, "dirs must be relative paths ! err path:", dir) log.Fatalln(PreError, "dirs must be relative paths ! err path:", dir)
} }
if darr[0] == "." { if darr[0] == "." {
cfg.Monitor.RootWatch = true
if len(darr) == 2 && darr[1] == "*" { if len(darr) == 2 && darr[1] == "*" {
// The highest priority // The highest priority
dirsMap = map[string]bool{ dirsMap = map[string]bool{
@@ -141,15 +151,14 @@ func addWatcher() {
} }
func initWatcher() { func initWatcher() {
parseConfig()
var err error var err error
if watcher != nil {
_ = watcher.Close()
}
watcher, err = fsnotify.NewWatcher() watcher, err = fsnotify.NewWatcher()
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
defer watcher.Close()
done := make(chan bool)
taskMan = newTaskMan(cfg.Command.DelayMillSecond, cfg.Notifier.CallUrl) taskMan = newTaskMan(cfg.Command.DelayMillSecond, cfg.Notifier.CallUrl)
go func() { go func() {
for { for {
@@ -168,22 +177,18 @@ func initWatcher() {
} }
}() }()
addWatcher() addWatcher()
<-done
} }
func parseArgs() { func parseArgs() {
l := len(os.Args) switch len(os.Args) {
if l == 1 { case 1:
_, err := ioutil.ReadFile(projectFolder + "/filegirl.yaml") parseConfig()
if err != nil { done := make(chan bool)
log.Println(PreError, "the filegirl.yaml file does not exist! ", err)
fmt.Print(firstRunHelp)
return
}
initWatcher() initWatcher()
defer watcher.Close()
<-done
return return
} case 2:
if l == 2 {
c := os.Args[1] c := os.Args[1]
switch c { switch c {
case "init": case "init":
@@ -204,6 +209,8 @@ func parseArgs() {
fmt.Print(helpStr) fmt.Print(helpStr)
} }
return return
default:
log.Fatalln("Unknown parameters, use `fileboy help` show help info.")
} }
} }

View File

@@ -8,6 +8,8 @@ type FileGirl struct {
Types []string `yaml:"types"` Types []string `yaml:"types"`
IncludeDirs []string `yaml:"includeDirs"` IncludeDirs []string `yaml:"includeDirs"`
ExceptDirs []string `yaml:"exceptDirs"` ExceptDirs []string `yaml:"exceptDirs"`
// project root watcher flag
RootWatch bool `yaml:"-"`
// convert to // convert to
TypesMap map[string]bool `yaml:"-"` TypesMap map[string]bool `yaml:"-"`
IncludeDirsMap map[string]bool `yaml:"-"` IncludeDirsMap map[string]bool `yaml:"-"`