1
0
mirror of https://github.com/beego/bee.git synced 2024-12-22 15:10:49 +00:00

Merge pull request #353 from sergeylanzman/improve-live-reload

Improve live reload by adding support for static files
This commit is contained in:
Faissal Elamraoui 2017-02-14 07:28:27 +01:00 committed by GitHub
commit 14eeb07402
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" {
NewWatcher(paths, files, true)
AutoBuild(files, true)
@ -145,11 +150,6 @@ func runApp(cmd *Command, args []string) int {
AutoBuild(files, false)
}
// Start the Reload server (if enabled)
if conf.EnableReload {
startReloadServer()
}
for {
select {
case <-exit:
@ -190,7 +190,7 @@ func readAppDirectories(directory string, paths *[]string) {
continue
}
if path.Ext(fileInfo.Name()) == ".go" {
if path.Ext(fileInfo.Name()) == ".go" || (ifStaticFile(fileInfo.Name()) && conf.EnableReload) {
*paths = append(*paths, directory)
useDirectory = true
}

View File

@ -28,10 +28,18 @@ import (
)
var (
cmd *exec.Cmd
state sync.Mutex
eventTime = make(map[string]int64)
scheduleTime time.Time
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
@ -45,8 +53,12 @@ func NewWatcher(paths []string, files []string, isgenerate bool) {
for {
select {
case e := <-watcher.Events:
isbuild := true
isBuild := true
if ifStaticFile(e.Name) || conf.EnableReload {
sendReload(e.String())
continue
}
// Skip ignored files
if shouldIgnoreFile(e.Name) {
continue
@ -58,12 +70,12 @@ func NewWatcher(paths []string, files []string, isgenerate bool) {
mt := getFileModTime(e.Name)
if t := eventTime[e.Name]; mt == t {
logger.Infof(bold("Skipping: ")+"%s", e.String())
isbuild = false
isBuild = false
}
eventTime[e.Name] = mt
if isbuild {
if isBuild {
logger.Infof("Event fired: %s", e)
go func() {
// Wait 1s before autobuild until there is no file change.
@ -239,6 +251,15 @@ func Start(appname string) {
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 {
@ -255,14 +276,6 @@ func shouldIgnoreFile(filename string) bool {
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
// hash a suffix that should be watched.
func shouldWatchFileWithExtension(name string) bool {