mirror of
https://github.com/beego/bee.git
synced 2025-06-11 18:40:40 +00:00
Refactor!
create sub packages delete unused code delete code from not use command cmdRouter,cmdTest, cmdRundocs make command plugins check with gosimple,staticcheck,go vet,unused,unconvert
This commit is contained in:
88
cmd/commands/run/docs.go
Normal file
88
cmd/commands/run/docs.go
Normal file
@ -0,0 +1,88 @@
|
||||
package run
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
beeLogger "github.com/beego/bee/logger"
|
||||
)
|
||||
|
||||
var (
|
||||
swaggerVersion = "2"
|
||||
swaggerlink = "https://github.com/beego/swagger/archive/v" + swaggerVersion + ".zip"
|
||||
)
|
||||
|
||||
func downloadFromURL(url, fileName string) {
|
||||
var down bool
|
||||
if fd, err := os.Stat(fileName); err != nil && os.IsNotExist(err) {
|
||||
down = true
|
||||
} else if fd.Size() == int64(0) {
|
||||
down = true
|
||||
} else {
|
||||
beeLogger.Log.Infof("'%s' already exists", fileName)
|
||||
return
|
||||
}
|
||||
if down {
|
||||
beeLogger.Log.Infof("Downloading '%s' to '%s'...", url, fileName)
|
||||
output, err := os.Create(fileName)
|
||||
if err != nil {
|
||||
beeLogger.Log.Errorf("Error while creating '%s': %s", fileName, err)
|
||||
return
|
||||
}
|
||||
defer output.Close()
|
||||
|
||||
response, err := http.Get(url)
|
||||
if err != nil {
|
||||
beeLogger.Log.Errorf("Error while downloading '%s': %s", url, err)
|
||||
return
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
n, err := io.Copy(output, response.Body)
|
||||
if err != nil {
|
||||
beeLogger.Log.Errorf("Error while downloading '%s': %s", url, err)
|
||||
return
|
||||
}
|
||||
beeLogger.Log.Successf("%d bytes downloaded!", n)
|
||||
}
|
||||
}
|
||||
|
||||
func unzipAndDelete(src string) error {
|
||||
beeLogger.Log.Infof("Unzipping '%s'...", src)
|
||||
r, err := zip.OpenReader(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer r.Close()
|
||||
|
||||
rp := strings.NewReplacer("swagger-"+swaggerVersion, "swagger")
|
||||
for _, f := range r.File {
|
||||
rc, err := f.Open()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer rc.Close()
|
||||
|
||||
fname := rp.Replace(f.Name)
|
||||
if f.FileInfo().IsDir() {
|
||||
os.MkdirAll(fname, f.Mode())
|
||||
} else {
|
||||
f, err := os.OpenFile(
|
||||
fname, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
_, err = io.Copy(f, rc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
beeLogger.Log.Successf("Done! Deleting '%s'...", src)
|
||||
return os.RemoveAll(src)
|
||||
}
|
192
cmd/commands/run/reload.go
Normal file
192
cmd/commands/run/reload.go
Normal file
@ -0,0 +1,192 @@
|
||||
// Copyright 2017 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.
|
||||
package run
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
beeLogger "github.com/beego/bee/logger"
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
// wsBroker maintains the set of active clients and broadcasts messages to the clients.
|
||||
type wsBroker struct {
|
||||
clients map[*wsClient]bool // Registered clients.
|
||||
broadcast chan []byte // Inbound messages from the clients.
|
||||
register chan *wsClient // Register requests from the clients.
|
||||
unregister chan *wsClient // Unregister requests from clients.
|
||||
}
|
||||
|
||||
func (br *wsBroker) run() {
|
||||
for {
|
||||
select {
|
||||
case client := <-br.register:
|
||||
br.clients[client] = true
|
||||
case client := <-br.unregister:
|
||||
if _, ok := br.clients[client]; ok {
|
||||
delete(br.clients, client)
|
||||
close(client.send)
|
||||
}
|
||||
case message := <-br.broadcast:
|
||||
for client := range br.clients {
|
||||
select {
|
||||
case client.send <- message:
|
||||
default:
|
||||
close(client.send)
|
||||
delete(br.clients, client)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// wsClient represents the end-client.
|
||||
type wsClient struct {
|
||||
broker *wsBroker // The broker.
|
||||
conn *websocket.Conn // The websocket connection.
|
||||
send chan []byte // Buffered channel of outbound messages.
|
||||
}
|
||||
|
||||
// readPump pumps messages from the websocket connection to the broker.
|
||||
func (c *wsClient) readPump() {
|
||||
defer func() {
|
||||
c.broker.unregister <- c
|
||||
c.conn.Close()
|
||||
}()
|
||||
|
||||
for {
|
||||
_, _, err := c.conn.ReadMessage()
|
||||
if err != nil {
|
||||
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
|
||||
beeLogger.Log.Errorf("An error happened when reading from the Websocket client: %v", err)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// write writes a message with the given message type and payload.
|
||||
func (c *wsClient) write(mt int, payload []byte) error {
|
||||
c.conn.SetWriteDeadline(time.Now().Add(writeWait))
|
||||
return c.conn.WriteMessage(mt, payload)
|
||||
}
|
||||
|
||||
// writePump pumps messages from the broker to the websocket connection.
|
||||
func (c *wsClient) writePump() {
|
||||
ticker := time.NewTicker(pingPeriod)
|
||||
defer func() {
|
||||
ticker.Stop()
|
||||
c.conn.Close()
|
||||
}()
|
||||
|
||||
for {
|
||||
select {
|
||||
case message, ok := <-c.send:
|
||||
if !ok {
|
||||
// The broker closed the channel.
|
||||
c.write(websocket.CloseMessage, []byte{})
|
||||
return
|
||||
}
|
||||
|
||||
c.conn.SetWriteDeadline(time.Now().Add(writeWait))
|
||||
w, err := c.conn.NextWriter(websocket.TextMessage)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
w.Write(message)
|
||||
|
||||
n := len(c.send)
|
||||
for i := 0; i < n; i++ {
|
||||
w.Write([]byte("/n"))
|
||||
w.Write(<-c.send)
|
||||
}
|
||||
|
||||
if err := w.Close(); err != nil {
|
||||
return
|
||||
}
|
||||
case <-ticker.C:
|
||||
if err := c.write(websocket.PingMessage, []byte{}); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
broker *wsBroker // The broker.
|
||||
reloadAddress = ":12450" // The port on which the reload server will listen to.
|
||||
|
||||
upgrader = websocket.Upgrader{
|
||||
ReadBufferSize: 1024,
|
||||
WriteBufferSize: 1024,
|
||||
CheckOrigin: func(r *http.Request) bool { return true },
|
||||
}
|
||||
)
|
||||
|
||||
const (
|
||||
writeWait = 10 * time.Second // Time allowed to write a message to the peer.
|
||||
pongWait = 60 * time.Second // Time allowed to read the next pong message from the peer.
|
||||
pingPeriod = (pongWait * 9) / 10 // Send pings to peer with this period. Must be less than pongWait.
|
||||
)
|
||||
|
||||
func startReloadServer() {
|
||||
broker = &wsBroker{
|
||||
broadcast: make(chan []byte),
|
||||
register: make(chan *wsClient),
|
||||
unregister: make(chan *wsClient),
|
||||
clients: make(map[*wsClient]bool),
|
||||
}
|
||||
|
||||
go broker.run()
|
||||
http.HandleFunc("/reload", func(w http.ResponseWriter, r *http.Request) {
|
||||
handleWsRequest(broker, w, r)
|
||||
})
|
||||
|
||||
go startServer()
|
||||
beeLogger.Log.Infof("Reload server listening at %s", reloadAddress)
|
||||
}
|
||||
|
||||
func startServer() {
|
||||
err := http.ListenAndServe(reloadAddress, nil)
|
||||
if err != nil {
|
||||
beeLogger.Log.Errorf("Failed to start up the Reload server: %v", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func sendReload(payload string) {
|
||||
message := bytes.TrimSpace([]byte(payload))
|
||||
broker.broadcast <- message
|
||||
}
|
||||
|
||||
// handleWsRequest handles websocket requests from the peer.
|
||||
func handleWsRequest(broker *wsBroker, w http.ResponseWriter, r *http.Request) {
|
||||
conn, err := upgrader.Upgrade(w, r, nil)
|
||||
if err != nil {
|
||||
beeLogger.Log.Errorf("error while upgrading server connection: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
client := &wsClient{
|
||||
broker: broker,
|
||||
conn: conn,
|
||||
send: make(chan []byte, 256),
|
||||
}
|
||||
client.broker.register <- client
|
||||
|
||||
go client.writePump()
|
||||
client.readPump()
|
||||
}
|
247
cmd/commands/run/run.go
Normal file
247
cmd/commands/run/run.go
Normal file
@ -0,0 +1,247 @@
|
||||
// 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.
|
||||
|
||||
package run
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
path "path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/beego/bee/cmd/commands"
|
||||
"github.com/beego/bee/cmd/commands/version"
|
||||
"github.com/beego/bee/config"
|
||||
beeLogger "github.com/beego/bee/logger"
|
||||
"github.com/beego/bee/utils"
|
||||
)
|
||||
|
||||
var CmdRun = &commands.Command{
|
||||
UsageLine: "run [appname] [watchall] [-main=*.go] [-downdoc=true] [-gendoc=true] [-vendor=true] [-e=folderToExclude] [-ex=extraPackageToWatch] [-tags=goBuildTags] [-runmode=BEEGO_RUNMODE]",
|
||||
Short: "Run the application by starting a local development server",
|
||||
Long: `
|
||||
Run command will supervise the filesystem of the application for any changes, and recompile/restart it.
|
||||
|
||||
`,
|
||||
PreRun: func(cmd *commands.Command, args []string) { version.ShowShortVersionBanner() },
|
||||
Run: RunApp,
|
||||
}
|
||||
|
||||
var (
|
||||
mainFiles utils.ListOpts
|
||||
downdoc utils.DocValue
|
||||
gendoc utils.DocValue
|
||||
// The flags list of the paths excluded from watching
|
||||
excludedPaths utils.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
|
||||
// Current runmode
|
||||
runmode string
|
||||
// Extra directories
|
||||
extraPackages utils.StrFlags
|
||||
)
|
||||
var started = make(chan bool)
|
||||
|
||||
func init() {
|
||||
CmdRun.Flag.Var(&mainFiles, "main", "Specify main go files.")
|
||||
CmdRun.Flag.Var(&gendoc, "gendoc", "Enable auto-generate the docs.")
|
||||
CmdRun.Flag.Var(&downdoc, "downdoc", "Enable auto-download of the swagger file if it does not exist.")
|
||||
CmdRun.Flag.Var(&excludedPaths, "e", "List of paths to exclude.")
|
||||
CmdRun.Flag.BoolVar(&vendorWatch, "vendor", false, "Enable watch vendor folder.")
|
||||
CmdRun.Flag.StringVar(&buildTags, "tags", "", "Set the build tags. See: https://golang.org/pkg/go/build/")
|
||||
CmdRun.Flag.StringVar(&runmode, "runmode", "", "Set the Beego run mode.")
|
||||
CmdRun.Flag.Var(&extraPackages, "ex", "List of extra package to watch.")
|
||||
exit = make(chan bool)
|
||||
commands.AvailableCommands = append(commands.AvailableCommands, CmdRun)
|
||||
}
|
||||
|
||||
func RunApp(cmd *commands.Command, args []string) int {
|
||||
if len(args) == 0 || args[0] == "watchall" {
|
||||
currpath, _ = os.Getwd()
|
||||
if found, _gopath, _ := utils.SearchGOPATHs(currpath); found {
|
||||
appname = path.Base(currpath)
|
||||
currentGoPath = _gopath
|
||||
} else {
|
||||
beeLogger.Log.Fatalf("No application '%s' found in your GOPATH", currpath)
|
||||
}
|
||||
} else {
|
||||
// Check if passed Bee application path/name exists in the GOPATH(s)
|
||||
if found, _gopath, _path := utils.SearchGOPATHs(args[0]); found {
|
||||
currpath = _path
|
||||
currentGoPath = _gopath
|
||||
appname = path.Base(currpath)
|
||||
} else {
|
||||
beeLogger.Log.Fatalf("No application '%s' found in your GOPATH", args[0])
|
||||
}
|
||||
|
||||
if strings.HasSuffix(appname, ".go") && utils.IsExist(currpath) {
|
||||
beeLogger.Log.Warnf("The appname is in conflict with file's current path. Do you want to build appname as '%s'", appname)
|
||||
beeLogger.Log.Info("Do you want to overwrite it? [yes|no] ")
|
||||
if !utils.AskForConfirmation() {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
beeLogger.Log.Infof("Using '%s' as 'appname'", appname)
|
||||
|
||||
beeLogger.Log.Debugf("Current path: %s", utils.FILE(), utils.LINE(), currpath)
|
||||
|
||||
if runmode == "prod" || runmode == "dev" {
|
||||
os.Setenv("BEEGO_RUNMODE", runmode)
|
||||
beeLogger.Log.Infof("Using '%s' as 'runmode'", os.Getenv("BEEGO_RUNMODE"))
|
||||
} else if runmode != "" {
|
||||
os.Setenv("BEEGO_RUNMODE", runmode)
|
||||
beeLogger.Log.Warnf("Using '%s' as 'runmode'", os.Getenv("BEEGO_RUNMODE"))
|
||||
} else if os.Getenv("BEEGO_RUNMODE") != "" {
|
||||
beeLogger.Log.Warnf("Using '%s' as 'runmode'", os.Getenv("BEEGO_RUNMODE"))
|
||||
}
|
||||
|
||||
var paths []string
|
||||
readAppDirectories(currpath, &paths)
|
||||
|
||||
// Because monitor files has some issues, we watch current directory
|
||||
// and ignore non-go files.
|
||||
for _, p := range config.Conf.DirStruct.Others {
|
||||
paths = append(paths, strings.Replace(p, "$GOPATH", currentGoPath, -1))
|
||||
}
|
||||
|
||||
if len(extraPackages) > 0 {
|
||||
// get the full path
|
||||
for _, packagePath := range extraPackages {
|
||||
if found, _, _fullPath := utils.SearchGOPATHs(packagePath); found {
|
||||
readAppDirectories(_fullPath, &paths)
|
||||
} else {
|
||||
beeLogger.Log.Warnf("No extra package '%s' found in your GOPATH", packagePath)
|
||||
}
|
||||
}
|
||||
// let paths unique
|
||||
strSet := make(map[string]struct{})
|
||||
for _, p := range paths {
|
||||
strSet[p] = struct{}{}
|
||||
}
|
||||
paths = make([]string, len(strSet))
|
||||
index := 0
|
||||
for i := range strSet {
|
||||
paths[index] = i
|
||||
index++
|
||||
}
|
||||
}
|
||||
|
||||
files := []string{}
|
||||
for _, arg := range mainFiles {
|
||||
if len(arg) > 0 {
|
||||
files = append(files, arg)
|
||||
}
|
||||
}
|
||||
if downdoc == "true" {
|
||||
if _, err := os.Stat(path.Join(currpath, "swagger", "index.html")); err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
downloadFromURL(swaggerlink, "swagger.zip")
|
||||
unzipAndDelete("swagger.zip")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Start the Reload server (if enabled)
|
||||
if config.Conf.EnableReload {
|
||||
startReloadServer()
|
||||
}
|
||||
if gendoc == "true" {
|
||||
NewWatcher(paths, files, true)
|
||||
AutoBuild(files, true)
|
||||
} else {
|
||||
NewWatcher(paths, files, false)
|
||||
AutoBuild(files, false)
|
||||
}
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-exit:
|
||||
runtime.Goexit()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func readAppDirectories(directory string, paths *[]string) {
|
||||
fileInfos, err := ioutil.ReadDir(directory)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
useDirectory := false
|
||||
for _, fileInfo := range fileInfos {
|
||||
if strings.HasSuffix(fileInfo.Name(), "docs") {
|
||||
continue
|
||||
}
|
||||
if strings.HasSuffix(fileInfo.Name(), "swagger") {
|
||||
continue
|
||||
}
|
||||
|
||||
if !vendorWatch && strings.HasSuffix(fileInfo.Name(), "vendor") {
|
||||
continue
|
||||
}
|
||||
|
||||
if isExcluded(path.Join(directory, fileInfo.Name())) {
|
||||
continue
|
||||
}
|
||||
|
||||
if fileInfo.IsDir() && fileInfo.Name()[0] != '.' {
|
||||
readAppDirectories(directory+"/"+fileInfo.Name(), paths)
|
||||
continue
|
||||
}
|
||||
|
||||
if useDirectory {
|
||||
continue
|
||||
}
|
||||
|
||||
if path.Ext(fileInfo.Name()) == ".go" || (ifStaticFile(fileInfo.Name()) && config.Conf.EnableReload) {
|
||||
*paths = append(*paths, directory)
|
||||
useDirectory = true
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// If a file is excluded
|
||||
func isExcluded(filePath string) bool {
|
||||
for _, p := range excludedPaths {
|
||||
absP, err := path.Abs(p)
|
||||
if err != nil {
|
||||
beeLogger.Log.Errorf("Cannot get absolute path of '%s'", p)
|
||||
continue
|
||||
}
|
||||
absFilePath, err := path.Abs(filePath)
|
||||
if err != nil {
|
||||
beeLogger.Log.Errorf("Cannot get absolute path of '%s'", filePath)
|
||||
break
|
||||
}
|
||||
if strings.HasPrefix(absFilePath, absP) {
|
||||
beeLogger.Log.Infof("'%s' is not being watched", filePath)
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
288
cmd/commands/run/watch.go
Normal file
288
cmd/commands/run/watch.go
Normal file
@ -0,0 +1,288 @@
|
||||
// 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.
|
||||
|
||||
package run
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/beego/bee/config"
|
||||
beeLogger "github.com/beego/bee/logger"
|
||||
"github.com/beego/bee/logger/colors"
|
||||
"github.com/beego/bee/utils"
|
||||
"github.com/fsnotify/fsnotify"
|
||||
)
|
||||
|
||||
var (
|
||||
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`,
|
||||
}
|
||||
)
|
||||
|
||||
// NewWatcher starts an fsnotify Watcher on the specified paths
|
||||
func NewWatcher(paths []string, files []string, isgenerate bool) {
|
||||
watcher, err := fsnotify.NewWatcher()
|
||||
if err != nil {
|
||||
beeLogger.Log.Fatalf("Failed to create watcher: %s", err)
|
||||
}
|
||||
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case e := <-watcher.Events:
|
||||
isBuild := true
|
||||
|
||||
if ifStaticFile(e.Name) && config.Conf.EnableReload {
|
||||
sendReload(e.String())
|
||||
continue
|
||||
}
|
||||
// Skip ignored files
|
||||
if shouldIgnoreFile(e.Name) {
|
||||
continue
|
||||
}
|
||||
if !shouldWatchFileWithExtension(e.Name) {
|
||||
continue
|
||||
}
|
||||
|
||||
mt := getFileModTime(e.Name)
|
||||
if t := eventTime[e.Name]; mt == t {
|
||||
beeLogger.Log.Infof(colors.Bold("Skipping: ")+"%s", e.String())
|
||||
isBuild = false
|
||||
}
|
||||
|
||||
eventTime[e.Name] = mt
|
||||
|
||||
if isBuild {
|
||||
beeLogger.Log.Infof("Event fired: %s", e)
|
||||
go func() {
|
||||
// Wait 1s before autobuild until there is no file change.
|
||||
scheduleTime = time.Now().Add(1 * time.Second)
|
||||
time.Sleep(scheduleTime.Sub(time.Now()))
|
||||
AutoBuild(files, isgenerate)
|
||||
}()
|
||||
}
|
||||
case err := <-watcher.Errors:
|
||||
beeLogger.Log.Warnf("Watcher error: %s", err.Error()) // No need to exit here
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
beeLogger.Log.Info("Initializing watcher...")
|
||||
for _, path := range paths {
|
||||
beeLogger.Log.Infof(colors.Bold("Watching: ")+"%s", path)
|
||||
err = watcher.Add(path)
|
||||
if err != nil {
|
||||
beeLogger.Log.Fatalf("Failed to watch directory: %s", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// getFileModTime returns unix timestamp of `os.File.ModTime` for the given path.
|
||||
func getFileModTime(path string) int64 {
|
||||
path = strings.Replace(path, "\\", "/", -1)
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
beeLogger.Log.Errorf("Failed to open file on '%s': %s", path, err)
|
||||
return time.Now().Unix()
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
fi, err := f.Stat()
|
||||
if err != nil {
|
||||
beeLogger.Log.Errorf("Failed to get file stats: %s", err)
|
||||
return time.Now().Unix()
|
||||
}
|
||||
|
||||
return fi.ModTime().Unix()
|
||||
}
|
||||
|
||||
// AutoBuild builds the specified set of files
|
||||
func AutoBuild(files []string, isgenerate bool) {
|
||||
state.Lock()
|
||||
defer state.Unlock()
|
||||
|
||||
os.Chdir(currpath)
|
||||
|
||||
cmdName := "go"
|
||||
if config.Conf.Gopm.Enable {
|
||||
cmdName = "gopm"
|
||||
}
|
||||
|
||||
var (
|
||||
err error
|
||||
stderr bytes.Buffer
|
||||
stdout bytes.Buffer
|
||||
)
|
||||
// For applications use full import path like "github.com/.../.."
|
||||
// are able to use "go install" to reduce build time.
|
||||
if config.Conf.GoInstall {
|
||||
icmd := exec.Command(cmdName, "install", "-v")
|
||||
icmd.Stdout = os.Stdout
|
||||
icmd.Stderr = os.Stderr
|
||||
icmd.Env = append(os.Environ(), "GOGC=off")
|
||||
icmd.Run()
|
||||
}
|
||||
if config.Conf.Gopm.Install {
|
||||
icmd := exec.Command("go", "list", "./...")
|
||||
icmd.Stdout = &stdout
|
||||
icmd.Env = append(os.Environ(), "GOGC=off")
|
||||
err = icmd.Run()
|
||||
if err == nil {
|
||||
list := strings.Split(stdout.String(), "\n")[1:]
|
||||
for _, pkg := range list {
|
||||
if len(pkg) == 0 {
|
||||
continue
|
||||
}
|
||||
icmd = exec.Command(cmdName, "install", pkg)
|
||||
icmd.Stdout = os.Stdout
|
||||
icmd.Stderr = os.Stderr
|
||||
icmd.Env = append(os.Environ(), "GOGC=off")
|
||||
err = icmd.Run()
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if isgenerate {
|
||||
beeLogger.Log.Info("Generating the docs...")
|
||||
icmd := exec.Command("bee", "generate", "docs")
|
||||
icmd.Env = append(os.Environ(), "GOGC=off")
|
||||
err = icmd.Run()
|
||||
if err != nil {
|
||||
beeLogger.Log.Errorf("Failed to generate the docs.")
|
||||
return
|
||||
}
|
||||
beeLogger.Log.Success("Docs generated!")
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
appName := appname
|
||||
if runtime.GOOS == "windows" {
|
||||
appName += ".exe"
|
||||
}
|
||||
|
||||
args := []string{"build"}
|
||||
args = append(args, "-o", appName)
|
||||
if buildTags != "" {
|
||||
args = append(args, "-tags", buildTags)
|
||||
}
|
||||
args = append(args, files...)
|
||||
|
||||
bcmd := exec.Command(cmdName, args...)
|
||||
bcmd.Env = append(os.Environ(), "GOGC=off")
|
||||
bcmd.Stderr = &stderr
|
||||
err = bcmd.Run()
|
||||
if err != nil {
|
||||
beeLogger.Log.Errorf("Failed to build the application: %s", stderr.String())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
beeLogger.Log.Success("Built Successfully!")
|
||||
Restart(appname)
|
||||
}
|
||||
|
||||
// Kill kills the running command process
|
||||
func Kill() {
|
||||
defer func() {
|
||||
if e := recover(); e != nil {
|
||||
beeLogger.Log.Infof("Kill recover: %s", e)
|
||||
}
|
||||
}()
|
||||
if cmd != nil && cmd.Process != nil {
|
||||
err := cmd.Process.Kill()
|
||||
if err != nil {
|
||||
beeLogger.Log.Errorf("Error while killing cmd process: %s", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Restart kills the running command process and starts it again
|
||||
func Restart(appname string) {
|
||||
beeLogger.Log.Debugf("Kill running process", utils.FILE(), utils.LINE())
|
||||
Kill()
|
||||
go Start(appname)
|
||||
}
|
||||
|
||||
// Start starts the command process
|
||||
func Start(appname string) {
|
||||
beeLogger.Log.Infof("Restarting '%s'...", appname)
|
||||
if !strings.Contains(appname, "./") {
|
||||
appname = "./" + appname
|
||||
}
|
||||
|
||||
cmd = exec.Command(appname)
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
cmd.Args = append([]string{appname}, config.Conf.CmdArgs...)
|
||||
cmd.Env = append(os.Environ(), config.Conf.Envs...)
|
||||
|
||||
go cmd.Run()
|
||||
beeLogger.Log.Successf("'%s' is running...", appname)
|
||||
started <- true
|
||||
}
|
||||
|
||||
func ifStaticFile(filename string) bool {
|
||||
for _, s := range watchExtsStatic {
|
||||
if strings.HasSuffix(filename, s) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// shouldIgnoreFile ignores filenames generated by Emacs, Vim or SublimeText.
|
||||
// It returns true if the file should be ignored, false otherwise.
|
||||
func shouldIgnoreFile(filename string) bool {
|
||||
for _, regex := range ignoredFilesRegExps {
|
||||
r, err := regexp.Compile(regex)
|
||||
if err != nil {
|
||||
beeLogger.Log.Fatalf("Could not compile regular expression: %s", err)
|
||||
}
|
||||
if r.MatchString(filename) {
|
||||
return true
|
||||
}
|
||||
continue
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// shouldWatchFileWithExtension returns true if the name of the file
|
||||
// hash a suffix that should be watched.
|
||||
func shouldWatchFileWithExtension(name string) bool {
|
||||
for _, s := range watchExts {
|
||||
if strings.HasSuffix(name, s) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
Reference in New Issue
Block a user