forked from liudng/dogo
/
dogo.go
215 lines (178 loc) · 4.21 KB
/
dogo.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright 2014 The dogo Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"github.com/zhgo/console"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
)
//Dogo struct
type Dogo struct {
//Decreasing
Decreasing uint8
//decreasing
decreasing uint8
//source files
SourceDir []string
//file extends
SourceExt string
//Working Dir
WorkingDir string
//build command
BuildCmd string
//run command
RunCmd string
//file list
Files map[string]time.Time
//Cmd object
cmd *exec.Cmd
//file modified
isModified bool
//build error
buildErr string
//build retry
retries int64
runCount int64
}
//start new monitor
func (d *Dogo) NewMonitor() {
//fmt.Printf("%#v\n", d.SourceDir)
if d.WorkingDir == "" {
//log.Fatalf("[dogo] dogo.json (BuildCmd) error. \n")
d.WorkingDir = console.WorkingDir
}
if len(d.SourceDir) == 0 {
//log.Fatalf("[dogo] dogo.json (SourceDir) error. \n")
d.SourceDir = append(d.SourceDir, console.WorkingDir)
}
if d.SourceExt == "" {
//log.Fatalf("[dogo] dogo.json (SourceExt) error. \n")
d.SourceExt = ".go|.c|.cpp|.h"
}
if d.BuildCmd == "" {
//log.Fatalf("[dogo] dogo.json (BuildCmd) error. \n")
d.BuildCmd = "go build ."
}
if d.RunCmd == "" {
//log.Fatalf("[dogo] dogo.json (RunCmd) error. \n")
d.RunCmd = filepath.Base(console.WorkingDir)
if runtime.GOOS == "windows" {
d.RunCmd += ".exe"
}
}
// Append the current directory to the PATH for compatible linux.
console.Setenv("PATH", console.Getenv("PATH")+string(os.PathListSeparator)+d.WorkingDir)
console.Chdir(d.WorkingDir)
fmt.Printf("[dogo] Working Directory:\n")
fmt.Printf(" %s\n", d.WorkingDir)
fmt.Printf("[dogo] Monitoring Directories:\n")
for _, dir := range d.SourceDir {
fmt.Printf(" %s\n", dir)
}
fmt.Printf("[dogo] File extends:\n")
fmt.Printf(" %s\n", d.SourceExt)
fmt.Printf("[dogo] Build command:\n")
fmt.Printf(" %s\n", d.BuildCmd)
fmt.Printf("[dogo] Run command:\n")
fmt.Printf(" %s\n", d.RunCmd)
d.Files = make(map[string]time.Time)
d.InitFiles()
//FIXME: add console support.
//FIXME: moniting directories: add file, delete file.
//FIXME: Multi commands.
}
func (d *Dogo) InitFiles() {
extends := strings.Split(d.SourceExt, "|")
//scan source directories
for _, dir := range d.SourceDir {
filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
if err != nil {
d.FmtPrintf("%s\n", err)
return err
}
for _, ext := range extends {
if filepath.Ext(path) == ext {
//fmt.Println(path)
d.Files[path] = f.ModTime()
break
}
}
return nil
})
}
}
func (d *Dogo) BuildAndRun() {
if d.cmd != nil && d.cmd.Process != nil {
d.FmtPrintf("[dogo] Terminate the process %d: ", d.cmd.Process.Pid)
if err := d.cmd.Process.Kill(); err != nil {
d.FmtPrintf("\n%s\n", err)
} else {
d.FmtPrintf("success.\n")
}
}
if err := d.Build(); err != nil {
d.FmtPrintf("[dogo] Build failed: %s\n\n", err)
} else {
//run program
d.FmtPrintf("[dogo] Start the process: %s\n\n", d.RunCmd)
if d.runCount > 0 {
d.decreasing = d.Decreasing
}
d.runCount++
go d.Run()
}
}
//build
func (d *Dogo) Build() error {
d.FmtPrintf("[dogo] Start build: ")
args := console.ParseText(d.BuildCmd)
out, err := exec.Command(args[0], args[1:]...).CombinedOutput()
if err != nil {
fullOut := string(out)
if d.buildErr == "" || d.buildErr != fullOut {
d.FmtPrintf("\n%s", fullOut)
d.retries = 0
d.buildErr = fullOut
} else {
//d.FmtPrintf(".")
d.retries++
}
return err
} else {
d.retries = 0
d.buildErr = ""
d.FmtPrintf("success.\n")
return nil
}
}
//run it
func (d *Dogo) Run() {
args := console.ParseText(d.RunCmd)
d.cmd = exec.Command(args[0], args[1:]...)
d.cmd.Stdin = os.Stdin
d.cmd.Stdout = os.Stdout
d.cmd.Stderr = os.Stderr
err := d.cmd.Run()
if err != nil {
fmt.Printf("%s\n", err)
} else {
d.cmd = nil
}
}
func (d *Dogo) LogPrintf(format string, v ...interface{}) {
if d.retries == 0 {
log.Printf(format, v...)
}
}
func (d *Dogo) FmtPrintf(format string, v ...interface{}) {
if d.retries == 0 {
fmt.Printf(format, v...)
}
}