@@ -21,8 +21,9 @@ import (
21
21
"unicode"
22
22
23
23
"github.com/russross/blackfriday/v2"
24
- "k8s.io/gengo/parser"
25
- "k8s.io/gengo/types"
24
+ "k8s.io/gengo/v2"
25
+ "k8s.io/gengo/v2/parser"
26
+ "k8s.io/gengo/v2/types"
26
27
"k8s.io/klog/v2"
27
28
)
28
29
@@ -189,7 +190,7 @@ func main() {
189
190
// groupName extracts the "//+groupName" meta-comment from the specified
190
191
// package's comments, or returns empty string if it cannot be found.
191
192
func groupName (pkg * types.Package ) string {
192
- m := types .ExtractCommentTags ("+" , pkg .Comments )
193
+ m := gengo .ExtractCommentTags ("+" , pkg .Comments )
193
194
v := m ["groupName" ]
194
195
if len (v ) == 1 {
195
196
return v [0 ]
@@ -198,39 +199,43 @@ func groupName(pkg *types.Package) string {
198
199
}
199
200
200
201
func parseAPIPackages (dir string ) ([]* types.Package , error ) {
201
- b := parser .New ()
202
- // the following will silently fail (turn on -v=4 to see logs)
203
- if err := b .AddDirRecursive (* flAPIDir ); err != nil {
204
- return nil , err
202
+ p := parser .New ()
203
+
204
+ pkgsFound , errFind := p .FindPackages (dir + "/..." )
205
+ if errFind != nil {
206
+ return nil , fmt .Errorf ("failed to find packages in %s: %w" , dir , errFind )
207
+ }
208
+ klog .Infof ("found %d packages" , len (pkgsFound ))
209
+
210
+ errLoad := p .LoadPackages (pkgsFound ... )
211
+ if errLoad != nil {
212
+ return nil , fmt .Errorf ("failed to load packages: %w" , errLoad )
205
213
}
206
- scan , err := b .FindTypes ()
214
+
215
+ scan , err := p .NewUniverse ()
207
216
if err != nil {
208
217
return nil , fmt .Errorf ("failed to parse pkgs and types: %w" , err )
209
218
}
210
- var pkgNames []string
211
- for p := range scan {
212
- pkg := scan [p ]
219
+
220
+ var pkgs []* types.Package
221
+ for _ , pkg := range scan {
222
+
213
223
klog .V (3 ).Infof ("trying package=%v groupName=%s" , p , groupName (pkg ))
214
224
215
225
// Do not pick up packages that are in vendor/ as API packages. (This
216
226
// happened in knative/eventing-sources/vendor/..., where a package
217
227
// matched the pattern, but it didn't have a compatible import path).
218
228
if isVendorPackage (pkg ) {
219
- klog .V (3 ).Infof ("package=%v coming from vendor/, ignoring." , p )
229
+ klog .V (3 ).Infof ("package=%v coming from vendor/, ignoring." , pkg . Name )
220
230
continue
221
231
}
222
232
223
233
if groupName (pkg ) != "" && len (pkg .Types ) > 0 || containsString (pkg .DocComments , docCommentForceIncludes ) {
224
- klog .V (3 ).Infof ("package=%v has groupName and has types" , p )
225
- pkgNames = append (pkgNames , p )
234
+ klog .V (3 ).Infof ("package=%v has groupName and has types" , pkg .Name )
235
+ klog .Info ("using package=" , pkg .Name )
236
+ pkgs = append (pkgs , pkg )
226
237
}
227
238
}
228
- sort .Strings (pkgNames )
229
- var pkgs []* types.Package
230
- for _ , p := range pkgNames {
231
- klog .Infof ("using package=%s" , p )
232
- pkgs = append (pkgs , scan [p ])
233
- }
234
239
return pkgs , nil
235
240
}
236
241
@@ -300,7 +305,7 @@ func combineAPIPackages(pkgs []*types.Package) ([]*apiPackage, error) {
300
305
// isVendorPackage determines if package is coming from vendor/ dir.
301
306
func isVendorPackage (pkg * types.Package ) bool {
302
307
vendorPattern := string (os .PathSeparator ) + "vendor" + string (os .PathSeparator )
303
- return strings .Contains (pkg .SourcePath , vendorPattern )
308
+ return strings .Contains (pkg .Dir , vendorPattern )
304
309
}
305
310
306
311
func findTypeReferences (pkgs []* apiPackage ) map [* types.Type ][]* types.Type {
@@ -589,7 +594,7 @@ func filterCommentTags(comments []string) []string {
589
594
}
590
595
591
596
func isOptionalMember (m types.Member ) bool {
592
- tags := types .ExtractCommentTags ("+" , m .CommentLines )
597
+ tags := gengo .ExtractCommentTags ("+" , m .CommentLines )
593
598
_ , ok := tags ["optional" ]
594
599
return ok
595
600
}
0 commit comments