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

     1  #!/bin/bash
     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  set -e
    21  
    22  YELLOW='\033[1;33m'
    23  NC='\033[0m'
    24  
    25  # converts a ',' separated string into list.
    26  convert_to_list() {
    27      old_iFS="$IFS"
    28      IFS=","
    29      IFS=${IFS:0:1} # this is useful to format your code with tabs
    30      list=( $1 )
    31      IFS="$old_iFS"
    32  }
    33  
    34  # Execute gauge --iplugins=$@nstall <plugin> for a provided list
    35  install_plugins() {
    36      for plugin in $@
    37      do
    38          echo "Installing plugin $plugin ..."
    39          $prefix/bin/gauge install $plugin
    40      done
    41  }
    42  
    43  # Install all the plugins in interactive mode.
    44  install_plugins_interactively() {
    45      plugins_list=( )
    46      if [[ -z "$GAUGE_PLUGINS" ]]; then
    47          echo "Enter comma(',') separated list of plugins which you would like to install :- "
    48          read -e plugins
    49          if [[ ! -z $plugins ]]; then
    50              convert_to_list $plugins
    51              plugins_list=( ${list[@]} ${plugins_list[@]} )
    52              plugins_list=($(echo "${plugins_list[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
    53          fi
    54      else
    55          convert_to_list $GAUGE_PLUGINS
    56          plugins_list=( ${list[@]} ${plugins_list[@]} )
    57          plugins_list=($(echo "${plugins_list[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
    58      fi
    59      install_plugins "${plugins_list[@]}"
    60  }
    61  
    62  # Install plugins mentioned in $GAUGE_PLUGINS
    63  install_plugins_noninteractively() {
    64      plugins_list=( )
    65      if [[ ! -z "$GAUGE_PLUGINS" ]]; then
    66          convert_to_list $GAUGE_PLUGINS
    67          plugins_list=( ${list[@]} ${plugins_list[@]} )
    68          plugins_list=($(echo "${plugins_list[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
    69      fi
    70      install_plugins "${plugins_list[@]}"
    71  }
    72  
    73  # Find absolute path
    74  get_absolute_path (){
    75      [[ -d $1 ]] && { cd "$1"; echo "$(pwd -P)"; } ||
    76      { cd "$(dirname "$1")" || exit 1; echo "$(pwd -P)/$(basename "$1")"; }
    77  }
    78  
    79  # Set GAUGE binaries to environment variable
    80  add_gauge_in_path() {
    81      # ensure gauge is on PATH
    82      if [[ "$(which gauge)" != $prefix/bin ]]; then
    83          echo "Adding gauge to system path..."
    84          echo "PATH=$PATH:$prefix/bin" >> ~/.profile
    85          echo -e "${YELLOW}gauge has been added to PATH. If you face errors, run '$ source ~/.profile'\n${NC}"
    86      fi
    87  
    88      source ~/.profile
    89  }
    90  
    91  # check permission for nested dir in reverse order and create non existing dir
    92  create_nested_repo() {
    93      parent=$(dirname "$1")
    94      if [[ -d $parent ]]; then
    95          echo "Creating $1"
    96          if [[ -w $parent ]]; then
    97              mkdir -p $1
    98          else
    99              echo "You do not have write permisions for '$parent ."
   100              sudo mkdir -p $1
   101          fi
   102      else
   103          create_nested_repo $parent
   104      fi
   105  }
   106  
   107  
   108  
   109  # Creates installation prefix and configuration dirs if doesn't exist
   110  create_prefix_interactively() {
   111      if  [[ ! -d $prefix ]]; then
   112          create_nested_repo $prefix
   113      fi
   114  }
   115  
   116  # Creates installation prefix and configuration dirs if doesn't exist in non tty mode
   117  create_prefix_noninteractively(){
   118       [[ -d $prefix ]] || echo "Creating $prefix ..." && mkdir -p $prefix
   119  }
   120  
   121  # Give option to change the permission or delete the dir if needed
   122  change_permission_if_needed() {
   123      if [[ -d $HOME/.gauge && ! -w $HOME/.gauge ]]; then
   124          echo "The dir .gauge already exist but was created with eleveted permision."
   125          echo "Enter [1] to change permissions or Enter [2] to delete the dir (By default it will change the permissions)"
   126          read -e choice
   127          group=`id -ng`
   128          case $choice in
   129              1)
   130                  sudo chown -R $USER:$group $HOME/.gauge ;;
   131              2)
   132                  sudo rm -rf ~/.gauge ;;
   133              *)
   134                  sudo chown -R $USER:$group $HOME/.gauge ;;
   135          esac
   136      fi
   137  }
   138  
   139  
   140  # Copy gauge binaries in $prefix dir
   141  copy_gauge_binaries_interactively() {
   142      # check for write permissions and Install gauge, asks for sudo access if not permitted
   143      if [[ ! -w $prefix || $prefix == "/usr/local" ]]; then
   144          echo "You do not have write permissions for $prefix"
   145          echo "Running script as sudo "
   146          sudo cp -rf bin $prefix
   147          echo "Installed gauge binaries at $prefix"
   148      else
   149          cp -rf bin $prefix
   150          echo "Installed gauge binaries at $prefix"
   151      fi
   152  }
   153  
   154  # Copy gauge binaries in $prefix dir
   155  copy_gauge_binaries_noninteractively() {
   156      cp -rf bin $prefix
   157      echo "Installed gauge binaries at $prefix"
   158  }
   159  
   160  # Get last modified timestamp of the file
   161  get_time_stamp() {
   162      if [[ `uname` != "Linux" ]]; then
   163          time_stamp=$(stat -f "%m" $1)
   164      else
   165          time_stamp=$(date +%s -r $1)
   166      fi
   167  }
   168  
   169  # Set prefix for installion in interactive mode
   170  set_prefix_interavctively() {
   171      if [[ -z "$GAUGE_PREFIX" ]]; then
   172          prefix=/usr/local
   173          echo "Installing gauge at $prefix/bin"
   174          echo -e "Enter custom install location :-"
   175          read -e install_location
   176          if [[ ! -z $install_location ]]; then
   177              prefix=$(get_absolute_path ${install_location/\~/$HOME})
   178          fi
   179      else
   180          prefix=$GAUGE_PREFIX
   181          echo "Installing gauge at $prefix/bin"
   182      fi
   183  }
   184  
   185  # Set prefix for installion in noninteractive mode
   186  set_prefix_noninteractively() {
   187      if [[ -z $GAUGE_PREFIX ]]; then
   188          prefix=/usr/local
   189      else
   190          prefix=$GAUGE_PREFIX
   191      fi
   192  }
   193  
   194  show_post_install_messages(){
   195      echo -e "Gauge core successfully installed.\n"
   196      echo -e "We are constantly looking to make Gauge better, and report usage statistics anonymously over time. If you do not want to participate please read instructions https://manpage.getgauge.io/gauge_telemetry_off.html on how to turn it off.\n"
   197  }
   198  
   199  # Install Gauge interactively
   200  install_gauge_interactively() {
   201      config=$HOME/.gauge/config
   202      set_prefix_interavctively
   203      create_prefix_interactively
   204      copy_gauge_binaries_interactively
   205      add_gauge_in_path
   206      show_post_install_messages
   207  }
   208  
   209  # Install gauge noninteractively
   210  install_gauge_noninteractively() {
   211      config=$HOME/.gauge/config
   212      set_prefix_noninteractively
   213      create_prefix_noninteractively
   214      copy_gauge_binaries_noninteractively
   215      add_gauge_in_path
   216      show_post_install_messages
   217  }
   218  
   219  # perform gauge installation in interactives mode
   220  do_interactive_installation() {
   221      install_gauge_interactively
   222      install_plugins_interactively
   223  }
   224  
   225  
   226  # perform gauge installation in non tty mode
   227  do_noninteractive_installation() {
   228      install_gauge_noninteractively
   229      install_plugins_noninteractively
   230  }
   231  
   232  
   233  # Print usage of this script
   234  display_usage() {
   235  	echo -e "On Linux, this script installs gauge and it's plugins.\n\nUsage:\n./install.sh\n\nSet GAUGE_PREFIX env to install gauge at custom location.
   236  Set GAUGE_PLUGINS env to install plugins along with gauge.
   237  Exp:-
   238      GAUGE_PREFIX=my/custom/path ./install.sh
   239      GAUGE_PLUGINS=java,ruby,spectacle ./install.sh
   240      GAUGE_PREFIX=my/custom/path GAUGE_PLUGINS=xml-report,java ./install.sh"
   241  }
   242  
   243  # check whether user has supplied -h or --help . If yes display usage if no diplay usage with an error
   244  if [[ $# != 0 ]]; then
   245      case $@ in
   246          "-h")
   247              display_usage
   248              exit 0 ;;
   249          "--help")
   250              display_usage
   251              exit 0 ;;
   252          "--force")
   253              ForceInstall=true ;;
   254          *)
   255              echo -e "unknown option $@. \n"
   256              display_usage
   257              exit 1 ;;
   258      esac
   259  fi
   260  
   261  # If tty then perform installation in interactive mode.
   262  if [[ "$CONTINUOUS_INTEGRATION" != "true" ]] && [[ "$CI" != "true" ]] && tty -s; then
   263      do_interactive_installation
   264  else
   265      do_noninteractive_installation
   266  fi