github.com/bazelbuild/rules_webtesting@v0.2.0/web/internal/extract.sh (about)

     1  #!/bin/bash
     2  # Copyright 2018 Google Inc.
     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  ################################################################################
    17  #
    18  
    19  wrap() {
    20    out=$("$@" 2>&1)
    21    ret="$?"
    22    if [[ "$ret" -ne 0 ]]; then
    23      >&2 printf "$out"
    24      >&2 printf "\n"
    25      exit "$ret"
    26    fi
    27  }
    28  
    29  ARCHIVE=$1
    30  OUT_DIR=$2
    31  STRIP_PREFIX=$3
    32  
    33  mkdir -p "${OUT_DIR}"
    34  
    35  BASENAME=$(basename "${ARCHIVE}")
    36  
    37  if [[ "${BASENAME}" == *.deb ]]; then
    38    wrap dpkg -x "${ARCHIVE}" "${OUT_DIR}"
    39  elif [[ "${BASENAME}" == *.tar ]]; then
    40    wrap tar xf "${ARCHIVE}" -C "${OUT_DIR}"
    41  elif [[ "${BASENAME}" == *.tar.bz2 || "${BASENAME}" == *.tbz2 ]]; then
    42    wrap tar xjf "${ARCHIVE}" -C "${OUT_DIR}"
    43  elif [[ "${BASENAME}" == *.tar.gz || "${BASENAME}" == *.tgz ]]; then
    44    wrap tar xzf "${ARCHIVE}" -C "${OUT_DIR}"
    45  elif [[ "${BASENAME}" == *.tar.Z ]]; then
    46    wrap tar xZf "${ARCHIVE}" -C "${OUT_DIR}"
    47  elif [[ "${BASENAME}" == *.zip ]]; then
    48    wrap unzip "${ARCHIVE}" -d "${OUT_DIR}"
    49  else
    50    >&2 printf "${ARCHIVE} is not a supported file type."
    51    exit -1
    52  fi
    53  
    54  if [[ ! -z "${STRIP_PREFIX}" ]]; then
    55    cd "${OUT_DIR}"
    56    # Intentionally not quoted so that globbing in STRIP_PREFIX works.
    57    mv ${STRIP_PREFIX}/* .
    58  fi