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.
|
|
|
|
|
2017-03-06 23:58:53 +00:00
|
|
|
package run
|
2012-12-15 16:56:28 +00:00
|
|
|
|
|
|
|
import (
|
2013-12-15 23:08:51 +00:00
|
|
|
"bytes"
|
2012-12-15 16:56:28 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2016-05-31 19:47:00 +00:00
|
|
|
"regexp"
|
2013-11-26 23:37:29 +00:00
|
|
|
"runtime"
|
2013-07-24 06:58:13 +00:00
|
|
|
"strings"
|
2013-07-06 07:30:57 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
2016-11-14 17:02:29 +00:00
|
|
|
|
2017-03-06 23:58:53 +00:00
|
|
|
"github.com/beego/bee/config"
|
|
|
|
beeLogger "github.com/beego/bee/logger"
|
|
|
|
"github.com/beego/bee/logger/colors"
|
|
|
|
"github.com/beego/bee/utils"
|
2016-12-08 17:31:24 +00:00
|
|
|
"github.com/fsnotify/fsnotify"
|
2012-12-15 16:56:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-02-13 21:05:32 +00:00
|
|
|
cmd *exec.Cmd
|
|
|
|
state sync.Mutex
|
|
|
|
eventTime = make(map[string]int64)
|
|
|
|
scheduleTime time.Time
|
|
|
|
watchExts = []string{".go"}
|
|
|
|
watchExtsStatic = []string{".html", ".tpl", ".js", ".css"}
|
|
|
|
ignoredFilesRegExps = []string{
|
|
|
|
`.#(\w+).go`,
|
|
|
|
`.(\w+).go.swp`,
|
|
|
|
`(\w+).go~`,
|
|
|
|
`(\w+).tmp`,
|
|
|
|
}
|
2012-12-15 16:56:28 +00:00
|
|
|
)
|
|
|
|
|
2016-11-13 14:14:48 +00:00
|
|
|
// NewWatcher starts an fsnotify Watcher on the specified paths
|
2014-06-18 13:31:54 +00:00
|
|
|
func NewWatcher(paths []string, files []string, isgenerate bool) {
|
2012-12-15 16:56:28 +00:00
|
|
|
watcher, err := fsnotify.NewWatcher()
|
|
|
|
if err != nil {
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Fatalf("Failed to create watcher: %s", err)
|
2012-12-15 16:56:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
2016-12-08 17:31:24 +00:00
|
|
|
case e := <-watcher.Events:
|
2017-02-13 21:05:32 +00:00
|
|
|
isBuild := true
|
2013-07-24 06:58:13 +00:00
|
|
|
|
2017-03-06 23:58:53 +00:00
|
|
|
if ifStaticFile(e.Name) && config.Conf.EnableReload {
|
2017-02-13 21:05:32 +00:00
|
|
|
sendReload(e.String())
|
|
|
|
continue
|
|
|
|
}
|
2016-05-31 19:47:00 +00:00
|
|
|
// Skip ignored files
|
|
|
|
if shouldIgnoreFile(e.Name) {
|
2013-07-24 06:58:13 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-01-22 21:28:52 +00:00
|
|
|
if !shouldWatchFileWithExtension(e.Name) {
|
2013-07-30 07:23:14 +00:00
|
|
|
continue
|
|
|
|
}
|
2013-07-24 06:58:13 +00:00
|
|
|
|
2017-04-02 21:36:17 +00:00
|
|
|
mt := utils.GetFileModTime(e.Name)
|
2013-08-15 04:51:26 +00:00
|
|
|
if t := eventTime[e.Name]; mt == t {
|
2017-03-10 18:32:22 +00:00
|
|
|
beeLogger.Log.Hintf(colors.Bold("Skipping: ")+"%s", e.String())
|
2017-02-13 21:05:32 +00:00
|
|
|
isBuild = false
|
2013-07-06 07:30:57 +00:00
|
|
|
}
|
2013-08-15 04:51:26 +00:00
|
|
|
|
|
|
|
eventTime[e.Name] = mt
|
2013-07-06 07:30:57 +00:00
|
|
|
|
2017-02-13 21:05:32 +00:00
|
|
|
if isBuild {
|
2017-03-10 18:32:22 +00:00
|
|
|
beeLogger.Log.Hintf("Event fired: %s", e)
|
2014-11-24 16:42:16 +00:00
|
|
|
go func() {
|
2017-02-11 19:54:12 +00:00
|
|
|
// Wait 1s before autobuild until there is no file change.
|
2014-11-24 16:42:16 +00:00
|
|
|
scheduleTime = time.Now().Add(1 * time.Second)
|
2017-02-11 19:54:12 +00:00
|
|
|
time.Sleep(scheduleTime.Sub(time.Now()))
|
2016-11-13 14:14:48 +00:00
|
|
|
AutoBuild(files, isgenerate)
|
2014-11-24 16:42:16 +00:00
|
|
|
}()
|
2013-07-06 07:30:57 +00:00
|
|
|
}
|
2016-12-08 17:31:24 +00:00
|
|
|
case err := <-watcher.Errors:
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Warnf("Watcher error: %s", err.Error()) // No need to exit here
|
2012-12-15 16:56:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2013-07-24 12:36:22 +00:00
|
|
|
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Info("Initializing watcher...")
|
2012-12-15 16:56:28 +00:00
|
|
|
for _, path := range paths {
|
2017-03-10 18:32:22 +00:00
|
|
|
beeLogger.Log.Hintf(colors.Bold("Watching: ")+"%s", path)
|
2016-12-08 17:31:24 +00:00
|
|
|
err = watcher.Add(path)
|
2012-12-15 16:56:28 +00:00
|
|
|
if err != nil {
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Fatalf("Failed to watch directory: %s", err)
|
2012-12-15 16:56:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-13 14:14:48 +00:00
|
|
|
// AutoBuild builds the specified set of files
|
|
|
|
func AutoBuild(files []string, isgenerate bool) {
|
2013-07-06 08:13:30 +00:00
|
|
|
state.Lock()
|
|
|
|
defer state.Unlock()
|
2012-12-15 16:56:28 +00:00
|
|
|
|
2016-07-22 15:33:05 +00:00
|
|
|
os.Chdir(currpath)
|
2013-07-25 08:26:54 +00:00
|
|
|
|
2013-11-04 03:16:15 +00:00
|
|
|
cmdName := "go"
|
|
|
|
|
2016-12-11 22:39:59 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
stderr bytes.Buffer
|
|
|
|
)
|
2013-07-25 08:26:54 +00:00
|
|
|
// For applications use full import path like "github.com/.../.."
|
|
|
|
// are able to use "go install" to reduce build time.
|
2017-03-06 23:58:53 +00:00
|
|
|
if config.Conf.GoInstall {
|
2016-12-12 20:30:50 +00:00
|
|
|
icmd := exec.Command(cmdName, "install", "-v")
|
|
|
|
icmd.Stdout = os.Stdout
|
|
|
|
icmd.Stderr = os.Stderr
|
|
|
|
icmd.Env = append(os.Environ(), "GOGC=off")
|
|
|
|
icmd.Run()
|
|
|
|
}
|
2013-07-25 08:26:54 +00:00
|
|
|
|
2014-06-18 13:31:54 +00:00
|
|
|
if isgenerate {
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Info("Generating the docs...")
|
2014-06-18 13:31:54 +00:00
|
|
|
icmd := exec.Command("bee", "generate", "docs")
|
2016-03-12 12:05:15 +00:00
|
|
|
icmd.Env = append(os.Environ(), "GOGC=off")
|
2016-12-11 22:39:59 +00:00
|
|
|
err = icmd.Run()
|
|
|
|
if err != nil {
|
2017-03-10 17:43:57 +00:00
|
|
|
utils.Notify("", "Failed to generate the docs.")
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Errorf("Failed to generate the docs.")
|
2016-12-11 22:39:59 +00:00
|
|
|
return
|
|
|
|
}
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Success("Docs generated!")
|
2014-06-18 13:31:54 +00:00
|
|
|
}
|
|
|
|
|
2013-07-25 08:26:54 +00:00
|
|
|
if err == nil {
|
2013-11-26 23:37:29 +00:00
|
|
|
appName := appname
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
appName += ".exe"
|
|
|
|
}
|
2013-12-15 23:08:51 +00:00
|
|
|
|
2014-02-21 17:51:18 +00:00
|
|
|
args := []string{"build"}
|
|
|
|
args = append(args, "-o", appName)
|
2016-02-10 19:39:02 +00:00
|
|
|
if buildTags != "" {
|
|
|
|
args = append(args, "-tags", buildTags)
|
|
|
|
}
|
2014-02-21 17:51:18 +00:00
|
|
|
args = append(args, files...)
|
|
|
|
|
|
|
|
bcmd := exec.Command(cmdName, args...)
|
2016-03-12 12:05:15 +00:00
|
|
|
bcmd.Env = append(os.Environ(), "GOGC=off")
|
2016-12-11 22:39:59 +00:00
|
|
|
bcmd.Stderr = &stderr
|
2013-12-15 23:08:51 +00:00
|
|
|
err = bcmd.Run()
|
2016-12-11 22:39:59 +00:00
|
|
|
if err != nil {
|
2017-03-10 17:43:57 +00:00
|
|
|
utils.Notify(stderr.String(), "Build Failed")
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Errorf("Failed to build the application: %s", stderr.String())
|
2016-12-11 22:39:59 +00:00
|
|
|
return
|
|
|
|
}
|
2013-07-25 08:26:54 +00:00
|
|
|
}
|
2013-07-06 08:13:30 +00:00
|
|
|
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Success("Built Successfully!")
|
2013-07-06 07:30:57 +00:00
|
|
|
Restart(appname)
|
2012-12-15 16:56:28 +00:00
|
|
|
}
|
|
|
|
|
2016-11-13 14:14:48 +00:00
|
|
|
// Kill kills the running command process
|
2012-12-15 16:56:28 +00:00
|
|
|
func Kill() {
|
2013-07-06 08:13:30 +00:00
|
|
|
defer func() {
|
|
|
|
if e := recover(); e != nil {
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Infof("Kill recover: %s", e)
|
2013-07-06 08:13:30 +00:00
|
|
|
}
|
|
|
|
}()
|
2013-09-24 02:42:02 +00:00
|
|
|
if cmd != nil && cmd.Process != nil {
|
2013-12-15 23:08:51 +00:00
|
|
|
err := cmd.Process.Kill()
|
|
|
|
if err != nil {
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Errorf("Error while killing cmd process: %s", err)
|
2013-12-15 23:08:51 +00:00
|
|
|
}
|
2012-12-15 16:56:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-13 14:14:48 +00:00
|
|
|
// Restart kills the running command process and starts it again
|
2013-07-06 07:30:57 +00:00
|
|
|
func Restart(appname string) {
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Debugf("Kill running process", utils.FILE(), utils.LINE())
|
2013-07-06 07:30:57 +00:00
|
|
|
Kill()
|
2013-07-06 08:13:30 +00:00
|
|
|
go Start(appname)
|
2013-07-06 07:30:57 +00:00
|
|
|
}
|
2013-07-06 08:13:30 +00:00
|
|
|
|
2016-11-13 14:14:48 +00:00
|
|
|
// Start starts the command process
|
2012-12-15 16:56:28 +00:00
|
|
|
func Start(appname string) {
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Infof("Restarting '%s'...", appname)
|
|
|
|
if !strings.Contains(appname, "./") {
|
2013-07-22 09:17:08 +00:00
|
|
|
appname = "./" + appname
|
|
|
|
}
|
2013-07-06 07:30:57 +00:00
|
|
|
|
2012-12-15 16:56:28 +00:00
|
|
|
cmd = exec.Command(appname)
|
2013-07-06 07:30:57 +00:00
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
2017-03-06 23:58:53 +00:00
|
|
|
cmd.Args = append([]string{appname}, config.Conf.CmdArgs...)
|
|
|
|
cmd.Env = append(os.Environ(), config.Conf.Envs...)
|
2013-07-06 07:30:57 +00:00
|
|
|
|
2013-07-06 08:13:30 +00:00
|
|
|
go cmd.Run()
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Successf("'%s' is running...", appname)
|
2013-08-24 11:00:08 +00:00
|
|
|
started <- true
|
2012-12-15 16:56:28 +00:00
|
|
|
}
|
2013-07-24 06:58:13 +00:00
|
|
|
|
2017-02-13 21:05:32 +00:00
|
|
|
func ifStaticFile(filename string) bool {
|
|
|
|
for _, s := range watchExtsStatic {
|
|
|
|
if strings.HasSuffix(filename, s) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-11-13 14:14:48 +00:00
|
|
|
// shouldIgnoreFile ignores filenames generated by Emacs, Vim or SublimeText.
|
|
|
|
// It returns true if the file should be ignored, false otherwise.
|
2016-05-31 19:47:00 +00:00
|
|
|
func shouldIgnoreFile(filename string) bool {
|
|
|
|
for _, regex := range ignoredFilesRegExps {
|
|
|
|
r, err := regexp.Compile(regex)
|
|
|
|
if err != nil {
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Fatalf("Could not compile regular expression: %s", err)
|
2016-05-31 19:47:00 +00:00
|
|
|
}
|
|
|
|
if r.MatchString(filename) {
|
|
|
|
return true
|
|
|
|
}
|
2016-09-26 18:32:14 +00:00
|
|
|
continue
|
2013-07-24 06:58:13 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2013-07-30 07:23:14 +00:00
|
|
|
|
2017-01-22 21:28:52 +00:00
|
|
|
// shouldWatchFileWithExtension returns true if the name of the file
|
|
|
|
// hash a suffix that should be watched.
|
|
|
|
func shouldWatchFileWithExtension(name string) bool {
|
2013-09-04 15:23:51 +00:00
|
|
|
for _, s := range watchExts {
|
|
|
|
if strings.HasSuffix(name, s) {
|
|
|
|
return true
|
|
|
|
}
|
2013-07-30 07:23:14 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|