diff --git a/.gitignore b/.gitignore index ffab5d0..a4c4e18 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ # Folders _obj _test +.idea # Architecture specific extensions/prefixes *.[568vq] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..7259aa6 --- /dev/null +++ b/Makefile @@ -0,0 +1,20 @@ +.PHONY: all test clean build install + +GOFLAGS ?= $(GOFLAGS:) + +all: install test + +build: + go build $(GOFLAGS) ./... + +install: + go get $(GOFLAGS) ./... + +test: install + go test $(GOFLAGS) ./... + +bench: install + go test -run=NONE -bench=. $(GOFLAGS) ./... + +clean: + go clean $(GOFLAGS) -i ./... diff --git a/run.go b/run.go index d887691..d4bc44a 100644 --- a/run.go +++ b/run.go @@ -15,6 +15,7 @@ package main import ( + "fmt" "io/ioutil" "os" path "path/filepath" @@ -32,18 +33,25 @@ it will recompile and restart the app after any modifications. `, } -var mainFiles ListOpts - -var downdoc docValue -var gendoc docValue - -// The flags list of the paths excluded from watching -var excludedPaths strFlags - -// Pass through to -tags arg of "go build" -var buildTags string - -var vendorWatch bool +var ( + mainFiles ListOpts + downdoc docValue + gendoc docValue + // The flags list of the paths excluded from watching + excludedPaths strFlags + // Pass through to -tags arg of "go build" + buildTags string + // Application path + currpath string + // Application name + appname string + // Channel to signal an Exit + exit chan bool + // Flag to watch the vendor folder + vendorWatch bool + // Current user workspace + currentGoPath string +) func init() { cmdRun.Run = runApp @@ -53,31 +61,44 @@ func init() { cmdRun.Flag.Var(&excludedPaths, "e", "Excluded paths[].") cmdRun.Flag.BoolVar(&vendorWatch, "vendor", false, "Watch vendor folder") cmdRun.Flag.StringVar(&buildTags, "tags", "", "Build tags (https://golang.org/pkg/go/build/)") + exit = make(chan bool) } -var appname string - func runApp(cmd *Command, args []string) int { ShowShortVersionBanner() - exit := make(chan bool) - crupath, _ := os.Getwd() - if len(args) == 0 || args[0] == "watchall" { - appname = path.Base(crupath) - ColorLog("[INFO] Uses '%s' as 'appname'\n", appname) + currpath, _ = os.Getwd() + + if found, _gopath, _ := SearchGOPATHs(currpath); found { + appname = path.Base(currpath) + currentGoPath = _gopath + } else { + exitPrint(fmt.Sprintf("Bee does not support non Beego project: %s", currpath)) + } + ColorLog("[INFO] Using '%s' as 'appname'\n", appname) } else { - appname = args[0] - ColorLog("[INFO] Uses '%s' as 'appname'\n", appname) - if strings.HasSuffix(appname, ".go") && isExist(path.Join(crupath, appname)) { - ColorLog("[WARN] The appname has conflic with crupath's file, do you want to build appname as %s\n", appname) + // Check if passed Bee application path/name exists in the GOPATH(s) + if found, _gopath, _path := SearchGOPATHs(args[0]); found { + currpath = _path + currentGoPath = _gopath + appname = path.Base(currpath) + } else { + panic(fmt.Sprintf("No Beego application '%s' found in your GOPATH", args[0])) + } + + ColorLog("[INFO] Using '%s' as 'appname'\n", appname) + + if strings.HasSuffix(appname, ".go") && isExist(currpath) { + ColorLog("[WARN] The appname is in conflict with currpath's file, do you want to build appname as %s\n", appname) ColorLog("[INFO] Do you want to overwrite it? [yes|no]] ") if !askForConfirmation() { return 0 } } } - Debugf("current path:%s\n", crupath) + + Debugf("current path:%s\n", currpath) err := loadConfig() if err != nil { @@ -85,19 +106,12 @@ func runApp(cmd *Command, args []string) int { } var paths []string - - readAppDirectories(crupath, &paths) + readAppDirectories(currpath, &paths) // Because monitor files has some issues, we watch current directory // and ignore non-go files. - gps := GetGOPATHs() - if len(gps) == 0 { - ColorLog("[ERRO] Fail to start[ %s ]\n", "$GOPATH is not set or empty") - os.Exit(2) - } - gopath := gps[0] for _, p := range conf.DirStruct.Others { - paths = append(paths, strings.Replace(p, "$GOPATH", gopath, -1)) + paths = append(paths, strings.Replace(p, "$GOPATH", currentGoPath, -1)) } files := []string{} @@ -115,7 +129,7 @@ func runApp(cmd *Command, args []string) int { Autobuild(files, false) } if downdoc == "true" { - if _, err := os.Stat(path.Join(crupath, "swagger")); err != nil { + if _, err := os.Stat(path.Join(currpath, "swagger")); err != nil { if os.IsNotExist(err) { downloadFromURL(swaggerlink, "swagger.zip") unzipAndDelete("swagger.zip", "swagger") @@ -164,7 +178,6 @@ func readAppDirectories(directory string, paths *[]string) { useDirectory = true } } - return } diff --git a/util.go b/util.go index 0f314df..2cd76d3 100644 --- a/util.go +++ b/util.go @@ -15,13 +15,14 @@ package main import ( - "fmt" "log" "os" "path/filepath" "runtime" "strings" "time" + "path" + "fmt" ) // Go is a basic promise implementation: it wraps calls a function in a goroutine @@ -173,6 +174,34 @@ func GetGOPATHs() []string { return paths } +func SearchGOPATHs(app string) (bool, string, string) { + gps := GetGOPATHs() + if len(gps) == 0 { + ColorLog("[ERRO] Fail to start [ %s ]\n", "GOPATH environment variable is not set or empty") + os.Exit(2) + } + + // Lookup the application inside the user workspace(s) + for _, gopath := range gps { + var currentPath string + + if !strings.Contains(app, "src") { + gopathsrc := path.Join(gopath, "src") + currentPath = path.Join(gopathsrc, app) + } else { + currentPath = app + } + + if isExist(currentPath) { + if !isBeegoProject(currentPath) { + continue + } + return true, gopath, currentPath + } + } + return false, "", "" +} + // askForConfirmation uses Scanln to parse user input. A user must type in "yes" or "no" and // then press enter. It has fuzzy matching, so "y", "Y", "yes", "YES", and "Yes" all count as // confirmations. If the input is not recognized, it will ask again. The function does not return diff --git a/watch.go b/watch.go index 7d966bd..1b4af32 100644 --- a/watch.go +++ b/watch.go @@ -121,8 +121,8 @@ func Autobuild(files []string, isgenerate bool) { defer state.Unlock() ColorLog("[INFO] Start building...\n") - path, _ := os.Getwd() - os.Chdir(path) + + os.Chdir(currpath) cmdName := "go" if conf.Gopm.Enable {