|
| 1 | +package modimports |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "io/fs" |
| 6 | + "path" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "cuelang.org/go/cue/ast" |
| 10 | + "cuelang.org/go/cue/parser" |
| 11 | + "cuelang.org/go/internal/cueimports" |
| 12 | +) |
| 13 | + |
| 14 | +type ModuleFile struct { |
| 15 | + // FilePath holds the path of the module file |
| 16 | + // relative to the root of the fs. This will be |
| 17 | + // valid even if there's an associated error. |
| 18 | + // |
| 19 | + // If there's an error, it might not a be CUE file. |
| 20 | + FilePath string |
| 21 | + |
| 22 | + // Syntax includes only the portion of the file up to and including |
| 23 | + // the imports. It will be nil if there was an error reading the file. |
| 24 | + Syntax *ast.File |
| 25 | +} |
| 26 | + |
| 27 | +// AllModuleFiles returns an iterator that produces all the CUE files inside the |
| 28 | +// module at the given root. |
| 29 | +func AllModuleFiles(fsys fs.FS, root string) func(func(ModuleFile, error) bool) { |
| 30 | + return func(yield func(ModuleFile, error) bool) { |
| 31 | + fs.WalkDir(fsys, root, func(fpath string, d fs.DirEntry, err error) (_err error) { |
| 32 | + if err != nil { |
| 33 | + if !yield(ModuleFile{ |
| 34 | + FilePath: fpath, |
| 35 | + }, err) { |
| 36 | + return fs.SkipAll |
| 37 | + } |
| 38 | + return nil |
| 39 | + } |
| 40 | + if path.Base(fpath) == "cue.mod" { |
| 41 | + return fs.SkipDir |
| 42 | + } |
| 43 | + if d.IsDir() { |
| 44 | + if fpath == root { |
| 45 | + return nil |
| 46 | + } |
| 47 | + base := path.Base(fpath) |
| 48 | + if strings.HasPrefix(base, ".") || strings.HasPrefix(base, "_") { |
| 49 | + return fs.SkipDir |
| 50 | + } |
| 51 | + _, err := fs.Stat(fsys, path.Join(fpath, "cue.mod")) |
| 52 | + if err == nil { |
| 53 | + // TODO is it enough to have a cue.mod directory |
| 54 | + // or should we look for cue.mod/module.cue too? |
| 55 | + return fs.SkipDir |
| 56 | + } |
| 57 | + if !errors.Is(err, fs.ErrNotExist) { |
| 58 | + // We haven't got a package file to produce with the |
| 59 | + // error here. Should we just ignore the error or produce |
| 60 | + // a ModuleFile with an empty path? |
| 61 | + yield(ModuleFile{}, err) |
| 62 | + return fs.SkipAll |
| 63 | + } |
| 64 | + return nil |
| 65 | + } |
| 66 | + if !strings.HasSuffix(fpath, ".cue") { |
| 67 | + return nil |
| 68 | + } |
| 69 | + if !yieldPackageFile(fsys, fpath, yield) { |
| 70 | + return fs.SkipAll |
| 71 | + } |
| 72 | + return nil |
| 73 | + }) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func yieldPackageFile(fsys fs.FS, path string, yield func(ModuleFile, error) bool) bool { |
| 78 | + pf := ModuleFile{ |
| 79 | + FilePath: path, |
| 80 | + } |
| 81 | + f, err := fsys.Open(path) |
| 82 | + if err != nil { |
| 83 | + return yield(pf, err) |
| 84 | + } |
| 85 | + defer f.Close() |
| 86 | + data, err := cueimports.Read(f) |
| 87 | + if err != nil { |
| 88 | + return yield(pf, err) |
| 89 | + } |
| 90 | + syntax, err := parser.ParseFile(path, data, parser.ParseComments) |
| 91 | + if err != nil { |
| 92 | + return yield(pf, err) |
| 93 | + } |
| 94 | + pf.Syntax = syntax |
| 95 | + return yield(pf, nil) |
| 96 | +} |
0 commit comments