-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcueimports.go
499 lines (431 loc) · 10.8 KB
/
cueimports.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
package cueimports
import (
"bytes"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/ast/astutil"
"cuelang.org/go/cue/format"
"cuelang.org/go/cue/parser"
)
// Import reads the given cue file and updates the import statements,
// adding missing ones and removing unused ones.
// If content is nil, the file is read from disk,
// otherwise the content is used without reading the file.
// It returns the update file content.
func Import(filename string, content []byte) ([]byte, error) {
if filename == "" && content == nil {
return nil, errors.New("filename or content must be provided")
}
if filename == "" {
filename = "_.cue"
}
var f *ast.File
var err error
opt := []parser.Option{
parser.ParseComments,
parser.AllowPartial,
}
// ParseFile is too strict and does not allow passing a nil byte slice
if content == nil {
f, err = parser.ParseFile(filename, nil, opt...)
} else {
f, err = parser.ParseFile(filename, content, opt...)
}
if err != nil {
return nil, fmt.Errorf("parse error: %w", err)
}
unresolved := make(map[string][]string, len(f.Unresolved))
// get a list of all unresolved identifiers
ast.Walk(f, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.SelectorExpr:
xIdent, ok := x.X.(*ast.Ident)
if !ok {
return true
}
xSel, ok := x.Sel.(*ast.Ident)
if !ok {
return true
}
for _, u := range f.Unresolved {
if u.Name == xIdent.Name {
unresolved[u.Name] = append(unresolved[u.Name], xSel.Name)
}
}
}
return true
}, nil)
// Load other files in the same package and filter out unresolved identifiers
// that are defined in those files.
err = filterSamePackageIdents(unresolved, filename, f.PackageName())
if err != nil {
return nil, err
}
if len(unresolved) == 0 {
// nothing to do
return insertImports(f, nil)
}
// resolve imports
resolved, err := resolve(unresolved, filename)
if err != nil {
return nil, err
}
// insert resolved imports
return insertImports(f, resolved)
}
func filterSamePackageIdents(unresolved map[string][]string, filename, packageName string) error {
dir, err := filepath.Abs(filepath.Dir(filename))
if err != nil {
return err
}
err = filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() && path != dir {
return filepath.SkipDir
}
if !strings.HasSuffix(path, ".cue") {
return nil
}
if d.Name() == filepath.Base(filename) {
return nil
}
content, err := os.ReadFile(path)
if err != nil {
return err
}
f, err := parser.ParseFile(path, content)
if err != nil {
return err
}
if f.PackageName() != packageName {
return nil
}
for _, decl := range f.Decls {
switch x := decl.(type) {
case *ast.Field:
ident, ok := x.Label.(*ast.Ident)
if !ok {
continue
}
if unresolved[ident.Name] != nil {
delete(unresolved, ident.Name)
}
}
}
return nil
})
return err
}
func resolve(unresolved map[string][]string, filename string) (map[string]string, error) {
resolved := make(map[string]string)
if len(unresolved) == 0 {
return resolved, nil
}
// resolve using local packages
err := resolveInLocalPackages(unresolved, resolved, filename)
if err != nil {
return nil, err
}
// resolve using the stdlib
resolveInStdlib(unresolved, resolved)
return resolved, nil
}
// list of std packages as of CUE v0.4.3
var stdPackages = map[string]string{
"crypto": "crypto",
"ed25519": "crypto/ed25519",
"hmac": "crypto/hmac",
"md5": "crypto/md5",
"sha1": "crypto/sha1",
"sha256": "crypto/sha256",
"sha512": "crypto/sha512",
"base64": "encoding/base64",
"encoding": "encoding",
"csv": "encoding/csv",
"hex": "encoding/hex",
"json": "encoding/json",
"yaml": "encoding/yaml",
"html": "html",
"list": "list",
"math": "math",
"bits": "math/bits",
"net": "net",
"path": "path",
"regexp": "regexp",
"strconv": "strconv",
"strings": "strings",
"struct": "struct",
"text": "text",
"tabwriter": "text/tabwriter",
"template": "text/template",
"time": "time",
"tool": "tool",
"cli": "tool/cli",
"exec": "tool/exec",
"file": "tool/file",
"http": "tool/http",
"os": "tool/os",
"uuid": "uuid",
}
func resolveInStdlib(unresolved map[string][]string, resolved map[string]string) {
for n := range unresolved {
if p, ok := stdPackages[n]; ok {
resolved[n] = p
delete(unresolved, n)
}
}
}
func resolveInLocalPackages(unresolved map[string][]string, resolved map[string]string, filename string) error {
dir := filepath.Dir(filename)
// find the cue.mod directory and module name
modDir, modName, err := findCueModDir(dir)
if err != nil {
return fmt.Errorf("find cue.mod: %w", err)
}
if modDir == "" {
// if no cue.mod is found, skip
return nil
}
errStop := errors.New("stop")
root := filepath.Dir(modDir)
// resolve from adjacent and sub packages
err = filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if !d.IsDir() {
return nil
}
if d.Name() == root {
return nil
}
if strings.HasPrefix(d.Name(), ".") {
return filepath.SkipDir
}
// skip nested cue.mod directories
if d.Name() == "cue.mod" && path != modDir {
return filepath.SkipDir
}
err = resolveInPackage(unresolved, resolved, path, root, modName)
if err != nil {
return err
}
if len(unresolved) == 0 {
return errStop
}
return nil
})
if err != nil && err != errStop {
return err
}
return nil
}
func resolveInPackage(unresolved map[string][]string, resolved map[string]string, dir, root, modName string) error {
err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if path == dir {
return nil
}
if d.IsDir() && d.Name() != dir {
return filepath.SkipDir
}
if !strings.HasSuffix(path, ".cue") {
return nil
}
content, err := os.ReadFile(path)
if err != nil {
return err
}
f, err := parser.ParseFile(path, content, parser.PackageClauseOnly)
if err != nil {
return err
}
pkgName := f.PackageName()
s, ok := unresolved[pkgName]
if !ok {
// if the package name is not in the unresolved list, we can skip the directory
return filepath.SkipDir
}
// this time parse the whole file
f, err = parser.ParseFile(path, content)
if err != nil {
return err
}
// look for one of the identifiers in the file
ast.Walk(f, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.Ident:
for _, name := range s {
if x.Name == name {
var rel string
rel, err = filepath.Rel(root, dir)
if err != nil {
return false
}
lastElem := filepath.Base(rel)
if strings.HasPrefix(rel, "cue.mod/") {
rel = strings.TrimPrefix(rel, "cue.mod/pkg/")
rel = strings.TrimPrefix(rel, "cue.mod/usr/")
resolved[pkgName] = rel
} else if lastElem != pkgName {
resolved[pkgName] = filepath.Join(modName, rel) + ":" + pkgName
} else {
resolved[pkgName] = filepath.Join(modName, rel)
}
delete(unresolved, pkgName)
}
}
}
return true
}, nil)
return err
})
return err
}
// findCueModDir returns the path of the cue.mod directory.
// find the cue.mod directory
// it can be either in the current directory or in a parent directory
func findCueModDir(from string) (string, string, error) {
parent, err := filepath.Abs(from)
if err != nil {
return "", "", err
}
LOOP:
for {
if _, err := os.Stat(filepath.Join(parent, "cue.mod")); err == nil {
break LOOP
}
if parent == "/" {
return "", "", nil
}
parent = filepath.Dir(parent)
}
// get module name
modDir := filepath.Join(parent, "cue.mod")
content, err := os.ReadFile(filepath.Join(modDir, "module.cue"))
if err != nil {
return "", "", fmt.Errorf("read module.cue: %w", err)
}
moduleName := string(bytes.TrimSuffix(bytes.TrimPrefix(content, []byte("module: ")), []byte("\n")))
module, err := strconv.Unquote(moduleName)
if err != nil {
return "", "", fmt.Errorf("unquote module name %s: %w", moduleName, err)
}
return modDir, module, nil
}
func insertImports(f *ast.File, resolved map[string]string) ([]byte, error) {
if resolved == nil {
resolved = map[string]string{}
}
var modAst ast.Node
var err error
if len(f.Imports) != 0 {
// filter out unused imports
ast.Walk(f, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.SelectorExpr:
xx, ok := x.X.(*ast.Ident)
if !ok {
return true
}
for _, i := range f.Imports {
var p string
p, err = strconv.Unquote(i.Path.Value)
if err != nil {
return false
}
lastElem := filepath.Base(p)
// handle package names with a different name
// i.e. "test.com/dimensions:alt
if idx := strings.Index(lastElem, ":"); idx != -1 {
lastElem = lastElem[idx+1:]
}
if xx.Name == lastElem {
resolved[xx.Name] = p
}
}
}
return true
}, nil)
if err != nil {
return nil, err
}
}
// remove all import statements
modAst = astutil.Apply(f, func(c astutil.Cursor) bool {
switch c.Node().(type) {
case *ast.ImportDecl:
c.Delete()
return false
}
return true
}, nil)
if len(resolved) == 0 {
return format.Node(modAst)
}
// sort the imports by group
// 1. standard library
// 2. other packages
// TODO separate local packages from cue.mod/ packages
var std []string
for _, p := range resolved {
// ensure the package actually belongs to the stdlib
// and not simply ends with the same name
if fullName, ok := stdPackages[filepath.Base(p)]; ok && fullName == p {
std = append(std, p)
}
}
sort.Strings(std)
var local []string
for _, p := range resolved {
if fullName, ok := stdPackages[filepath.Base(p)]; !ok || fullName != p {
local = append(local, p)
}
}
sort.Strings(local)
var idecl ast.ImportDecl
for _, r := range std {
idecl.Specs = append(idecl.Specs, ast.NewImport(nil, r))
}
// add a newline between the stdlib and local imports
if len(idecl.Specs) > 0 && len(local) > 0 {
idecl.Specs = append(idecl.Specs, &ast.ImportSpec{Path: &ast.BasicLit{}})
}
for _, r := range local {
idecl.Specs = append(idecl.Specs, ast.NewImport(nil, r))
}
var inserted bool
// add single import statements with all resolved imports
modAst = astutil.Apply(modAst, func(c astutil.Cursor) bool {
switch c.Node().(type) {
case *ast.File:
return true
case *ast.Package:
if inserted {
return false
}
inserted = true
c.InsertAfter(&idecl)
return false
default:
if inserted {
return false
}
inserted = true
c.InsertBefore(&idecl)
return false
}
}, nil)
return format.Node(modAst)
}