cmd/bundle: move down imports of external package

imports should be ordered like

    import (
    	"fmt"

    	"golang.org/x/text"
    )

Change-Id: I000374833de370463d772c2596c7ac6ee5e9725c
Reviewed-on: https://go-review.googlesource.com/23026
Reviewed-by: Mikio Hara <mikioh.mikioh@gmail.com>
Reviewed-by: Nigel Tao <nigeltao@golang.org>
This commit is contained in:
Yasuhiro Matsumoto 2016-05-12 13:39:32 +09:00 committed by Nigel Tao
parent 6d483ee832
commit ba484b064f
5 changed files with 46 additions and 8 deletions

View File

@ -176,6 +176,16 @@ func main() {
}
}
// isStandardImportPath is copied from cmd/go in the standard library.
func isStandardImportPath(path string) bool {
i := strings.Index(path, "/")
if i < 0 {
i = len(path)
}
elem := path[:i]
return !strings.Contains(elem, ".")
}
var ctxt = &build.Default
func bundle(src, dst, dstpkg, prefix string) ([]byte, error) {
@ -259,16 +269,32 @@ func bundle(src, dst, dstpkg, prefix string) ([]byte, error) {
}
}
}
fmt.Fprintln(&out, "import (")
var pkgStd, pkgExt []string
for _, p := range info.Pkg.Imports() {
if p.Path() == dst {
continue
}
if x, ok := importMap[p.Path()]; ok {
fmt.Fprintf(&out, "\t%q\n", x)
continue
x, ok := importMap[p.Path()]
if !ok {
x = p.Path()
}
if isStandardImportPath(x) {
pkgStd = append(pkgStd, x)
} else {
pkgExt = append(pkgExt, x)
}
}
fmt.Fprintln(&out, "import (")
for _, p := range pkgStd {
fmt.Fprintf(&out, "\t%q\n", p)
}
if len(pkgExt) > 0 {
fmt.Fprintln(&out)
for _, p := range pkgExt {
fmt.Fprintf(&out, "\t%q\n", p)
}
fmt.Fprintf(&out, "\t%q\n", p.Path())
}
fmt.Fprintln(&out, ")\n")

View File

@ -29,6 +29,9 @@ func TestBundle(t *testing.T) {
"a.go": load("testdata/src/initial/a.go"),
"b.go": load("testdata/src/initial/b.go"),
},
"domain.name/importdecl": {
"p.go": load("testdata/src/domain.name/importdecl/p.go"),
},
"fmt": {
"print.go": `package fmt; func Println(...interface{})`,
},

View File

@ -8,6 +8,8 @@ package dest
import (
"fmt"
"domain.name/importdecl"
)
// init functions are not renamed
@ -27,5 +29,5 @@ type prefixt int
const prefixc = 1
func prefixfoo() {
fmt.Println()
fmt.Println(importdecl.F())
}

View File

@ -0,0 +1,3 @@
package importdecl
func F() int { return 1 }

View File

@ -1,12 +1,16 @@
// The package doc comment
package initial
import "fmt"
import (
"fmt"
"domain.name/importdecl"
)
type t int
const c = 1
func foo() {
fmt.Println()
fmt.Println(importdecl.F())
}