dev
This commit is contained in:
52
internal/plugin/generate.go
Normal file
52
internal/plugin/generate.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package plugin
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"git.apinb.com/bsm-tools/protoc-gen-ts/internal/codegen"
|
||||
"google.golang.org/protobuf/proto"
|
||||
"google.golang.org/protobuf/reflect/protodesc"
|
||||
"google.golang.org/protobuf/reflect/protoreflect"
|
||||
"google.golang.org/protobuf/types/descriptorpb"
|
||||
"google.golang.org/protobuf/types/pluginpb"
|
||||
)
|
||||
|
||||
func Generate(request *pluginpb.CodeGeneratorRequest) (*pluginpb.CodeGeneratorResponse, error) {
|
||||
generate := make(map[string]struct{})
|
||||
registry, err := protodesc.NewFiles(&descriptorpb.FileDescriptorSet{
|
||||
File: request.GetProtoFile(),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create proto registry: %w", err)
|
||||
}
|
||||
for _, f := range request.GetFileToGenerate() {
|
||||
generate[f] = struct{}{}
|
||||
}
|
||||
packaged := make(map[protoreflect.FullName][]protoreflect.FileDescriptor)
|
||||
for _, f := range request.GetFileToGenerate() {
|
||||
file, err := registry.FindFileByPath(f)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("find file %s: %w", f, err)
|
||||
}
|
||||
packaged[file.Package()] = append(packaged[file.Package()], file)
|
||||
}
|
||||
|
||||
var res pluginpb.CodeGeneratorResponse
|
||||
for pkg, files := range packaged {
|
||||
var index codegen.File
|
||||
indexPathElems := append(strings.Split(string(pkg), "."), "index.ts")
|
||||
if err := (packageGenerator{pkg: pkg, files: files}).Generate(&index); err != nil {
|
||||
return nil, fmt.Errorf("generate package '%s': %w", pkg, err)
|
||||
}
|
||||
index.P()
|
||||
index.P("// @@protoc_insertion_point(typescript-http-eof)")
|
||||
res.File = append(res.File, &pluginpb.CodeGeneratorResponse_File{
|
||||
Name: proto.String(path.Join(indexPathElems...)),
|
||||
Content: proto.String(string(index.Content())),
|
||||
})
|
||||
}
|
||||
res.SupportedFeatures = proto.Uint64(uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL))
|
||||
return &res, nil
|
||||
}
|
||||
Reference in New Issue
Block a user