/
main.go
185 lines (149 loc) · 5.42 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main
import (
"encoding/json"
"flag"
"github.com/go-openapi/spec"
"github.com/jackmanlabs/errors"
"go/build"
"log"
"os"
"runtime/pprof"
"strings"
)
var (
// Command-line parameters
pkgPath *string = flag.String("pkg", "", "The main package of your application.")
profilePath *string = flag.String("profile", "", "The path where you'd like to store profiling results.")
ignore *string = flag.String("ignore", "", "The comma seperated package paths that you want to ignore.")
naming *string = flag.String("naming", "full", "One of 'full', 'partial', or 'simple' to describe the amount of the package path on the resulting JSON models.")
)
var (
// Global variables
// Normally, I don't like global variables. The fact is, however, that if we
// were to pass these three things around, it would get very tedious very
// fast. This is not a multi-threaded program, and we've been careful to
// avoid modifying maps during iterations.
pkgInfos map[string]PackageInfo = make(map[string]PackageInfo)
srcPath string
ignoredPackages []string = make([]string, 0)
)
func main() {
flag.Parse()
if *profilePath != "" {
f, err := os.Create(*profilePath)
if err != nil {
log.Fatal(errors.Stack(err))
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
if *pkgPath == "" {
flag.Usage()
log.Fatal("Package path is required.")
}
if !(*naming == "full" || *naming == "partial" || *naming == "simple") {
flag.Usage()
log.Fatal("Unrecognized value provided for naming convention: " + *naming)
}
ignores := strings.Split(*ignore, ",")
for _, i := range ignores {
if i != "" {
ignoredPackages = append(ignoredPackages, i)
}
}
var err error
// Determine the source path of the package specified.
srcPath, err = getPackageSourceDir(*pkgPath)
if err != nil {
log.Fatal(errors.Stack(err))
}
// Which packages need to be analyzed? Get a list of all pkgInfos.
pkgInfos, err = getPackageInfoRecursive(*pkgPath)
if err != nil {
log.Fatal(errors.Stack(err))
}
// What comments need to be parsed?
// Find all comments that could conceivably have our tags in them.
packageCommentBlocks := make(map[string][]string, 0)
for importPath := range pkgInfos {
//log.Print("Scanning package for comments: ", importPath)
newBlocks, err := getCommentBlocks(importPath)
if err != nil {
log.Fatal(errors.Stack(err))
}
packageCommentBlocks[importPath] = newBlocks
}
// Now, let's check all of the comment blocks we found for tags, parsing them as necessary.
var (
apiCommentBlocks []string = make([]string, 0)
operationCommentBlocks map[string][]string = make(map[string][]string, 0)
tagCommentBlocks []string = make([]string, 0)
)
for importPath, commentBlocks := range packageCommentBlocks {
newApiCommentBlocks := detectApiCommentBlocks(commentBlocks)
//jlog.Log(newApiCommentBlocks)
apiCommentBlocks = append(apiCommentBlocks, newApiCommentBlocks...)
newOperationCommentBlocks := detectOperationComments(commentBlocks)
// We need to know the package so we know where to look for the types.
operationCommentBlocks[importPath] = newOperationCommentBlocks
newTagCommentBlocks := detectOperationComments(commentBlocks)
tagCommentBlocks = append(tagCommentBlocks, newTagCommentBlocks...)
}
// Let's turn our detected comments into our internal, intermediate types.
var (
apiIntermediate ApiIntermediate // There's only one.
operationIntermediates []OperationIntermediate = make([]OperationIntermediate, 0)
tagIntermediates []TagIntermediate = make([]TagIntermediate, 0)
)
// This function takes all API comment blocks, as they should all condense into a single API description.
apiIntermediate = intermediatateApi(apiCommentBlocks)
for importPath, commentBlocks := range operationCommentBlocks {
for _, commentBlock := range commentBlocks {
// This only scrapes the information found in the comment block.
// It doesn't do any further processing.
operationIntermediate := intermediatateOperation(commentBlock)
// We need this for later.
operationIntermediate.PackagePath = importPath
operationIntermediates = append(operationIntermediates, operationIntermediate)
}
}
for _, commentBlock := range tagCommentBlocks {
newTagIntermediates := intermediatateTags(commentBlock)
tagIntermediates = append(tagIntermediates, newTagIntermediates...)
}
// I really don't like the way this is done.
// TODO: Make this more functional.
defStore, err := deriveDefinitionsFromOperations(operationIntermediates)
if err != nil {
log.Fatal(errors.Stack(err))
}
// Transform the extractions above and combine them into a single Swagger Spec.
var swagger *spec.Swagger = swaggerizeApi(apiIntermediate)
swagger.Paths = swaggerizeOperations(operationIntermediates)
swagger.Tags = swaggerizeTags(tagIntermediates)
swagger.Definitions = swaggerizeDefinitions(defStore)
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", "\t")
err = enc.Encode(swagger)
if err != nil {
log.Fatal(errors.Stack(err))
}
}
func getPackageSourceDir(pkgPath string) (string, error) {
var (
bpkg *build.Package
err error
)
// I should hope there's an easier way of resolving the source path.
srcDirs := build.Default.SrcDirs()
for _, srcDir := range srcDirs {
bpkg, err = build.Import(pkgPath, srcDir, 0)
if err == nil {
break
}
}
if err != nil {
return "", errors.Stack(err)
}
return bpkg.Dir, nil
}