forked from whyrusleeping/gx-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
importer.go_bak
347 lines (288 loc) · 6.91 KB
/
importer.go_bak
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package main
import (
"fmt"
"go/build"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
rw "github.com/whyrusleeping/gx-go/rewrite"
gx "github.com/whyrusleeping/gx/gxutil"
. "github.com/whyrusleeping/stump"
)
func doUpdate(dir, oldimp, newimp string) error {
rwf := func(in string) string {
if in == oldimp {
return newimp
}
if strings.HasPrefix(in, oldimp+"/") {
return strings.Replace(in, oldimp, newimp, 1)
}
return in
}
filter := func(in string) bool {
return strings.HasSuffix(in, ".go") && !strings.HasPrefix(in, "vendor")
}
return rw.RewriteImports(dir, rwf, filter)
}
func pathIsNotStdlib(path string) bool {
first := strings.Split(path, "/")[0]
if len(strings.Split(first, ".")) > 1 {
return true
}
return false
}
type Importer struct {
pkgs map[string]*gx.Dependency
gopath string
pm *gx.PM
rewrite bool
yesall bool
preMap map[string]string
bctx build.Context
}
func NewImporter(rw bool, gopath string, premap map[string]string) (*Importer, error) {
cfg, err := gx.LoadConfig()
if err != nil {
return nil, err
}
pm, err := gx.NewPM(cfg)
if err != nil {
return nil, err
}
if premap == nil {
premap = make(map[string]string)
}
bctx := build.Default
bctx.GOPATH = gopath
return &Importer{
pkgs: make(map[string]*gx.Dependency),
gopath: gopath,
pm: pm,
rewrite: rw,
preMap: premap,
bctx: bctx,
}, nil
}
// this function is an attempt to keep subdirectories of a package as part of
// the same logical gx package. It has a special case for golang.org/x/ packages
func getBaseDVCS(path string) string {
parts := strings.Split(path, "/")
depth := 3
/*
if parts[0] == "golang.org" && parts[1] == "x" {
depth = 4
}
*/
if len(parts) > depth {
return strings.Join(parts[:3], "/")
}
return path
}
func (i *Importer) GxPublishGoPackage(imppath string) (*gx.Dependency, error) {
imppath = getBaseDVCS(imppath)
if d, ok := i.pkgs[imppath]; ok {
return d, nil
}
if hash, ok := i.preMap[imppath]; ok {
pkg, err := i.pm.GetPackageTo(hash, filepath.Join(vendorDir, hash))
if err != nil {
return nil, err
}
dep := &gx.Dependency{
Hash: hash,
Name: pkg.Name,
Version: pkg.Version,
}
i.pkgs[imppath] = dep
return dep, nil
}
// make sure its local
err := i.GoGet(imppath)
if err != nil {
if !strings.Contains(err.Error(), "no buildable Go source files") {
return nil, err
}
}
pkgpath := path.Join(i.gopath, "src", imppath)
pkgFilePath := path.Join(pkgpath, gx.PkgFileName)
pkg, err := LoadPackageFile(pkgFilePath)
if err != nil {
if !os.IsNotExist(err) {
return nil, err
}
// init as gx package
parts := strings.Split(imppath, "/")
pkgname := parts[len(parts)-1]
if !i.yesall {
p := fmt.Sprintf("enter name for import '%s'", imppath)
nname, err := prompt(p, pkgname)
if err != nil {
return nil, err
}
pkgname = nname
}
err = i.pm.InitPkg(pkgpath, pkgname, "go", nil)
if err != nil {
return nil, err
}
pkg, err = LoadPackageFile(pkgFilePath)
if err != nil {
return nil, err
}
}
// wipe out existing dependencies
pkg.Dependencies = nil
// recurse!
depsToVendor, err := i.depsToVendorForPackage(imppath)
if err != nil {
return nil, err
}
for n, child := range depsToVendor {
Log("- processing dep %s for %s [%d / %d]", child, imppath, n+1, len(depsToVendor))
if strings.HasPrefix(child, imppath) {
continue
}
childdep, err := i.GxPublishGoPackage(child)
if err != nil {
return nil, err
}
pkg.Dependencies = append(pkg.Dependencies, childdep)
}
err = gx.SavePackageFile(pkg, pkgFilePath)
if err != nil {
return nil, err
}
fullpkgpath, err := filepath.Abs(pkgpath)
if err != nil {
return nil, err
}
err = i.rewriteImports(fullpkgpath)
if err != nil {
return nil, err
}
err = writeGxIgnore(pkgpath, []string{"Godeps/*"})
if err != nil {
return nil, err
}
hash, err := i.pm.PublishPackage(pkgpath, &pkg.PackageBase)
if err != nil {
return nil, err
}
Log("published %s as %s", imppath, hash)
dep := &gx.Dependency{
Hash: hash,
Name: pkg.Name,
Version: pkg.Version,
}
i.pkgs[imppath] = dep
return dep, nil
}
func (i *Importer) depsToVendorForPackage(path string) ([]string, error) {
rdeps := make(map[string]struct{})
gopkg, err := i.bctx.Import(path, "", 0)
if err != nil {
_, ok := err.(*build.NoGoError)
if !ok {
return nil, err
}
// if theres no go code here, there still might be some in lower directories
} else {
imps := append(gopkg.Imports, gopkg.TestImports...)
// if the package existed and has go code in it
gdeps := getBaseDVCS(path) + "/Godeps/_workspace/src/"
for _, child := range imps {
if strings.HasPrefix(child, gdeps) {
child = child[len(gdeps):]
}
child = getBaseDVCS(child)
if pathIsNotStdlib(child) && !strings.HasPrefix(child, path) {
rdeps[child] = struct{}{}
}
}
}
dirents, err := ioutil.ReadDir(filepath.Join(i.gopath, "src", path))
if err != nil {
return nil, err
}
for _, e := range dirents {
if !e.IsDir() || skipDir(e.Name()) {
continue
}
out, err := i.depsToVendorForPackage(filepath.Join(path, e.Name()))
if err != nil {
return nil, err
}
for _, o := range out {
rdeps[o] = struct{}{}
}
}
var depsToVendor []string
for d, _ := range rdeps {
depsToVendor = append(depsToVendor, d)
}
return depsToVendor, nil
}
func skipDir(name string) bool {
switch name {
case "Godeps", "vendor", ".git":
return true
default:
return false
}
}
func (i *Importer) rewriteImports(pkgpath string) error {
filter := func(p string) bool {
return !strings.HasPrefix(p, "vendor") &&
!strings.HasPrefix(p, ".git") &&
strings.HasSuffix(p, ".go") &&
!strings.HasPrefix(p, "Godeps")
}
base := pkgpath[len(i.gopath)+5:]
gdepath := base + "/Godeps/_workspace/src/"
rwf := func(in string) string {
if strings.HasPrefix(in, gdepath) {
in = in[len(gdepath):]
}
if !i.rewrite {
// if rewrite not specified, just fixup godeps paths
return in
}
dep, ok := i.pkgs[in]
if ok {
return "gx/" + dep.Hash + "/" + dep.Name
}
parts := strings.Split(in, "/")
if len(parts) > 3 {
obase := strings.Join(parts[:3], "/")
dep, bok := i.pkgs[obase]
if !bok {
return in
}
return strings.Replace(in, obase, "gx/"+dep.Hash+"/"+dep.Name, 1)
}
return in
}
return rw.RewriteImports(pkgpath, rwf, filter)
}
// TODO: take an option to grab packages from local GOPATH
func (imp *Importer) GoGet(path string) error {
cmd := exec.Command("go", "get", path)
env := os.Environ()
for i, e := range env {
if strings.HasPrefix(e, "GOPATH=") {
env[i] = "GOPATH=" + imp.gopath
}
}
cmd.Env = env
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("go get failed: %s - %s", string(out), err)
}
return nil
}
func writeGxIgnore(dir string, ignore []string) error {
return ioutil.WriteFile(filepath.Join(dir, ".gxignore"), []byte(strings.Join(ignore, "\n")), 0644)
}