This repository has been archived by the owner on Mar 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 399
/
old.go
97 lines (88 loc) · 2.45 KB
/
old.go
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
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package migrate
import (
"os"
"path/filepath"
"github.com/kardianos/govendor/context"
)
func init() {
register("internal", sysInternal{})
register("old-vendor", sysOldVendor{})
}
type sysInternal struct{}
func (sys sysInternal) Check(root string) (system, error) {
vendorFolder := "internal"
override := os.Getenv("GOVENDORFOLDER")
if len(override) != 0 {
vendorFolder = override
}
if hasDirs(root, vendorFolder) && hasFiles(root, filepath.Join(vendorFolder, "vendor.json")) {
return sys, nil
}
return nil, nil
}
func (sysInternal) Migrate(root string) error {
// Un-rewrite import paths.
// Copy files from internal to vendor.
// Update and move vendor file from "internal/vendor.json" to "vendor.json".
ctx, err := context.NewContext(root, filepath.Join("internal", "vendor.json"), "internal", true)
if err != nil {
return err
}
list, err := ctx.Status()
if err != nil {
return err
}
remove := make([]string, 0, len(list))
for _, item := range list {
if item.Status.Location != context.LocationVendor {
continue
}
pkg := ctx.Package[item.Local]
ctx.Operation = append(ctx.Operation, &context.Operation{
Pkg: pkg,
Src: pkg.Dir,
Dest: filepath.Join(ctx.RootDir, "vendor", filepath.ToSlash(item.Pkg.Path)),
})
remove = append(remove, filepath.Join(ctx.RootGopath, filepath.ToSlash(item.Local)))
ctx.RewriteRule[item.Local] = item.Pkg.Path
}
ctx.VendorFilePath = filepath.Join(ctx.RootDir, "vendor", "vendor.json")
err = ctx.WriteVendorFile()
if err != nil {
return err
}
err = ctx.Alter()
if err != nil {
return err
}
// Remove existing.
for _, r := range remove {
err = context.RemovePackage(r, "", false)
if err != nil {
return err
}
}
return os.Remove(filepath.Join(ctx.RootDir, "internal", "vendor.json"))
}
type sysOldVendor struct{}
func (sys sysOldVendor) Check(root string) (system, error) {
if hasDirs(root, "vendor") && hasFiles(root, "vendor.json") {
return sys, nil
}
return nil, nil
}
func (sysOldVendor) Migrate(root string) error {
ctx, err := context.NewContext(root, "vendor.json", "vendor", false)
if err != nil {
return err
}
ctx.VendorFilePath = filepath.Join(ctx.RootDir, "vendor", "vendor.json")
err = ctx.WriteVendorFile()
if err != nil {
return err
}
return os.Remove(filepath.Join(ctx.RootDir, "vendor.json"))
}