Skip to content

Commit

Permalink
finish fsnotify
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Sep 1, 2014
1 parent 7e03743 commit 9f8cc43
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 21 deletions.
3 changes: 0 additions & 3 deletions gorazor/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ func GenFolder(indir string, outdir string, options Option) (err error) {
return err
}
}

//Make sure we have a clean output directory
os.RemoveAll(outdir)
//Make it
if !exists(outdir) {
os.MkdirAll(outdir, 0775)
Expand Down
78 changes: 60 additions & 18 deletions gorazor/gogen.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,43 +429,85 @@ func watchDir(input, output string, options Option) error {

done := make(chan bool)

output_path := func(path string) string {
res := strings.Replace(path, input, output, 1)
return res
}

gen := func(filename string) error {
outpath := output_path(filename)
outpath = strings.Replace(outpath, ".gohtml", ".go", 1)
outdir := filepath.Dir(outpath)
if !exists(outdir) {
os.MkdirAll(outdir, 0775)
}
err := GenFile(filename, outpath, options)
if err == nil {
log.Printf("%s -> %s\n", filename, outpath)
}
return err
}

visit_gen := func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
//Just do file with exstension .gohtml
if !strings.HasSuffix(path, ".gohtml") {
return nil
}
filename := filepath.Base(path)
if strings.HasPrefix(filename, ".#") {
return nil
}
err := gen(path)
if err != nil {
return err
}
}
return nil
}

go func() {
for {
select {
case event := <-watcher.Events:
filename := event.Name
if event.Op&fsnotify.Write == fsnotify.Write ||
event.Op&fsnotify.Create == fsnotify.Create {
if filename == "" {
//should be a bug for fsnotify
continue
}
if event.Op&fsnotify.Remove != fsnotify.Remove &&
(event.Op&fsnotify.Write == fsnotify.Write ||
event.Op&fsnotify.Create == fsnotify.Create) {
stat, err := os.Stat(filename)
if err != nil {
continue
}
if stat.IsDir() {
log.Println("add dir:", filename)
watcher.Add(filename)
output := output_path(filename)
log.Println("mkdir:", output)
if !exists(output) {
os.MkdirAll(output, 0755)
err = filepath.Walk(filename, visit_gen)
if err != nil {
done <- true
}
}
continue
}
if !strings.HasPrefix(filepath.Base(filename), ".#") &&
strings.HasSuffix(filename, ".gohtml") {
name := strings.Replace(filename, input, "", 1)
name = strings.Replace(name, ".gohtml", ".go", 1)
outpath := output + name
outdir := filepath.Dir(outpath)
if !exists(outdir) {
os.MkdirAll(outdir, 0775)
}
err := GenFile(filename, outpath, options)
if err == nil {
log.Printf("%s -> %s\n", filename, outpath)
}
gen(filename)
}
} else if event.Op&fsnotify.Remove == fsnotify.Remove {
watcher.Remove(filename)
output := strings.Replace(filename, input, output, 1)
} else if event.Op&fsnotify.Remove == fsnotify.Remove ||
event.Op&fsnotify.Rename == fsnotify.Rename {
output := output_path(filename)
if exists(output) {
//shoud be dir
watcher.Remove(filename)
os.RemoveAll(output)
log.Println("remove dir:", filename)
log.Println("remove dir:", output)
} else if strings.HasSuffix(output, ".gohtml") {
output = strings.Replace(output, ".gohtml", ".go", 1)
if exists(output) {
Expand All @@ -476,7 +518,7 @@ func watchDir(input, output string, options Option) error {
}
case err := <-watcher.Errors:
log.Println("error:", err)
done <- true
continue
}
}
}()
Expand Down

0 comments on commit 9f8cc43

Please sign in to comment.