Skip to content

Added package watcher to allow gin to listen to changes in packages #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/codegangsta/cli"
"github.com/codegangsta/envy/lib"
"github.com/codegangsta/gin/lib"
"github.com/theonejb/watchman"

"log"
"os"
Expand Down Expand Up @@ -58,6 +59,10 @@ func main() {
Name: "godep,g",
Usage: "use godep when building",
},
cli.BoolFlag{
Name: "pkgwatch,w",
Usage: "watch the package and any imported packages for change instead of files in the current dir",
},
}
app.Commands = []cli.Command{
{
Expand All @@ -81,6 +86,7 @@ func MainAction(c *cli.Context) {
port := c.GlobalInt("port")
appPort := strconv.Itoa(c.GlobalInt("appPort"))
immediate = c.GlobalBool("immediate")
pkgWatch := c.GlobalBool("pkgwatch")

// Bootstrap the environment
envy.Bootstrap()
Expand Down Expand Up @@ -116,10 +122,17 @@ func MainAction(c *cli.Context) {
build(builder, runner, logger)

// scan for changes
scanChanges(c.GlobalString("path"), func(path string) {
cb := func(path string) {
runner.Kill()
build(builder, runner, logger)
})
}
if pkgWatch {
fmt.Println("Using Pkg Watcher")
scanChangesWithWatcher(c.GlobalString("path"), cb)
} else {
fmt.Println("Using file watcher")
scanChanges(c.GlobalString("path"), cb)
}
}

func EnvAction(c *cli.Context) {
Expand Down Expand Up @@ -181,6 +194,13 @@ func scanChanges(watchPath string, cb scanCallback) {
}
}

func scanChangesWithWatcher(watchPath string, cb scanCallback) {
for {
watchman.WatchPackageAndReturnOnChange(watchPath)
cb(watchPath)
}
}

func shutdown(runner gin.Runner) {
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
Expand Down