Skip to content

Commit

Permalink
Adds ImportName, ImportAlias
Browse files Browse the repository at this point in the history
  • Loading branch information
dave committed Mar 9, 2018
1 parent 585b61e commit 010a36a
Show file tree
Hide file tree
Showing 7 changed files with 519 additions and 46 deletions.
76 changes: 66 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Output:
```go
package main

import fmt "fmt"
import "fmt"

func main() {
fmt.Println("Hello, world")
Expand Down Expand Up @@ -847,7 +847,7 @@ fmt.Printf("%#v", f)
// Output:
// package a
//
// import unsafe "unsafe"
// import "unsafe"
//
// /*
// #include <stdio.h>
Expand Down Expand Up @@ -919,7 +919,7 @@ if err != nil {
```

### Anon
Anon adds an anonymous import:
Anon adds an anonymous import.

```go
f := NewFile("c")
Expand All @@ -934,6 +934,62 @@ fmt.Printf("%#v", f)
// func init() {}
```

### ImportName
ImportName provides the package name for a path. If specified, the alias will be omitted from the
import block. This is optional. If not specified, a sensible package name is used based on the path
and this is added as an alias in the import block.

```go
f := NewFile("main")

// package a should use name "a"
f.ImportName("github.com/foo/a", "a")

// package b is not used in the code so will not be included
f.ImportName("github.com/foo/b", "b")

f.Func().Id("main").Params().Block(
Qual("github.com/foo/a", "A").Call(),
)
fmt.Printf("%#v", f)

// Output:
// package main
//
// import "github.com/foo/a"
//
// func main() {
// a.A()
// }
```

### ImportAlias
ImportAlias provides the alias for a package path that should be used in the import block.

```go
f := NewFile("main")

// package a should be aliased to "b"
f.ImportAlias("github.com/foo/a", "b")

// package c is not used in the code so will not be included
f.ImportAlias("github.com/foo/c", "c")

f.Func().Id("main").Params().Block(
Qual("github.com/foo/a", "A").Call(),
)
fmt.Printf("%#v", f)

// Output:
// package main
//
// import b "github.com/foo/a"
//
// func main() {
// b.A()
// }
```

### Comments
PackageComment adds a comment to the top of the file, above the package
keyword.
Expand Down Expand Up @@ -961,22 +1017,22 @@ CgoPreamble adds a cgo preamble comment that is rendered directly before the "C"
import.

### PackagePrefix
If you're worried about package aliases conflicting with local variable
names, you can set a prefix here. Package foo becomes {prefix}_foo.
If you're worried about generated package aliases conflicting with local variable names, you
can set a prefix here. Package foo becomes {prefix}_foo.

```go
f := NewFile("c")
f := NewFile("a")
f.PackagePrefix = "pkg"
f.Func().Id("main").Params().Block(
Qual("fmt", "Println").Call(),
Qual("b.c/d", "E").Call(),
)
fmt.Printf("%#v", f)
// Output:
// package c
// package a
//
// import pkg_fmt "fmt"
// import pkg_d "b.c/d"
//
// func main() {
// pkg_fmt.Println()
// pkg_d.E()
// }
```
10 changes: 10 additions & 0 deletions README.md.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,16 @@ the import is separated, and preceded by the preamble.

{{ "ExampleFile_Anon" | example }}

### ImportName
{{ "File.ImportName" | doc }}

{{ "ExampleFile_ImportName" | example }}

### ImportAlias
{{ "File.ImportAlias" | doc }}

{{ "ExampleFile_ImportAlias" | example }}

### Comments
{{ "File.PackageComment" | doc }}

Expand Down
45 changes: 45 additions & 0 deletions genjen/render.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package main

import (
"go/build"
"io"
"os/exec"
"strings"

"fmt"
"path/filepath"

. "github.com/dave/jennifer/jen"
)

Expand Down Expand Up @@ -202,6 +207,23 @@ func render(w io.Writer) error {
)
}

packages, err := getStandardLibraryPackages()
if err != nil {
return err
}
/*
// PackageNameHints is a map containing hints for the names of all standard library packages
var PackageNameHints = map[string]string{
...
}
*/
file.Comment("PackageNameHints is a map containing hints for the names of all standard library packages")
file.Var().Id("PackageNameHints").Op("=").Map(String()).String().Values(DictFunc(func(d Dict) {
for path, name := range packages {
d[Lit(path)] = Lit(name)
}
}))

