github.com/apache/beam/sdks/v2@v2.48.2/go/scripts/genproto.sh (about)

     1  #!/bin/bash
     2  # Licensed to the Apache Software Foundation (ASF) under one
     3  # or more contributor license agreements.  See the NOTICE file
     4  # distributed with this work for additional information
     5  # regarding copyright ownership.  The ASF licenses this file
     6  # to you under the Apache License, Version 2.0 (the
     7  # "License"); you may not use this file except in compliance
     8  # with the License.  You may obtain a copy of the License at
     9  #
    10  #   http://www.apache.org/licenses/LICENSE-2.0
    11  #
    12  # Unless required by applicable law or agreed to in writing,
    13  # software distributed under the License is distributed on an
    14  # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15  # KIND, either express or implied.  See the License for the
    16  # specific language governing permissions and limitations
    17  # under the License.
    18  
    19  set -e
    20  
    21  read -r -d '\0' LICENSE << EOM
    22  //
    23  // Licensed to the Apache Software Foundation (ASF) under one
    24  // or more contributor license agreements.  See the NOTICE file
    25  // distributed with this work for additional information
    26  // regarding copyright ownership.  The ASF licenses this file
    27  // to you under the Apache License, Version 2.0 (the
    28  // "License"); you may not use this file except in compliance
    29  // with the License.  You may obtain a copy of the License at
    30  //
    31  //     http://www.apache.org/licenses/LICENSE-2.0
    32  //
    33  // Unless required by applicable law or agreed to in writing, software
    34  // distributed under the License is distributed on an "AS IS" BASIS,
    35  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    36  // See the License for the specific language governing permissions and
    37  // limitations under the License.
    38  \0
    39  EOM
    40  
    41  if [[ -z "$(which protoc)" ]]; then
    42    echo "protoc not found on path"
    43    exit 1
    44  fi
    45  
    46  SCRIPT_DIR="$( realpath "$( dirname "${BASH_SOURCE[0]}" )" )"
    47  if [[ -z $SCRIPT_DIR ]]; then
    48    echo "unable to resolve path to script"
    49    exit 1
    50  fi
    51  
    52  SDK_PATH="$( realpath "$(dirname $SCRIPT_DIR)/.." )"
    53  if [[ -z "$SDK_PATH" ]]; then
    54    echo "unable to resolve path to project root"
    55    exit 1
    56  fi
    57  
    58  PROJECT_ROOT="$(realpath "$(dirname $SCRIPT_DIR)/../..")"
    59  if [[ -z "$PROJECT_ROOT" ]]; then
    60    echo "unable to resolve path to project root"
    61    exit 1
    62  fi
    63  
    64  if [[ -z "$(which go)" ]]; then
    65    echo "go runtime missing"
    66    exit 1
    67  fi
    68  
    69  if [[ -z "$GOBIN" ]]; then
    70    export GOBIN="$(go env GOPATH)/bin"
    71  fi
    72  
    73  export PATH="$PATH:$GOBIN"
    74  GEN_DIR=$PWD
    75  
    76  # NB: Keep these two versions in sync with those defined in
    77  #     the go.mod.
    78  go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@ebf6a4b
    79  go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.27.1
    80  
    81  function insert_license_header() {
    82    local depth="$1"
    83    # protoc-gen-go-grpc does not yet output comments from the original
    84    # proto file so we need to manually add the license header.
    85    while IFS= read -d $'\0' -r file ; do
    86      tmp_file=$(mktemp)
    87      echo "$LICENSE" > $tmp_file
    88      cat $file >> $tmp_file
    89      mv $tmp_file $file
    90    done < <(find $GEN_DIR $depth -iname "*grpc.pb.go" -print0)
    91  }
    92  
    93  function gen_go_sdk_protos() {
    94    LIBRARY_PATH="${PWD##${SDK_PATH}/}"
    95    PROTOS="$LIBRARY_PATH/*.proto"
    96  
    97    cd "$SDK_PATH"
    98  
    99    # TODO: make this more data driven.
   100    PKG_MAP=Mbeam_fn_api.proto=github.com/apache/beam/sdks/go/pkg/beam/model/fnexecution_v1
   101  
   102    declare -a TO_INC=(
   103      "."
   104      "$PROJECT_ROOT/model/job-management/src/main/proto/"
   105      "$PROJECT_ROOT/model/pipeline/src/main/proto/"
   106      "$PROJECT_ROOT/model/fn-execution/src/main/proto/"
   107    )
   108  
   109    declare -a INCLUDES=()
   110    for package in "${TO_INC[@]}"
   111    do
   112      INCLUDES+=("-I${package}")
   113    done
   114  
   115    protoc \
   116      "${INCLUDES[@]}" \
   117      --go_opt=module=github.com/apache/beam/sdks/v2 \
   118      --go-grpc_opt=module=github.com/apache/beam/sdks/v2 \
   119      --go_out=$PKG_MAP:. \
   120      --go-grpc_out=. \
   121      $PROTOS
   122  
   123    insert_license_header '-d 1'
   124  }
   125  
   126  function gen_beam_model_protos() {
   127    cd "$PROJECT_ROOT"
   128  
   129    declare -a TO_GEN=(
   130      'model/job-management/src/main/proto/org/apache/beam/model/job_management/v1/*.proto'
   131      'model/pipeline/src/main/proto/org/apache/beam/model/pipeline/v1/*.proto'
   132      'model/fn-execution/src/main/proto/org/apache/beam/model/fn_execution/v1/*.proto'
   133    )
   134  
   135    declare -a INCLUDES=()
   136    for package in "${TO_GEN[@]}"
   137    do
   138      INCLUDES+=("-I${package%/org*}")
   139    done
   140  
   141    for package in "${TO_GEN[@]}"
   142    do
   143      protoc \
   144        "${INCLUDES[@]}" \
   145        --go_opt=module=github.com/apache/beam/sdks/v2 \
   146        --go-grpc_opt=module=github.com/apache/beam/sdks/v2 \
   147        --go_out="$PROJECT_ROOT/sdks" \
   148        --go-grpc_out="$PROJECT_ROOT/sdks" \
   149        $package
   150    done
   151  
   152    insert_license_header
   153  }
   154  
   155  if [[ $1 == "model" ]]; then
   156    gen_beam_model_protos
   157  else
   158    gen_go_sdk_protos
   159  fi