Merge branch 'dev' of https://github.com/tockins/realize
This commit is contained in:
commit
0937dce84c
|
@ -7,3 +7,9 @@ go:
|
|||
matrix:
|
||||
allow_failures:
|
||||
- go: tip
|
||||
|
||||
install:
|
||||
- go get ./...
|
||||
|
||||
script:
|
||||
- go install .
|
|
@ -8,6 +8,7 @@ import (
|
|||
w "github.com/tockins/realize/watcher"
|
||||
"gopkg.in/urfave/cli.v2"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -46,6 +47,8 @@ func init() {
|
|||
Settings: c.Settings{
|
||||
Config: c.Config{
|
||||
Flimit: 0,
|
||||
Polling: false,
|
||||
PollingInterval: time.Millisecond * 200,
|
||||
},
|
||||
Resources: c.Resources{
|
||||
Config: config,
|
||||
|
|
|
@ -3,6 +3,7 @@ package settings
|
|||
import (
|
||||
"gopkg.in/yaml.v2"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Settings defines a group of general settings
|
||||
|
@ -16,6 +17,8 @@ type Settings struct {
|
|||
// Config defines structural options
|
||||
type Config struct {
|
||||
Flimit uint64 `yaml:"flimit" json:"flimit"`
|
||||
Polling bool `yaml:"polling" json:"polling"`
|
||||
PollingInterval time.Duration `yaml:"polling_interval" json:"polling_interval"`
|
||||
}
|
||||
|
||||
// Server settings, used for the web panel
|
||||
|
|
|
@ -16,8 +16,12 @@ func (h *Blueprint) Run() error {
|
|||
wg.Add(len(h.Projects))
|
||||
for k := range h.Projects {
|
||||
h.Projects[k].parent = h
|
||||
if h.Polling {
|
||||
go h.Projects[k].watchByPolling()
|
||||
} else {
|
||||
go h.Projects[k].watching()
|
||||
}
|
||||
}
|
||||
wg.Wait()
|
||||
return nil
|
||||
}
|
||||
|
@ -28,7 +32,7 @@ func (h *Blueprint) Run() error {
|
|||
func (h *Blueprint) Add(p *cli.Context) error {
|
||||
project := Project{
|
||||
Name: h.name(p),
|
||||
Path: filepath.Clean(p.String("path")),
|
||||
Path: strings.Replace(filepath.Clean(p.String("path")), "\\", "/", -1),
|
||||
Build: p.Bool("build"),
|
||||
Bin: !p.Bool("no-bin"),
|
||||
Run: !p.Bool("no-run"),
|
||||
|
|
|
@ -34,6 +34,8 @@ func (p *Project) goRun(channel chan bool, runner chan bool, wr *sync.WaitGroup)
|
|||
} else {
|
||||
if _, err := os.Stat(filepath.Join(os.Getenv("GOBIN"), filepath.Base(p.path))); err == nil {
|
||||
build = exec.Command(filepath.Join(os.Getenv("GOBIN"), filepath.Base(p.path)), params...)
|
||||
} else if _, err := os.Stat(filepath.Join(os.Getenv("GOBIN"), filepath.Base(p.path)) + ".exe"); err == nil {
|
||||
build = exec.Command(filepath.Join(os.Getenv("GOBIN"), filepath.Base(p.path)) + ".exe", params...)
|
||||
} else {
|
||||
p.Buffer.StdLog = append(p.Buffer.StdLog, BufferOut{Time: time.Now(), Text: "Can't run a not compiled project"})
|
||||
p.Fatal(err, "Can't run a not compiled project", ":")
|
||||
|
|
|
@ -16,6 +16,97 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
type pollWatcher struct {
|
||||
paths map[string]bool
|
||||
}
|
||||
|
||||
func (w *pollWatcher) isWatching(path string) bool {
|
||||
a, b := w.paths[path]
|
||||
return a && b
|
||||
}
|
||||
|
||||
func (w *pollWatcher) Add(path string) error {
|
||||
if w.paths == nil {
|
||||
w.paths = map[string]bool{}
|
||||
}
|
||||
w.paths[path] = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Project) watchByPolling() {
|
||||
var wr sync.WaitGroup
|
||||
var watcher = new(pollWatcher)
|
||||
channel, exit := make(chan bool, 1), make(chan bool, 1)
|
||||
p.path = p.Path
|
||||
defer func() {
|
||||
wg.Done()
|
||||
}()
|
||||
p.cmd(exit)
|
||||
if err := p.walks(watcher); err != nil {
|
||||
log.Fatalln(p.pname(p.Name, 2), ":", p.Red.Bold(err.Error()))
|
||||
return
|
||||
}
|
||||
go p.routines(channel, &wr)
|
||||
p.LastChangedOn = time.Now().Truncate(time.Second)
|
||||
// waiting for an event
|
||||
|
||||
var walk = func(changed string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
} else if !watcher.isWatching(changed) {
|
||||
return nil
|
||||
} else if !info.ModTime().Truncate(time.Second).After(p.LastChangedOn) {
|
||||
return nil
|
||||
}
|
||||
|
||||
var ext string
|
||||
if index := strings.Index(filepath.Ext(changed), "_"); index == -1 {
|
||||
ext = filepath.Ext(changed)
|
||||
} else {
|
||||
ext = filepath.Ext(changed)[0:index]
|
||||
}
|
||||
i := strings.Index(changed, filepath.Ext(changed))
|
||||
file := changed[:i] + ext
|
||||
path := filepath.Dir(changed[:i])
|
||||
if changed[:i] != "" && inArray(ext, p.Watcher.Exts) {
|
||||
p.LastChangedOn = time.Now().Truncate(time.Second)
|
||||
msg := fmt.Sprintln(p.pname(p.Name, 4), ":", p.Magenta.Bold(strings.ToUpper(ext[1:]) + " changed"), p.Magenta.Bold(file))
|
||||
out := BufferOut{Time: time.Now(), Text: strings.ToUpper(ext[1:]) + " changed " + file}
|
||||
p.print("log", out, msg, "")
|
||||
// stop and run again
|
||||
if p.Run {
|
||||
close(channel)
|
||||
channel = make(chan bool)
|
||||
}
|
||||
// handle multiple errors, need a better way
|
||||
p.fmt(file)
|
||||
p.test(path)
|
||||
p.generate(path)
|
||||
go p.routines(channel, &wr)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
for {
|
||||
for _, dir := range p.Watcher.Paths {
|
||||
base := filepath.Join(p.base, dir)
|
||||
if _, err := os.Stat(base); err == nil {
|
||||
if err := filepath.Walk(base, walk); err != nil {
|
||||
log.Println(p.Red.Bold(err.Error()))
|
||||
}
|
||||
} else {
|
||||
log.Println(p.Red.Bold(base + " path doesn't exist"))
|
||||
}
|
||||
|
||||
select {
|
||||
case <-exit:
|
||||
return
|
||||
case <-time.After(p.parent.Config.PollingInterval / time.Duration(len(p.Watcher.Paths))):
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Watching method is the main core. It manages the livereload and the watching
|
||||
func (p *Project) watching() {
|
||||
var wr sync.WaitGroup
|
||||
|
@ -224,8 +315,12 @@ func (p *Project) cmd(exit chan bool) {
|
|||
}()
|
||||
}
|
||||
|
||||
type watcher interface {
|
||||
Add(path string) error
|
||||
}
|
||||
|
||||
// Walks the file tree of a project
|
||||
func (p *Project) walks(watcher *fsnotify.Watcher) error {
|
||||
func (p *Project) walks(watcher watcher) error {
|
||||
var files, folders int64
|
||||
wd, _ := os.Getwd()
|
||||
walk := func(path string, info os.FileInfo, err error) error {
|
||||
|
|
Loading…
Reference in New Issue