Skip to content

Commit 6c30310

Browse files
authored
Merge pull request #1782 from dolmen-go/codegen-copy-dependency
_codegen: copy dependency github.com/ernesto-jimenez/gogen/imports
2 parents a0ac157 + 242e746 commit 6c30310

File tree

4 files changed

+109
-5
lines changed

4 files changed

+109
-5
lines changed

_codegen/go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
module github.com/stretchr/testify/_codegen
22

33
go 1.11
4-
5-
require github.com/ernesto-jimenez/gogen v0.0.0-20180125220232-d7d4131e6607

_codegen/go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +0,0 @@
1-
github.com/ernesto-jimenez/gogen v0.0.0-20180125220232-d7d4131e6607 h1:cTavhURetDkezJCvxFggiyLeP40Mrk/TtVg2+ycw1Es=
2-
github.com/ernesto-jimenez/gogen v0.0.0-20180125220232-d7d4131e6607/go.mod h1:Cg4fM0vhYWOZdgM7RIOSTRNIc8/VT7CXClC3Ni86lu4=
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2015 Ernesto Jiménez
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*/
24+
25+
package imports
26+
27+
import (
28+
"go/types"
29+
"os"
30+
"path/filepath"
31+
"strings"
32+
)
33+
34+
type Importer interface {
35+
AddImportsFrom(t types.Type)
36+
Imports() map[string]string
37+
}
38+
39+
// imports contains metadata about all the imports from a given package
40+
type imports struct {
41+
currentpkg string
42+
imp map[string]string
43+
}
44+
45+
// AddImportsFrom adds imports used in the passed type
46+
func (imp *imports) AddImportsFrom(t types.Type) {
47+
switch el := t.(type) {
48+
case *types.Basic:
49+
case *types.Slice:
50+
imp.AddImportsFrom(el.Elem())
51+
case *types.Pointer:
52+
imp.AddImportsFrom(el.Elem())
53+
case *types.Named:
54+
pkg := el.Obj().Pkg()
55+
if pkg == nil {
56+
return
57+
}
58+
if pkg.Name() == imp.currentpkg {
59+
return
60+
}
61+
imp.imp[cleanImportPath(pkg.Path())] = pkg.Name()
62+
case *types.Tuple:
63+
for i := 0; i < el.Len(); i++ {
64+
imp.AddImportsFrom(el.At(i).Type())
65+
}
66+
default:
67+
}
68+
}
69+
70+
func cleanImportPath(ipath string) string {
71+
return gopathlessImportPath(
72+
vendorlessImportPath(ipath),
73+
)
74+
}
75+
76+
func gopathlessImportPath(ipath string) string {
77+
paths := strings.Split(os.Getenv("GOPATH"), ":")
78+
for _, p := range paths {
79+
ipath = strings.TrimPrefix(ipath, filepath.Join(p, "src")+string(filepath.Separator))
80+
}
81+
return ipath
82+
}
83+
84+
// vendorlessImportPath returns the devendorized version of the provided import path.
85+
// e.g. "foo/bar/vendor/a/b" => "a/b"
86+
func vendorlessImportPath(ipath string) string {
87+
// Devendorize for use in import statement.
88+
if i := strings.LastIndex(ipath, "/vendor/"); i >= 0 {
89+
return ipath[i+len("/vendor/"):]
90+
}
91+
if strings.HasPrefix(ipath, "vendor/") {
92+
return ipath[len("vendor/"):]
93+
}
94+
return ipath
95+
}
96+
97+
// AddImportsFrom adds imports used in the passed type
98+
func (imp *imports) Imports() map[string]string {
99+
return imp.imp
100+
}
101+
102+
// New initializes a new structure to track packages imported by the currentpkg
103+
func New(currentpkg string) Importer {
104+
return &imports{
105+
currentpkg: currentpkg,
106+
imp: make(map[string]string),
107+
}
108+
}

_codegen/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"strings"
2424
"text/template"
2525

26-
"github.com/ernesto-jimenez/gogen/imports"
26+
"github.com/stretchr/testify/_codegen/internal/imports"
2727
)
2828

2929
var (

0 commit comments

Comments
 (0)