1
0
mirror of https://github.com/beego/bee.git synced 2024-11-25 20:10:55 +00:00

improve live reload

This commit is contained in:
Sergey Lanzman 2017-02-13 23:05:32 +02:00
parent 268a9d8346
commit b867a57d65
2 changed files with 34 additions and 21 deletions

12
run.go
View File

@ -137,6 +137,11 @@ func runApp(cmd *Command, args []string) int {
} }
} }
} }
// Start the Reload server (if enabled)
if conf.EnableReload {
startReloadServer()
}
if gendoc == "true" { if gendoc == "true" {
NewWatcher(paths, files, true) NewWatcher(paths, files, true)
AutoBuild(files, true) AutoBuild(files, true)
@ -145,11 +150,6 @@ func runApp(cmd *Command, args []string) int {
AutoBuild(files, false) AutoBuild(files, false)
} }
// Start the Reload server (if enabled)
if conf.EnableReload {
startReloadServer()
}
for { for {
select { select {
case <-exit: case <-exit:
@ -190,7 +190,7 @@ func readAppDirectories(directory string, paths *[]string) {
continue continue
} }
if path.Ext(fileInfo.Name()) == ".go" { if path.Ext(fileInfo.Name()) == ".go" || (ifStaticFile(fileInfo.Name()) && conf.EnableReload) {
*paths = append(*paths, directory) *paths = append(*paths, directory)
useDirectory = true useDirectory = true
} }

View File

@ -32,6 +32,14 @@ var (
state sync.Mutex state sync.Mutex
eventTime = make(map[string]int64) eventTime = make(map[string]int64)
scheduleTime time.Time 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 // NewWatcher starts an fsnotify Watcher on the specified paths
@ -45,8 +53,12 @@ func NewWatcher(paths []string, files []string, isgenerate bool) {
for { for {
select { select {
case e := <-watcher.Events: case e := <-watcher.Events:
isbuild := true isBuild := true
if ifStaticFile(e.Name) || conf.EnableReload {
sendReload(e.String())
continue
}
// Skip ignored files // Skip ignored files
if shouldIgnoreFile(e.Name) { if shouldIgnoreFile(e.Name) {
continue continue
@ -58,12 +70,12 @@ func NewWatcher(paths []string, files []string, isgenerate bool) {
mt := getFileModTime(e.Name) mt := getFileModTime(e.Name)
if t := eventTime[e.Name]; mt == t { if t := eventTime[e.Name]; mt == t {
logger.Infof(bold("Skipping: ")+"%s", e.String()) logger.Infof(bold("Skipping: ")+"%s", e.String())
isbuild = false isBuild = false
} }
eventTime[e.Name] = mt eventTime[e.Name] = mt
if isbuild { if isBuild {
logger.Infof("Event fired: %s", e) logger.Infof("Event fired: %s", e)
go func() { go func() {
// Wait 1s before autobuild until there is no file change. // Wait 1s before autobuild until there is no file change.
@ -239,6 +251,15 @@ func Start(appname string) {
started <- true 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. // shouldIgnoreFile ignores filenames generated by Emacs, Vim or SublimeText.
// It returns true if the file should be ignored, false otherwise. // It returns true if the file should be ignored, false otherwise.
func shouldIgnoreFile(filename string) bool { func shouldIgnoreFile(filename string) bool {
@ -255,14 +276,6 @@ func shouldIgnoreFile(filename string) bool {
return false return false
} }
var watchExts = []string{".go"}
var ignoredFilesRegExps = []string{
`.#(\w+).go`,
`.(\w+).go.swp`,
`(\w+).go~`,
`(\w+).tmp`,
}
// shouldWatchFileWithExtension returns true if the name of the file // shouldWatchFileWithExtension returns true if the name of the file
// hash a suffix that should be watched. // hash a suffix that should be watched.
func shouldWatchFileWithExtension(name string) bool { func shouldWatchFileWithExtension(name string) bool {