Skip to content

Commit

Permalink
Merge pull request #122 from tockins/dev
Browse files Browse the repository at this point in the history
v1.5.2
  • Loading branch information
Alessio Pracchia authored Nov 14, 2017
2 parents 561f044 + cf97995 commit 7f02f52
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 163 deletions.
28 changes: 14 additions & 14 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@ import (

// Tool options customizable, should be moved in Cmd
type tool struct {
dir bool
status bool
name string
err string
cmd []string
options []string
name, err, out string
cmd, options []string
dir, status bool
}

// Cmds list of go commands
Expand All @@ -32,11 +29,11 @@ type Cmds struct {

// Cmd single command fields and options
type Cmd struct {
Status bool `yaml:"status,omitempty" json:"status,omitempty"`
Method string `yaml:"method,omitempty" json:"method,omitempty"`
Args []string `yaml:"args,omitempty" json:"args,omitempty"`
method []string
Status bool `yaml:"status,omitempty" json:"status,omitempty"`
tool bool
method []string
name, startTxt, endTxt string
}

Expand All @@ -46,6 +43,8 @@ func (r *realize) clean() error {
arr := r.Schema
for key, val := range arr {
if _, err := duplicates(val, arr[key+1:]); err != nil {
// path validation

r.Schema = append(arr[:key], arr[key+1:]...)
break
}
Expand All @@ -56,14 +55,15 @@ func (r *realize) clean() error {
}

// Add a new project
func (r *realize) add(p *cli.Context) error {
path, err := filepath.Abs(p.String("path"))
if err != nil {
return err
func (r *realize) add(p *cli.Context) (err error) {
// project init
name := filepath.Base(p.String("path"))
if name == "." {
name = filepath.Base(wdir())
}
project := Project{
Name: filepath.Base(filepath.Clean(p.String("path"))),
Path: path,
Name: name,
Path: p.String("path"),
Cmds: Cmds{
Vet: Cmd{
Status: p.Bool("vet"),
Expand Down
33 changes: 19 additions & 14 deletions exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func (p *Project) goCompile(stop <-chan bool, method []string, args []string) (s
if err != nil {
return stderr.String(), err
}
return "", nil
}
return "", nil
}
Expand All @@ -47,7 +48,6 @@ func (p *Project) goCompile(stop <-chan bool, method []string, args []string) (s
func (p *Project) goRun(stop <-chan bool, runner chan bool) {
var build *exec.Cmd
var args []string

// custom error pattern
isErrorText := func(string) bool {
return false
Expand All @@ -72,13 +72,16 @@ func (p *Project) goRun(stop <-chan bool, runner chan bool) {
}

gobin := os.Getenv("GOBIN")
path := filepath.Join(gobin, p.name)
dirPath := filepath.Base(p.Path)
if p.Path == "." {
dirPath = filepath.Base(wdir())
}
path := filepath.Join(gobin, dirPath)
if _, err := os.Stat(path); err == nil {
build = exec.Command(path, args...)
} else if _, err := os.Stat(path + extWindows); err == nil {
build = exec.Command(path+extWindows, args...)
} else {
path := filepath.Join(p.Path, p.name)
if _, err = os.Stat(path); err == nil {
build = exec.Command(path, args...)
} else if _, err = os.Stat(path + extWindows); err == nil {
Expand Down Expand Up @@ -148,26 +151,26 @@ func (p *Project) command(stop <-chan bool, cmd Command) (string, string) {
var stderr bytes.Buffer
done := make(chan error)
args := strings.Split(strings.Replace(strings.Replace(cmd.Command, "'", "", -1), "\"", "", -1), " ")
exec := exec.Command(args[0], args[1:]...)
exec.Dir = p.Path
ex := exec.Command(args[0], args[1:]...)
ex.Dir = p.Path
// make cmd path
if cmd.Path != "" {
if strings.Contains(cmd.Path, p.Path) {
exec.Dir = cmd.Path
ex.Dir = cmd.Path
} else {
exec.Dir = filepath.Join(p.Path, cmd.Path)
ex.Dir = filepath.Join(p.Path, cmd.Path)
}
}
exec.Stdout = &stdout
exec.Stderr = &stderr
ex.Stdout = &stdout
ex.Stderr = &stderr
// Start command
exec.Start()
go func() { done <- exec.Wait() }()
ex.Start()
go func() { done <- ex.Wait() }()
// Wait a result
select {
case <-stop:
// Stop running command
exec.Process.Kill()
ex.Process.Kill()
return "", ""
case err := <-done:
// Command completed
Expand Down Expand Up @@ -206,15 +209,17 @@ func (p *Project) goTool(wg *sync.WaitGroup, stop <-chan bool, result chan<- too
case <-stop:
// Stop running command
cmd.Process.Kill()
break
return
case err := <-done:
// Command completed
if err != nil {
tool.err = stderr.String() + out.String()
// send command result
result <- tool
} else {
tool.out = out.String()
}
break
return
}

}
Expand Down
37 changes: 12 additions & 25 deletions notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func (w *fsNotifyWatcher) Events() <-chan fsnotify.Event {
return w.Watcher.Events
}

// Walk fsnotify
func (w *fsNotifyWatcher) Walk(path string, init bool) string {
if err := w.Add(path); err != nil {
return ""
Expand All @@ -106,9 +107,8 @@ func (w *fsNotifyWatcher) Walk(path string, init bool) string {
// All watches are stopped, removed, and the poller cannot be added to
func (w *filePoller) Close() error {
w.mu.Lock()
defer w.mu.Unlock()

if w.closed {
w.mu.Unlock()
return nil
}

Expand All @@ -117,6 +117,7 @@ func (w *filePoller) Close() error {
w.remove(name)
delete(w.watches, name)
}
w.mu.Unlock()
return nil
}

Expand Down Expand Up @@ -157,6 +158,7 @@ func (w *filePoller) Add(name string) error {
return nil
}

// Remove poller
func (w *filePoller) remove(name string) error {
if w.closed {
return errPollerClosed
Expand Down Expand Up @@ -184,6 +186,7 @@ func (w *filePoller) Events() <-chan fsnotify.Event {
return w.events
}

// Walk poller
func (w *filePoller) Walk(path string, init bool) string {
check := w.watches[path]
if err := w.Add(path); err != nil {
Expand Down Expand Up @@ -232,50 +235,34 @@ func (w *filePoller) watch(f *os.File, lastFi os.FileInfo, chClose chan struct{}
}

fi, err := os.Stat(f.Name())
if err != nil {
// if we got an error here and lastFi is not set, we can presume that nothing has changed
// This should be safe since before `watch()` is called, a stat is performed, there is any error `watch` is not called
if lastFi == nil {
continue
}
switch {
case err != nil && lastFi != nil:
// If it doesn't exist at this point, it must have been removed
// no need to send the error here since this is a valid operation
if os.IsNotExist(err) {
if err := w.sendEvent(fsnotify.Event{Op: fsnotify.Remove, Name: f.Name()}, chClose); err != nil {
return
}
lastFi = nil
continue
}
// at this point, send the error
if err := w.sendErr(err, chClose); err != nil {
return
}
continue
}

if lastFi == nil {
w.sendErr(err, chClose)
return
case lastFi == nil:
if err := w.sendEvent(fsnotify.Event{Op: fsnotify.Create, Name: f.Name()}, chClose); err != nil {
return
}
lastFi = fi
continue
}

if fi.Mode() != lastFi.Mode() {
case fi.Mode() != lastFi.Mode():
if err := w.sendEvent(fsnotify.Event{Op: fsnotify.Chmod, Name: f.Name()}, chClose); err != nil {
return
}
lastFi = fi
continue
}

if fi.ModTime() != lastFi.ModTime() || fi.Size() != lastFi.Size() {
case fi.ModTime() != lastFi.ModTime() || fi.Size() != lastFi.Size():
if err := w.sendEvent(fsnotify.Event{Op: fsnotify.Write, Name: f.Name()}, chClose); err != nil {
return
}
lastFi = fi
continue
}
}
}
32 changes: 11 additions & 21 deletions realize.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

const (
version = "1.5.1r2"
version = "1.5.2"
)

// New realize instance
Expand All @@ -35,26 +35,16 @@ type realize struct {
// Cli commands
func main() {
app := &cli.App{
Name: "Realize",
Version: version,
Authors: []*cli.Author{
{
Name: "Alessio Pracchia",
Email: "[email protected]",
},
{
Name: "Daniele Conventi",
Email: "[email protected]",
},
},
Name: "Realize",
Version: version,
Description: "Go build system with file watchers, output streams and live reload. Run, build and watch file changes with custom paths",
Commands: []*cli.Command{
{
Name: "start",
Aliases: []string{"r"},
Aliases: []string{"s"},
Description: "Start a toolchain on a project or a list of projects. If not exist a config file it creates a new one",
Flags: []cli.Flag{
&cli.StringFlag{Name: "path", Aliases: []string{"p"}, Value: wdir(), Usage: "Project base path"},
&cli.StringFlag{Name: "path", Aliases: []string{"p"}, Value: ".", Usage: "Project base path"},
&cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: "", Usage: "Run a project by its name"},
&cli.BoolFlag{Name: "fmt", Aliases: []string{"f"}, Value: false, Usage: "Enable go fmt"},
&cli.BoolFlag{Name: "vet", Aliases: []string{"v"}, Value: false, Usage: "Enable go vet"},
Expand Down Expand Up @@ -172,7 +162,7 @@ func main() {
if err != nil {
return d.Err()
}
r.Settings.FileLimit = val
r.Settings.FileLimit = int32(val)
return nil
},
},
Expand Down Expand Up @@ -1217,11 +1207,11 @@ func prefix(s string) string {
if s != "" {
return fmt.Sprint(yellow.bold("["), "REALIZE", yellow.bold("]"), " : ", s)
}
return ""
return s
}

// Before is launched before each command
func before(*cli.Context) error {
func before(*cli.Context) (err error) {
// custom log
log.SetFlags(0)
log.SetOutput(logWriter{})
Expand All @@ -1230,7 +1220,7 @@ func before(*cli.Context) error {
if gopath == "" {
return errors.New("$GOPATH isn't set properly")
}
if err := os.Setenv("GOPATH", gopath); err != nil {
if err = os.Setenv("GOPATH", gopath); err != nil {
return err
}
// new realize instance
Expand All @@ -1239,11 +1229,11 @@ func before(*cli.Context) error {
r.Settings.read(&r)
// increase the file limit
if r.Settings.FileLimit != 0 {
if err := r.Settings.flimit(); err != nil {
if err = r.Settings.flimit(); err != nil {
return err
}
}
return nil
return
}

// Rewrite the layout of the log timestamp
Expand Down
14 changes: 8 additions & 6 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,15 @@ type Server struct {
parent *realize
Status bool `yaml:"status" json:"status"`
Open bool `yaml:"open" json:"open"`
Host string `yaml:"host" json:"host"`
Port int `yaml:"port" json:"port"`
Host string `yaml:"host" json:"host"`
}

// Websocket projects
func (s *Server) projects(c echo.Context) error {
func (s *Server) projects(c echo.Context) (err error) {
websocket.Handler(func(ws *websocket.Conn) {
defer ws.Close()
msg, _ := json.Marshal(s.parent)
err := websocket.Message.Send(ws, string(msg))
err = websocket.Message.Send(ws, string(msg))
go func() {
for {
select {
Expand All @@ -51,7 +50,7 @@ func (s *Server) projects(c echo.Context) error {
for {
// Read
text := ""
err := websocket.Message.Receive(ws, &text)
err = websocket.Message.Receive(ws, &text)
if err != nil {
break
} else {
Expand All @@ -62,14 +61,17 @@ func (s *Server) projects(c echo.Context) error {
}
}
}
ws.Close()
}).ServeHTTP(c.Response(), c.Request())
return nil
}

// Start the web server
func (s *Server) start(p *cli.Context) (err error) {
if p.Bool("server") {
s.parent.Server.Status = p.Bool("server")
s.parent.Server.Status = true
}
if p.Bool("open") {
s.parent.Server.Open = true
}

Expand Down
2 changes: 1 addition & 1 deletion settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ const (
type Settings struct {
file string
Files `yaml:"files,omitempty" json:"files,omitempty"`
FileLimit int64 `yaml:"flimit,omitempty" json:"flimit,omitempty"`
Legacy Legacy `yaml:"legacy" json:"legacy"`
FileLimit int32 `yaml:"flimit,omitempty" json:"flimit,omitempty"`
Recovery bool `yaml:"recovery,omitempty" json:"recovery,omitempty"`
}

Expand Down
Loading

0 comments on commit 7f02f52

Please sign in to comment.