GoFmt only on watched paths fixed and closed #10

This commit is contained in:
Daniele Conventi 2016-08-23 16:10:17 +02:00
parent f9e04069c1
commit 6c64284767
2 changed files with 21 additions and 11 deletions

View File

@ -107,9 +107,9 @@ func (p *Project) GoInstall() error {
} }
// GoFmt is an implementation of the gofmt // GoFmt is an implementation of the gofmt
func (p *Project) GoFmt() (io.Writer, error) { func (p *Project) GoFmt(path string) (io.Writer, error) {
var out bytes.Buffer var out bytes.Buffer
build := exec.Command("gofmt", "-s", "-w", "-e", ".") build := exec.Command("gofmt", "-s", "-w", "-e", path)
build.Dir = p.base build.Dir = p.base
build.Stdout = &out build.Stdout = &out
build.Stderr = &out build.Stderr = &out

View File

@ -78,6 +78,7 @@ func (p *Project) watching() {
walk := func(path string, info os.FileInfo, err error) error { walk := func(path string, info os.FileInfo, err error) error {
if !p.ignore(path) { if !p.ignore(path) {
if (info.IsDir() && len(filepath.Ext(path)) == 0 && !strings.HasPrefix(path, ".")) && !strings.Contains(path, "/.") || (inArray(filepath.Ext(path), p.Watcher.Exts)) { if (info.IsDir() && len(filepath.Ext(path)) == 0 && !strings.HasPrefix(path, ".")) && !strings.Contains(path, "/.") || (inArray(filepath.Ext(path), p.Watcher.Exts)) {
if p.Watcher.Preview { if p.Watcher.Preview {
fmt.Println(pname(p.Name, 1), ":", path) fmt.Println(pname(p.Name, 1), ":", path)
} }
@ -86,6 +87,11 @@ func (p *Project) watching() {
} }
if inArray(filepath.Ext(path), p.Watcher.Exts) { if inArray(filepath.Ext(path), p.Watcher.Exts) {
files++ files++
err := p.fmt(path)
if err == nil {
} else {
fmt.Println(Red(err))
}
} else { } else {
folders++ folders++
} }
@ -139,12 +145,16 @@ func (p *Project) watching() {
i := strings.Index(event.Name, filepath.Ext(event.Name)) i := strings.Index(event.Name, filepath.Ext(event.Name))
if event.Name[:i] != "" { if event.Name[:i] != "" {
log.Println(pname(p.Name, 4), ":", Magenta(event.Name[:i]+ext)) log.Println(pname(p.Name, 4), ":", Magenta(event.Name[:i]+ext))
// stop and run again // stop and run again
if p.Run { if p.Run {
close(channel) close(channel)
wr.Wait() wr.Wait()
} }
err := p.fmt(event.Name[:i]+ext)
if err == nil {
} else {
fmt.Println(Red(err))
}
go routines(p, channel, &wr) go routines(p, channel, &wr)
p.reload = time.Now().Truncate(time.Second) p.reload = time.Now().Truncate(time.Second)
} }
@ -200,10 +210,10 @@ func (p *Project) build() {
} }
// Build call an implementation of the "gofmt" // Build call an implementation of the "gofmt"
func (p *Project) fmt() error { func (p *Project) fmt(path string) error {
if p.Fmt { if p.Fmt {
if msg, err := p.GoFmt(); err != nil { if msg, err := p.GoFmt(path); err != nil {
log.Println(pname(p.Name, 1), Red("There are some errors:")) log.Println(pname(p.Name, 1), Red("There are some errors in "), Red(path), Red(":"))
fmt.Println(msg) fmt.Println(msg)
return err return err
} }
@ -224,15 +234,15 @@ func (p *Project) ignore(str string) bool {
// Routines launches the following methods: run, build, fmt, install // Routines launches the following methods: run, build, fmt, install
func routines(p *Project, channel chan bool, wr *sync.WaitGroup) { func routines(p *Project, channel chan bool, wr *sync.WaitGroup) {
channel = make(chan bool) channel = make(chan bool)
err := p.fmt() //err := p.fmt()
if err == nil { //if err == nil {
wr.Add(1) wr.Add(1)
go p.build() go p.build()
go p.install(channel, wr) go p.install(channel, wr)
wr.Wait() wr.Wait()
} else { //} else {
fmt.Println(Red(err)) // fmt.Println(Red(err))
} //}
} }
// check if a string is inArray // check if a string is inArray