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>
diff --git a/cmd/bundle/main.go b/cmd/bundle/main.go
index 962ccd0..ac09ee8 100644
--- a/cmd/bundle/main.go
+++ b/cmd/bundle/main.go
@@ -176,6 +176,16 @@
 	}
 }
 
+// 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 @@
 			}
 		}
 	}
-	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()
 		}
-		fmt.Fprintf(&out, "\t%q\n", 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.Fprintln(&out, ")\n")
 
diff --git a/cmd/bundle/main_test.go b/cmd/bundle/main_test.go
index 009fc99..96c8cf7 100644
--- a/cmd/bundle/main_test.go
+++ b/cmd/bundle/main_test.go
@@ -29,6 +29,9 @@
 			"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{})`,
 		},
diff --git a/cmd/bundle/testdata/out.golden b/cmd/bundle/testdata/out.golden
index 2b1784e..59f98fe 100644
--- a/cmd/bundle/testdata/out.golden
+++ b/cmd/bundle/testdata/out.golden
@@ -8,6 +8,8 @@
 
 import (
 	"fmt"
+
+	"domain.name/importdecl"
 )
 
 // init functions are not renamed
@@ -27,5 +29,5 @@
 const prefixc = 1
 
 func prefixfoo() {
-	fmt.Println()
+	fmt.Println(importdecl.F())
 }
diff --git a/cmd/bundle/testdata/src/domain.name/importdecl/p.go b/cmd/bundle/testdata/src/domain.name/importdecl/p.go
new file mode 100644
index 0000000..36dfd15
--- /dev/null
+++ b/cmd/bundle/testdata/src/domain.name/importdecl/p.go
@@ -0,0 +1,3 @@
+package importdecl
+
+func F() int { return 1 }
diff --git a/cmd/bundle/testdata/src/initial/b.go b/cmd/bundle/testdata/src/initial/b.go
index 399b6ed..5ec45ad 100644
--- a/cmd/bundle/testdata/src/initial/b.go
+++ b/cmd/bundle/testdata/src/initial/b.go
@@ -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())
 }