#119 fixed workflow

This commit is contained in:
asoseil 2017-11-14 16:12:37 +01:00
parent f3fc262994
commit 331b181b69
3 changed files with 96 additions and 75 deletions

2
cmd.go
View File

@ -9,7 +9,7 @@ import (
// Tool options customizable, should be moved in Cmd
type tool struct {
name, err string
name, err, out string
cmd, options []string
dir, status bool
}

View File

@ -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
}
@ -213,6 +214,8 @@ func (p *Project) goTool(wg *sync.WaitGroup, stop <-chan bool, result chan<- too
tool.err = stderr.String() + out.String()
// send command result
result <- tool
} else {
tool.out = out.String()
}
return
}

View File

@ -118,16 +118,27 @@ L:
select {
case event := <-p.watcher.Events():
if time.Now().Truncate(time.Second).After(p.lastTime) || event.Name != p.lastFile {
// event time
eventTime := time.Now()
// file extension
ext := ext(event.Name)
if ext == "" {
ext = "DIR"
}
// change message
msg = fmt.Sprintln(p.pname(p.Name, 4), ":", magenta.bold(strings.ToUpper(ext)), "changed", magenta.bold(event.Name))
out = BufferOut{Time: time.Now(), Text: ext + " changed " + event.Name}
// switch event type
switch event.Op {
case fsnotify.Chmod:
case fsnotify.Remove:
ext := ext(event.Name)
p.watcher.Remove(event.Name)
if !strings.Contains(ext, "_") && !strings.Contains(ext, ".") && array(ext, p.Watcher.Exts) {
close(stop)
stop = make(chan bool)
p.changed(event, stop) // stop
p.stamp("log", out, msg, "")
go p.routines(stop, p.watcher, "")
}
p.watcher.Remove(event.Name)
default:
file, err := os.Stat(event.Name)
if err != nil {
@ -136,15 +147,20 @@ L:
if file.IsDir() {
filepath.Walk(event.Name, p.walk)
} else if file.Size() > 0 {
// used only for test and debug
if p.parent.Settings.Recovery {
log.Println(event)
}
ext := ext(event.Name)
if !strings.Contains(ext, "_") && !strings.Contains(ext, ".") && array(ext, p.Watcher.Exts) {
// change watched
// check if a file is still writing #119
if event.Op != fsnotify.Write || eventTime.Truncate(time.Millisecond).After(file.ModTime().Truncate(time.Millisecond)) {
close(stop)
stop = make(chan bool)
p.changed(event, stop)
// stop and start again
p.stamp("log", out, msg, "")
go p.routines(stop, p.watcher, event.Name)
}
}
p.lastTime = time.Now().Truncate(time.Second)
p.lastFile = event.Name
@ -189,24 +205,12 @@ func (p *Project) config(r *realize) {
if len(p.Cmds.Fmt.Args) == 0 {
p.Cmds.Fmt.Args = []string{"-s", "-w", "-e", "./"}
}
p.tools = append(p.tools, tool{
status: p.Cmds.Fix.Status,
cmd: replace([]string{"go fix"}, p.Cmds.Fix.Method),
options: split([]string{}, p.Cmds.Fix.Args),
name: "Fix",
})
p.tools = append(p.tools, tool{
status: p.Cmds.Clean.Status,
cmd: replace([]string{"go clean"}, p.Cmds.Clean.Method),
options: split([]string{}, p.Cmds.Clean.Args),
name: "Clean",
})
p.tools = append(p.tools, tool{
status: p.Cmds.Fmt.Status,
cmd: replace([]string{"gofmt"}, p.Cmds.Fmt.Method),
options: split([]string{}, p.Cmds.Fmt.Args),
name: "Fmt",
})
p.tools = append(p.tools, tool{
status: p.Cmds.Generate.Status,
cmd: replace([]string{"go", "generate"}, p.Cmds.Generate.Method),
@ -215,11 +219,16 @@ func (p *Project) config(r *realize) {
dir: true,
})
p.tools = append(p.tools, tool{
status: p.Cmds.Test.Status,
cmd: replace([]string{"go", "test"}, p.Cmds.Test.Method),
options: split([]string{}, p.Cmds.Test.Args),
name: "Test",
dir: true,
status: p.Cmds.Fix.Status,
cmd: replace([]string{"go fix"}, p.Cmds.Fix.Method),
options: split([]string{}, p.Cmds.Fix.Args),
name: "Fix",
})
p.tools = append(p.tools, tool{
status: p.Cmds.Fmt.Status,
cmd: replace([]string{"gofmt"}, p.Cmds.Fmt.Method),
options: split([]string{}, p.Cmds.Fmt.Args),
name: "Fmt",
})
p.tools = append(p.tools, tool{
status: p.Cmds.Vet.Status,
@ -228,6 +237,13 @@ func (p *Project) config(r *realize) {
name: "Vet",
dir: true,
})
p.tools = append(p.tools, tool{
status: p.Cmds.Test.Status,
cmd: replace([]string{"go", "test"}, p.Cmds.Test.Method),
options: split([]string{}, p.Cmds.Test.Args),
name: "Test",
dir: true,
})
p.Cmds.Install = Cmd{
Status: p.Cmds.Install.Status,
Args: append([]string{}, p.Cmds.Install.Args...),
@ -291,10 +307,11 @@ func (p *Project) cmd(stop <-chan bool, flag string, global bool) {
// Compile is used for run and display the result of a compiling
func (p *Project) compile(stop <-chan bool, cmd Cmd) error {
if cmd.Status {
start := time.Now()
var start time.Time
channel := make(chan Result)
go func() {
log.Println(p.pname(p.Name, 1), ":", cmd.startTxt)
start = time.Now()
stream, err := p.goCompile(stop, cmd.method, cmd.Args)
if stream != msgStop {
channel <- Result{stream, err}
@ -352,7 +369,7 @@ func (p *Project) tool(stop <-chan bool, path string) error {
// no need a sequence, these commands can be asynchronous
if element.status {
wg.Add(1)
go p.goTool(&wg, stop, result, path, element)
p.goTool(&wg, stop, result, path, element)
}
}
wg.Wait()
@ -362,9 +379,15 @@ func (p *Project) tool(stop <-chan bool, path string) error {
for {
select {
case tool := <-result:
if tool.err != "" {
msg = fmt.Sprintln(p.pname(p.Name, 2), ":", red.bold(tool.name), red.regular("there are some errors in"), ":", magenta.bold(path))
buff := BufferOut{Time: time.Now(), Text: "there are some errors in", Path: path, Type: tool.name, Stream: tool.err}
p.stamp("error", buff, msg, tool.err)
} else if tool.out != "" {
msg = fmt.Sprintln(p.pname(p.Name, 3), ":", red.bold(tool.name), red.regular("outputs"), ":", blue.bold(path))
buff := BufferOut{Time: time.Now(), Text: "outputs", Path: path, Type: tool.name, Stream: tool.out}
p.stamp("out", buff, msg, tool.out)
}
case <-done:
break loop
case <-stop:
@ -375,19 +398,6 @@ func (p *Project) tool(stop <-chan bool, path string) error {
return nil
}
// Changed detect a file/directory change
func (p *Project) changed(event fsnotify.Event, stop chan bool) {
e := ext(event.Name)
if e == "" {
e = "DIR"
}
msg = fmt.Sprintln(p.pname(p.Name, 4), ":", magenta.bold(strings.ToUpper(e)), "changed", magenta.bold(event.Name))
out = BufferOut{Time: time.Now(), Text: ext(event.Name) + " changed " + event.Name}
p.stamp("log", out, msg, "")
//stop running process
go p.routines(stop, p.watcher, event.Name)
}
// Watch the files tree of a project
func (p *Project) walk(path string, info os.FileInfo, err error) error {
for _, v := range p.Watcher.Ignore {
@ -476,32 +486,39 @@ func (p *Project) routines(stop <-chan bool, watcher FileWatcher, path string) {
}
}
}()
if !done {
if done {
return
}
// before command
p.cmd(stop, "before", false)
if done {
return
}
if !done {
// Go supported tools
p.tool(stop, path)
// Prevent fake events on polling startup
p.init = true
}
// prevent errors using realize without config with only run flag
if p.Cmds.Run && !p.Cmds.Install.Status && !p.Cmds.Build.Status {
p.Cmds.Install.Status = true
}
if !done {
if done {
return
}
install = p.compile(stop, p.Cmds.Install)
if done {
return
}
if !done {
build = p.compile(stop, p.Cmds.Build)
if done {
return
}
if !done && (install == nil && build == nil) {
if p.Cmds.Run {
start := time.Now()
if install == nil && build == nil && p.Cmds.Run {
var start time.Time
runner := make(chan bool, 1)
go func() {
log.Println(p.pname(p.Name, 1), ":", "Running..")
start = time.Now()
p.goRun(stop, runner)
}()
select {
@ -513,8 +530,9 @@ func (p *Project) routines(stop <-chan bool, watcher FileWatcher, path string) {
return
}
}
if done {
return
}
if !done {
p.cmd(stop, "after", false)
}
}