|
| 1 | +//go:generate go run gensyntax.go |
| 2 | + |
| 3 | +// Command gensyntax generates a vim syntax for Ginkgo and related packages by |
| 4 | +// parsing and searching their exported matchers and assertions. |
| 5 | +package main |
| 6 | + |
| 7 | +import ( |
| 8 | + "flag" |
| 9 | + "fmt" |
| 10 | + "go/importer" |
| 11 | + "go/types" |
| 12 | + "html/template" |
| 13 | + "log" |
| 14 | + "os" |
| 15 | + "sort" |
| 16 | + "time" |
| 17 | +) |
| 18 | + |
| 19 | +const warning = `" This file was generated by gensyntax.go on %s |
| 20 | +" DO NOT modify this file by hand! |
| 21 | +" Instead, edit ginkgo.vim.tpl and/or run: go generate` |
| 22 | + |
| 23 | +var ( |
| 24 | + packages = []string{ |
| 25 | + "github.com/onsi/gomega", |
| 26 | + "github.com/onsi/gomega/gbytes", |
| 27 | + "github.com/onsi/gomega/gexec", |
| 28 | + "github.com/onsi/gomega/ghttp", |
| 29 | + "github.com/onsi/gomega/gstruct", |
| 30 | + "github.com/sclevine/agouti/matchers", |
| 31 | + } |
| 32 | + |
| 33 | + srcImporter = importer.For("source", nil) |
| 34 | +) |
| 35 | + |
| 36 | +func init() { |
| 37 | + flag.Parse() |
| 38 | +} |
| 39 | + |
| 40 | +func main() { |
| 41 | + tpl, err := template.ParseFiles("syntax/ginkgo.vim.tpl") |
| 42 | + if err != nil { |
| 43 | + log.Fatal(err) |
| 44 | + } |
| 45 | + |
| 46 | + keywords := &Keywords{ |
| 47 | + Assertions: make([]string, 0), |
| 48 | + Matchers: make([]string, 0), |
| 49 | + } |
| 50 | + if err := keywords.Search(packages); err != nil { |
| 51 | + log.Fatal(err) |
| 52 | + } |
| 53 | + log.Printf("Found %d assertions and %d matchers", |
| 54 | + len(keywords.Assertions), len(keywords.Matchers)) |
| 55 | + |
| 56 | + f, err := os.OpenFile("syntax/ginkgo.vim", |
| 57 | + os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) |
| 58 | + if err != nil { |
| 59 | + log.Fatal(err) |
| 60 | + } |
| 61 | + defer f.Close() |
| 62 | + |
| 63 | + _, err = f.Write([]byte(fmt.Sprintf(warning, |
| 64 | + time.Now().UTC().Format(time.RFC1123Z)))) |
| 65 | + if err != nil { |
| 66 | + log.Fatal(err) |
| 67 | + } |
| 68 | + |
| 69 | + tpl.Execute(f, keywords) |
| 70 | +} |
| 71 | + |
| 72 | +type Keywords struct { |
| 73 | + Assertions []string |
| 74 | + Matchers []string |
| 75 | +} |
| 76 | + |
| 77 | +func (k *Keywords) Search(pkgs []string) (first error) { |
| 78 | + for _, path := range pkgs { |
| 79 | + log.Printf("Searching %s...", path) |
| 80 | + pkg, err := srcImporter.Import(path) |
| 81 | + if err != nil { |
| 82 | + first = err |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + for _, fn := range filterExports(pkg) { |
| 87 | + sig := fn.Type().(*types.Signature) |
| 88 | + res := sig.Results() |
| 89 | + if res != nil && res.Len() == 1 { |
| 90 | + if named, ok := res.At(0).Type().(*types.Named); ok { |
| 91 | + switch named.Obj().Name() { |
| 92 | + case "GomegaAssertion", "GomegaAsyncAssertion": |
| 93 | + k.Assertions = append(k.Assertions, fn.Name()) |
| 94 | + case "GomegaMatcher": |
| 95 | + k.Matchers = append(k.Matchers, fn.Name()) |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + sort.Strings(k.Assertions) |
| 102 | + sort.Strings(k.Matchers) |
| 103 | + return |
| 104 | +} |
| 105 | + |
| 106 | +func filterExports(pkg *types.Package) (funcs []*types.Func) { |
| 107 | + for _, name := range pkg.Scope().Names() { |
| 108 | + obj := pkg.Scope().Lookup(name) |
| 109 | + if obj != nil && obj.Exported() { |
| 110 | + if fn, ok := obj.(*types.Func); ok { |
| 111 | + funcs = append(funcs, fn) |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + return |
| 116 | +} |
0 commit comments