|
@@ -0,0 +1,178 @@
|
|
1
|
+package main
|
|
2
|
+
|
|
3
|
+import (
|
|
4
|
+ "flag"
|
|
5
|
+ "fmt"
|
|
6
|
+ "log"
|
|
7
|
+ "os"
|
|
8
|
+ "os/exec"
|
|
9
|
+ "os/signal"
|
|
10
|
+ "path/filepath"
|
|
11
|
+ "syscall"
|
|
12
|
+
|
|
13
|
+ "github.com/fsnotify/fsnotify"
|
|
14
|
+)
|
|
15
|
+
|
|
16
|
+var appPath string
|
|
17
|
+var mainSourceFile string
|
|
18
|
+var gopherjsAppPath string
|
|
19
|
+
|
|
20
|
+func start() *exec.Cmd {
|
|
21
|
+
|
|
22
|
+ if gopherjsAppPath != "" {
|
|
23
|
+
|
|
24
|
+ cwd, err := os.Getwd()
|
|
25
|
+ if err != nil {
|
|
26
|
+ log.Fatal("Encountered an error while attempting to get the cwd: ", err)
|
|
27
|
+ } else {
|
|
28
|
+ os.Chdir(gopherjsAppPath)
|
|
29
|
+ gjsCommand := exec.Command("gopherjs", "build")
|
|
30
|
+ gjsCommand.Start()
|
|
31
|
+ os.Chdir(cwd)
|
|
32
|
+ }
|
|
33
|
+ }
|
|
34
|
+
|
|
35
|
+ cmd := exec.Command("go", "run", appPath+"/"+mainSourceFile)
|
|
36
|
+ cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
|
37
|
+ cmd.Start()
|
|
38
|
+ return cmd
|
|
39
|
+
|
|
40
|
+}
|
|
41
|
+
|
|
42
|
+func stop(cmd *exec.Cmd) {
|
|
43
|
+ pgid, err := syscall.Getpgid(cmd.Process.Pid)
|
|
44
|
+ if err == nil {
|
|
45
|
+ syscall.Kill(-pgid, 15)
|
|
46
|
+ }
|
|
47
|
+}
|
|
48
|
+
|
|
49
|
+func restart(cmd *exec.Cmd) *exec.Cmd {
|
|
50
|
+ var newCommand *exec.Cmd
|
|
51
|
+ stop(cmd)
|
|
52
|
+ newCommand = start()
|
|
53
|
+ return newCommand
|
|
54
|
+
|
|
55
|
+}
|
|
56
|
+
|
|
57
|
+func initializeWatcher(shouldRestart chan bool, dirList []string) {
|
|
58
|
+
|
|
59
|
+ watcher, err := fsnotify.NewWatcher()
|
|
60
|
+ if err != nil {
|
|
61
|
+ log.Fatal(err)
|
|
62
|
+ }
|
|
63
|
+ defer watcher.Close()
|
|
64
|
+
|
|
65
|
+ done := make(chan bool)
|
|
66
|
+ go func() {
|
|
67
|
+ for {
|
|
68
|
+ select {
|
|
69
|
+ case event := <-watcher.Events:
|
|
70
|
+
|
|
71
|
+ if event.Op == fsnotify.Write || event.Op == fsnotify.Rename {
|
|
72
|
+ if filepath.Ext(event.Name) == ".go" {
|
|
73
|
+ shouldRestart <- true
|
|
74
|
+ }
|
|
75
|
+ }
|
|
76
|
+
|
|
77
|
+ case err := <-watcher.Errors:
|
|
78
|
+ if err != nil {
|
|
79
|
+ log.Println("error:", err)
|
|
80
|
+ }
|
|
81
|
+ }
|
|
82
|
+ }
|
|
83
|
+ }()
|
|
84
|
+
|
|
85
|
+ err = watcher.Add(appPath)
|
|
86
|
+ if err != nil {
|
|
87
|
+ log.Fatal(err)
|
|
88
|
+ }
|
|
89
|
+ // watch subdirectories also
|
|
90
|
+ for _, element := range dirList {
|
|
91
|
+ watcher.Add(element)
|
|
92
|
+ }
|
|
93
|
+
|
|
94
|
+ <-done
|
|
95
|
+
|
|
96
|
+}
|
|
97
|
+
|
|
98
|
+func pathExists(path string) (bool, error) {
|
|
99
|
+ _, err := os.Stat(path)
|
|
100
|
+ if err == nil {
|
|
101
|
+ return true, nil
|
|
102
|
+ }
|
|
103
|
+ if os.IsNotExist(err) {
|
|
104
|
+ return false, nil
|
|
105
|
+ }
|
|
106
|
+ return true, err
|
|
107
|
+}
|
|
108
|
+
|
|
109
|
+func main() {
|
|
110
|
+
|
|
111
|
+ flag.StringVar(&appPath, "appPath", "", "The path to your Go project")
|
|
112
|
+ flag.StringVar(&mainSourceFile, "mainSourceFile", "", "The Go source file with the main func()")
|
|
113
|
+ flag.StringVar(&gopherjsAppPath, "gopherjsAppPath", "", "The path to your GopherJS project (optional)")
|
|
114
|
+ flag.Parse()
|
|
115
|
+
|
|
116
|
+ // Exit if no appPath is supplied
|
|
117
|
+ if appPath == "" {
|
|
118
|
+ fmt.Println("You must supply the appPath parameter")
|
|
119
|
+ os.Exit(1)
|
|
120
|
+ }
|
|
121
|
+
|
|
122
|
+ if appPathExists, appPathErr := pathExists(appPath); appPathExists != true || appPathErr != nil {
|
|
123
|
+ fmt.Println("The path you specified to your Go application project does not exist.")
|
|
124
|
+ os.Exit(1)
|
|
125
|
+ }
|
|
126
|
+
|
|
127
|
+ if mainSourceFile == "" {
|
|
128
|
+ fmt.Println("You must supply the mainSourceFile parameter")
|
|
129
|
+ os.Exit(1)
|
|
130
|
+ }
|
|
131
|
+
|
|
132
|
+ if sourceFileExists, sourceFilePathErr := pathExists(appPath + "/" + mainSourceFile); sourceFileExists != true || sourceFilePathErr != nil {
|
|
133
|
+ fmt.Println("The path to the main source file you provided does not exist.")
|
|
134
|
+ os.Exit(1)
|
|
135
|
+ }
|
|
136
|
+
|
|
137
|
+ if gopherjsAppPath != "" {
|
|
138
|
+ if gopherjsFileExists, gopherjsFileErr := pathExists(gopherjsAppPath); gopherjsFileExists != true || gopherjsFileErr != nil {
|
|
139
|
+ fmt.Println("The path you specified to the GopherJS application project does not exist.")
|
|
140
|
+ os.Exit(1)
|
|
141
|
+ }
|
|
142
|
+ }
|
|
143
|
+
|
|
144
|
+ dirList := []string{}
|
|
145
|
+ filepath.Walk(appPath, func(path string, f os.FileInfo, err error) error {
|
|
146
|
+
|
|
147
|
+ if f.IsDir() == true {
|
|
148
|
+ dirList = append(dirList, path)
|
|
149
|
+ }
|
|
150
|
+ return nil
|
|
151
|
+ })
|
|
152
|
+
|
|
153
|
+ shouldRestart := make(chan bool, 1)
|
|
154
|
+
|
|
155
|
+ go initializeWatcher(shouldRestart, dirList)
|
|
156
|
+
|
|
157
|
+ interrupt := make(chan os.Signal, 1)
|
|
158
|
+ signal.Notify(interrupt, os.Interrupt)
|
|
159
|
+
|
|
160
|
+ cmd := start()
|
|
161
|
+
|
|
162
|
+ for {
|
|
163
|
+
|
|
164
|
+ select {
|
|
165
|
+
|
|
166
|
+ case <-interrupt:
|
|
167
|
+ stop(cmd)
|
|
168
|
+ os.Exit(0)
|
|
169
|
+
|
|
170
|
+ case <-shouldRestart:
|
|
171
|
+ fmt.Println("Recompiling and Restarting")
|
|
172
|
+ cmd = restart(cmd)
|
|
173
|
+
|
|
174
|
+ }
|
|
175
|
+
|
|
176
|
+ }
|
|
177
|
+
|
|
178
|
+}
|