internal/cmd/generate-protos: initial commit

Create a single binary for handling generation of protos.
This replaces previous logic spread throughout the repo in:
* regenerate.bash
* cmd/protoc-gen-go/golden_test.go
* cmd/protoc-gen-go-grpc/golden_test.go
* (indirectly) internal/protogen/goldentest

One of the problems with the former approaches is that they relied on
a version of protoc that was specific to a developer's workstation.
This meant that the result of generation was not hermetic.
To address this, we rely on the hard-coded version of protobuf specified
in the test.bash script.

A summary of changes in this CL are:
* The internal_gengo.GenerateFile and internal_gengogrpc.GenerateFile
functions are unified to have consistent signatures. It seems that the
former accepted a *protogen.GeneratedFile to support v1 where gRPC code
was generated into the same file as the base .pb.go file. However, the
same functionality can be achieved by having the function return
the generated file object.
* The test.bash script patches the protobuf toolchain to have properly
specified go_package options in each proto source file.
* The test.bash script accepts a "-regenerate" argument.
* Add generation for the well-known types. Contrary to how these were
laid out in the v1 repo, all the well-known types are placed in the
same Go package.
* Add generation for the conformance proto.
* Remove regenerate.bash
* Remove internal/protogen
* Remove cmd/protoc-gen-go/golden_test.go
* Remove cmd/protoc-gen-go-grpc/golden_test.go
* Add cmd/protoc-gen-go/annotation_test.go

Change-Id: I4a1a97ae6f66e2fabcf4e4d292c95ab2a2db0248
Reviewed-on: https://go-review.googlesource.com/c/164477
Reviewed-by: Damien Neil <dneil@google.com>
diff --git a/cmd/protoc-gen-go/golden_test.go b/cmd/protoc-gen-go/annotation_test.go
similarity index 70%
rename from cmd/protoc-gen-go/golden_test.go
rename to cmd/protoc-gen-go/annotation_test.go
index b2754a6..7463215 100644
--- a/cmd/protoc-gen-go/golden_test.go
+++ b/cmd/protoc-gen-go/annotation_test.go
@@ -2,49 +2,25 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// +build golden
-
 package main
 
 import (
 	"bytes"
-	"flag"
 	"io/ioutil"
-	"os"
-	"path/filepath"
 	"testing"
 
 	"github.com/golang/protobuf/proto"
-	"github.com/golang/protobuf/v2/internal/protogen/goldentest"
 	"github.com/golang/protobuf/v2/internal/scalar"
 
 	descriptorpb "github.com/golang/protobuf/v2/types/descriptor"
 )
 
-// Set --regenerate to regenerate the golden files.
-var regenerate = flag.Bool("regenerate", false, "regenerate golden files")
-
-func init() {
-	goldentest.Plugin(main)
-}
-
-func TestGolden(t *testing.T) {
-	goldentest.Run(t, *regenerate)
-}
-
 func TestAnnotations(t *testing.T) {
-	workdir, err := ioutil.TempDir("", "proto-test")
+	sourceFile, err := ioutil.ReadFile("testdata/annotations/annotations.pb.go")
 	if err != nil {
 		t.Fatal(err)
 	}
-	defer os.RemoveAll(workdir)
-
-	goldentest.Protoc(t, []string{"--go_out=paths=source_relative,annotate_code:" + workdir, "-Itestdata/annotations", "testdata/annotations/annotations.proto"})
-	sourceFile, err := ioutil.ReadFile(filepath.Join(workdir, "annotations.pb.go"))
-	if err != nil {
-		t.Fatal(err)
-	}
-	metaFile, err := ioutil.ReadFile(filepath.Join(workdir, "annotations.pb.go.meta"))
+	metaFile, err := ioutil.ReadFile("testdata/annotations/annotations.pb.go.meta")
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -85,7 +61,7 @@
 			Path:       want.path,
 			Begin:      scalar.Int32(int32(begin)),
 			End:        scalar.Int32(int32(end)),
-			SourceFile: scalar.String("annotations.proto"),
+			SourceFile: scalar.String("annotations/annotations.proto"),
 		})
 	}
 	if !proto.Equal(gotInfo, wantInfo) {
diff --git a/cmd/protoc-gen-go/internal_gengo/main.go b/cmd/protoc-gen-go/internal_gengo/main.go
index 63598aa..4748f67 100644
--- a/cmd/protoc-gen-go/internal_gengo/main.go
+++ b/cmd/protoc-gen-go/internal_gengo/main.go
@@ -74,7 +74,9 @@
 }
 
 // GenerateFile generates the contents of a .pb.go file.
