Skip to content
Permalink
Browse files

update copies of gofmt and goimports

  • Loading branch information
mvdan committed Nov 5, 2019
1 parent f2b90f2 commit 7340a50c732ed0525f4844fb9c773347fe500819
20 gen.go
@@ -18,6 +18,10 @@ func main() {
cfg := &packages.Config{Mode: packages.NeedName | packages.NeedFiles}
pkgs, err := packages.Load(cfg,
"cmd/gofmt",

// These are internal cmd dependencies. Copy them.
"cmd/internal/diff",

"golang.org/x/tools/cmd/goimports",

// These are internal goimports dependencies. Copy them.
@@ -38,8 +42,12 @@ func main() {
copyGoimports(pkg.GoFiles)
default:
parts := strings.Split(pkg.PkgPath, "/")
dir := filepath.Join(append([]string{"gofumports"}, parts[3:]...)...)
copyInternal(pkg.GoFiles, dir)
if parts[0] == "cmd" {
copyInternal(pkg.GoFiles, filepath.Join(parts[1:]...))
} else {
dir := filepath.Join(append([]string{"gofumports"}, parts[3:]...)...)
copyInternal(pkg.GoFiles, dir)
}
}
}
}
@@ -69,6 +77,7 @@ func copyGofmt(files []string) {
`
for _, path := range files {
body := readFile(path)
body = fixImports(body)
name := filepath.Base(path)
switch name {
case "doc.go":
@@ -124,8 +133,13 @@ func copyInternal(files []string, dir string) {
}

func fixImports(body string) string {
return strings.Replace(body,
body = strings.Replace(body,
"golang.org/x/tools/internal/",
"mvdan.cc/gofumpt/gofumports/internal/",
-1)
body = strings.Replace(body,
"cmd/internal/",
"mvdan.cc/gofumpt/internal/",
-1)
return body
}
4 go.mod
@@ -3,7 +3,7 @@ module mvdan.cc/gofumpt
go 1.12

require (
github.com/rogpeppe/go-internal v1.3.2
golang.org/x/tools v0.0.0-20190917215024-905c8ffbfa41
github.com/rogpeppe/go-internal v1.5.0
golang.org/x/tools v0.0.0-20191104232314-dc038396d1f0
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
)
8 go.sum
@@ -3,15 +3,15 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/rogpeppe/go-internal v1.3.2 h1:XU784Pr0wdahMY2bYcyK6N1KuaRAdLtqD4qd8D18Bfs=
github.com/rogpeppe/go-internal v1.3.2/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.5.0 h1:Usqs0/lDK/NqTkvrmKSwA/3XkZAs7ZAW/eLeQ2MVBTw=
github.com/rogpeppe/go-internal v1.5.0/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190917215024-905c8ffbfa41 h1:b81roplyyD40MvaAPpAaKtN/Aahd9L3t35zoiycwjRI=
golang.org/x/tools v0.0.0-20190917215024-905c8ffbfa41/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191104232314-dc038396d1f0 h1:azkp5oIgy7LNGQ64URezZccjePaEGSYIHEgYTn/bfXI=
golang.org/x/tools v0.0.0-20191104232314-dc038396d1f0/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
@@ -16,13 +16,13 @@ import (
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"runtime/pprof"
"strings"

"mvdan.cc/gofumpt/internal"
"mvdan.cc/gofumpt/internal/diff"
)

var (
@@ -147,7 +147,7 @@ func processFile(filename string, in io.Reader, out io.Writer, stdin bool) error
}
}
if *doDiff {
data, err := diff(src, res, filename)
data, err := diffWithReplaceTempFile(src, res, filename)
if err != nil {
return fmt.Errorf("computing diff: %s", err)
}
@@ -233,47 +233,12 @@ func gofumptMain() {
}
}

func writeTempFile(dir, prefix string, data []byte) (string, error) {
file, err := ioutil.TempFile(dir, prefix)
if err != nil {
return "", err
}
_, err = file.Write(data)
if err1 := file.Close(); err == nil {
err = err1
}
if err != nil {
os.Remove(file.Name())
return "", err
}
return file.Name(), nil
}

func diff(b1, b2 []byte, filename string) (data []byte, err error) {
f1, err := writeTempFile("", "gofumpt", b1)
if err != nil {
return
}
defer os.Remove(f1)

f2, err := writeTempFile("", "gofumpt", b2)
if err != nil {
return
}
defer os.Remove(f2)

cmd := "diff"
if runtime.GOOS == "plan9" {
cmd = "/bin/ape/diff"
}

data, err = exec.Command(cmd, "-u", f1, f2).CombinedOutput()
func diffWithReplaceTempFile(b1, b2 []byte, filename string) ([]byte, error) {
data, err := diff.Diff("gofumpt", b1, b2)
if len(data) > 0 {
// diff exits with a non-zero status when the files don't match.
// Ignore that failure as long as we get output.
return replaceTempFilename(data, filename)
}
return
return data, err
}

// replaceTempFilename replaces temporary filenames in diff with actual one.
@@ -16,6 +16,7 @@ import (
"os"
"path/filepath"
"strings"
"time"

"mvdan.cc/gofumpt/gofumports/internal/fastwalk"
)
@@ -83,8 +84,9 @@ func walkDir(root Root, add func(Root, string), skip func(root Root, dir string)
}
return
}
start := time.Now()
if opts.Debug {
log.Printf("scanning %s", root.Path)
log.Printf("gopathwalk: scanning %s", root.Path)
}
w := &walker{
root: root,
@@ -98,7 +100,7 @@ func walkDir(root Root, add func(Root, string), skip func(root Root, dir string)
}

if opts.Debug {
log.Printf("scanned %s", root.Path)
log.Printf("gopathwalk: scanned %s in %v", root.Path, time.Since(start))
}
}

0 comments on commit 7340a50

Please sign in to comment.
You can’t perform that action at this time.