Skip to content

Commit

Permalink
encoding/gocode: simplify testdata/gen.go
Browse files Browse the repository at this point in the history
We can use cue/load directly to load all CUE packages in one go.
This saves us having to manually read the directory and loop.
Moreover, we can use a simpler cue/load.Config as well.

Signed-off-by: Daniel Martí <[email protected]>
Change-Id: I2290bfa4f6e4352e54df8dc5dad0c45ef01f5f19
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1201335
TryBot-Result: CUEcueckoo <[email protected]>
Reviewed-by: Roger Peppe <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
  • Loading branch information
mvdan committed Sep 17, 2024
1 parent 445082e commit 80a862c
Showing 1 changed file with 8 additions and 25 deletions.
33 changes: 8 additions & 25 deletions encoding/gocode/testdata/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,45 +19,28 @@ import (
"os"
"path/filepath"

"cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext"
"cuelang.org/go/cue/load"
"cuelang.org/go/encoding/gocode"
_ "cuelang.org/go/pkg"
)

func main() {
dirs, err := os.ReadDir("testdata")
if err != nil {
log.Fatal(err)
}

cwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}

for _, d := range dirs {
if !d.IsDir() || d.Name() == "cue.mod" {
continue
}
dir := filepath.Join(cwd, "testdata")
pkg := "." + string(filepath.Separator) + d.Name()
inst := cue.Build(load.Instances([]string{pkg}, &load.Config{
Dir: dir,
ModuleRoot: dir,
Module: "cuelang.org/go/encoding/gocode/testdata@v0",
}))[0]
insts := load.Instances([]string{"./..."}, &load.Config{
Dir: "testdata",
})
ctx := cuecontext.New()
for _, inst := range insts {
if err := inst.Err; err != nil {
log.Fatal(err)
}

goPkg := "./testdata/" + d.Name()
b, err := gocode.Generate(goPkg, inst, nil)
b, err := gocode.Generate(inst.Dir, ctx.BuildInstance(inst), nil)
if err != nil {
log.Fatal(err)
}

goFile := filepath.Join("testdata", d.Name(), "cue_gen.go")
goFile := filepath.Join(inst.Dir, "cue_gen.go")
if err := os.WriteFile(goFile, b, 0666); err != nil {
log.Fatal(err)
}
Expand Down

0 comments on commit 80a862c

Please sign in to comment.