-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.go
90 lines (73 loc) · 1.98 KB
/
project.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
package aph
import (
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"time"
)
type Project struct {
CheckLatestGoVersion bool
Executables map[string][]string
Services map[string][]string
SkipDirs []string
serviceCmdsByFile map[string]*exec.Cmd
tmpDir string
watchDirs *concurrentStringSet
}
func (p *Project) Run() {
p.init()
if p.CheckLatestGoVersion {
checkLatestGoVersionNumber()
}
ensureFile("glide.lock", "run 'glide install'")
checkFileNotStale("glide.lock")
if fileExists("package.json") {
checkFileNotStale("package.json")
npmInstall()
npmWatch()
}
watchNotifications := make(chan bool, 1)
watchNotifications <- true
go p.watch(watchNotifications)
debouncedCallbackLoop(watchNotifications, 100*time.Millisecond, func() {
log.Print("starting vet-lint-test loop")
dirs := p.watchDirs.slice()
if run("go vet", "go", append([]string{"vet"}, dirs...)) &&
runForEach("golint", "golint", []string{"-set_exit_status"}, dirs) &&
run("go test", "go", append([]string{"test"}, dirs...)) {
p.restartServices()
p.runExecutables()
}
})
}
func (p *Project) init() {
p.serviceCmdsByFile = map[string]*exec.Cmd{}
tmpDir, err := ioutil.TempDir("", "go-project-harness")
if err != nil {
log.Fatalf("ERROR: while establishing a tempdir: %s", err)
}
p.tmpDir = tmpDir
p.watchDirs = newConcurrentStringSet()
for _, dir := range findAllDirs(".", p.SkipDirs) {
p.watchDirs.add("." + string(os.PathSeparator) + dir)
}
}
func (p *Project) restartServices() {
for goFile, args := range p.Services {
if cmd := p.serviceCmdsByFile[goFile]; cmd != nil {
kill(cmd, goFile)
}
exePath := filepath.Join(p.tmpDir, goFile+".exe")
build(goFile, exePath)
p.serviceCmdsByFile[goFile] = start(goFile, exePath, args)
}
}
func (p *Project) runExecutables() {
for goFile, args := range p.Executables {
exePath := filepath.Join(p.tmpDir, goFile+".exe")
build(goFile, exePath)
run(goFile, exePath, args)
}
}