/
import.go
81 lines (73 loc) · 2.06 KB
/
import.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
// chris 071615 Routines dealing with import management.
package main
import (
"fmt"
"go/ast"
"go/token"
)
// findImports returns the imports mentioned in the given *ast.File as
// simple strings. Basically, it pulls out the bit in quotes.
func findImports(astfile *ast.File) []string {
r := make([]string, 0, len(astfile.Imports))
for _, is := range astfile.Imports {
if is.Path.Value == "" {
continue
}
// Cut off leading and trailing double quote characters.
v := is.Path.Value[1 : len(is.Path.Value)-1]
r = append(r, v)
}
return r
}
// hasImport determines if the given *ast.File already has an import of
// the given name.
func hasImport(astfile *ast.File, name string) bool {
for _, existingImport := range findImports(astfile) {
if existingImport == name {
return true
}
}
return false
}
// prependImport prepends,in place, an import of the given name to the
// list of imports in the *ast.File.
func prependImport(astfile *ast.File, name string) {
litvalue := fmt.Sprintf("\"%s\"", name)
// XXX The newline here at the beginning of the comment is a bit of
// a hack. We'd like to find a way to include this comment
// above or to the right of the added import in the printed
// code. However, the spacing doesn't come out correctly if
// just inserted into the AST as you'd think. So in order for
// the generated code to be idempotent under gofmt, we have to
// insert a newline here at the beginning.
commentText := fmt.Sprintf("\n// *** IMPORT ADDED BY %s ***", args.progUpper)
comment := &ast.CommentGroup{
List: []*ast.Comment{
&ast.Comment{
Slash: token.NoPos,
Text: commentText,
},
},
}
decl := &ast.GenDecl{
Doc: comment,
TokPos: token.NoPos,
Tok: token.IMPORT,
Lparen: token.NoPos,
Specs: []ast.Spec{
&ast.ImportSpec{
Doc: nil,
Name: nil,
Path: &ast.BasicLit{
ValuePos: token.NoPos,
Kind: token.STRING,
Value: litvalue,
},
Comment: nil,
EndPos: token.NoPos,
},
},
Rparen: token.NoPos,
}
astfile.Decls = append([]ast.Decl{decl}, astfile.Decls...)
}