...

Source file src/cuelang.org/go/encoding/gocode/testdata/gen.go

Documentation: cuelang.org/go/encoding/gocode/testdata

     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  	"log"
    19  	"os"
    20  	"path/filepath"
    21  
    22  	"cuelang.org/go/cue"
    23  	"cuelang.org/go/cue/load"
    24  	"cuelang.org/go/encoding/gocode"
    25  	_ "cuelang.org/go/pkg"
    26  )
    27  
    28  func main() {
    29  	dirs, err := os.ReadDir("testdata")
    30  	if err != nil {
    31  		log.Fatal(err)
    32  	}
    33  
    34  	cwd, err := os.Getwd()
    35  	if err != nil {
    36  		log.Fatal(err)
    37  	}
    38  
    39  	for _, d := range dirs {
    40  		if !d.IsDir() {
    41  			continue
    42  		}
    43  		dir := filepath.Join(cwd, "testdata")
    44  		pkg := "." + string(filepath.Separator) + d.Name()
    45  		inst := cue.Build(load.Instances([]string{pkg}, &load.Config{
    46  			Dir:        dir,
    47  			ModuleRoot: dir,
    48  			Module:     "cuelang.org/go/encoding/gocode/testdata",
    49  		}))[0]
    50  		if err := inst.Err; err != nil {
    51  			log.Fatal(err)
    52  		}
    53  
    54  		goPkg := "./testdata/" + d.Name()
    55  		b, err := gocode.Generate(goPkg, inst, nil)
    56  		if err != nil {
    57  			log.Fatal(err)
    58  		}
    59  
    60  		goFile := filepath.Join("testdata", d.Name(), "cue_gen.go")
    61  		if err := os.WriteFile(goFile, b, 0644); err != nil {
    62  			log.Fatal(err)
    63  		}
    64  	}
    65  }
    66  

View as plain text