Skip to content

Commit

Permalink
Merge pull request #61 from kzys/protoc-upgrade
Browse files Browse the repository at this point in the history
Don't support github.com/golang/protobuf/[email protected]
  • Loading branch information
kzys authored Aug 21, 2023
2 parents 7e123b2 + 32a4deb commit 9f6f856
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 314 deletions.
51 changes: 1 addition & 50 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,55 +54,6 @@ jobs:
run: |
go test
v1:
name: Run with protoc-gen-go v1.3.5
runs-on: ubuntu-22.04
timeout-minutes: 5
steps:

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.16.x
id: go

- name: Setup environment
shell: bash
run: |
echo "GOPATH=${{ github.workspace }}" >> $GITHUB_ENV
echo "${{ github.workspace }}/bin" >> $GITHUB_PATH
- name: Check out code
uses: actions/checkout@v3
with:
path: src/github.com/containerd/protobuild

- name: Build
working-directory: src/github.com/containerd/protobuild
run: |
go build .
- name: Install protoc
run: |
curl -LO https://github.com/protocolbuffers/protobuf/releases/download/v3.5.0/protoc-3.5.0-linux-x86_64.zip
sudo unzip -x protoc-3.5.0-linux-x86_64.zip -d /usr/local
sudo chmod -R go+rX /usr/local/include
sudo chmod go+x /usr/local/bin/protoc
# Both google.golang.org/protobuf/cmd/protoc-gen-go and github.com/golang/protobuf/protoc-gen-go > 1.4.0 don't
# support import_path. So this step has to use 1.3.x here.
- name: Install protoc-gen-go
run: |
go install github.com/golang/protobuf/[email protected]
- name: Run protobuild to see all committed auto-generated files can be generated as is
working-directory: src/github.com/containerd/protobuild
run: |
rm examples/foo/*.pb.* examples/bar/*.pb.* examples/nogrpc/*.pb.*
export PATH=$(go env GOBIN):$PATH
./protobuild github.com/containerd/protobuild/examples/bar github.com/containerd/protobuild/examples/foo github.com/containerd/protobuild/examples/nogrpc
git diff --exit-code
v2:
name: Run with protoc-gen-go v1.26
runs-on: ubuntu-22.04
Expand Down Expand Up @@ -148,5 +99,5 @@ jobs:
run: |
rm examples/v2/*.pb.*
export PATH=$(go env GOBIN):$PATH
./protobuild -f v2.toml github.com/containerd/protobuild/examples/v2
./protobuild github.com/containerd/protobuild/examples/v2
git diff --exit-code
138 changes: 5 additions & 133 deletions Protobuild.toml
Original file line number Diff line number Diff line change
@@ -1,138 +1,10 @@
version = "unstable"
version = "2"

# Generator defines which generator to go. The default is "go". This will be
# formatted into the --xxx_out flag provided to protoc. Below, we have an
# example that selects the ctrd vanity binary.
# generator = "go"
generators = ["go", "go-grpc"]

# Plugins allows one to specify one or more plugins for use in generation.
#
# The common example grpc is provided below.
plugins = ["grpc"]

# Control protoc include paths. Below are usually some good defaults, but feel
# free to try it without them if it works for your project.
[includes]
# Include paths that will be added before all others. Typically, you want to
# treat the root of the project as an include, but this may not be necessary.
#
# "." is included by default
# before = ["."]

# Paths that should be treated as include roots in relation to the vendor
# directory. These will be calculated with the vendor directory nearest the
# target package.
#
# This should be used only in cases where imports must be resolved from the
# vendor package and not a GOPATH package. Most users should employ the
# `packages` configuration and let protobuild resolve the existing package.
#
# With the example below, we import github.com/golang/protobuf from the vendor
# directory.
#
# This is empty by default.
# vendored = ["github.com/golang/protobuf"]

# Packages will add imports that are relative to the vendor path or GOPATH.
#
# If in doubt, use this over `vendored` configuration for your project.
#
# This is empty by default.
# packages = ["github.com/golang/protobuf"]

# Paths that will be added untouched to the end of the includes. We use
# a few paths to pickup the common install locations for protobuf.
# This is the default.
# after = ["usr/local/include", "/usr/include"]

# This section let's us map protobuf imports to Go packages. These will become
# `-M` directives in the call to the go protobuf generator.
#
# We have a few examples to map packages from the examples.
[packages]
"google/protobuf/empty.proto" = "github.com/containerd/protobuild/internal"

# Overrides allows one to specify configuration overrides for specific
# packages. Currently, only overriding the generator and plugin set are
# supported.
#
# This can be used to use a different plugin set for a given package.
[[overrides]]

# Prefix specifies the protobuf import path to which the configuration
# overrides apply.
#
# This works the same as the descriptors selection prefix except that each path
# that is overridden must be specified, rather than applying to the whole tree.
#
# For this example, if there were a package that needed this overrides applied,
# you would need to specify that explicitly in the prefixes list. This allows
# one to granularly select which packages have the overrides.
prefixes = ["github.com/containerd/protobuild/examples/nogrpc"]
"google/protobuf/empty.proto" = "github.com/containerd/protobuild/internal"

# Generator allows one to override the generator for the given prefix.
# generator = ""

# Plugins will override the entire plugin set for the specified prefix.
plugins = []

# Descriptors allows you to aggregate generated protobuf descriptors under a
# particular Go import path into a single, de-duplicated file. The result of
# configuring this option for one or more paths will be a text format protobuf
# dump of all the descriptors in the API.
#
# While this is useful for other applications that require access to
# descriptors, the goal of this features is to provide visibility into changes
# that may affect API stability. Each service, message and field will be
# included in the file, along with dependencies. If a change occurs that
# impacts API stability, it will be obvious in a diff with a file from the
# previous version.
#
# Save these descriptors in your repository. When the next round of protobuf
# changes are built and merged, additions and incompatible changes can be
# detected by diffing these files. When you release, move this file to the
# release version to keep track of the exports over time from various releases.
#
# This takes inspiration from Go's API checker in
# https://github.com/golang/go/tree/master/api, but does so for protobufs.
# Stablize packages and collections of packages of generated protobufs.
[[descriptors]]

# Prefix specifies the protobuf import prefix relative to items that you want
# to stabilize.
#
# For example, if the files are imported as
# github.com/containerd/protobuild/examples/foo/foo.proto, and you want to
# stabilize everything in package "foo", it should be
# "github.com/containerd/protobuild/example/foo".
#
# The example below stablizes the protobufs under the examples directory. The
# generated descriptor file will include both the "foo" and "bar" subpackages.
prefix = "github.com/containerd/protobuild/examples"

# Target defines the output location of the merged descriptor file.
target = "examples/next.pb.txt"

# Ignore files by their protobuf import path.
#
# This operates directly on the FileDescriptorProto.Name field, so it will
# filter out imported protobufs. This allows one to remove descriptors from the
# result that don't impact API stability, such as generation extension
# directives.
#
# The example below will exclude empty.proto. Experiment with this feature by
# uncommenting the line below and notice that the description of that file will
# be removed.
# ignore_files = ["google/protobuf/empty.proto"]

# TODO(stevvooe): Possibly automate build rejection under certain classes of
# changes. Left alone now until we get a solid idea about the noise present in
# this process.
#

# Remember that you can have multiple aggregation points for descriptors. Here,
# we aggregate the descriptors for examples/foo, in addition to the above.
# Uncomment these lines to try it out!
# [[descriptors]]
# target = "examples/foo/descriptors.pb.txt"
# prefix = "github.com/containerd/protobuild/examples/foo"
prefix = "github.com/containerd/protobuild/examples/v2"
target = "examples/v2/next.pb.txt"
38 changes: 0 additions & 38 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package main

import (
"fmt"
"io/ioutil"
"log"

Expand All @@ -28,10 +27,6 @@ type config struct {
Version string
Generators []string

// Generator is a code generator which is used from protoc.
// Deprecated: Use Generators instead.
Generator string

// Parameters are custom parameters to be passed to the generators.
// The parameter key must be the generator name with a table value
// of keys and string values to be passed.
Expand All @@ -40,11 +35,6 @@ type config struct {
// customkey = "somevalue"
Parameters map[string]map[string]string

// Plugins will be deprecated. It has to be per-Generator setting,
// but neither protoc-gen-go nor protoc-gen-go-grpc support plugins.
// So the refactoring is not worth to do.
Plugins []string

Includes struct {
Before []string
Vendored []string
Expand All @@ -56,12 +46,8 @@ type config struct {

Overrides []struct {
Prefixes []string
// Generator is a code generator which is used from protoc.
// Deprecated: Use Generators instead.
Generator string
Generators []string
Parameters map[string]map[string]string
Plugins *[]string

// TODO(stevvooe): We could probably support overriding of includes and
// package maps, but they don't seem to be as useful. Likely,
Expand Down Expand Up @@ -104,30 +90,6 @@ func readConfigFrom(p []byte) (config, error) {
log.Fatalln(err)
}

if c.Generator != "" {
if len(c.Generators) > 0 {
return config{}, fmt.Errorf(
`specify either "generators = %v" or "generator = %v", not both`,
c.Generators, c.Generator,
)
}
c.Generators = []string{c.Generator}
c.Generator = ""
}

for i, o := range c.Overrides {
if o.Generator != "" {
if len(o.Generators) > 0 {
return config{}, fmt.Errorf(
`specify either "overrides[%d].generators" or "overrides[%d].generator", not both`,
i, i,
)
}
c.Overrides[i].Generators = []string{o.Generator}
c.Overrides[i].Generator = ""
}
}

if len(c.Generators) == 0 {
c.Generators = []string{"go"}
}
Expand Down
3 changes: 0 additions & 3 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ generators=["go"]
if err != nil {
t.Fatalf("err must be nil, but got %v", err)
}
if c.Generator == "go" {
t.Fatalf("Generator must be cleared, but got %v", c.Generator)
}
if len(c.Generators) != 1 || c.Generators[0] != "go" {
t.Fatalf("Generators must be [go], but got %v", c.Generators)
}
Expand Down
31 changes: 4 additions & 27 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"os/exec"
"path"
"path/filepath"
"strconv"
"strings"
"syscall"

Expand All @@ -49,19 +48,10 @@ func init() {
}

func parseVersion(s string) (int, error) {
if s == "unstable" {
return 0, nil
if s != "2" {
return 0, fmt.Errorf("unsupported file version %q: Protobuild >= 1.0.0 no longer supports the version", s)
}

v, err := strconv.Atoi(s)
if err != nil {
return 0, fmt.Errorf("unknown file version %q: %w", s, err)
}

if v < 1 || v > 2 {
return 0, fmt.Errorf(`unknown file version %q; valid versions are "unstable", "1" and "2"`, s)
}
return v, nil
return 2, nil
}

func importPath(base, target string) (string, error) {
Expand All @@ -80,7 +70,7 @@ func main() {
log.Fatalln(err)
}

version, err := parseVersion(c.Version)
_, err = parseVersion(c.Version)
if err != nil {
log.Fatalln(err)
}
Expand Down Expand Up @@ -108,10 +98,8 @@ func main() {
// Index overrides by target import path
overrides := map[string]struct {
Prefixes []string
Generator string
Generators []string
Parameters map[string]map[string]string
Plugins *[]string
}{}
for _, override := range c.Overrides {
for _, prefix := range override.Prefixes {
Expand Down Expand Up @@ -200,13 +188,7 @@ func main() {
protoc := protocCmd{
Generators: generators(c.Generators, outputDir),
Files: pkg.ProtoFiles,
OutputDir: outputDir,
Includes: includes,
Version: version,
Names: c.Generators,
Plugins: c.Plugins,
ImportPath: pkg.GoImportPath,
PackageMap: c.Packages,
}

importDirPath, err := importPath(outputDir, pkg.Dir)
Expand All @@ -227,14 +209,9 @@ func main() {
if override, ok := overrides[importDirPath]; ok {
// selectively apply the overrides to the protoc structure.
if len(override.Generators) > 0 {
protoc.Names = override.Generators
protoc.Generators = generators(override.Generators, outputDir)
}

if override.Plugins != nil {
protoc.Plugins = *override.Plugins
}

for k, v := range override.Parameters {
parameters[k] = mergeMap(parameters[k], v)
}
Expand Down
Loading

0 comments on commit 9f6f856

Please sign in to comment.