github.com/argoproj/argo-cd@v1.8.7/hack/generate-proto.sh (about)

     1  #! /usr/bin/env bash
     2  
     3  # This script auto-generates protobuf related files. It is intended to be run manually when either
     4  # API types are added/modified, or server gRPC calls are added. The generated files should then
     5  # be checked into source control.
     6  
     7  set -x
     8  set -o errexit
     9  set -o nounset
    10  set -o pipefail
    11  
    12  # output tool versions
    13  protoc --version
    14  swagger version
    15  jq --version
    16  
    17  PROJECT_ROOT=$(cd $(dirname ${BASH_SOURCE})/..; pwd)
    18  CODEGEN_PKG=${CODEGEN_PKG:-$(cd ${PROJECT_ROOT}; ls -d -1 ./vendor/k8s.io/code-generator 2>/dev/null || echo ../code-generator)}
    19  PATH="${PROJECT_ROOT}/dist:${PATH}"
    20  MOD_ROOT=${GOPATH}/pkg/mod
    21  
    22  . ${PROJECT_ROOT}/hack/versions.sh
    23  
    24  export GO111MODULE=off
    25  
    26  # protobuf tooling required to build .proto files from go annotations from k8s-like api types
    27  go build -i -o dist/go-to-protobuf ./vendor/k8s.io/code-generator/cmd/go-to-protobuf
    28  go build -i -o dist/protoc-gen-gogo ./vendor/k8s.io/code-generator/cmd/go-to-protobuf/protoc-gen-gogo
    29  
    30  # Generate pkg/apis/<group>/<apiversion>/(generated.proto,generated.pb.go)
    31  # NOTE: any dependencies of our types to the k8s.io apimachinery types should be added to the
    32  # --apimachinery-packages= option so that go-to-protobuf can locate the types, but prefixed with a
    33  # '-' so that go-to-protobuf will not generate .proto files for it.
    34  PACKAGES=(
    35      github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1
    36  )
    37  APIMACHINERY_PKGS=(
    38      +k8s.io/apimachinery/pkg/util/intstr
    39      +k8s.io/apimachinery/pkg/api/resource
    40      +k8s.io/apimachinery/pkg/runtime/schema
    41      +k8s.io/apimachinery/pkg/runtime
    42      k8s.io/apimachinery/pkg/apis/meta/v1
    43      k8s.io/api/core/v1
    44  )
    45  
    46  ${PROJECT_ROOT}/dist/go-to-protobuf \
    47      --go-header-file=${PROJECT_ROOT}/hack/custom-boilerplate.go.txt \
    48      --packages=$(IFS=, ; echo "${PACKAGES[*]}") \
    49      --apimachinery-packages=$(IFS=, ; echo "${APIMACHINERY_PKGS[*]}") \
    50      --proto-import=./vendor
    51  
    52  # Either protoc-gen-go, protoc-gen-gofast, or protoc-gen-gogofast can be used to build
    53  # server/*/<service>.pb.go from .proto files. golang/protobuf and gogo/protobuf can be used
    54  # interchangeably. The difference in the options are:
    55  # 1. protoc-gen-go - official golang/protobuf
    56  #go build -i -o dist/protoc-gen-go ./vendor/github.com/golang/protobuf/protoc-gen-go
    57  #GOPROTOBINARY=go
    58  # 2. protoc-gen-gofast - fork of golang golang/protobuf. Faster code generation
    59  #go build -i -o dist/protoc-gen-gofast ./vendor/github.com/gogo/protobuf/protoc-gen-gofast
    60  #GOPROTOBINARY=gofast
    61  # 3. protoc-gen-gogofast - faster code generation and gogo extensions and flexibility in controlling
    62  # the generated go code (e.g. customizing field names, nullable fields)
    63  go build -i -o dist/protoc-gen-gogofast ./vendor/github.com/gogo/protobuf/protoc-gen-gogofast
    64  GOPROTOBINARY=gogofast
    65  
    66  # protoc-gen-grpc-gateway is used to build <service>.pb.gw.go files from from .proto files
    67  go build -i -o dist/protoc-gen-grpc-gateway ./vendor/github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
    68  # protoc-gen-swagger is used to build swagger.json
    69  go build -i -o dist/protoc-gen-swagger ./vendor/github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger
    70  
    71  # Generate server/<service>/(<service>.pb.go|<service>.pb.gw.go)
    72  PROTO_FILES=$(find $PROJECT_ROOT \( -name "*.proto" -and -path '*/server/*' -or -path '*/reposerver/*' -and -name "*.proto" \) | sort)
    73  for i in ${PROTO_FILES}; do
    74      GOOGLE_PROTO_API_PATH=${MOD_ROOT}/github.com/grpc-ecosystem/grpc-gateway@${grpc_gateway_version}/third_party/googleapis
    75      GOGO_PROTOBUF_PATH=${PROJECT_ROOT}/vendor/github.com/gogo/protobuf
    76      protoc \
    77          -I${PROJECT_ROOT} \
    78          -I/usr/local/include \
    79          -I./vendor \
    80          -I$GOPATH/src \
    81          -I${GOOGLE_PROTO_API_PATH} \
    82          -I${GOGO_PROTOBUF_PATH} \
    83          --${GOPROTOBINARY}_out=plugins=grpc:$GOPATH/src \
    84          --grpc-gateway_out=logtostderr=true:$GOPATH/src \
    85          --swagger_out=logtostderr=true:. \
    86          $i
    87  done
    88  
    89  # collect_swagger gathers swagger files into a subdirectory
    90  collect_swagger() {
    91      SWAGGER_ROOT="$1"
    92      EXPECTED_COLLISIONS="$2"
    93      SWAGGER_OUT="${PROJECT_ROOT}/assets/swagger.json"
    94      PRIMARY_SWAGGER=`mktemp`
    95      COMBINED_SWAGGER=`mktemp`
    96  
    97      cat <<EOF > "${PRIMARY_SWAGGER}"
    98  {
    99    "swagger": "2.0",
   100    "info": {
   101      "title": "Consolidate Services",
   102      "description": "Description of all APIs",
   103      "version": "version not set"
   104    },
   105    "paths": {}
   106  }
   107  EOF
   108  
   109      rm -f "${SWAGGER_OUT}"
   110  
   111      find "${SWAGGER_ROOT}" -name '*.swagger.json' -exec swagger mixin -c "${EXPECTED_COLLISIONS}" "${PRIMARY_SWAGGER}" '{}' \+ > "${COMBINED_SWAGGER}"
   112      jq -r 'del(.definitions[].properties[]? | select(."$ref"!=null and .description!=null).description) | del(.definitions[].properties[]? | select(."$ref"!=null and .title!=null).title)' "${COMBINED_SWAGGER}" > "${SWAGGER_OUT}"
   113  
   114      /bin/rm "${PRIMARY_SWAGGER}" "${COMBINED_SWAGGER}"
   115  }
   116  
   117  # clean up generated swagger files (should come after collect_swagger)
   118  clean_swagger() {
   119      SWAGGER_ROOT="$1"
   120      find "${SWAGGER_ROOT}" -name '*.swagger.json' -delete
   121  }
   122  
   123  echo "If additional types are added, the number of expected collisions may need to be increased"
   124  EXPECTED_COLLISION_COUNT=33
   125  collect_swagger server ${EXPECTED_COLLISION_COUNT}
   126  clean_swagger server
   127  clean_swagger reposerver
   128  clean_swagger controller