github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/util/interval/generic/gen.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  set -eu -o pipefail
     4  
     5  if [ $# -ne 2 ]; then
     6      echo "Usage: $0 <type> <package>"
     7      exit 1
     8  fi
     9  
    10  type=$1
    11  pkg=$2
    12  internal_pkg=$(dirname "$0")/internal
    13  templates=(
    14      interval_btree_tmpl.go
    15      interval_btree_tmpl_test.go
    16  )
    17  
    18  # Make filename prefix from type. Lower case and strip pointer.
    19  dst_prefix=$(echo ${type} | awk '{print tolower($0)}' | sed 's/*//')
    20  
    21  # Add code generation comment to beginning of files.
    22  gen_header_comment="// Code generated by go_generics. DO NOT EDIT.\n"
    23  
    24  # Generate files:
    25  # 1. strip internal/contract.go of its comments and package declaration. This
    26  #    script originally used github.com/mmatczuk/go_generics/cmd/go_merge to
    27  #    merge files, but that passes the AST through go/ast.MergePackageFiles,
    28  #    which causes all "unassociated" comments (those not tied to AST nodes)
    29  #    to be stripped due to https://github.com/golang/go/issues/20744.
    30  # 2. for each template file, concatenate the code gen comment, the template
    31  #    file, and the stripped contract file, remove any build tags, and pass
    32  #    this all to go_generics.
    33  # 3. crlfmt the result because go_generics might re-order imports.
    34  # 4. remove the temporary stripped contract file.
    35  grep -vE '(//|package)' ${internal_pkg}/contract.go > ${internal_pkg}/contract_stripped.tmp
    36  for template in "${templates[@]}" ; do
    37      dst=${dst_prefix}_${template//_tmpl/}
    38      echo -e ${gen_header_comment} \
    39      | cat - ${internal_pkg}/${template} ${internal_pkg}/contract_stripped.tmp \
    40      | grep -v '// +build ignore' \
    41      | go_generics -i /dev/stdin -t T=${type} -p ${pkg} -o ${dst}
    42      crlfmt -w -diff=false ${dst}
    43  done
    44  rm ${internal_pkg}/contract_stripped.tmp