grafana/public/app/plugins/gen.go

64 lines
1.4 KiB
Go
Raw Normal View History

// go:build ignore
//go:build ignore
// +build ignore
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"cuelang.org/go/cue/cuecontext"
"github.com/grafana/grafana/pkg/codegen"
)
// Generate TypeScript for all plugin models.cue
func main() {
if len(os.Args) > 1 {
fmt.Fprintf(os.Stderr, "plugin thema code generator does not currently accept any arguments\n, got %q", os.Args)
os.Exit(1)
}
cwd, err := os.Getwd()
if err != nil {
fmt.Fprintf(os.Stderr, "could not get working directory: %s", err)
os.Exit(1)
}
var find func(path string) (string, error)
find = func(path string) (string, error) {
parent := filepath.Dir(path)
if parent == path {
return "", errors.New("grafana root directory could not be found")
}
fp := filepath.Join(path, "go.mod")
if _, err := os.Stat(fp); err == nil {
return path, nil
}
return find(parent)
}
groot, err := find(cwd)
if err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
wd, err := codegen.CuetsifyPlugins(cuecontext.New(), groot)
if _, set := os.LookupEnv("CODEGEN_VERIFY"); set {
err = wd.Verify()
if err != nil {
fmt.Fprintf(os.Stderr, "generated code is out of sync with inputs:\n%s\nrun `make gen-cue` to regenerate\n\n", err)
os.Exit(1)
}
} else {
err = wd.Write()
if err != nil {
fmt.Fprintf(os.Stderr, "error while writing generated code to disk:\n%s\n", err)
os.Exit(1)
}
}
}