-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
97 lines (79 loc) · 1.9 KB
/
main.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
package main
import (
"fmt"
"os"
"path"
"path/filepath"
"github.com/MarcGrol/golangAnnotations/model"
"github.com/MarcGrol/golangAnnotations/parser"
"github.com/tartale/go/pkg/errors"
"github.com/tartale/go-kitt/generators"
"github.com/tartale/go-kitt/generators/gokit"
)
func main() {
var inputPath string
if len(os.Args) == 1 {
cwd, err := os.Getwd()
if err != nil {
panic(err)
}
inputPath = cwd
} else if len(os.Args) == 2 {
inputPath = os.Args[1]
} else {
panic(fmt.Sprintf("usage: %s [<path>]", os.Args[0]))
}
parsedSourceMap := parseAll(inputPath)
parsedSources := generators.ParsedSourceData{
Map: parsedSourceMap,
Keys: parsedSourceMap.Keys(),
}
var errs errors.Errors
err := gokit.Generate(parsedSources)
if err != nil {
errs = append(errs, err)
}
if len(errs) > 0 {
panic(errs)
}
}
func parseAll(inputPath string) generators.ParsedSourceMap {
const (
excludeMatchPattern = "^.*" + generators.GeneratedPathNewExtension + "$" + "|" +
"^.*" + generators.GeneratedPathExtension + "$"
includeMatchPattern = "^.*.go$"
)
result := make(generators.ParsedSourceMap)
info, err := os.Stat(inputPath)
if err != nil {
panic(err)
}
if !info.IsDir() {
inputPath = path.Dir(inputPath)
}
err = filepath.Walk(inputPath, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
return nil
}
parsedSources, err := parser.New().ParseSourceDir(path, includeMatchPattern, excludeMatchPattern)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return nil
}
if !isEmpty(parsedSources) {
result[path] = parsedSources
}
return nil
})
if err != nil {
panic(err)
}
return result
}
func isEmpty(parsedSources model.ParsedSources) bool {
return len(parsedSources.Operations) == 0 &&
len(parsedSources.Interfaces) == 0 &&
len(parsedSources.Typedefs) == 0 &&
len(parsedSources.Structs) == 0 &&
len(parsedSources.Enums) == 0
}