github.com/getgauge/gauge@v1.6.9/build/download.sh (about)

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