-func GenerateFile(gen *protogen.Plugin, file *protogen.File, g *protogen.GeneratedFile) {
+func GenerateFile(gen *protogen.Plugin, file *protogen.File) *protogen.GeneratedFile {
+	filename := file.GeneratedFilenamePrefix + ".pb.go"
+	g := gen.NewGeneratedFile(filename, file.GoImportPath)
 	f := &fileInfo{
 		File: file,
 	}
@@ -151,6 +153,8 @@
 	genInitFunction(gen, g, f)
 	genFileDescriptor(gen, g, f)
 	genReflectFileDescriptor(gen, g, f)
+
+	return g
 }
 
 // walkMessages calls f on each message and all of its descendants.
@@ -182,9 +186,8 @@
 
 	// Generate public imports by generating the imported file, parsing it,
 	// and extracting every symbol that should receive a forwarding declaration.
-	impGen := gen.NewGeneratedFile("temp.go", impFile.GoImportPath)
+	impGen := GenerateFile(gen, impFile)
 	impGen.Skip()
-	GenerateFile(gen, impFile, impGen)
 	b, err := impGen.Content()
 	if err != nil {
 		gen.Error(err)
diff --git a/cmd/protoc-gen-go/main.go b/cmd/protoc-gen-go/main.go
index 8c72be8..b55d642 100644
--- a/cmd/protoc-gen-go/main.go
+++ b/cmd/protoc-gen-go/main.go
@@ -31,12 +31,9 @@
 			return errors.New("protoc-gen-go: import_prefix is not supported")
 		}
 		for _, f := range gen.Files {
-			if !f.Generate {
-				continue
+			if f.Generate {
+				gengo.GenerateFile(gen, f)
 			}
-			filename := f.GeneratedFilenamePrefix + ".pb.go"
-			g := gen.NewGeneratedFile(filename, f.GoImportPath)
-			gengo.GenerateFile(gen, f, g)
 		}
 		return nil
 	})
diff --git a/cmd/protoc-gen-go/testdata/annotations/annotations.pb.go.meta b/cmd/protoc-gen-go/testdata/annotations/annotations.pb.go.meta
new file mode 100644
index 0000000..503b614
--- /dev/null
+++ b/cmd/protoc-gen-go/testdata/annotations/annotations.pb.go.meta
@@ -0,0 +1 @@
+annotation:<path:5 path:0 source_file:"annotations/annotations.proto" begin:687 end:706 > annotation:<path:5 path:0 path:2 path:0 source_file:"annotations/annotations.proto" begin:723 end:770 > annotation:<path:4 path:0 source_file:"annotations/annotations.proto" begin:1801 end:1823 > annotation:<path:4 path:0 path:2 path:0 source_file:"annotations/annotations.proto" begin:1834 end:1854 > annotation:<path:4 path:0 path:2 path:0 source_file:"annotations/annotations.proto" begin:3339 end:3362 >
\ No newline at end of file
diff --git a/cmd/protoc-gen-go/testdata/extensions/proto3/ext3.pb.go b/cmd/protoc-gen-go/testdata/extensions/proto3/ext3.pb.go
index c9bc7bc..f1b831b 100644
--- a/cmd/protoc-gen-go/testdata/extensions/proto3/ext3.pb.go
+++ b/cmd/protoc-gen-go/testdata/extensions/proto3/ext3.pb.go
@@ -6,9 +6,9 @@
 import (
 	proto "github.com/golang/protobuf/proto"
 	protoapi "github.com/golang/protobuf/protoapi"
-	descriptor "github.com/golang/protobuf/protoc-gen-go/descriptor"
 	protoreflect "github.com/golang/protobuf/v2/reflect/protoreflect"
 	protoimpl "github.com/golang/protobuf/v2/runtime/protoimpl"
+	descriptor "github.com/golang/protobuf/v2/types/descriptor"
 	reflect "reflect"
 )