Skip to content

Commit

Permalink
Merge pull request #176 from oxequa/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
asoseil authored Apr 14, 2018
2 parents 7f988a2 + e823573 commit 13ee93c
Show file tree
Hide file tree
Showing 11 changed files with 169 additions and 138 deletions.
41 changes: 25 additions & 16 deletions realize.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func main() {
&cli.BoolFlag{Name: "install", Aliases: []string{"i"}, Value: false, Usage: "Enable go install"},
&cli.BoolFlag{Name: "build", Aliases: []string{"b"}, Value: false, Usage: "Enable go build"},
&cli.BoolFlag{Name: "run", Aliases: []string{"nr"}, Value: false, Usage: "Enable go run"},
&cli.BoolFlag{Name: "legacy", Aliases: []string{"l"}, Value: false, Usage: "Legacy watch by polling instead fsnotify"},
&cli.BoolFlag{Name: "no-config", Aliases: []string{"nc"}, Value: false, Usage: "Ignore existing config and doesn't create a new one"},
},
Action: func(c *cli.Context) error {
Expand Down Expand Up @@ -794,7 +795,7 @@ func setup(c *cli.Context) (err error) {
Resolve: func(d interact.Context) bool {
val, _ := d.Ans().Bool()
if val {
r.Schema.Projects[len(r.Schema.Projects)-1].Watcher.Ignore = r.Schema.Projects[len(r.Schema.Projects)-1].Watcher.Ignore[:len(r.Schema.Projects[len(r.Schema.Projects)-1].Watcher.Ignore)-1]
r.Schema.Projects[len(r.Schema.Projects)-1].Watcher.Ignore.Paths = r.Schema.Projects[len(r.Schema.Projects)-1].Watcher.Ignore.Paths[:len(r.Schema.Projects[len(r.Schema.Projects)-1].Watcher.Ignore.Paths)-1]
}
return val
},
Expand All @@ -814,7 +815,7 @@ func setup(c *cli.Context) (err error) {
if err != nil {
return d.Err()
}
r.Schema.Projects[len(r.Schema.Projects)-1].Watcher.Ignore = append(r.Schema.Projects[len(r.Schema.Projects)-1].Watcher.Ignore, val)
r.Schema.Projects[len(r.Schema.Projects)-1].Watcher.Ignore.Paths = append(r.Schema.Projects[len(r.Schema.Projects)-1].Watcher.Ignore.Paths, val)
d.Reload()
return nil
},
Expand Down Expand Up @@ -1080,7 +1081,7 @@ func setup(c *cli.Context) (err error) {
if err != nil {
return d.Err()
}
r.Schema.Projects[len(r.Schema.Projects)-1].ErrorOutputPattern = val
r.Schema.Projects[len(r.Schema.Projects)-1].ErrPattern = val
return nil
},
},
Expand Down Expand Up @@ -1116,7 +1117,14 @@ func setup(c *cli.Context) (err error) {

// Start realize workflow
func start(c *cli.Context) (err error) {
r.Server = realize.Server{Parent: &r, Status: false, Open: false, Port: realize.Port, Host: realize.Host}
// set legacy watcher
if c.Bool("legacy") {
r.Settings.Legacy.Set(c.Bool("legacy"), 1)
}
// set server
if c.Bool("server") {
r.Server.Set(c.Bool("server"), c.Bool("open"), realize.Port, realize.Host)
}
// check no-config and read
if !c.Bool("no-config") {
// read a config if exist
Expand All @@ -1133,20 +1141,9 @@ func start(c *cli.Context) (err error) {
}

}
// config and start server
if c.Bool("server") || r.Server.Status {
r.Server.Status = true
if c.Bool("open") || r.Server.Open {
r.Server.Open = true
r.Server.OpenURL()
}
err = r.Server.Start()
if err != nil {
return err
}
}
// check project list length
if len(r.Schema.Projects) <= 0 {
println("len", r.Schema.Projects)
// create a new project based on given params
project := r.Schema.New(c)
// Add to projects list
Expand All @@ -1159,6 +1156,18 @@ func start(c *cli.Context) (err error) {
}
}
}
// Start web server
if r.Server.Status {
r.Server.Parent = &r
err = r.Server.Start()
if err != nil {
return err
}
err = r.Server.OpenURL()
if err != nil {
return err
}
}
// start workflow
return r.Start()
}
Expand Down
4 changes: 2 additions & 2 deletions realize/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var (
// RPrefix tool name
RPrefix = "realize"
// RVersion current version
RVersion = "2.0.1"
RVersion = "2.1"
// RExt file extension
RExt = ".yaml"
// RFile config file name
Expand All @@ -34,7 +34,7 @@ type (
// Realize main struct
Realize struct {
Settings Settings `yaml:"settings" json:"settings"`
Server Server `yaml:"server" json:"server"`
Server Server `yaml:"server,omitempty" json:"server,omitempty"`
Schema `yaml:",inline" json:",inline"`
Sync chan string `yaml:"-" json:"-"`
Err Func `yaml:"-" json:"-"`
Expand Down
10 changes: 5 additions & 5 deletions realize/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package realize

// this code is imported from moby, unfortunately i can't import it directly as dependencies from its repo,
// cause there was a problem between moby vendor and fsnotify
// i have just added only walk methods and some little changes to polling interval, originally set as static.
// i have just added only the walk methods and some little changes to polling interval, originally set as static.

import (
"errors"
Expand Down Expand Up @@ -57,7 +57,7 @@ type (
// PollingWatcher returns a poll-based file watcher
func PollingWatcher(interval time.Duration) FileWatcher {
if interval == 0 {
interval = 100 * time.Millisecond
interval = time.Duration(1) * time.Second
}
return &filePoller{
interval: interval,
Expand All @@ -67,13 +67,13 @@ func PollingWatcher(interval time.Duration) FileWatcher {
}

// NewFileWatcher tries to use an fs-event watcher, and falls back to the poller if there is an error
func NewFileWatcher(force bool, interval time.Duration) (FileWatcher, error) {
if !force {
func NewFileWatcher(l Legacy) (FileWatcher, error) {
if !l.Force {
if w, err := EventWatcher(); err == nil {
return w, nil
}
}
return PollingWatcher(interval), nil
return PollingWatcher(l.Interval), nil
}

// EventWatcher returns an fs-event based file watcher
Expand Down
76 changes: 45 additions & 31 deletions realize/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,22 @@ var (

// Watch info
type Watch struct {
Paths []string `yaml:"paths" json:"paths"`
Exts []string `yaml:"extensions" json:"extensions"`
Ignore []string `yaml:"ignored_paths,omitempty" json:"ignored_paths,omitempty"`
Paths []string `yaml:"paths" json:"paths"`
Scripts []Command `yaml:"scripts,omitempty" json:"scripts,omitempty"`
Hidden bool `yaml:"skip_hidden,omitempty" json:"skip_hidden,omitempty"`
Hidden bool `yaml:"hidden,omitempty" json:"hidden,omitempty"`
Ignore Ignore `yaml:"ignore,omitempty" json:"ignore,omitempty"`
}

type Ignore struct{
Exts []string `yaml:"exts,omitempty" json:"exts,omitempty"`
Paths []string `yaml:"paths,omitempty" json:"paths,omitempty"`
}

// Command fields
type Command struct {
Type string `yaml:"type" json:"type"`
Cmd string `yaml:"command" json:"command"`
Type string `yaml:"type" json:"type"`
Path string `yaml:"path,omitempty" json:"path,omitempty"`
Global bool `yaml:"global,omitempty" json:"global,omitempty"`
Output bool `yaml:"output,omitempty" json:"output,omitempty"`
Expand All @@ -46,21 +51,21 @@ type Command struct {
type Project struct {
parent *Realize
watcher FileWatcher
init bool
exit chan os.Signal
stop chan bool
exit chan os.Signal
paths []string
last last
files int64
folders int64
last last
paths []string
init bool
Name string `yaml:"name" json:"name"`
Path string `yaml:"path" json:"path"`
Environment map[string]string `yaml:"environment,omitempty" json:"environment,omitempty"`
Tools Tools `yaml:"commands" json:"commands"`
Env map[string]string `yaml:"env,omitempty" json:"env,omitempty"`
Args []string `yaml:"args,omitempty" json:"args,omitempty"`
Tools Tools `yaml:"commands" json:"commands"`
Watcher Watch `yaml:"watcher" json:"watcher"`
Buffer Buffer `yaml:"-" json:"buffer"`
ErrorOutputPattern string `yaml:"errorOutputPattern,omitempty" json:"errorOutputPattern,omitempty"`
ErrPattern string `yaml:"pattern,omitempty" json:"pattern,omitempty"`
}

// Last is used to save info about last file changed
Expand Down Expand Up @@ -111,7 +116,7 @@ func (p *Project) Before() {
// setup go tools
p.Tools.Setup()
// set env const
for key, item := range p.Environment {
for key, item := range p.Env {
if err := os.Setenv(key, item); err != nil {
p.Buffer.StdErr = append(p.Buffer.StdErr, BufferOut{Time: time.Now(), Text: err.Error(), Type: "Env error", Stream: ""})
}
Expand Down Expand Up @@ -191,7 +196,7 @@ func (p *Project) Reload(path string, stop <-chan bool) {
}
// Go supported tools
if len(path) > 0 {
fi, err := os.Stat(filepath.Dir(path))
fi, err := os.Stat(path)
if filepath.Ext(path) == "" {
fi, err = os.Stat(path)
}
Expand Down Expand Up @@ -274,7 +279,7 @@ func (p *Project) Watch(wg *sync.WaitGroup) {
// change channel
p.stop = make(chan bool)
// init a new watcher
p.watcher, err = NewFileWatcher(p.parent.Settings.Legacy.Force, p.parent.Settings.Legacy.Interval)
p.watcher, err = NewFileWatcher(p.parent.Settings.Legacy)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -347,14 +352,28 @@ func (p *Project) Validate(path string, fcheck bool) bool {
}
// check for a valid ext or path
if e := ext(path); e != "" {
// supported exts
if !array(e, p.Watcher.Exts) {
if len(p.Watcher.Exts) == 0{
return false
}
// check ignored
for _, v := range p.Watcher.Ignore.Exts {
if v == e {
return false
}
}
// supported extensions
for index, v := range p.Watcher.Exts{
if e == v {
break
}
if index == len(p.Watcher.Exts)-1{
return false
}
}
}
separator := string(os.PathSeparator)
// supported paths
for _, v := range p.Watcher.Ignore {
for _, v := range p.Watcher.Ignore.Paths {
s := append([]string{p.Path}, strings.Split(v, separator)...)
abs, _ := filepath.Abs(filepath.Join(s...))
if path == abs || strings.HasPrefix(path, abs+separator) {
Expand All @@ -364,16 +383,9 @@ func (p *Project) Validate(path string, fcheck bool) bool {
// file check
if fcheck {
fi, err := os.Stat(path)
if err != nil {
if err != nil || fi.Mode()&os.ModeSymlink != 0 || !fi.IsDir() && ext(path) == "" || fi.Size() <= 0{
return false
}
if !fi.IsDir() && ext(path) == "" {
return false
}
if fi.Size() > 0 {
return true
}
return false
}
return true

Expand Down Expand Up @@ -465,8 +477,8 @@ func (p *Project) cmd(stop <-chan bool, flag string, global bool) {
case <-done:
return
case r := <-result:
msg = fmt.Sprintln(p.pname(p.Name, 5), ":", Green.Bold("Command"), Green.Bold("\"")+r.Name+Green.Bold("\""))
if r.Err != nil {
msg = fmt.Sprintln(p.pname(p.Name, 5), ":", Green.Bold("Command"), Green.Bold("\"")+r.Name+Green.Bold("\""))
if r.Err != nil {
out = BufferOut{Time: time.Now(), Text: r.Err.Error(), Type: flag}
p.stamp("error", out, msg, fmt.Sprint(Red.Regular(r.Err.Error())))
} else {
Expand All @@ -485,9 +497,9 @@ func (p *Project) walk(path string, info os.FileInfo, err error) error {
if p.parent.Settings.Recovery.Index {
log.Println("Indexing", path)
}
p.tools(p.stop, path, info)
if info.IsDir() {
// tools dir
p.tools(p.stop, path, info)
p.folders++
} else {
// tools files
Expand Down Expand Up @@ -532,7 +544,7 @@ func (p *Project) stamp(t string, o BufferOut, msg string, stream string) {
log.Print(msg)
}
if stream != "" {
fmt.Fprint(Output, stream)
fmt.Fprintln(Output, stream)
}
go func() {
p.parent.Sync <- "sync"
Expand All @@ -547,14 +559,16 @@ func (p *Project) run(path string, stream chan Response, stop <-chan bool) (err
defer func() {
// https://github.com/golang/go/issues/5615
// https://github.com/golang/go/issues/6720
build.Process.Signal(os.Interrupt)
if build != nil {
build.Process.Signal(os.Interrupt)
}
}()

// custom error pattern
isErrorText := func(string) bool {
return false
}
errRegexp, err := regexp.Compile(p.ErrorOutputPattern)
errRegexp, err := regexp.Compile(p.ErrPattern)
if err != nil {
r.Err = err
stream <- r
Expand Down
16 changes: 11 additions & 5 deletions realize/projects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestProject_Before(t *testing.T) {
r = Realize{}
r.Projects = append(r.Projects, Project{
parent: &r,
Environment: map[string]string{
Env: map[string]string{
input: input,
},
})
Expand Down Expand Up @@ -100,7 +100,9 @@ func TestProject_Reload(t *testing.T) {
parent: &r,
})
input := "test/path"
r.Projects[0].watcher, _ = NewFileWatcher(false, 0)
r.Settings.Legacy.Force = false
r.Settings.Legacy.Interval = 0
r.Projects[0].watcher, _ = NewFileWatcher(r.Settings.Legacy)
r.Reload = func(context Context) {
log.Println(context.Path)
}
Expand All @@ -126,12 +128,16 @@ func TestProject_Validate(t *testing.T) {
r.Projects = append(r.Projects, Project{
parent: &r,
Watcher: Watch{
Ignore: []string{"/test/ignore"},
Exts: []string{},
Ignore: Ignore{
Paths:[]string{"/test/ignore"},
},
},
})
for i, v := range data {
if r.Projects[0].Validate(i, false) != v {
t.Error("Unexpected error", i, "expected", v)
result := r.Projects[0].Validate(i, false)
if result != v {
t.Error("Unexpected error", i, "expected", v, result)
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion realize/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ func (s *Schema) New(c *cli.Context) Project {
Args: params(c),
Watcher: Watch{
Paths: []string{"/"},
Ignore: []string{".git", ".realize", "vendor"},
Ignore: Ignore{
Paths:[]string{".git", ".realize", "vendor"},
},
Exts: []string{"go"},
},
}
Expand Down
Loading

0 comments on commit 13ee93c

Please sign in to comment.