Skip to content

Commit 7f02f52

Browse files
author
Alessio Pracchia
authored
Merge pull request #122 from tockins/dev
v1.5.2
2 parents 561f044 + cf97995 commit 7f02f52

File tree

7 files changed

+160
-163
lines changed

7 files changed

+160
-163
lines changed

cmd.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,9 @@ import (
99

1010
// Tool options customizable, should be moved in Cmd
1111
type tool struct {
12-
dir bool
13-
status bool
14-
name string
15-
err string
16-
cmd []string
17-
options []string
12+
name, err, out string
13+
cmd, options []string
14+
dir, status bool
1815
}
1916

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

3330
// Cmd single command fields and options
3431
type Cmd struct {
35-
Status bool `yaml:"status,omitempty" json:"status,omitempty"`
3632
Method string `yaml:"method,omitempty" json:"method,omitempty"`
3733
Args []string `yaml:"args,omitempty" json:"args,omitempty"`
38-
method []string
34+
Status bool `yaml:"status,omitempty" json:"status,omitempty"`
3935
tool bool
36+
method []string
4037
name, startTxt, endTxt string
4138
}
4239

@@ -46,6 +43,8 @@ func (r *realize) clean() error {
4643
arr := r.Schema
4744
for key, val := range arr {
4845
if _, err := duplicates(val, arr[key+1:]); err != nil {
46+
// path validation
47+
4948
r.Schema = append(arr[:key], arr[key+1:]...)
5049
break
5150
}
@@ -56,14 +55,15 @@ func (r *realize) clean() error {
5655
}
5756

5857
// Add a new project
59-
func (r *realize) add(p *cli.Context) error {
60-
path, err := filepath.Abs(p.String("path"))
61-
if err != nil {
62-
return err
58+
func (r *realize) add(p *cli.Context) (err error) {
59+
// project init
60+
name := filepath.Base(p.String("path"))
61+
if name == "." {
62+
name = filepath.Base(wdir())
6363
}
6464
project := Project{
65-
Name: filepath.Base(filepath.Clean(p.String("path"))),
66-
Path: path,
65+
Name: name,
66+
Path: p.String("path"),
6767
Cmds: Cmds{
6868
Vet: Cmd{
6969
Status: p.Bool("vet"),

exec.go

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func (p *Project) goCompile(stop <-chan bool, method []string, args []string) (s
3939
if err != nil {
4040
return stderr.String(), err
4141
}
42+
return "", nil
4243
}
4344
return "", nil
4445
}
@@ -47,7 +48,6 @@ func (p *Project) goCompile(stop <-chan bool, method []string, args []string) (s
4748
func (p *Project) goRun(stop <-chan bool, runner chan bool) {
4849
var build *exec.Cmd
4950
var args []string
50-
5151
// custom error pattern
5252
isErrorText := func(string) bool {
5353
return false
@@ -72,13 +72,16 @@ func (p *Project) goRun(stop <-chan bool, runner chan bool) {
7272
}
7373

7474
gobin := os.Getenv("GOBIN")
75-
path := filepath.Join(gobin, p.name)
75+
dirPath := filepath.Base(p.Path)
76+
if p.Path == "." {
77+
dirPath = filepath.Base(wdir())
78+
}
79+
path := filepath.Join(gobin, dirPath)
7680
if _, err := os.Stat(path); err == nil {
7781
build = exec.Command(path, args...)
7882
} else if _, err := os.Stat(path + extWindows); err == nil {
7983
build = exec.Command(path+extWindows, args...)
8084
} else {
81-
path := filepath.Join(p.Path, p.name)
8285
if _, err = os.Stat(path); err == nil {
8386
build = exec.Command(path, args...)
8487
} else if _, err = os.Stat(path + extWindows); err == nil {
@@ -148,26 +151,26 @@ func (p *Project) command(stop <-chan bool, cmd Command) (string, string) {
148151
var stderr bytes.Buffer
149152
done := make(chan error)
150153
args := strings.Split(strings.Replace(strings.Replace(cmd.Command, "'", "", -1), "\"", "", -1), " ")
151-
exec := exec.Command(args[0], args[1:]...)
152-
exec.Dir = p.Path
154+
ex := exec.Command(args[0], args[1:]...)
155+
ex.Dir = p.Path
153156
// make cmd path
154157
if cmd.Path != "" {
155158
if strings.Contains(cmd.Path, p.Path) {
156-
exec.Dir = cmd.Path
159+
ex.Dir = cmd.Path
157160
} else {
158-
exec.Dir = filepath.Join(p.Path, cmd.Path)
161+
ex.Dir = filepath.Join(p.Path, cmd.Path)
159162
}
160163
}
161-
exec.Stdout = &stdout
162-
exec.Stderr = &stderr
164+
ex.Stdout = &stdout
165+
ex.Stderr = &stderr
163166
// Start command
164-
exec.Start()
165-
go func() { done <- exec.Wait() }()
167+
ex.Start()
168+
go func() { done <- ex.Wait() }()
166169
// Wait a result
167170
select {
168171
case <-stop:
169172
// Stop running command
170-
exec.Process.Kill()
173+
ex.Process.Kill()
171174
return "", ""
172175
case err := <-done:
173176
// Command completed
@@ -206,15 +209,17 @@ func (p *Project) goTool(wg *sync.WaitGroup, stop <-chan bool, result chan<- too
206209
case <-stop:
207210
// Stop running command
208211
cmd.Process.Kill()
209-
break
212+
return
210213
case err := <-done:
211214
// Command completed
212215
if err != nil {
213216
tool.err = stderr.String() + out.String()
214217
// send command result
215218
result <- tool
219+
} else {
220+
tool.out = out.String()
216221
}
217-
break
222+
return
218223
}
219224

220225
}

notify.go

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ func (w *fsNotifyWatcher) Events() <-chan fsnotify.Event {
9595
return w.Watcher.Events
9696
}
9797

98+
// Walk fsnotify
9899
func (w *fsNotifyWatcher) Walk(path string, init bool) string {
99100
if err := w.Add(path); err != nil {
100101
return ""
@@ -106,9 +107,8 @@ func (w *fsNotifyWatcher) Walk(path string, init bool) string {
106107
// All watches are stopped, removed, and the poller cannot be added to
107108
func (w *filePoller) Close() error {
108109
w.mu.Lock()
109-
defer w.mu.Unlock()
110-
111110
if w.closed {
111+
w.mu.Unlock()
112112
return nil
113113
}
114114

@@ -117,6 +117,7 @@ func (w *filePoller) Close() error {
117117
w.remove(name)
118118
delete(w.watches, name)
119119
}
120+
w.mu.Unlock()
120121
return nil
121122
}
122123

@@ -157,6 +158,7 @@ func (w *filePoller) Add(name string) error {
157158
return nil
158159
}
159160

161+
// Remove poller
160162
func (w *filePoller) remove(name string) error {
161163
if w.closed {
162164
return errPollerClosed
@@ -184,6 +186,7 @@ func (w *filePoller) Events() <-chan fsnotify.Event {
184186
return w.events
185187
}
186188

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

234237
fi, err := os.Stat(f.Name())
235-
if err != nil {
236-
// if we got an error here and lastFi is not set, we can presume that nothing has changed
237-
// This should be safe since before `watch()` is called, a stat is performed, there is any error `watch` is not called
238-
if lastFi == nil {
239-
continue
240-
}
238+
switch {
239+
case err != nil && lastFi != nil:
241240
// If it doesn't exist at this point, it must have been removed
242241
// no need to send the error here since this is a valid operation
243242
if os.IsNotExist(err) {
244243
if err := w.sendEvent(fsnotify.Event{Op: fsnotify.Remove, Name: f.Name()}, chClose); err != nil {
245244
return
246245
}
247246
lastFi = nil
248-
continue
249247
}
250248
// at this point, send the error
251-
if err := w.sendErr(err, chClose); err != nil {
252-
return
253-
}
254-
continue
255-
}
256-
257-
if lastFi == nil {
249+
w.sendErr(err, chClose)
250+
return
251+
case lastFi == nil:
258252
if err := w.sendEvent(fsnotify.Event{Op: fsnotify.Create, Name: f.Name()}, chClose); err != nil {
259253
return
260254
}
261255
lastFi = fi
262-
continue
263-
}
264-
265-
if fi.Mode() != lastFi.Mode() {
256+
case fi.Mode() != lastFi.Mode():
266257
if err := w.sendEvent(fsnotify.Event{Op: fsnotify.Chmod, Name: f.Name()}, chClose); err != nil {
267258
return
268259
}
269260
lastFi = fi
270-
continue
271-
}
272-
273-
if fi.ModTime() != lastFi.ModTime() || fi.Size() != lastFi.Size() {
261+
case fi.ModTime() != lastFi.ModTime() || fi.Size() != lastFi.Size():
274262
if err := w.sendEvent(fsnotify.Event{Op: fsnotify.Write, Name: f.Name()}, chClose); err != nil {
275263
return
276264
}
277265
lastFi = fi
278-
continue
279266
}
280267
}
281268
}

realize.go

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
)
1616

1717
const (
18-
version = "1.5.1r2"
18+
version = "1.5.2"
1919
)
2020

2121
// New realize instance
@@ -35,26 +35,16 @@ type realize struct {
3535
// Cli commands
3636
func main() {
3737
app := &cli.App{
38-
Name: "Realize",
39-
Version: version,
40-
Authors: []*cli.Author{
41-
{
42-
Name: "Alessio Pracchia",
43-
Email: "pracchia@hastega.it",
44-
},
45-
{
46-
Name: "Daniele Conventi",
47-
Email: "conventi@hastega.it",
48-
},
49-
},
38+
Name: "Realize",
39+
Version: version,
5040
Description: "Go build system with file watchers, output streams and live reload. Run, build and watch file changes with custom paths",
5141
Commands: []*cli.Command{
5242
{
5343
Name: "start",
54-
Aliases: []string{"r"},
44+
Aliases: []string{"s"},
5545
Description: "Start a toolchain on a project or a list of projects. If not exist a config file it creates a new one",
5646
Flags: []cli.Flag{
57-
&cli.StringFlag{Name: "path", Aliases: []string{"p"}, Value: wdir(), Usage: "Project base path"},
47+
&cli.StringFlag{Name: "path", Aliases: []string{"p"}, Value: ".", Usage: "Project base path"},
5848
&cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: "", Usage: "Run a project by its name"},
5949
&cli.BoolFlag{Name: "fmt", Aliases: []string{"f"}, Value: false, Usage: "Enable go fmt"},
6050
&cli.BoolFlag{Name: "vet", Aliases: []string{"v"}, Value: false, Usage: "Enable go vet"},
@@ -172,7 +162,7 @@ func main() {
172162
if err != nil {
173163
return d.Err()
174164
}
175-
r.Settings.FileLimit = val
165+
r.Settings.FileLimit = int32(val)
176166
return nil
177167
},
178168
},
@@ -1217,11 +1207,11 @@ func prefix(s string) string {
12171207
if s != "" {
12181208
return fmt.Sprint(yellow.bold("["), "REALIZE", yellow.bold("]"), " : ", s)
12191209
}
1220-
return ""
1210+
return s
12211211
}
12221212

12231213
// Before is launched before each command
1224-
func before(*cli.Context) error {
1214+
func before(*cli.Context) (err error) {
12251215
// custom log
12261216
log.SetFlags(0)
12271217
log.SetOutput(logWriter{})
@@ -1230,7 +1220,7 @@ func before(*cli.Context) error {
12301220
if gopath == "" {
12311221
return errors.New("$GOPATH isn't set properly")
12321222
}
1233-
if err := os.Setenv("GOPATH", gopath); err != nil {
1223+
if err = os.Setenv("GOPATH", gopath); err != nil {
12341224
return err
12351225
}
12361226
// new realize instance
@@ -1239,11 +1229,11 @@ func before(*cli.Context) error {
12391229
r.Settings.read(&r)
12401230
// increase the file limit
12411231
if r.Settings.FileLimit != 0 {
1242-
if err := r.Settings.flimit(); err != nil {
1232+
if err = r.Settings.flimit(); err != nil {
12431233
return err
12441234
}
12451235
}
1246-
return nil
1236+
return
12471237
}
12481238

12491239
// Rewrite the layout of the log timestamp

server.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,15 @@ type Server struct {
2626
parent *realize
2727
Status bool `yaml:"status" json:"status"`
2828
Open bool `yaml:"open" json:"open"`
29-
Host string `yaml:"host" json:"host"`
3029
Port int `yaml:"port" json:"port"`
30+
Host string `yaml:"host" json:"host"`
3131
}
3232

3333
// Websocket projects
34-
func (s *Server) projects(c echo.Context) error {
34+
func (s *Server) projects(c echo.Context) (err error) {
3535
websocket.Handler(func(ws *websocket.Conn) {
36-
defer ws.Close()
3736
msg, _ := json.Marshal(s.parent)
38-
err := websocket.Message.Send(ws, string(msg))
37+
err = websocket.Message.Send(ws, string(msg))
3938
go func() {
4039
for {
4140
select {
@@ -51,7 +50,7 @@ func (s *Server) projects(c echo.Context) error {
5150
for {
5251
// Read
5352
text := ""
54-
err := websocket.Message.Receive(ws, &text)
53+
err = websocket.Message.Receive(ws, &text)
5554
if err != nil {
5655
break
5756
} else {
@@ -62,14 +61,17 @@ func (s *Server) projects(c echo.Context) error {
6261
}
6362
}
6463
}
64+
ws.Close()
6565
}).ServeHTTP(c.Response(), c.Request())
6666
return nil
6767
}
6868

6969
// Start the web server
7070
func (s *Server) start(p *cli.Context) (err error) {
7171
if p.Bool("server") {
72-
s.parent.Server.Status = p.Bool("server")
72+
s.parent.Server.Status = true
73+
}
74+
if p.Bool("open") {
7375
s.parent.Server.Open = true
7476
}
7577

settings.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ const (
3232
type Settings struct {
3333
file string
3434
Files `yaml:"files,omitempty" json:"files,omitempty"`
35-
FileLimit int64 `yaml:"flimit,omitempty" json:"flimit,omitempty"`
3635
Legacy Legacy `yaml:"legacy" json:"legacy"`
36+
FileLimit int32 `yaml:"flimit,omitempty" json:"flimit,omitempty"`
3737
Recovery bool `yaml:"recovery,omitempty" json:"recovery,omitempty"`
3838
}
3939

0 commit comments

Comments
 (0)