2013-09-03 17:23:58 +00:00
|
|
|
// Copyright 2013 bee authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"): you may
|
|
|
|
// not use this file except in compliance with the License. You may obtain
|
|
|
|
// a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
// License for the specific language governing permissions and limitations
|
|
|
|
// under the License.
|
|
|
|
|
2013-07-24 12:01:14 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-01-22 16:57:16 +00:00
|
|
|
"io/ioutil"
|
2013-07-24 12:01:14 +00:00
|
|
|
"os"
|
|
|
|
path "path/filepath"
|
|
|
|
"runtime"
|
2013-09-11 23:06:04 +00:00
|
|
|
"strings"
|
2013-07-24 12:01:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var cmdRun = &Command{
|
2014-08-08 16:55:55 +00:00
|
|
|
UsageLine: "run [appname] [watchall] [-main=*.go] [-downdoc=true] [-gendoc=true]",
|
2013-07-24 12:01:14 +00:00
|
|
|
Short: "run the app which can hot compile",
|
|
|
|
Long: `
|
|
|
|
start the appname throw exec.Command
|
|
|
|
|
|
|
|
then start a inotify watch for current dir
|
|
|
|
|
|
|
|
when the file has changed bee will auto go build and restart the app
|
|
|
|
|
|
|
|
file changed
|
|
|
|
|
|
|
|
|
check if it's go file
|
|
|
|
|
|
|
|
|
yes no
|
|
|
|
| |
|
|
|
|
go build do nothing
|
|
|
|
|
|
|
|
|
restart app
|
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
2014-02-21 17:51:18 +00:00
|
|
|
var mainFiles ListOpts
|
|
|
|
|
2014-06-23 15:02:46 +00:00
|
|
|
var downdoc docValue
|
2014-08-08 16:55:55 +00:00
|
|
|
var gendoc docValue
|
2014-06-23 15:02:46 +00:00
|
|
|
|
2013-07-24 12:01:14 +00:00
|
|
|
func init() {
|
|
|
|
cmdRun.Run = runApp
|
2014-02-21 17:51:18 +00:00
|
|
|
cmdRun.Flag.Var(&mainFiles, "main", "specify main go files")
|
2014-08-08 16:55:55 +00:00
|
|
|
cmdRun.Flag.Var(&gendoc, "gendoc", "auto generate the docs")
|
2014-06-23 15:02:46 +00:00
|
|
|
cmdRun.Flag.Var(&downdoc, "downdoc", "auto download swagger file when not exist")
|
2013-07-24 12:01:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var appname string
|
|
|
|
|
2014-08-15 09:38:51 +00:00
|
|
|
func runApp(cmd *Command, args []string) int {
|
2013-08-09 09:49:14 +00:00
|
|
|
exit := make(chan bool)
|
2013-09-11 22:35:24 +00:00
|
|
|
crupath, _ := os.Getwd()
|
2014-01-22 16:57:16 +00:00
|
|
|
|
|
|
|
if len(args) == 0 || args[0] == "watchall" {
|
2013-09-11 22:35:24 +00:00
|
|
|
appname = path.Base(crupath)
|
2013-10-30 23:39:44 +00:00
|
|
|
ColorLog("[INFO] Uses '%s' as 'appname'\n", appname)
|
2013-09-11 22:35:24 +00:00
|
|
|
} else {
|
|
|
|
appname = args[0]
|
2013-07-24 12:01:14 +00:00
|
|
|
}
|
|
|
|
Debugf("current path:%s\n", crupath)
|
|
|
|
|
|
|
|
err := loadConfig()
|
|
|
|
if err != nil {
|
2013-10-30 23:39:44 +00:00
|
|
|
ColorLog("[ERRO] Fail to parse bee.json[ %s ]\n", err)
|
2013-07-24 12:01:14 +00:00
|
|
|
}
|
2013-11-04 03:16:15 +00:00
|
|
|
|
2013-07-24 12:01:14 +00:00
|
|
|
var paths []string
|
2014-01-22 16:57:16 +00:00
|
|
|
|
2014-06-18 13:31:54 +00:00
|
|
|
readAppDirectories(crupath, &paths)
|
|
|
|
|
2013-07-31 06:44:56 +00:00
|
|
|
// Because monitor files has some issues, we watch current directory
|
|
|
|
// and ignore non-go files.
|
2013-10-30 23:39:44 +00:00
|
|
|
gps := GetGOPATHs()
|
2013-09-11 23:06:04 +00:00
|
|
|
if len(gps) == 0 {
|
2013-10-30 23:39:44 +00:00
|
|
|
ColorLog("[ERRO] Fail to start[ %s ]\n", "$GOPATH is not set or empty")
|
2013-09-11 23:06:04 +00:00
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
gopath := gps[0]
|
|
|
|
for _, p := range conf.DirStruct.Others {
|
|
|
|
paths = append(paths, strings.Replace(p, "$GOPATH", gopath, -1))
|
|
|
|
}
|
2013-07-24 12:01:14 +00:00
|
|
|
|
2014-02-21 17:51:18 +00:00
|
|
|
files := []string{}
|
|
|
|
for _, arg := range mainFiles {
|
|
|
|
if len(arg) > 0 {
|
|
|
|
files = append(files, arg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-08 16:55:55 +00:00
|
|
|
if gendoc == "true" {
|
2014-06-18 13:31:54 +00:00
|
|
|
NewWatcher(paths, files, true)
|
|
|
|
Autobuild(files, true)
|
|
|
|
} else {
|
|
|
|
NewWatcher(paths, files, false)
|
|
|
|
Autobuild(files, false)
|
|
|
|
}
|
2014-06-23 15:02:46 +00:00
|
|
|
if downdoc == "true" {
|
|
|
|
if _, err := os.Stat(path.Join(crupath, "swagger")); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
downloadFromUrl(swaggerlink, "swagger.zip")
|
2014-06-23 15:03:10 +00:00
|
|
|
unzipAndDelete("swagger.zip", "swagger")
|
2014-06-23 15:02:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-24 12:01:14 +00:00
|
|
|
for {
|
2013-08-09 09:49:14 +00:00
|
|
|
select {
|
|
|
|
case <-exit:
|
|
|
|
runtime.Goexit()
|
|
|
|
}
|
2013-07-24 12:01:14 +00:00
|
|
|
}
|
2014-08-15 09:38:51 +00:00
|
|
|
return 0
|
2013-07-24 12:01:14 +00:00
|
|
|
}
|
2014-01-22 16:57:16 +00:00
|
|
|
|
|
|
|
func readAppDirectories(directory string, paths *[]string) {
|
|
|
|
fileInfos, err := ioutil.ReadDir(directory)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
useDiectory := false
|
|
|
|
for _, fileInfo := range fileInfos {
|
2014-06-18 13:31:54 +00:00
|
|
|
if strings.HasSuffix(fileInfo.Name(), "docs") {
|
|
|
|
continue
|
|
|
|
}
|
2014-01-22 16:57:16 +00:00
|
|
|
if fileInfo.IsDir() == true && fileInfo.Name()[0] != '.' {
|
|
|
|
readAppDirectories(directory+"/"+fileInfo.Name(), paths)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if useDiectory == true {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if path.Ext(fileInfo.Name()) == ".go" {
|
|
|
|
*paths = append(*paths, directory)
|
|
|
|
useDiectory = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|