waveterm/cmd/generatets/main-generatets.go

137 lines
4.4 KiB
Go
Raw Permalink Normal View History

2024-05-27 08:05:11 +02:00
// Copyright 2024, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
package main
import (
"bytes"
2024-05-27 08:05:11 +02:00
"fmt"
2024-06-12 02:42:10 +02:00
"os"
2024-05-27 08:05:11 +02:00
"reflect"
2024-06-12 02:42:10 +02:00
"sort"
"strings"
2024-05-27 08:05:11 +02:00
2024-09-05 23:25:45 +02:00
"github.com/wavetermdev/waveterm/pkg/service"
"github.com/wavetermdev/waveterm/pkg/tsgen"
"github.com/wavetermdev/waveterm/pkg/util/utilfn"
"github.com/wavetermdev/waveterm/pkg/wshrpc"
2024-05-27 08:05:11 +02:00
)
func generateTypesFile(tsTypesMap map[reflect.Type]string) error {
fileName := "frontend/types/gotypes.d.ts"
fmt.Fprintf(os.Stderr, "generating types file to %s\n", fileName)
2024-06-12 02:42:10 +02:00
tsgen.GenerateWaveObjTypes(tsTypesMap)
err := tsgen.GenerateServiceTypes(tsTypesMap)
2024-06-12 02:42:10 +02:00
if err != nil {
fmt.Fprintf(os.Stderr, "Error generating service types: %v\n", err)
os.Exit(1)
}
err = tsgen.GenerateWshServerTypes(tsTypesMap)
2024-09-16 20:59:39 +02:00
if err != nil {
return fmt.Errorf("error generating wsh server types: %w", err)
}
var buf bytes.Buffer
fmt.Fprintf(&buf, "// Copyright 2024, Command Line Inc.\n")
fmt.Fprintf(&buf, "// SPDX-License-Identifier: Apache-2.0\n\n")
fmt.Fprintf(&buf, "// generated by cmd/generate/main-generatets.go\n\n")
fmt.Fprintf(&buf, "declare global {\n\n")
2024-06-12 02:42:10 +02:00
var keys []reflect.Type
for key := range tsTypesMap {
keys = append(keys, key)
}
sort.Slice(keys, func(i, j int) bool {
iname, _ := tsgen.TypeToTSType(keys[i], tsTypesMap)
jname, _ := tsgen.TypeToTSType(keys[j], tsTypesMap)
2024-06-12 02:42:10 +02:00
return iname < jname
})
for _, key := range keys {
// don't output generic types
if strings.Contains(key.Name(), "[") {
continue
}
2024-06-12 02:42:10 +02:00
tsCode := tsTypesMap[key]
istr := utilfn.IndentString(" ", tsCode)
fmt.Fprint(&buf, istr)
}
fmt.Fprintf(&buf, "}\n\n")
fmt.Fprintf(&buf, "export {}\n")
written, err := utilfn.WriteFileIfDifferent(fileName, buf.Bytes())
if !written {
fmt.Fprintf(os.Stderr, "no changes to %s\n", fileName)
2024-06-12 02:42:10 +02:00
}
return err
2024-06-12 02:42:10 +02:00
}
func generateServicesFile(tsTypesMap map[reflect.Type]string) error {
fileName := "frontend/app/store/services.ts"
var buf bytes.Buffer
fmt.Fprintf(os.Stderr, "generating services file to %s\n", fileName)
fmt.Fprintf(&buf, "// Copyright 2024, Command Line Inc.\n")
fmt.Fprintf(&buf, "// SPDX-License-Identifier: Apache-2.0\n\n")
fmt.Fprintf(&buf, "// generated by cmd/generate/main-generatets.go\n\n")
fmt.Fprintf(&buf, "import * as WOS from \"./wos\";\n\n")
2024-06-12 02:42:10 +02:00
orderedKeys := utilfn.GetOrderedMapKeys(service.ServiceMap)
for _, serviceName := range orderedKeys {
serviceObj := service.ServiceMap[serviceName]
svcStr := tsgen.GenerateServiceClass(serviceName, serviceObj, tsTypesMap)
fmt.Fprint(&buf, svcStr)
fmt.Fprint(&buf, "\n")
2024-06-12 02:42:10 +02:00
}
written, err := utilfn.WriteFileIfDifferent(fileName, buf.Bytes())
if !written {
fmt.Fprintf(os.Stderr, "no changes to %s\n", fileName)
}
return err
2024-06-12 02:42:10 +02:00
}
2024-09-16 20:59:39 +02:00
func generateWshClientApiFile(tsTypeMap map[reflect.Type]string) error {
fileName := "frontend/app/store/wshclientapi.ts"
var buf bytes.Buffer
declMap := wshrpc.GenerateWshCommandDeclMap()
fmt.Fprintf(os.Stderr, "generating wshclientapi file to %s\n", fileName)
fmt.Fprintf(&buf, "// Copyright 2024, Command Line Inc.\n")
fmt.Fprintf(&buf, "// SPDX-License-Identifier: Apache-2.0\n\n")
fmt.Fprintf(&buf, "// generated by cmd/generate/main-generatets.go\n\n")
fmt.Fprintf(&buf, "import { WshClient } from \"./wshclient\";\n\n")
orderedKeys := utilfn.GetOrderedMapKeys(declMap)
fmt.Fprintf(&buf, "// WshServerCommandToDeclMap\n")
fmt.Fprintf(&buf, "class RpcApiType {\n")
for _, methodDecl := range orderedKeys {
methodDecl := declMap[methodDecl]
2024-09-16 20:59:39 +02:00
methodStr := tsgen.GenerateWshClientApiMethod(methodDecl, tsTypeMap)
fmt.Fprint(&buf, methodStr)
fmt.Fprintf(&buf, "\n")
}
fmt.Fprintf(&buf, "}\n\n")
fmt.Fprintf(&buf, "export const RpcApi = new RpcApiType();\n")
written, err := utilfn.WriteFileIfDifferent(fileName, buf.Bytes())
if !written {
fmt.Fprintf(os.Stderr, "no changes to %s\n", fileName)
}
return err
}
2024-06-12 02:42:10 +02:00
func main() {
err := service.ValidateServiceMap()
if err != nil {
fmt.Fprintf(os.Stderr, "Error validating service map: %v\n", err)
os.Exit(1)
}
tsTypesMap := make(map[reflect.Type]string)
err = generateTypesFile(tsTypesMap)
2024-06-12 02:42:10 +02:00
if err != nil {
fmt.Fprintf(os.Stderr, "Error generating types file: %v\n", err)
os.Exit(1)
}
err = generateServicesFile(tsTypesMap)
2024-06-12 02:42:10 +02:00
if err != nil {
fmt.Fprintf(os.Stderr, "Error generating services file: %v\n", err)
os.Exit(1)
2024-05-27 08:05:11 +02:00
}
2024-09-16 20:59:39 +02:00
err = generateWshClientApiFile(tsTypesMap)
if err != nil {
fmt.Fprintf(os.Stderr, "Error generating wshserver file: %v\n", err)
os.Exit(1)
}
2024-05-27 08:05:11 +02:00
}