-
Notifications
You must be signed in to change notification settings - Fork 3
/
gensyntax.go
116 lines (101 loc) · 2.44 KB
/
gensyntax.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
//go:generate go run gensyntax.go
// Command gensyntax generates a vim syntax for Ginkgo and related packages by
// parsing and searching their exported matchers and assertions.
package main
import (
"flag"
"fmt"
"go/importer"
"go/types"
"html/template"
"log"
"os"
"sort"
"time"
)
const warning = `" This file was generated by gensyntax.go on %s
" DO NOT modify this file by hand!
" Instead, edit ginkgo.vim.tpl and/or run: go generate`
var (
packages = []string{
"github.com/onsi/gomega",
"github.com/onsi/gomega/gbytes",
"github.com/onsi/gomega/gexec",
"github.com/onsi/gomega/ghttp",
"github.com/onsi/gomega/gstruct",
"github.com/sclevine/agouti/matchers",
}
srcImporter = importer.For("source", nil)
)
func init() {
flag.Parse()
}
func main() {
tpl, err := template.ParseFiles("syntax/ginkgo.vim.tpl")
if err != nil {
log.Fatal(err)
}
keywords := &Keywords{
Assertions: make([]string, 0),
Matchers: make([]string, 0),
}
if err := keywords.Search(packages); err != nil {
log.Fatal(err)
}
log.Printf("Found %d assertions and %d matchers",
len(keywords.Assertions), len(keywords.Matchers))
f, err := os.OpenFile("syntax/ginkgo.vim",
os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
log.Fatal(err)
}
defer f.Close()
_, err = f.Write([]byte(fmt.Sprintf(warning,
time.Now().UTC().Format(time.RFC1123Z))))
if err != nil {
log.Fatal(err)
}
tpl.Execute(f, keywords)
}
type Keywords struct {
Assertions []string
Matchers []string
}
func (k *Keywords) Search(pkgs []string) (first error) {
for _, path := range pkgs {
log.Printf("Searching %s...", path)
pkg, err := srcImporter.Import(path)
if err != nil {
first = err
return
}
for _, fn := range filterExports(pkg) {
sig := fn.Type().(*types.Signature)
res := sig.Results()
if res != nil && res.Len() == 1 {
if named, ok := res.At(0).Type().(*types.Named); ok {
switch named.Obj().Name() {
case "GomegaAssertion", "GomegaAsyncAssertion":
k.Assertions = append(k.Assertions, fn.Name())
case "GomegaMatcher":
k.Matchers = append(k.Matchers, fn.Name())
}
}
}
}
}
sort.Strings(k.Assertions)
sort.Strings(k.Matchers)
return
}
func filterExports(pkg *types.Package) (funcs []*types.Func) {
for _, name := range pkg.Scope().Names() {
obj := pkg.Scope().Lookup(name)
if obj != nil && obj.Exported() {
if fn, ok := obj.(*types.Func); ok {
funcs = append(funcs, fn)
}
}
}
return
}