@@ -7,13 +7,18 @@ import (
77 "os"
88 "path/filepath"
99 "regexp"
10+ "runtime"
1011 "strings"
12+ "time"
1113
14+ "github.com/mgutz/minimist"
1215 "github.com/mgutz/str"
1316 "gopkg.in/godo.v1"
1417 "gopkg.in/godo.v1/util"
1518)
1619
20+ var isWindows = runtime .GOOS == "windows"
21+
1722func checkError (err error , format string , args ... interface {}) {
1823 if err != nil {
1924 util .Error ("ERR" , format , args ... )
@@ -34,6 +39,18 @@ func isPackageMain(data []byte) bool {
3439}
3540
3641func main () {
42+ // cfg := profile.Config{
43+ // BlockProfile: true,
44+ // CPUProfile: true,
45+ // MemProfile: true,
46+ // NoShutdownHook: true, // do not hook SIGINT
47+ // }
48+
49+ // // p.Stop() must be called before the program exits to
50+ // // ensure profiling information is written to disk.
51+ // p := profile.Start(&cfg)
52+ // defer p.Stop()
53+
3754 // legacy version used tasks/
3855 godoFiles := []string {"Gododir/Godofile.go" , "tasks/Godofile.go" }
3956 src := ""
@@ -57,67 +74,89 @@ func main() {
5774 os .Exit (1 )
5875 }
5976
60- mainFile := buildMain (rel )
61- if mainFile != "" {
62- src = mainFile
63- defer os .RemoveAll (filepath .Dir (mainFile ))
64- }
65- cmd := "go run " + src + " " + strings .Join (os .Args [1 :], " " )
77+ exe := buildMain (rel )
78+ cmd := exe + " " + strings .Join (os .Args [1 :], " " )
79+ cmd = str .Clean (cmd )
6680 // errors are displayed by tasks
81+
6782 godo .Run (cmd )
6883}
6984
70- func buildMain (src string ) string {
71- tempFile := ""
85+ type godorc struct {
86+ ModTime time.Time
87+ Size int64
88+ }
89+
90+ func mustBeMain (src string ) {
7291 data , err := ioutil .ReadFile (src )
7392 if err != nil {
7493 fmt .Fprintln (os .Stderr , err )
7594 os .Exit (1 )
7695 }
7796
7897 if ! hasMain (data ) {
79- if isPackageMain (data ) {
80- msg := `%s is not runnable. Rename package OR make it runnable by adding
81-
82- func main() {
83- godo.Godo(Tasks)
84- }
85- `
86- fmt .Printf (msg , src )
87- os .Exit (1 )
88- }
98+ msg := `%s is not runnable. Rename package OR make it runnable by adding
99+
100+ func main() {
101+ godo.Godo(tasks)
102+ }
103+ `
104+ fmt .Printf (msg , src )
105+ os .Exit (1 )
106+ }
107+
108+ if ! isPackageMain (data ) {
109+ msg := `%s is not runnable. It must be package main`
110+ fmt .Printf (msg , src )
111+ os .Exit (1 )
112+ }
113+ }
114+
115+ func buildMain (src string ) string {
116+ mustBeMain (src )
117+
118+ exeFile := "godobin"
119+ if isWindows {
120+ exeFile = "godobin.exe"
121+ }
122+
123+ dir := filepath .Dir (src )
124+ exe := filepath .Join (dir , exeFile )
125+ reasonFormat := "Godofile changed. Rebuilding %s...\n "
126+
127+ argm := minimist .Parse ()
128+ rebuild := argm .ZeroBool ("rebuild" )
129+ if rebuild {
130+ os .Remove (exe )
131+ }
132+
133+ // see if last run exists .godoinfo
134+ fiExe , err := os .Stat (exe )
135+ build := os .IsNotExist (err )
136+ if build {
137+ reasonFormat = "Building %s...\n "
138+ }
139+
140+ fiGodofile , err := os .Stat (src )
141+ if os .IsNotExist (err ) {
142+ log .Fatalln (err )
143+ os .Exit (1 )
144+ }
145+ build = build || fiExe .ModTime ().Before (fiGodofile .ModTime ())
146+
147+ if build {
148+ util .Debug ("godo" , reasonFormat , exe )
89149
90- template := `
91- package main
92- import (
93- "gopkg.in/godo.v1"
94- pkg "{{package}}"
95- )
96- func main() {
97- godo.Godo(pkg.Tasks)
98- }
99- `
100- packageName , err := util .PackageName (src )
150+ err := godo .Run ("go build -a -o " + exeFile , godo.In {dir })
101151 if err != nil {
102152 panic (err )
103153 }
104- code := str .Template (template , map [string ]interface {}{
105- "package" : filepath .ToSlash (packageName ),
106- })
107- //log.Println("DBG template", code)
108- tempDir , err := ioutil .TempDir ("" , "godo" )
109- if err != nil {
110- panic ("Could not create temp directory" )
111- }
112- //log.Printf("code\n %s\n", code)
113- tempFile = filepath .Join (tempDir , "Godofile_main.go" )
114- err = ioutil .WriteFile (tempFile , []byte (code ), 0644 )
115- if err != nil {
116- log .Panicf ("Could not write temp file %s\n " , tempFile )
117- }
154+ }
118155
119- src = tempFile
120- return src
156+ if rebuild {
157+ util .Info ("godo" , "ok" )
158+ os .Exit (0 )
121159 }
122- return ""
160+
161+ return exe
123162}
0 commit comments