| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"io/ioutil" |
| 6 |
"log" |
| 7 |
"os" |
| 8 |
"os/exec" |
| 9 |
"os/signal" |
| 10 |
"path" |
| 11 |
"path/filepath" |
| 12 |
"regexp" |
| 13 |
"runtime" |
| 14 |
"syscall" |
| 15 |
"time" |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
"github.com/mgutz/minimist" |
| 20 |
"gopkg.in/godo.v2" |
| 21 |
"gopkg.in/godo.v2/util" |
| 22 |
"gopkg.in/godo.v2/watcher" |
| 23 |
) |
| 24 |
|
| 25 |
var isWindows = runtime.GOOS == "windows" |
| 26 |
var isRebuild bool |
| 27 |
var isWatch bool |
| 28 |
var isVerbose bool |
| 29 |
var hasTasks bool |
| 30 |
|
| 31 |
func checkError(err error, format string, args ...interface{}) { |
| 32 |
if err != nil { |
| 33 |
util.Error("ERR", format, args...) |
| 34 |
os.Exit(1) |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
func hasMain(data []byte) bool { |
| 39 |
hasMainRe := regexp.MustCompile(`\nfunc main\(`) |
| 40 |
matches := hasMainRe.Find(data) |
| 41 |
return len(matches) > 0 |
| 42 |
} |
| 43 |
|
| 44 |
func isPackageMain(data []byte) bool { |
| 45 |
isMainRe := regexp.MustCompile(`(\n|^)?package main\b`) |
| 46 |
matches := isMainRe.Find(data) |
| 47 |
return len(matches) > 0 |
| 48 |
} |
| 49 |
|
| 50 |
func main() { |
| 51 |
|
| 52 |
godoFiles := []string{"Gododir/main.go", "Gododir/Godofile.go", "tasks/Godofile.go"} |
| 53 |
src := "" |
| 54 |
for _, filename := range godoFiles { |
| 55 |
src = util.FindUp(".", filename) |
| 56 |
if src != "" { |
| 57 |
break |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
if src == "" { |
| 62 |
godo.Usage("") |
| 63 |
os.Exit(0) |
| 64 |
} |
| 65 |
|
| 66 |
wd, err := os.Getwd() |
| 67 |
if err != nil { |
| 68 |
util.Error("godo", "Could not get working directory: %s\n", err.Error()) |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
absParentDir, err := filepath.Abs(filepath.Dir(filepath.Dir(src))) |
| 73 |
if err != nil { |
| 74 |
util.Error("godo", "Could not get absolute parent of %s: %s\n", src, err.Error()) |
| 75 |
} |
| 76 |
if wd != absParentDir { |
| 77 |
relDir, _ := filepath.Rel(wd, src) |
| 78 |
os.Chdir(absParentDir) |
| 79 |
util.Info("godo", "Using %s\n", relDir) |
| 80 |
} |
| 81 |
|
| 82 |
os.Setenv("GODOFILE", src) |
| 83 |
argm := minimist.Parse() |
| 84 |
isRebuild = argm.AsBool("rebuild") |
| 85 |
isWatch = argm.AsBool("w", "watch") |
| 86 |
isVerbose = argm.AsBool("v", "verbose") |
| 87 |
hasTasks = len(argm.NonFlags()) > 0 |
| 88 |
run(src) |
| 89 |
} |
| 90 |
|
| 91 |
func run(godoFile string) { |
| 92 |
if isWatch { |
| 93 |
runAndWatch(godoFile) |
| 94 |
} else { |
| 95 |
cmd, _ := buildCommand(godoFile, false) |
| 96 |
err := cmd.Run() |
| 97 |
if err != nil { |
| 98 |
log.Fatal(err) |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
func buildCommand(godoFile string, forceBuild bool) (*exec.Cmd, string) { |
| 104 |
exe := buildMain(godoFile, forceBuild) |
| 105 |
cmd := exec.Command(exe, os.Args[1:]...) |
| 106 |
cmd.Stdout = os.Stdout |
| 107 |
cmd.Stderr = os.Stderr |
| 108 |
cmd.Stdin = os.Stdin |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
env := godoenv(godoFile) |
| 113 |
if env != "" { |
| 114 |
cmd.Env = godo.EffectiveEnv(godo.ParseStringEnv(env)) |
| 115 |
} |
| 116 |
|
| 117 |
return cmd, exe |
| 118 |
} |
| 119 |
|
| 120 |
func godoenv(godoFile string) string { |
| 121 |
godoenvFile := filepath.Join(filepath.Dir(godoFile), "godoenv") |
| 122 |
if _, err := os.Stat(godoenvFile); err == nil { |
| 123 |
b, err := ioutil.ReadFile(godoenvFile) |
| 124 |
if err != nil { |
| 125 |
util.Error("godo", "Cannot read %s file", godoenvFile) |
| 126 |
os.Exit(1) |
| 127 |
} |
| 128 |
return string(b) |
| 129 |
} |
| 130 |
return "" |
| 131 |
} |
| 132 |
|
| 133 |
func runAndWatch(godoFile string) { |
| 134 |
done := make(chan bool, 1) |
| 135 |
run := func(forceBuild bool) (*exec.Cmd, string) { |
| 136 |
cmd, exe := buildCommand(godoFile, forceBuild) |
| 137 |
cmd.Start() |
| 138 |
go func() { |
| 139 |
err := cmd.Wait() |
| 140 |
done <- true |
| 141 |
if err != nil { |
| 142 |
if isVerbose { |
| 143 |
util.Debug("godo", "godo process killed\n") |
| 144 |
} |
| 145 |
} |
| 146 |
}() |
| 147 |
return cmd, exe |
| 148 |
} |
| 149 |
|
| 150 |
bufferSize := 2048 |
| 151 |
watchr, err := watcher.NewWatcher(bufferSize) |
| 152 |
if err != nil { |
| 153 |
util.Panic("project", "%v\n", err) |
| 154 |
} |
| 155 |
godoDir := filepath.Dir(godoFile) |
| 156 |
watchr.WatchRecursive(godoDir) |
| 157 |
watchr.ErrorHandler = func(err error) { |
| 158 |
util.Error("godo", "Watcher error %v\n", err) |
| 159 |
} |
| 160 |
|
| 161 |
cmd, exe := run(false) |
| 162 |
|
| 163 |
|
| 164 |
watchr.Start() |
| 165 |
util.Info("godo", "watching %s\n", godoDir) |
| 166 |
|
| 167 |
<-time.After(godo.GetWatchDelay() + (300 * time.Millisecond)) |
| 168 |
|
| 169 |
c := make(chan os.Signal, 1) |
| 170 |
signal.Notify(c, os.Interrupt) |
| 171 |
go func() { |
| 172 |
for range c { |
| 173 |
killGodo(cmd, false) |
| 174 |
os.Exit(0) |
| 175 |
} |
| 176 |
}() |
| 177 |
|
| 178 |
|
| 179 |
for { |
| 180 |
select { |
| 181 |
case event := <-watchr.Event: |
| 182 |
|
| 183 |
|
| 184 |
if event.Path == exe || event.Path == path.Join(path.Dir(exe), path.Base(path.Dir(exe))) { |
| 185 |
continue |
| 186 |
} |
| 187 |
util.Debug("watchmain", "%+v\n", event) |
| 188 |
killGodo(cmd, true) |
| 189 |
<-done |
| 190 |
cmd, _ = run(true) |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
func killGodo(cmd *exec.Cmd, killProcessGroup bool) { |
| 198 |
cmd.Process.Kill() |
| 199 |
|
| 200 |
|
| 201 |
if killProcessGroup { |
| 202 |
pgid, err := syscall.Getpgid(cmd.Process.Pid) |
| 203 |
if err != nil { |
| 204 |
panic(err) |
| 205 |
} |
| 206 |
syscall.Kill(-pgid, syscall.SIGKILL) |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
func mustBeMain(src string) { |
| 211 |
data, err := ioutil.ReadFile(src) |
| 212 |
if err != nil { |
| 213 |
fmt.Fprintln(os.Stderr, err) |
| 214 |
os.Exit(1) |
| 215 |
} |
| 216 |
|
| 217 |
if !hasMain(data) { |
| 218 |
msg := `%s is not runnable. Rename package OR make it runnable by adding |
| 219 |
|
| 220 |
func main() { |
| 221 |
godo.Godo(tasks) |
| 222 |
} |
| 223 |
` |
| 224 |
fmt.Printf(msg, src) |
| 225 |
os.Exit(1) |
| 226 |
} |
| 227 |
|
| 228 |
if !isPackageMain(data) { |
| 229 |
msg := `%s is not runnable. It must be package main` |
| 230 |
fmt.Printf(msg, src) |
| 231 |
os.Exit(1) |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
func buildMain(src string, forceBuild bool) string { |
| 236 |
mustBeMain(src) |
| 237 |
dir := filepath.Dir(src) |
| 238 |
|
| 239 |
exeFile := "godobin-" + godo.Version |
| 240 |
if isWindows { |
| 241 |
exeFile += ".exe" |
| 242 |
} |
| 243 |
|
| 244 |
exe := filepath.Join(dir, exeFile) |
| 245 |
|
| 246 |
build := false |
| 247 |
reasonFormat := "" |
| 248 |
if isRebuild || forceBuild { |
| 249 |
build = true |
| 250 |
reasonFormat = "Rebuilding %s...\n" |
| 251 |
} else { |
| 252 |
build = util.Outdated([]string{dir + "/**/*.go"}, []string{exe}) |
| 253 |
reasonFormat = "Godo tasks changed. Rebuilding %s...\n" |
| 254 |
} |
| 255 |
|
| 256 |
if build { |
| 257 |
util.Debug("godo", reasonFormat, exe) |
| 258 |
env := godoenv(src) |
| 259 |
if env != "" { |
| 260 |
godo.Env = env |
| 261 |
} |
| 262 |
_, err := godo.Run("go build -a -o "+exeFile, godo.M{"$in": dir}) |
| 263 |
if err != nil { |
| 264 |
panic(fmt.Sprintf("Error building %s: %s\n", src, err.Error())) |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
if filepath.Base(dir) == "Gododir" { |
| 269 |
orphanedFile := filepath.Join(dir, filepath.Base(dir)) |
| 270 |
if _, err := os.Stat(orphanedFile); err == nil { |
| 271 |
os.Remove(orphanedFile) |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
if isRebuild { |
| 277 |
util.Info("godo", "ok\n") |
| 278 |
} |
| 279 |
|
| 280 |
return exe |
| 281 |
} |