Skip to content

Commit

Permalink
run goimports in parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
philhofer committed Jun 6, 2015
1 parent 6bf8f2e commit ad11069
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions printer/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ func PrintFile(file string, f *parse.FileSet, mode gen.Method) error {
if err != nil {
return err
}
err = format(file, out.Bytes())
if err != nil {
return err
}
infof(">>> Wrote and formatted \"%s\"\n", file)

// we'll run goimports on the main file
// in another goroutine, and run it here
// for the test file. empirically, this
// takes about the same amount of time as
// doing them in serial when GOMAXPROCS=1,
// and faster otherwise.
res := goformat(file, out.Bytes())
if tests != nil {
testfile := strings.TrimSuffix(file, ".go") + "_test.go"
err = format(testfile, tests.Bytes())
Expand All @@ -37,6 +40,10 @@ func PrintFile(file string, f *parse.FileSet, mode gen.Method) error {
}
infof(">>> Wrote and formatted \"%s\"\n", testfile)
}
err = <-res
if err != nil {
return err
}
infof(">>> Done.\n")
return nil
}
Expand All @@ -49,6 +56,15 @@ func format(file string, data []byte) error {
return ioutil.WriteFile(file, out, 0600)
}

func goformat(file string, data []byte) <-chan error {
out := make(chan error, 1)
go func(file string, data []byte, end chan error) {
end <- format(file, data)
infof(">>> Wrote and formatted \"%s\"\n", file)
}(file, data, out)
return out
}

func generate(f *parse.FileSet, mode gen.Method) (*bytes.Buffer, *bytes.Buffer, error) {
outbuf := bytes.NewBuffer(make([]byte, 0, 4096))
writePkgHeader(outbuf, f.Package)
Expand Down

0 comments on commit ad11069

Please sign in to comment.