forked from skelterjohn/gorf
/
scan.go
159 lines (130 loc) · 3.21 KB
/
scan.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
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
// Copyright 2011 John Asmuth. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"os"
"fmt"
//"go/token"
"code.google.com/p/rog-go/exp/go/token"
//"go/ast"
"code.google.com/p/rog-go/exp/go/ast"
"path/filepath"
"strings"
"code.google.com/p/rog-go/exp/go/types"
"code.google.com/p/rog-go/exp/go/parser"
//"go/parser"
)
var (
AllSourceTops = token.NewFileSet()
AllSources = token.NewFileSet()
ImportedBy = make(map[string][]string)
PackageTops = make(map[string]*ast.Package)
Packages = make(map[string]*ast.Package)
)
func LocalImporter(path string) (pkg *ast.Package) {
path = filepath.Clean(path)
//fmt.Printf("Importing %s\n", path)
var ok bool
var pkgtop *ast.Package
if pkgtop, ok = PackageTops[path]; !ok {
pkg = types.DefaultImporter(path)
return
}
if pkg, ok = Packages[path]; ok {
return
}
var sourcefiles []string
for srcfile := range pkgtop.Files {
sourcefiles = append(sourcefiles, srcfile)
}
//fmt.Printf("Parsing %v\n", sourcefiles)
dirpkgs, err := parser.ParseFiles(AllSources, sourcefiles, parser.ParseComments)
if err != nil {
fmt.Println(err)
return
}
pkg = dirpkgs[pkgtop.Name]
Packages[path] = pkg
//fmt.Printf("nil: %v name: %s\n", pkg == nil, pkgtop.Name)
return
}
func ScanAllForImports(dir string) (err error) {
sw := ScanWalker{}
filepath.Walk(dir, sw.Walk)
err = sw.err
return
}
func PreloadImportedBy(path string) {
for _, ipath := range ImportedBy[path] {
LocalImporter(ipath)
}
}
type ScanWalker struct {
err error
}
func (this *ScanWalker) Walk(path string, info os.FileInfo, err error) error {
if info.IsDir() {
this.VisitDir(path, &info)
} else {
this.VisitFile(path, &info)
}
return err
}
func (s *ScanWalker) VisitDir(path string, f *os.FileInfo) bool {
err := ScanForImports(path)
if err != nil {
s.err = err
}
return true
}
func (s *ScanWalker) VisitFile(fpath string, f *os.FileInfo) {
//if strings.HasSuffix(fpath, ".gorf") || strings.HasSuffix(fpath, ".gorfn") {
// os.Remove(fpath)
//}
}
//Look at the imports, and build up ImportedBy
func ScanForImports(path string) (err error) {
sourcefiles, _ := filepath.Glob(filepath.Join(path, "*.go"))
dirpkgs, err := parser.ParseFiles(AllSourceTops, sourcefiles, parser.ImportsOnly)
if err != nil {
fmt.Println(err)
}
//take the first non-main. otherwise, main is ok.
var prime *ast.Package
for name, pkg := range dirpkgs {
prime = pkg
if name != "main" {
break
}
}
if prime == nil {
return
}
PackageTops[path] = prime
is := make(ImportScanner)
ast.Walk(is, prime)
if v, ok := is["."]; !v && ok {
return MakeErr("gorf can not deal with unnamed import in '%s'", path)
}
for path, _ := range is {
if strings.HasPrefix(path, ".") {
return MakeErr("gorf can not deal with relative import in '%s'", path)
}
}
for imp := range is {
ImportedBy[imp] = append(ImportedBy[imp], path)
}
return
}
type ImportScanner map[string]bool
func (is ImportScanner) Visit(node ast.Node) ast.Visitor {
switch n := node.(type) {
case *ast.ImportSpec:
if n.Name != nil && n.Name.Name == "." {
is["."] = false
}
is[string(n.Path.Value)] = true
}
return is
}