github.com/ezbuy/gauge@v0.9.4-0.20171013092048-7ac5bd3931cd/build/download.sh (about)

     1  #!/bin/sh
     2  
     3  # Copyright 2015 ThoughtWorks, Inc.
     4  
     5  # This file is part of Gauge.
     6  
     7  # Gauge is free software: you can redistribute it and/or modify
     8  # it under the terms of the GNU General Public License as published by
     9  # the Free Software Foundation, either version 3 of the License, or
    10  # (at your option) any later version.
    11  
    12  # Gauge is distributed in the hope that it will be useful,
    13  # but WITHOUT ANY WARRANTY; without even the implied warranty of
    14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15  # GNU General Public License for more details.
    16  
    17  # You should have received a copy of the GNU General Public License
    18  # along with Gauge.  If not, see <http://www.gnu.org/licenses/>.
    19  
    20  download() {
    21      CWD="$(ensure pwd)"
    22      # Create tmp directory
    23      tmp="$(ensure mktemp -d)"
    24      ensure cd "$tmp"
    25  
    26      # Download latest Gauge zip installer
    27      url="$(ensure curl -Ss https://api.github.com/repos/getgauge/gauge/releases/latest \
    28      | grep "browser_download_url.*$OS.$ARCH.zip" \
    29      | cut -d : -f 2,3 \
    30      | tr -d \")"
    31  
    32      say "Downloading binary from URL:$url"
    33      ensure curl -L -o gauge.zip $url
    34      verbose_say "Downloaded the binary to dir:$tmp"
    35  
    36      # unzip and copy the binary to the original directory
    37      ensure unzip -q gauge.zip
    38  
    39      verbose_say "Copying the binary to $LOCATION"
    40      if [ -w $LOCATION ]; then
    41          mkdir -p "$LOCATION"
    42          ensure cp ./bin/gauge "$LOCATION/"
    43      else
    44          echo "You do not have write permision for $LOCATION. Trying with sudo."
    45          sudo cp ./bin/gauge "$LOCATION/"
    46      fi
    47  
    48      ensure cd "$CWD"
    49      verbose_say "Cleaning up..."
    50      ensure rm -rf "$tmp"
    51  
    52      if $_ansi_escapes_are_valid; then
    53          printf "\33[1mDownloaded the binary to '$LOCATION'.\33[0m\n" 1>&2
    54          printf "\33[1mMake sure the above path is available in 'PATH' environment variable.\33[0m\n"
    55      else
    56          say "Downloaded the binary to '$LOCATION'."
    57          say "Make sure the above path is available in 'PATH' environment variable."
    58      fi
    59  }
    60  
    61  set_os_architecture() {
    62      verbose_say "Detecting architecture"
    63      local _ostype="$(uname -s)"
    64      local _cputype="$(uname -m)"
    65  
    66      verbose_say "uname -s reports: $_ostype"
    67      verbose_say "uname -m reports: $_cputype"
    68  
    69      if [ "$_ostype" = Darwin -a "$_cputype" = i386 ]; then
    70          # Darwin `uname -s` lies
    71          if sysctl hw.optional.x86_64 | grep -q ': 1'; then
    72              local _cputype=x86_64
    73          fi
    74      fi
    75  
    76      case "$_ostype" in
    77  
    78          Linux)
    79              local _ostype=linux
    80              ;;
    81  
    82          FreeBSD)
    83              local _ostype=linux
    84              ;;
    85  
    86          DragonFly)
    87              local _ostype=linux
    88              ;;
    89  
    90          Darwin)
    91              local _ostype=darwin
    92              ;;
    93          *)
    94              err "Unknown OS type: $_ostype"
    95              ;;
    96  
    97      esac
    98  
    99      case "$_cputype" in
   100  
   101          i386 | i486 | i686 | i786 | x86)
   102              local _cputype=x86
   103              ;;
   104          x86_64 | x86-64 | x64 | amd64)
   105              local _cputype=x86_64
   106              ;;
   107          *)
   108              err "Unknown CPU type: $_cputype"
   109              ;;
   110      esac
   111  
   112      verbose_say "OS is $_ostype"
   113      verbose_say "Architecture is $_cputype"
   114      ARCH="$_cputype"
   115      OS="$_ostype"
   116  }
   117  
   118  handle_cmd_line_args() {
   119      LOCATION="/usr/local/bin"
   120      for _arg in "$@"; do
   121          case "${_arg%%=*}" in
   122              --verbose)
   123                  VERBOSE=true
   124                  ;;
   125              --location)
   126                  if is_value_arg "$_arg" "location"; then
   127                      LOCATION="$(get_value_arg "$_arg")"
   128                  fi
   129                  ;;
   130          esac
   131      done
   132  }
   133  
   134  is_value_arg() {
   135      local _arg="$1"
   136      local _name="$2"
   137  
   138      echo "$_arg" | grep -q -- "--$_name="
   139      return $?
   140  }
   141  
   142  get_value_arg() {
   143      local _arg="$1"
   144  
   145      echo "$_arg" | cut -f2 -d=
   146  }
   147  
   148  assert_cmds_available() {
   149      need_cmd echo
   150      need_cmd curl
   151      need_cmd mktemp
   152      need_cmd mkdir
   153      need_cmd pwd
   154      need_cmd grep
   155      need_cmd cut
   156      need_cmd tr
   157      need_cmd uname
   158      need_cmd rm
   159      need_cmd unzip
   160  }
   161  
   162  need_cmd() {
   163      if ! command -v "$1" > /dev/null 2>&1
   164      then err "need '$1' (command not found)"
   165      fi
   166  }
   167  
   168  ensure() {
   169      "$@"
   170      if [ $? != 0 ]; then err "command failed: $*"; fi
   171  }
   172  
   173  say() {
   174      echo "$1"
   175  }
   176  
   177  verbose_say() {
   178      if [ "$VERBOSE" = true ]; then
   179          say "[DEBUG] $1"
   180      fi
   181  }
   182  
   183  err() {
   184      say "$1" >&2
   185      exit 1
   186  }
   187  
   188  main() {
   189      assert_cmds_available
   190      local _ansi_escapes_are_valid=false
   191      if [ -t 2 ]; then
   192          if [ "${TERM+set}" = 'set' ]; then
   193              case "$TERM" in
   194                  xterm*|rxvt*|urxvt*|linux*|vt*)
   195                      _ansi_escapes_are_valid=true
   196                  ;;
   197              esac
   198          fi
   199      fi
   200      handle_cmd_line_args "$@"
   201      set_os_architecture
   202      download
   203  }
   204  
   205  main "$@"