forked from gravityblast/fresh
/
start.go
131 lines (114 loc) · 2.31 KB
/
start.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
package runner
import (
"fmt"
"os"
"runtime"
"strings"
"time"
)
var (
startChannel chan string
stopChannel chan bool
mainLog logFunc
watcherLog logFunc
runnerLog logFunc
buildLog logFunc
testLog logFunc
appLog logFunc
)
func flushEvents() {
for {
select {
case eventName := <-startChannel:
mainLog("receiving event %s", eventName)
default:
return
}
}
}
func start() {
loopIndex := 0
buildDelay := buildDelay()
started := false
go func() {
for {
loopIndex++
mainLog("Waiting (loop %d)...", loopIndex)
eventName := <-startChannel
mainLog("receiving first event %s", eventName)
mainLog("sleeping for %d milliseconds", buildDelay)
time.Sleep(buildDelay * time.Millisecond)
mainLog("flushing events")
flushEvents()
mainLog("Started! (%d Goroutines)", runtime.NumGoroutine())
err := removeBuildErrorsLog()
if err != nil {
mainLog(err.Error())
}
errorMessage, ok := build()
if !ok {
mainLog("Build Failed: \n %s", errorMessage)
if !started {
os.Exit(1)
}
createBuildErrorsLog(errorMessage)
} else {
if shouldRunTests() == "1" {
errorMessage, ok := test()
if !ok {
mainLog("Test Failed: \n %s", errorMessage)
createBuildErrorsLog(errorMessage)
} else {
if started {
stopChannel <- true
}
run()
}
} else {
if started {
stopChannel <- true
}
run()
}
}
started = true
mainLog(strings.Repeat("-", 20))
}
}()
}
func init() {
startChannel = make(chan string, 1000)
stopChannel = make(chan bool)
}
func initLogFuncs() {
mainLog = newLogFunc("main")
watcherLog = newLogFunc("watcher")
runnerLog = newLogFunc("runner")
buildLog = newLogFunc("build")
testLog = newLogFunc("test")
appLog = newLogFunc("app")
}
func setEnvVars() {
os.Setenv("DEV_RUNNER", "1")
wd, err := os.Getwd()
if err == nil {
os.Setenv("RUNNER_WD", wd)
}
for k, v := range settings {
key := strings.ToUpper(fmt.Sprintf("%s%s", envSettingsPrefix, k))
os.Setenv(key, v)
}
}
// Watches for file changes in the root directory.
// After each file system event it builds and (re)starts the application.
func Start() {
initLimit()
initSettings()
initLogFuncs()
initFolders()
setEnvVars()
watch()
start()
startChannel <- "/"
<-make(chan int)
}