package main
//go get github.com/fsnotify/fsnotify
import (
"fmt"
"github.com/fsnotify/fsnotify"
"os"
"os/exec"
"path/filepath"
"strings"
)
var dir="D:\\GoProgram\\SelfGame\\Proto"
var goOupDir = "D:\\GoProgram\\SelfGame\\Proto"
var csharpOutDir="D:\\UintyProgram\\SelfGame\\SelfGame\\Assets\\Proto"
func main() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
fmt.Println(err)
}
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
if strings.Contains(info.Name(),".proto") {
fmt.Println(path)
err := watcher.Add(path)
if err != nil {
fmt.Println(err)
}
}
}
return err
})
Start(watcher)
}
func Start(watcher *fsnotify.Watcher) {
for {
select {
case event:=<-watcher.Events:
if (event.Op&fsnotify.Write==fsnotify.Write) {
update(event.Name)
file, err := os.Open(event.Name)
if err != nil {
fmt.Println("文件打开失败",err)
fmt.Println(file)
}
fmt.Println(event.Name)
fmt.Println("文件被修改了")
}
case err:=<-watcher.Errors:
if err != nil {
fmt.Println(err)
return
}
}
}
}
func update(name string) {
fmt.Println(dir)
cmd := exec.Command("protoc", "-I="+dir,"--go_out="+goOupDir, name)
cmd2 := exec.Command("protoc","-I="+dir,"--csharp_out="+csharpOutDir,name)
bytes,err:=cmd.CombinedOutput();
if err != nil {
fmt.Println(err)
}
fmt.Println(string(bytes))
bytes,err=cmd2.CombinedOutput();
if err != nil {
fmt.Println(err)
}
fmt.Println(string(bytes))
}