Repository navigation
Expand file tree
/
Copy pathguess_deps.go
More file actions
169 lines (142 loc) · 4.1 KB
/
Copy pathguess_deps.go
File metadata and controls
169 lines (142 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package cmd
import (
"os"
"path/filepath"
"sort"
"strings"
"github.com/Masterminds/cookoo"
"github.com/Masterminds/glide/cfg"
"github.com/Masterminds/glide/dependency"
"github.com/Masterminds/glide/util"
)
// GuessDeps tries to get the dependencies for the current directory.
//
// Params
// - dirname (string): Directory to use as the base. Default: "."
// - skipImport (book): Whether to skip importing from Godep, GPM, and gb
func GuessDeps(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
buildContext, err := util.GetBuildContext()
if err != nil {
return nil, err
}
base := p.Get("dirname", ".").(string)
skipImport := p.Get("skipImport", false).(bool)
name := guessPackageName(buildContext, base)
Info("Generating a YAML configuration file and guessing the dependencies")
config := new(cfg.Config)
// Get the name of the top level package
config.Name = name
// Import by looking at other package managers and looking over the
// entire directory structure.
// Attempt to import from other package managers.
if !skipImport {
Info("Attempting to import from other package managers (use --skip-import to skip)")
deps := []*cfg.Dependency{}
absBase, err := filepath.Abs(base)
if err != nil {
return nil, err
}
if d, ok := guessImportGodep(absBase); ok {
Info("Importing Godep configuration")
Warn("Godep uses commit id versions. Consider using Semantic Versions with Glide")
deps = d
} else if d, ok := guessImportGPM(absBase); ok {
Info("Importing GPM configuration")
deps = d
} else if d, ok := guessImportGB(absBase); ok {
Info("Importing GB configuration")
deps = d
}
for _, i := range deps {
Info("Found imported reference to %s\n", i.Name)
config.Imports = append(config.Imports, i)
}
}
// Resolve dependencies by looking at the tree.
r, err := dependency.NewResolver(base)
if err != nil {
return nil, err
}
h := &dependency.DefaultMissingPackageHandler{Missing: []string{}, Gopath: []string{}}
r.Handler = h
sortable, err := r.ResolveLocal(false)
if err != nil {
return nil, err
}
sort.Strings(sortable)
vpath := r.VendorDir
if !strings.HasSuffix(vpath, "/") {
vpath = vpath + string(os.PathSeparator)
}
for _, pa := range sortable {
n := strings.TrimPrefix(pa, vpath)
root := util.GetRootFromPackage(n)
if !config.HasDependency(root) {
Info("Found reference to %s\n", n)
d := &cfg.Dependency{
Name: root,
}
subpkg := strings.TrimPrefix(n, root)
if len(subpkg) > 0 && subpkg != "/" {
d.Subpackages = []string{subpkg}
}
config.Imports = append(config.Imports, d)
} else {
subpkg := strings.TrimPrefix(n, root)
if len(subpkg) > 0 && subpkg != "/" {
subpkg = strings.TrimPrefix(subpkg, "/")
d := config.Imports.Get(root)
f := false
for _, v := range d.Subpackages {
if v == subpkg {
f = true
}
}
if !f {
Info("Adding sub-package %s to %s\n", subpkg, root)
d.Subpackages = append(d.Subpackages, subpkg)
}
}
}
}
return config, nil
}
// Attempt to guess at the package name at the top level. When unable to detect
// a name goes to default of "main".
func guessPackageName(b *util.BuildCtxt, base string) string {
cwd, err := os.Getwd()
if err != nil {
return "main"
}
pkg, err := b.Import(base, cwd, 0)
if err != nil {
// There may not be any top level Go source files but the project may
// still be within the GOPATH.
if strings.HasPrefix(base, b.GOPATH) {
p := strings.TrimPrefix(base, b.GOPATH)
return strings.Trim(p, string(os.PathSeparator))
}
}
return pkg.ImportPath
}
func guessImportGodep(dir string) ([]*cfg.Dependency, bool) {
d, err := parseGodepGodeps(dir)
if err != nil || len(d) == 0 {
return []*cfg.Dependency{}, false
}
return d, true
}
func guessImportGPM(dir string) ([]*cfg.Dependency, bool) {
d, err := parseGPMGodeps(dir)
if err != nil || len(d) == 0 {
return []*cfg.Dependency{}, false
}
return d, true
}
func guessImportGB(dir string) ([]*cfg.Dependency, bool) {
d, err := parseGbManifest(dir)
if err != nil || len(d) == 0 {
return []*cfg.Dependency{}, false
}
return d, true
}