|
| 1 | +// Copyright 2019 CUE Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package main |
| 16 | + |
| 17 | +import ( |
| 18 | + "io/ioutil" |
| 19 | + "log" |
| 20 | + "os" |
| 21 | + "path/filepath" |
| 22 | + |
| 23 | + "cuelang.org/go/cue" |
| 24 | + "cuelang.org/go/cue/load" |
| 25 | + "cuelang.org/go/encoding/gocode" |
| 26 | + _ "cuelang.org/go/pkg" |
| 27 | +) |
| 28 | + |
| 29 | +func main() { |
| 30 | + dirs, err := ioutil.ReadDir("testdata") |
| 31 | + if err != nil { |
| 32 | + log.Fatal(err) |
| 33 | + } |
| 34 | + |
| 35 | + cwd, err := os.Getwd() |
| 36 | + if err != nil { |
| 37 | + log.Fatal(err) |
| 38 | + } |
| 39 | + |
| 40 | + for _, d := range dirs { |
| 41 | + if !d.IsDir() { |
| 42 | + continue |
| 43 | + } |
| 44 | + dir := filepath.Join(cwd, "testdata") |
| 45 | + pkg := "." + string(filepath.Separator) + d.Name() |
| 46 | + inst := cue.Build(load.Instances([]string{pkg}, &load.Config{ |
| 47 | + Dir: dir, |
| 48 | + ModuleRoot: dir, |
| 49 | + Module: "cuelang.org/go/encoding/gocode/testdata", |
| 50 | + }))[0] |
| 51 | + if err := inst.Err; err != nil { |
| 52 | + log.Fatal(err) |
| 53 | + } |
| 54 | + |
| 55 | + goPkg := "./testdata/" + d.Name() |
| 56 | + b, err := gocode.Generate(goPkg, inst, nil) |
| 57 | + if err != nil { |
| 58 | + log.Fatal(err) |
| 59 | + } |
| 60 | + |
| 61 | + goFile := filepath.Join("testdata", d.Name(), "cue_gen.go") |
| 62 | + if err := os.WriteFile(goFile, b, 0644); err != nil { |
| 63 | + log.Fatal(err) |
| 64 | + } |
| 65 | + } |
| 66 | +} |
0 commit comments