forked from karan/unit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeps.go
More file actions
79 lines (61 loc) · 1.34 KB
/
Copy pathdeps.go
File metadata and controls
79 lines (61 loc) · 1.34 KB
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
package main
import (
"fmt"
"io/ioutil"
"go/parser"
"go/token"
"os"
"strings"
)
var UNITS_DIR = "units/"
var QUALIFIER_DOC = "- unit-dep"
var MARTINI_PATH = "github.com/go-martini/martini"
type DepSet struct {
set map[string]bool
}
func NewDepSet() *DepSet {
return &DepSet{make(map[string]bool)}
}
func (set *DepSet) Add(s string) bool {
_, found := set.set[s]
set.set[s] = true
return !found
}
func (set *DepSet) Len() int {
return len(set.set)
}
func (set *DepSet) Keys() []string {
keys := make([]string, 0, set.Len())
for k := range set.set {
keys = append(keys, k)
}
return keys
}
func getUnitsFiles() []os.FileInfo {
files, _ := ioutil.ReadDir(UNITS_DIR)
return files
}
func GetDeps() []string {
units_files := getUnitsFiles()
set := NewDepSet()
for _, unit := range units_files {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, UNITS_DIR + unit.Name(), nil, parser.ParseComments)
if err != nil {
fmt.Println(err)
return nil
}
for _, s := range f.Imports {
comment := strings.Trim(s.Comment.Text(), "\n")
path := strings.Trim(s.Path.Value, "\"")
if comment == QUALIFIER_DOC && path != MARTINI_PATH {
set.Add(path)
}
}
}
return set.Keys()
}
func main() {
deps := GetDeps()
fmt.Println(strings.Join(deps, " "))
}