main.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2019 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. //go:generate go run . -execute
  5. package main
  6. import (
  7. "flag"
  8. "fmt"
  9. "io/ioutil"
  10. "os"
  11. "os/exec"
  12. "path"
  13. "path/filepath"
  14. "strings"
  15. "github.com/golang/protobuf/proto"
  16. gengo "google.golang.org/protobuf/cmd/protoc-gen-go/internal_gengo"
  17. "google.golang.org/protobuf/compiler/protogen"
  18. "google.golang.org/protobuf/reflect/protodesc"
  19. "google.golang.org/protobuf/reflect/protoreflect"
  20. "google.golang.org/protobuf/types/descriptorpb"
  21. "google.golang.org/protobuf/types/known/anypb"
  22. "google.golang.org/protobuf/types/known/durationpb"
  23. "google.golang.org/protobuf/types/known/emptypb"
  24. "google.golang.org/protobuf/types/known/structpb"
  25. "google.golang.org/protobuf/types/known/timestamppb"
  26. "google.golang.org/protobuf/types/known/wrapperspb"
  27. "google.golang.org/protobuf/types/pluginpb"
  28. )
  29. func main() {
  30. run := flag.Bool("execute", false, "Write generated files to destination.")
  31. flag.Parse()
  32. // Set of generated proto packages to forward to v2.
  33. files := []struct {
  34. goPkg string
  35. pbDesc protoreflect.FileDescriptor
  36. }{{
  37. goPkg: "github.com/golang/protobuf/protoc-gen-go/descriptor;descriptor",
  38. pbDesc: descriptorpb.File_google_protobuf_descriptor_proto,
  39. }, {
  40. goPkg: "github.com/golang/protobuf/protoc-gen-go/plugin;plugin_go",
  41. pbDesc: pluginpb.File_google_protobuf_compiler_plugin_proto,
  42. }, {
  43. goPkg: "github.com/golang/protobuf/ptypes/any;any",
  44. pbDesc: anypb.File_google_protobuf_any_proto,
  45. }, {
  46. goPkg: "github.com/golang/protobuf/ptypes/duration;duration",
  47. pbDesc: durationpb.File_google_protobuf_duration_proto,
  48. }, {
  49. goPkg: "github.com/golang/protobuf/ptypes/timestamp;timestamp",
  50. pbDesc: timestamppb.File_google_protobuf_timestamp_proto,
  51. }, {
  52. goPkg: "github.com/golang/protobuf/ptypes/wrappers;wrappers",
  53. pbDesc: wrapperspb.File_google_protobuf_wrappers_proto,
  54. }, {
  55. goPkg: "github.com/golang/protobuf/ptypes/struct;structpb",
  56. pbDesc: structpb.File_google_protobuf_struct_proto,
  57. }, {
  58. goPkg: "github.com/golang/protobuf/ptypes/empty;empty",
  59. pbDesc: emptypb.File_google_protobuf_empty_proto,
  60. }}
  61. // For each package, construct a proto file that public imports the package.
  62. var req pluginpb.CodeGeneratorRequest
  63. for _, file := range files {
  64. pkgPath := file.goPkg[:strings.IndexByte(file.goPkg, ';')]
  65. fd := &descriptorpb.FileDescriptorProto{
  66. Name: proto.String(pkgPath + "/" + path.Base(pkgPath) + ".proto"),
  67. Syntax: proto.String(file.pbDesc.Syntax().String()),
  68. Dependency: []string{file.pbDesc.Path()},
  69. PublicDependency: []int32{0},
  70. Options: &descriptorpb.FileOptions{GoPackage: proto.String(file.goPkg)},
  71. }
  72. req.ProtoFile = append(req.ProtoFile, protodesc.ToFileDescriptorProto(file.pbDesc), fd)
  73. req.FileToGenerate = append(req.FileToGenerate, fd.GetName())
  74. }
  75. // Use the internal logic of protoc-gen-go to generate the files.
  76. gen, err := protogen.New(&req, nil)
  77. check(err)
  78. for _, file := range gen.Files {
  79. if file.Generate {
  80. gengo.GenerateVersionMarkers = false
  81. gengo.GenerateFile(gen, file)
  82. }
  83. }
  84. // Write the generated files.
  85. resp := gen.Response()
  86. if resp.Error != nil {
  87. panic("gengo error: " + resp.GetError())
  88. }
  89. for _, file := range resp.File {
  90. relPath, err := filepath.Rel(filepath.FromSlash("github.com/golang/protobuf"), file.GetName())
  91. check(err)
  92. check(ioutil.WriteFile(relPath+".bak", []byte(file.GetContent()), 0664))
  93. if *run {
  94. fmt.Println("#", relPath)
  95. check(os.Rename(relPath+".bak", relPath))
  96. } else {
  97. cmd := exec.Command("diff", relPath, relPath+".bak", "-N", "-u")
  98. cmd.Stdout = os.Stdout
  99. cmd.Run()
  100. os.Remove(relPath + ".bak") // best-effort delete
  101. }
  102. }
  103. }
  104. func check(err error) {
  105. if err != nil {
  106. panic(err)
  107. }
  108. }