-
Notifications
You must be signed in to change notification settings - Fork 11
/
rewrite.go
98 lines (81 loc) · 1.75 KB
/
rewrite.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
98
package main
import (
"go/parser"
"go/printer"
"go/token"
"os"
"strconv"
"strings"
)
func rewrite(pkgs []*Pkg, prefix string) error {
importPaths := make([]string, 0)
files := make([]string, 0)
for _, pkg := range pkgs {
importPaths = append(importPaths, pkg.ImportPath)
files = append(files, pkg.GoFiles()...)
}
for _, file := range files {
err := rewriteGoFile(file, prefix, importPaths)
if err != nil {
return err
}
}
return nil
}
func rewriteGoFile(file, prefix string, importPaths []string) error {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, file, nil, parser.ParseComments)
if err != nil {
return err
}
var changed bool
for _, s := range f.Imports {
name, err := strconv.Unquote(s.Path.Value)
if err != nil {
return err
}
q := qualify(unqualify(name), prefix, importPaths)
if q != name {
s.Path.Value = strconv.Quote(q)
changed = true
}
}
if !changed {
return nil
}
wpath := file + ".temp"
w, err := os.Create(wpath)
if err != nil {
return err
}
err = (&printer.Config{Mode: printer.TabIndent | printer.UseSpaces, Tabwidth: 8}).Fprint(w, fset, f)
if err != nil {
return err
}
err = w.Close()
if err != nil {
return err
}
return os.Rename(wpath, file)
}
const sep = "/vendor/_nuts/"
func unqualify(importPath string) string {
if i := strings.LastIndex(importPath, sep); i != -1 {
importPath = importPath[i+len(sep):]
}
return importPath
}
func qualify(importPath, pkg string, paths []string) string {
if containsPathPrefix(paths, importPath) {
return pkg + sep + importPath
}
return importPath
}
func containsPathPrefix(pats []string, s string) bool {
for _, pat := range pats {
if pat == s || strings.HasPrefix(s, pat+"/") {
return true
}
}
return false
}