waveterm/cmd/generatego/main-generatego.go

95 lines
3.1 KiB
Go
Raw Normal View History

2024-08-28 03:49:49 +02:00
// Copyright 2024, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
package main
import (
"fmt"
"os"
"reflect"
"strings"
2024-09-05 23:25:45 +02:00
"github.com/wavetermdev/waveterm/pkg/gogen"
"github.com/wavetermdev/waveterm/pkg/util/utilfn"
"github.com/wavetermdev/waveterm/pkg/waveobj"
"github.com/wavetermdev/waveterm/pkg/wconfig"
"github.com/wavetermdev/waveterm/pkg/wshrpc"
2024-08-28 03:49:49 +02:00
)
const WshClientFileName = "pkg/wshrpc/wshclient/wshclient.go"
const WaveObjMetaConstsFileName = "pkg/waveobj/metaconsts.go"
const SettingsMetaConstsFileName = "pkg/wconfig/metaconsts.go"
func GenerateWshClient() error {
2024-08-28 03:49:49 +02:00
fmt.Fprintf(os.Stderr, "generating wshclient file to %s\n", WshClientFileName)
var buf strings.Builder
gogen.GenerateBoilerplate(&buf, "wshclient", []string{
2024-09-05 23:25:45 +02:00
"github.com/wavetermdev/waveterm/pkg/wshutil",
"github.com/wavetermdev/waveterm/pkg/wshrpc",
"github.com/wavetermdev/waveterm/pkg/waveobj",
"github.com/wavetermdev/waveterm/pkg/wps",
2024-10-17 23:50:36 +02:00
"github.com/wavetermdev/waveterm/pkg/vdom",
2024-08-28 03:49:49 +02:00
})
wshDeclMap := wshrpc.GenerateWshCommandDeclMap()
for _, key := range utilfn.GetOrderedMapKeys(wshDeclMap) {
methodDecl := wshDeclMap[key]
if methodDecl.CommandType == wshrpc.RpcType_ResponseStream {
gogen.GenMethod_ResponseStream(&buf, methodDecl)
} else if methodDecl.CommandType == wshrpc.RpcType_Call {
gogen.GenMethod_Call(&buf, methodDecl)
} else {
panic("unsupported command type " + methodDecl.CommandType)
}
}
buf.WriteString("\n")
written, err := utilfn.WriteFileIfDifferent(WshClientFileName, []byte(buf.String()))
if !written {
fmt.Fprintf(os.Stderr, "no changes to %s\n", WshClientFileName)
2024-08-28 03:49:49 +02:00
}
return err
2024-08-28 03:49:49 +02:00
}
func GenerateWaveObjMetaConsts() error {
2024-08-28 03:49:49 +02:00
fmt.Fprintf(os.Stderr, "generating waveobj meta consts file to %s\n", WaveObjMetaConstsFileName)
var buf strings.Builder
gogen.GenerateBoilerplate(&buf, "waveobj", []string{})
gogen.GenerateMetaMapConsts(&buf, "MetaKey_", reflect.TypeOf(waveobj.MetaTSType{}))
buf.WriteString("\n")
written, err := utilfn.WriteFileIfDifferent(WaveObjMetaConstsFileName, []byte(buf.String()))
if !written {
fmt.Fprintf(os.Stderr, "no changes to %s\n", WaveObjMetaConstsFileName)
2024-08-28 03:49:49 +02:00
}
return err
2024-08-28 03:49:49 +02:00
}
func GenerateSettingsMetaConsts() error {
2024-08-28 03:49:49 +02:00
fmt.Fprintf(os.Stderr, "generating settings meta consts file to %s\n", SettingsMetaConstsFileName)
var buf strings.Builder
gogen.GenerateBoilerplate(&buf, "wconfig", []string{})
gogen.GenerateMetaMapConsts(&buf, "ConfigKey_", reflect.TypeOf(wconfig.SettingsType{}))
buf.WriteString("\n")
written, err := utilfn.WriteFileIfDifferent(SettingsMetaConstsFileName, []byte(buf.String()))
if !written {
fmt.Fprintf(os.Stderr, "no changes to %s\n", SettingsMetaConstsFileName)
2024-08-28 03:49:49 +02:00
}
return err
2024-08-28 03:49:49 +02:00
}
func main() {
err := GenerateWshClient()
if err != nil {
fmt.Fprintf(os.Stderr, "error generating wshclient: %v\n", err)
return
}
err = GenerateWaveObjMetaConsts()
if err != nil {
fmt.Fprintf(os.Stderr, "error generating waveobj meta consts: %v\n", err)
return
}
err = GenerateSettingsMetaConsts()
if err != nil {
fmt.Fprintf(os.Stderr, "error generating settings meta consts: %v\n", err)
return
}
2024-08-28 03:49:49 +02:00
}