Skip to content

Commit

Permalink
Merge pull request etcd-io#16883 from mjlshen/16760
Browse files Browse the repository at this point in the history
scripts: macOS support for scripts/genproto.sh
  • Loading branch information
serathius authored Nov 8, 2023
2 parents e526bdd + 27272c3 commit 649b365
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions scripts/genproto.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ if ! [[ "$0" =~ scripts/genproto.sh ]]; then
exit 255
fi

# Set SED variable
if LANG=C sed --help 2>&1 | grep -q GNU; then
SED="sed"
elif command -v gsed &>/dev/null; then
SED="gsed"
else
echo "Failed to find GNU sed as sed or gsed. If you are on Mac: brew install gnu-sed." >&2
exit 1
fi

source ./scripts/test_lib.sh

if [[ $(protoc --version | cut -f2 -d' ') != "3.20.3" ]]; then
Expand Down Expand Up @@ -98,14 +108,14 @@ for pb in api/etcdserverpb/rpc server/etcdserver/api/v3lock/v3lockpb/v3lock serv
pkg=$(basename "${pkgpath}")
gwfile="${pb}.pb.gw.go"

run sed -i -E "s#package $pkg#package gw#g" "${gwfile}"
run sed -i -E "s#import \\(#import \\(\"go.etcd.io/etcd/${pkgpath}\"#g" "${gwfile}"
run sed -i -E "s#([ (])([a-zA-Z0-9_]*(Client|Server|Request)([^(]|$))#\\1${pkg}.\\2#g" "${gwfile}"
run sed -i -E "s# (New[a-zA-Z0-9_]*Client\\()# ${pkg}.\\1#g" "${gwfile}"
run sed -i -E "s|go.etcd.io/etcd|go.etcd.io/etcd/v3|g" "${gwfile}"
run sed -i -E "s|go.etcd.io/etcd/v3/api|go.etcd.io/etcd/api/v3|g" "${gwfile}"
run sed -i -E "s|go.etcd.io/etcd/v3/server|go.etcd.io/etcd/server/v3|g" "${gwfile}"
run ${SED?} -i -E "s#package $pkg#package gw#g" "${gwfile}"
run ${SED?} -i -E "s#import \\(#import \\(\"go.etcd.io/etcd/${pkgpath}\"#g" "${gwfile}"
run ${SED?} -i -E "s#([ (])([a-zA-Z0-9_]*(Client|Server|Request)([^(]|$))#\\1${pkg}.\\2#g" "${gwfile}"
run ${SED?} -i -E "s# (New[a-zA-Z0-9_]*Client\\()# ${pkg}.\\1#g" "${gwfile}"
run ${SED?} -i -E "s|go.etcd.io/etcd|go.etcd.io/etcd/v3|g" "${gwfile}"
run ${SED?} -i -E "s|go.etcd.io/etcd/v3/api|go.etcd.io/etcd/api/v3|g" "${gwfile}"
run ${SED?} -i -E "s|go.etcd.io/etcd/v3/server|go.etcd.io/etcd/server/v3|g" "${gwfile}"

run go fmt "${gwfile}"

gwdir="${pkgpath}/gw/"
Expand All @@ -129,25 +139,25 @@ for pb in api/etcdserverpb/rpc server/etcdserver/api/v3lock/v3lockpb/v3lock serv
# import (
# + protov1 "github.com/golang/protobuf/proto"
# +
run sed -i -E "s|import \(|import \(\n\tprotov1 \"github.com/golang/protobuf/proto\"\n|g" "${gwfile}"
run ${SED?} -i -E "s|import \(|import \(\n\tprotov1 \"github.com/golang/protobuf/proto\"\n|g" "${gwfile}"

# Changes something like below,
# - return msg, metadata, err
# + return protov1.MessageV2(msg), metadata, err
run sed -i -E "s|return msg, metadata, err|return protov1.MessageV2\(msg\), metadata, err|g" "${gwfile}"
run ${SED?} -i -E "s|return msg, metadata, err|return protov1.MessageV2\(msg\), metadata, err|g" "${gwfile}"

# Changes something like below,
# - if err := marshaler.NewDecoder(newReader()).Decode(&protoReq); err != nil && err != io.EOF {
# + if err := marshaler.NewDecoder(newReader()).Decode(protov1.MessageV2(&protoReq)); err != nil && err != io.EOF {
run sed -i -E "s|Decode\(\&protoReq\)|Decode\(protov1\.MessageV2\(\&protoReq\)\)|g" "${gwfile}"
run ${SED?} -i -E "s|Decode\(\&protoReq\)|Decode\(protov1\.MessageV2\(\&protoReq\)\)|g" "${gwfile}"

# Changes something like below,
# - forward_Lease_LeaseKeepAlive_0(annotatedContext, mux, outboundMarshaler, w, req, func() (proto.Message, error) { return resp.Recv() }, mux.GetForwardResponseOptions()...)
# + forward_Lease_LeaseKeepAlive_0(annotatedContext, mux, outboundMarshaler, w, req, func() (proto.Message, error) {
# + m1, err := resp.Recv()
# + return protov1.MessageV2(m1), err
# + }, mux.GetForwardResponseOptions()...)
run sed -i -E "s|return resp.Recv\(\)|\n\t\t\tm1, err := resp.Recv\(\)\n\t\t\treturn protov1.MessageV2\(m1\), err\n\t\t|g" "${gwfile}"
run ${SED?} -i -E "s|return resp.Recv\(\)|\n\t\t\tm1, err := resp.Recv\(\)\n\t\t\treturn protov1.MessageV2\(m1\), err\n\t\t|g" "${gwfile}"

run go fmt "${gwfile}"
done
Expand All @@ -168,7 +178,7 @@ title: API reference
This API reference is autogenerated from the named \`.proto\` files." || exit 2

# remove the first 3 lines of the doc as an empty --title adds '### ' to the top of the file.
run sed -i -e 1,3d ${API_REFERENCE_FILE}
run ${SED?} -i -e 1,3d ${API_REFERENCE_FILE}

# API reference: concurrency
API_REFERENCE_CONCURRENCY_FILE="Documentation/dev-guide/api_concurrency_reference_v3.md"
Expand All @@ -182,7 +192,7 @@ title: \"API reference: concurrency\"
This API reference is autogenerated from the named \`.proto\` files." || exit 2

# remove the first 3 lines of the doc as an empty --title adds '### ' to the top of the file.
run sed -i -e 1,3d ${API_REFERENCE_CONCURRENCY_FILE}
run ${SED?} -i -e 1,3d ${API_REFERENCE_CONCURRENCY_FILE}

log_success "protodoc is finished."
log_warning -e "\\nThe API references have NOT been automatically published on the website."
Expand Down

0 comments on commit 649b365

Please sign in to comment.