From 04990b3e508ed16e9ffa38f39d575ad6302ebcf2 Mon Sep 17 00:00:00 2001 From: glacials Date: Wed, 15 Apr 2015 19:42:41 -0700 Subject: [PATCH 1/3] Allow watch path and root to be set independently --- runner.conf.sample | 1 + runner/settings.go | 5 +++++ runner/watcher.go | 4 ++-- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/runner.conf.sample b/runner.conf.sample index 5ac3c05..23a4beb 100644 --- a/runner.conf.sample +++ b/runner.conf.sample @@ -1,4 +1,5 @@ root: . +watch_path: . tmp_path: ./tmp build_name: runner-build build_log: runner-build-errors.log diff --git a/runner/settings.go b/runner/settings.go index 61e2412..deffbe4 100644 --- a/runner/settings.go +++ b/runner/settings.go @@ -18,6 +18,7 @@ const ( var settings = map[string]string{ "config_path": "./runner.conf", "root": ".", + "watch_path": ".", "tmp_path": "./tmp", "build_name": "runner-build", "build_log": "runner-build-errors.log", @@ -108,6 +109,10 @@ func root() string { return settings["root"] } +func watchPath() string { + return settings["watch_path"] +} + func tmpPath() string { return settings["tmp_path"] } diff --git a/runner/watcher.go b/runner/watcher.go index 7df2706..d5e24db 100644 --- a/runner/watcher.go +++ b/runner/watcher.go @@ -36,8 +36,8 @@ func watchFolder(path string) { } func watch() { - root := root() - filepath.Walk(root, func(path string, info os.FileInfo, err error) error { + watchPath := watchPath() + filepath.Walk(watchPath, func(path string, info os.FileInfo, err error) error { if info.IsDir() && !isTmpDir(path) { if len(path) > 1 && strings.HasPrefix(filepath.Base(path), ".") { return filepath.SkipDir From 9130765876fc30d7bfe766028932bafa37fabee6 Mon Sep 17 00:00:00 2001 From: icattlecoder Date: Wed, 26 Aug 2015 10:29:02 +0800 Subject: [PATCH 2/3] cp os.Args to runner --- main.go | 17 ++++++++++------- runner/runner.go | 5 +++++ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index 1dea9dd..f0a780b 100644 --- a/main.go +++ b/main.go @@ -13,22 +13,25 @@ Traffic (https://github.com/pilu/traffic) already has a middleware that shows th package main import ( - "flag" "fmt" "github.com/pilu/fresh/runner" "os" ) func main() { - configPath := flag.String("c", "", "config file path") - flag.Parse() - if *configPath != "" { - if _, err := os.Stat(*configPath); err != nil { - fmt.Printf("Can't find config file `%s`\n", *configPath) + configPath := "" + args := os.Args + if len(args) > 2 && args[1] == "-c" { + configPath = args[2] + } + + if configPath != "" { + if _, err := os.Stat(configPath); err != nil { + fmt.Printf("Can't find config file `%s`\n", configPath) os.Exit(1) } else { - os.Setenv("RUNNER_CONFIG_PATH", *configPath) + os.Setenv("RUNNER_CONFIG_PATH", configPath) } } diff --git a/runner/runner.go b/runner/runner.go index f15f89a..8d60911 100644 --- a/runner/runner.go +++ b/runner/runner.go @@ -2,6 +2,7 @@ package runner import ( "io" + "os" "os/exec" ) @@ -10,6 +11,10 @@ func run() bool { cmd := exec.Command(buildPath()) + if len(os.Args) > 2 && os.Args[1] == "-c" { + cmd.Args = append([]string{buildPath()}, os.Args[3:]...) + } + stderr, err := cmd.StderrPipe() if err != nil { fatal(err) From 2f9a2c4422f6fa9e9552e7e90edaa093684960a0 Mon Sep 17 00:00:00 2001 From: icattlecoder Date: Mon, 6 Mar 2017 13:07:40 +0800 Subject: [PATCH 3/3] fix delay --- main.go | 2 +- runner/settings.go | 3 +-- runner/start.go | 13 ++++++++----- runner/watcher.go | 13 +++++++++---- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/main.go b/main.go index f0a780b..cbdbac4 100644 --- a/main.go +++ b/main.go @@ -14,7 +14,7 @@ package main import ( "fmt" - "github.com/pilu/fresh/runner" + "github.com/icattlecoder/fresh/runner" "os" ) diff --git a/runner/settings.go b/runner/settings.go index deffbe4..0f2fbcc 100644 --- a/runner/settings.go +++ b/runner/settings.go @@ -138,6 +138,5 @@ func configPath() string { func buildDelay() time.Duration { value, _ := strconv.Atoi(settings["build_delay"]) - - return time.Duration(value) + return time.Duration(value) * time.Millisecond } diff --git a/runner/start.go b/runner/start.go index b382dc3..c1c1175 100644 --- a/runner/start.go +++ b/runner/start.go @@ -19,12 +19,16 @@ var ( appLog logFunc ) -func flushEvents() { +func flushEvents(delay time.Duration) { + for { + t := time.NewTicker(delay) select { case eventName := <-startChannel: mainLog("receiving event %s", eventName) - default: + t.Stop() + case _ = <-t.C: + t.Stop() return } } @@ -43,11 +47,10 @@ func start() { eventName := <-startChannel mainLog("receiving first event %s", eventName) - mainLog("sleeping for %d milliseconds", buildDelay) - time.Sleep(buildDelay * time.Millisecond) + mainLog("sleeping for %d milliseconds", buildDelay/1e6) mainLog("flushing events") - flushEvents() + flushEvents(buildDelay) mainLog("Started! (%d Goroutines)", runtime.NumGoroutine()) err := removeBuildErrorsLog() diff --git a/runner/watcher.go b/runner/watcher.go index d5e24db..ec4a583 100644 --- a/runner/watcher.go +++ b/runner/watcher.go @@ -1,7 +1,7 @@ package runner import ( - "github.com/howeyc/fsnotify" + "github.com/fsnotify/fsnotify" "os" "path/filepath" "strings" @@ -16,19 +16,19 @@ func watchFolder(path string) { go func() { for { select { - case ev := <-watcher.Event: + case ev := <-watcher.Events: if isWatchedFile(ev.Name) { watcherLog("sending event %s", ev) startChannel <- ev.String() } - case err := <-watcher.Error: + case err := <-watcher.Errors: watcherLog("error: %s", err) } } }() watcherLog("Watching %s", path) - err = watcher.Watch(path) + err = watcher.Add(path) if err != nil { fatal(err) @@ -37,6 +37,11 @@ func watchFolder(path string) { func watch() { watchPath := watchPath() + + if !filepath.IsAbs(watchPath) { + watchPath, _ = filepath.Abs(watchPath) + } + filepath.Walk(watchPath, func(path string, info os.FileInfo, err error) error { if info.IsDir() && !isTmpDir(path) { if len(path) > 1 && strings.HasPrefix(filepath.Base(path), ".") {