2024-05-27 08:05:11 +02:00
|
|
|
// Copyright 2024, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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"
|
2024-07-26 22:30:11 +02:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2024-06-12 22:47:13 +02:00
|
|
|
func generateTypesFile(tsTypesMap map[reflect.Type]string) error {
|
2024-06-12 02:42:10 +02:00
|
|
|
fd, err := os.Create("frontend/types/gotypes.d.ts")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fd.Close()
|
|
|
|
fmt.Fprintf(os.Stderr, "generating types file to %s\n", fd.Name())
|
|
|
|
tsgen.GenerateWaveObjTypes(tsTypesMap)
|
|
|
|
err = tsgen.GenerateServiceTypes(tsTypesMap)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Error generating service types: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2024-07-18 00:24:43 +02:00
|
|
|
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)
|
|
|
|
}
|
2024-06-12 02:42:10 +02:00
|
|
|
fmt.Fprintf(fd, "// Copyright 2024, Command Line Inc.\n")
|
|
|
|
fmt.Fprintf(fd, "// SPDX-License-Identifier: Apache-2.0\n\n")
|
2024-09-16 20:59:39 +02:00
|
|
|
fmt.Fprintf(fd, "// generated by cmd/generate/main-generatets.go\n\n")
|
2024-06-12 02:42:10 +02:00
|
|
|
fmt.Fprintf(fd, "declare global {\n\n")
|
|
|
|
var keys []reflect.Type
|
|
|
|
for key := range tsTypesMap {
|
|
|
|
keys = append(keys, key)
|
|
|
|
}
|
|
|
|
sort.Slice(keys, func(i, j int) bool {
|
2024-06-12 22:47:13 +02:00
|
|
|
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 {
|
2024-07-26 22:30:11 +02:00
|
|
|
// don't output generic types
|
|
|
|
if strings.Index(key.Name(), "[") != -1 {
|
|
|
|
continue
|
|
|
|
}
|
2024-06-12 02:42:10 +02:00
|
|
|
tsCode := tsTypesMap[key]
|
|
|
|
istr := utilfn.IndentString(" ", tsCode)
|
|
|
|
fmt.Fprint(fd, istr)
|
|
|
|
}
|
|
|
|
fmt.Fprintf(fd, "}\n\n")
|
|
|
|
fmt.Fprintf(fd, "export {}\n")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-12 22:47:13 +02:00
|
|
|
func generateServicesFile(tsTypesMap map[reflect.Type]string) error {
|
2024-06-12 02:42:10 +02:00
|
|
|
fd, err := os.Create("frontend/app/store/services.ts")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fd.Close()
|
|
|
|
fmt.Fprintf(os.Stderr, "generating services file to %s\n", fd.Name())
|
|
|
|
fmt.Fprintf(fd, "// Copyright 2024, Command Line Inc.\n")
|
|
|
|
fmt.Fprintf(fd, "// SPDX-License-Identifier: Apache-2.0\n\n")
|
2024-09-16 20:59:39 +02:00
|
|
|
fmt.Fprintf(fd, "// generated by cmd/generate/main-generatets.go\n\n")
|
2024-06-12 02:42:10 +02:00
|
|
|
fmt.Fprintf(fd, "import * as WOS from \"./wos\";\n\n")
|
|
|
|
orderedKeys := utilfn.GetOrderedMapKeys(service.ServiceMap)
|
|
|
|
for _, serviceName := range orderedKeys {
|
|
|
|
serviceObj := service.ServiceMap[serviceName]
|
2024-06-12 22:47:13 +02:00
|
|
|
svcStr := tsgen.GenerateServiceClass(serviceName, serviceObj, tsTypesMap)
|
2024-06-12 02:42:10 +02:00
|
|
|
fmt.Fprint(fd, svcStr)
|
|
|
|
fmt.Fprint(fd, "\n")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-16 20:59:39 +02:00
|
|
|
func generateWshClientApiFile(tsTypeMap map[reflect.Type]string) error {
|
|
|
|
fd, err := os.Create("frontend/app/store/wshclientapi.ts")
|
2024-07-18 00:24:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fd.Close()
|
2024-07-26 22:30:11 +02:00
|
|
|
declMap := wshrpc.GenerateWshCommandDeclMap()
|
2024-09-16 20:59:39 +02:00
|
|
|
fmt.Fprintf(os.Stderr, "generating wshclientapi file to %s\n", fd.Name())
|
2024-07-18 00:24:43 +02:00
|
|
|
fmt.Fprintf(fd, "// Copyright 2024, Command Line Inc.\n")
|
|
|
|
fmt.Fprintf(fd, "// SPDX-License-Identifier: Apache-2.0\n\n")
|
2024-09-16 20:59:39 +02:00
|
|
|
fmt.Fprintf(fd, "// generated by cmd/generate/main-generatets.go\n\n")
|
|
|
|
fmt.Fprintf(fd, "import { WshClient } from \"./wshclient\";\n\n")
|
2024-07-26 22:30:11 +02:00
|
|
|
orderedKeys := utilfn.GetOrderedMapKeys(declMap)
|
2024-07-18 00:24:43 +02:00
|
|
|
fmt.Fprintf(fd, "// WshServerCommandToDeclMap\n")
|
2024-09-16 20:59:39 +02:00
|
|
|
fmt.Fprintf(fd, "class RpcApiType {\n")
|
2024-07-18 00:24:43 +02:00
|
|
|
for _, methodDecl := range orderedKeys {
|
2024-07-26 22:30:11 +02:00
|
|
|
methodDecl := declMap[methodDecl]
|
2024-09-16 20:59:39 +02:00
|
|
|
methodStr := tsgen.GenerateWshClientApiMethod(methodDecl, tsTypeMap)
|
2024-07-18 00:24:43 +02:00
|
|
|
fmt.Fprint(fd, methodStr)
|
|
|
|
fmt.Fprintf(fd, "\n")
|
|
|
|
}
|
|
|
|
fmt.Fprintf(fd, "}\n\n")
|
2024-09-16 20:59:39 +02:00
|
|
|
fmt.Fprintf(fd, "export const RpcApi = new RpcApiType();\n")
|
2024-07-18 00:24:43 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
2024-06-12 22:47:13 +02:00
|
|
|
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)
|
|
|
|
}
|
2024-06-12 22:47:13 +02:00
|
|
|
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)
|
2024-07-18 00:24:43 +02:00
|
|
|
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
|
|
|
}
|