|
| 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 | +} |
0 commit comments