return file.Render(w)
}

Expand Down Expand Up @@ -254,3 +276,26 @@ func addFunctionAndGroupMethod(
Return(Id("s")),
)
}

func getStandardLibraryPackages() (map[string]string, error) {
cmd := exec.Command("go", "list", "-f", "{{ .ImportPath }} {{ .Name }}", "./...")
cmd.Env = []string{
fmt.Sprintf("GOPATH=%s", build.Default.GOPATH),
fmt.Sprintf("GOROOT=%s", build.Default.GOROOT),
}
cmd.Dir = filepath.Join(build.Default.GOROOT, "src")
b, err := cmd.CombinedOutput()
if err != nil {
return nil, err
}
all := strings.Split(strings.TrimSpace(string(b)), "\n")

packages := map[string]string{}
for _, j := range all {
parts := strings.Split(j, " ")
path := parts[0]
name := parts[1]
packages[path] = name
}
return packages, nil
}
68 changes: 58 additions & 10 deletions jen/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,54 @@ import (
. "github.com/dave/jennifer/jen"
)

func ExampleFile_ImportName() {
f := NewFile("main")

// package a should use name "a"
f.ImportName("github.com/foo/a", "a")

// package b is not used in the code so will not be included
f.ImportName("github.com/foo/b", "b")

f.Func().Id("main").Params().Block(
Qual("github.com/foo/a", "A").Call(),
)
fmt.Printf("%#v", f)

// Output:
// package main
//
// import "github.com/foo/a"
//
// func main() {
// a.A()
// }
}

func ExampleFile_ImportAlias() {
f := NewFile("main")

// package a should be aliased to "b"
f.ImportAlias("github.com/foo/a", "b")

// package c is not used in the code so will not be included
f.ImportAlias("github.com/foo/c", "c")

f.Func().Id("main").Params().Block(
Qual("github.com/foo/a", "A").Call(),
)
fmt.Printf("%#v", f)

// Output:
// package main
//
// import b "github.com/foo/a"
//
// func main() {
// b.A()
// }
}

func ExampleFile_CgoPreamble() {
f := NewFile("a")
f.CgoPreamble(`#include <stdio.h>
Expand All @@ -26,7 +74,7 @@ void myprint(char* s) {
// Output:
// package a
//
// import unsafe "unsafe"
// import "unsafe"
//
// /*
// #include <stdio.h>
Expand Down Expand Up @@ -82,7 +130,7 @@ func ExampleFile_CgoPreamble_no_preamble() {
//
// import (
// "C"
// fmt "fmt"
// "fmt"
// )
//
// func init() {
Expand Down Expand Up @@ -132,7 +180,7 @@ func ExampleFile_CgoPreamble_no_preamble_anon() {
//
// import (
// "C"
// fmt "fmt"
// "fmt"
// )
//
// func init() {
Expand Down Expand Up @@ -1298,7 +1346,7 @@ func ExampleId_remote() {
// Output:
// package main
//
// import fmt "fmt"
// import "fmt"
//
// func main() {
// fmt.Println("Hello, world")
Expand Down Expand Up @@ -1329,7 +1377,7 @@ func ExampleNewFile() {
// Output:
// package main
//
// import fmt "fmt"
// import "fmt"
//
// func main() {
// fmt.Println("Hello, world")
Expand Down Expand Up @@ -1379,18 +1427,18 @@ func ExampleFile_Anon() {
}

func ExampleFile_PackagePrefix() {
f := NewFile("c")
f := NewFile("a")
f.PackagePrefix = "pkg"
f.Func().Id("main").Params().Block(
Qual("fmt", "Println").Call(),
Qual("b.c/d", "E").Call(),
)
fmt.Printf("%#v", f)
// Output:
// package c
// package a
//
// import pkg_fmt "fmt"
// import pkg_d "b.c/d"
//
// func main() {
// pkg_fmt.Println()
// pkg_d.E()
// }
}
Loading

0 comments on commit 010a36a

Please sign in to comment.