/
runner.go
155 lines (145 loc) · 4.21 KB
/
runner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
- Copyright 2014 Yua Shizuki. All rights reserved.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
-*/
package main
import "fmt"
import "github.com/howeyc/fsnotify"
import "io/ioutil"
import "path/filepath"
import "os/exec"
import "os"
import "net"
import "strings"
import "regexp"
import "github.com/YuaShizuki/gok/txtserve"
import "time"
var controllerListener net.Listener
var controllerConn net.Conn
func runner() {
err := run()
if err != nil {
errExit(err, "")
}
done := make(chan bool)
<-done
}
func run() error {
err := build(false)
if err != nil {
return err
}
pwd,_ := os.Getwd()
exe := filepath.Base(pwd)
port, err := controller()
if err != nil {
return err
}
cmd := exec.Command("sudo", "./"+exe, "gokcontroller="+port)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
go cmd.Run()
return nil
}
/*- http server (child process) controller -*/
func controller() (string, error) {
var err error
controllerListener, err = net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return "", err
}
port := strings.Split(controllerListener.Addr().String(), ":")[1]
go controllerStart()
return port, nil
}
func switchOffController() {
if controllerListener != nil {
controllerListener.Close()
controllerListener = nil
}
if controllerConn != nil {
controllerConn.Close()
controllerConn = nil
}
}
func controllerStart() {
var err error
controllerConn, err = controllerListener.Accept()
if err != nil {
return
}
go startNotifier(".", make(chan bool))
ioutil.ReadAll(controllerConn)
switchOffController()
}
/*- End -*/
func startNotifier(dir string, end chan bool) {
lastUpdate := time.Now()
goorgok,_ := regexp.Compile("^([^.]*\\.go|[^.]*\\.gok)$")
watch, err := fsnotify.NewWatcher()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
dirNotifiers := make([](chan bool),0,10)
files, err := ioutil.ReadDir(dir)
if err != nil {
errExit(err, "")
}
for _, f := range files {
if f.IsDir() {
lenD := len(dirNotifiers)
dirNotifiers = append(dirNotifiers, make(chan bool))
startNotifier(dir+"/"+f.Name(), dirNotifiers[lenD])
}
}
watch.Watch(dir)
for ;; {
select {
case event := <-watch.Event:
if goorgok.Match([]byte(event.Name)) || isDirectory(event.Name) {
if time.Since(lastUpdate) < (1000 * time.Millisecond) {
continue
}
lastUpdate = time.Now()
fmt.Printf("%v: src code update => ", lastUpdate)
txtserve.StopServer()
switchOffController()
time.Sleep(200 * time.Millisecond)
err = run()
if err != nil {
fmt.Printf("error building server binary\n")
txtserve.StartServer(err.Error())
} else {
fmt.Printf("server binary build successful\n")
watch.Close()
for _, c := range dirNotifiers {
c <- true
close(c)
}
return
}
}
case shouldEnd := <-end:
if shouldEnd {
watch.Close()
for _, c := range dirNotifiers {
c <- true
close(c)
}
return
}
}
}
}