kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/release/package_release.sh (about)

     1  #!/bin/bash
     2  # Copyright 2015 The Kythe Authors. All rights reserved.
     3  #
     4  # Licensed under the Apache License, Version 2.0 (the "License");
     5  # you may not use this file except in compliance with the License.
     6  # You may obtain a copy of the License at
     7  #
     8  #   http://www.apache.org/licenses/LICENSE-2.0
     9  #
    10  # Unless required by applicable law or agreed to in writing, software
    11  # distributed under the License is distributed on an "AS IS" BASIS,
    12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  # See the License for the specific language governing permissions and
    14  # limitations under the License.
    15  
    16  # Script to package a release tar and create its associated .sha256 checksum.
    17  #
    18  # Usage: package_release.sh <shasum_tool> <path-to-output-tar.gz> [package contents]
    19  #
    20  # In the simplest case, each file given will be placed in the root of the
    21  # resulting archive.  The --relpath, --path, and --cp flags change this behavior
    22  # so that file paths can be structured.
    23  #
    24  #   --verbose: Log each action as it is taken.
    25  #   --path <path>: Each file is copied to ARCHIVE_ROOT/<path>/$(basename file).
    26  #   --relpaths <prefix>: Strip $GENBIR, $BINDIR and then <prefix> from each
    27  #                        file's path.  The resulting path is used for the file
    28  #                        inside of the archive.  This combines with --path to
    29  #                        change the root of the resulting file path.
    30  #   --cp <path> <path>: Copy the first file to the archive using exactly the
    31  #                       second path.
    32  #   --exclude <glob>: Exclude all further files matching the given glob.
    33  #
    34  # Example:
    35  #   BINDIR=bazel-bin/ \
    36  #     package_release.sh /path/to/sha /tmp/b.tar.gz README.adoc LICENSE \
    37  #       --path some/path/for/docs kythe/docs/kythe-{overview,storage}.txt \
    38  #       --relpaths kythe/docs bazel-bin/kythe/docs/schema/schema.html \
    39  #       --cp CONTRIBUTING.md kythe/docs/how-to-contribute.md
    40  #
    41  #   Resulting tree in /tmp/b.tar.gz:
    42  #     README.adoc
    43  #     LICENSE
    44  #     kythe/docs/
    45  #       kythe-overview.txt
    46  #       kythe-storage.txt
    47  #       schema.html
    48  #       how-to-contribute.md
    49  set -e
    50  
    51  SHASUM_TOOL="$1"
    52  shift
    53  
    54  OUT="$1"
    55  shift
    56  
    57  PBASE="$OUT.dir/$(basename "$OUT" .tar.gz)"
    58  P=$PBASE
    59  
    60  mkdir -p "$PBASE"
    61  # shellcheck disable=SC2064
    62  trap "rm -rf '$PWD/$OUT.dir'" EXIT ERR INT
    63  
    64  VERBOSE=
    65  function log() {
    66    if [[ -z "$VERBOSE" ]]; then
    67      return
    68    fi
    69    echo "$@" >&2
    70  }
    71  
    72  EXCLUDE=()
    73  
    74  while [[ $# -gt 0 ]]; do
    75    case "$1" in
    76      --verbose)
    77        VERBOSE=true
    78        ;;
    79      --relpaths)
    80        RELPATHS=$2
    81        shift
    82        ;;
    83      --path)
    84        P="$PBASE/$2"
    85        mkdir -p "$P"
    86        shift
    87        ;;
    88      --cp)
    89        mkdir -p "$PBASE/$(dirname "$3")"
    90        cp "$2" "$PBASE/$3"
    91        shift 2
    92        ;;
    93      --exclude)
    94        EXCLUDE+=("$2")
    95        shift
    96        ;;
    97      *)
    98        skip=
    99        for exclude in "${EXCLUDE[@]}"; do
   100          if [[ "$1" =~ $exclude ]]; then
   101            skip=true
   102            break
   103          fi
   104        done
   105        if [[ -n "$skip" ]]; then
   106          log "Excluding $1"
   107        elif [[ -z "$RELPATHS" ]]; then
   108          log "Copying $1 to $P"
   109          cp "$1" "$P"/
   110        else
   111          rp="${1#"$GENDIR"/}"
   112          rp="${rp#"$BINDIR"/}"
   113          rp="$(dirname "${rp#"$RELPATHS"/}")"
   114          mkdir -p "$P/$rp"
   115          log "Copying $1 to $P/$rp"
   116          cp "$1" "$P/$rp"
   117        fi
   118        ;;
   119    esac
   120    shift
   121  done
   122  
   123  tar czf "$OUT" -C "$OUT.dir" "$(basename "$PBASE")"
   124  
   125  $SHASUM_TOOL "$OUT" > "$OUT.sha256"
   126  
   127  echo "Release outputs are ${OUT} and ${OUT}.sha256"