golang.org/x/build@v0.0.0-20240506185731-218518f32b70/env/wasip1-wasm-wasmer/install.sh (about)

     1  #!/bin/sh
     2  
     3  # This install script is intended to download and install the latest available
     4  # release of Wasmer.
     5  # It attempts to identify the current platform and an error will be thrown if
     6  # the platform is not supported.
     7  #
     8  # Environment variables:
     9  # - WASMER_DIR (optional): defaults to $HOME/.wasmer
    10  #
    11  # You can install using this script:
    12  # $ curl https://raw.githubusercontent.com/wasmerio/wasmer-install/master/install.sh | sh
    13  
    14  # Installer script inspired by:
    15  #  1) https://raw.githubusercontent.com/golang/dep/master/install.sh
    16  #  2) https://sh.rustup.rs
    17  #  3) https://yarnpkg.com/install.sh
    18  #  4) https://raw.githubusercontent.com/brainsik/virtualenv-burrito/master/virtualenv-burrito.sh
    19  
    20  reset="\033[0m"
    21  red="\033[31m"
    22  green="\033[32m"
    23  yellow="\033[33m"
    24  white="\033[37m"
    25  bold="\e[1m"
    26  dim="\e[2m"
    27  
    28  RELEASES_URL="https://github.com/wasmerio/wasmer/releases"
    29  WAPM_RELEASES_URL="https://github.com/wasmerio/wapm-cli/releases"
    30  
    31  WASMER_VERBOSE="verbose"
    32  if [ -z "$WASMER_INSTALL_LOG" ]; then
    33    WASMER_INSTALL_LOG="$WASMER_VERBOSE"
    34  fi
    35  
    36  wasmer_download_json() {
    37    url="$2"
    38  
    39    # echo "Fetching $url.."
    40    if test -x "$(command -v curl)"; then
    41      response=$(curl -s -L -w 'HTTPSTATUS:%{http_code}' -H 'Accept: application/json' "$url")
    42      body=$(echo "$response" | sed -e 's/HTTPSTATUS\:.*//g')
    43      code=$(echo "$response" | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
    44    elif test -x "$(command -v wget)"; then
    45      temp=$(mktemp)
    46      body=$(wget -q --header='Accept: application/json' -O - --server-response "$url" 2>"$temp")
    47      code=$(awk '/^  HTTP/{print $2}' <"$temp" | tail -1)
    48      rm "$temp"
    49    else
    50      wasmer_error "Neither curl nor wget was available to perform http requests"
    51      return 1
    52    fi
    53    if [ "$code" != 200 ]; then
    54      wasmer_error "File download failed with code $code"
    55      return 1
    56    fi
    57  
    58    eval "$1='$body'"
    59    return 0
    60  }
    61  
    62  wasmer_download_file() {
    63    url="$1"
    64    destination="$2"
    65  
    66    # echo "Fetching $url.."
    67    if test -x "$(command -v curl)"; then
    68      if [ "$WASMER_INSTALL_LOG" = "$WASMER_VERBOSE" ]; then
    69        code=$(curl --progress-bar -w '%{http_code}' -L "$url" -o "$destination")
    70        printf "\033[K\n\033[1A"
    71      else
    72        code=$(curl -s -w '%{http_code}' -L "$url" -o "$destination")
    73      fi
    74    elif test -x "$(command -v wget)"; then
    75      if [ "$WASMER_INSTALL_LOG" = "$WASMER_VERBOSE" ]; then
    76        code=$(wget --show-progress --progress=bar:force:noscroll -q -O "$destination" --server-response "$url" 2>&1 | awk '/^  HTTP/{print $2}' | tail -1)
    77        printf "\033[K\n\033[1A"
    78      else
    79        code=$(wget --quiet -O "$destination" --server-response "$url" 2>&1 | awk '/^  HTTP/{print $2}' | tail -1)
    80      fi
    81    else
    82      wasmer_error "Neither curl nor wget was available to perform http requests."
    83      return 1
    84    fi
    85  
    86    if [ "$code" = 404 ]; then
    87      wasmer_error "Your platform is not yet supported ($OS-$ARCH).$reset\nPlease open an issue on the project if you would like to use wasmer in your project: https://github.com/wasmerio/wasmer"
    88      return 1
    89    elif [ "$code" != 200 ]; then
    90      wasmer_error "File download failed with code $code"
    91      return 1
    92    fi
    93    return 0
    94  }
    95  
    96  wasmer_detect_profile() {
    97    if [ -n "${PROFILE}" ] && [ -f "${PROFILE}" ]; then
    98      echo "${PROFILE}"
    99      return
   100    fi
   101  
   102    local DETECTED_PROFILE
   103    DETECTED_PROFILE=''
   104    local SHELLTYPE
   105    SHELLTYPE="$(basename "/$SHELL")"
   106  
   107    if [ "$SHELLTYPE" = "bash" ]; then
   108      if [ -f "$HOME/.bashrc" ]; then
   109        DETECTED_PROFILE="$HOME/.bashrc"
   110      elif [ -f "$HOME/.bash_profile" ]; then
   111        DETECTED_PROFILE="$HOME/.bash_profile"
   112      fi
   113    elif [ "$SHELLTYPE" = "zsh" ]; then
   114      DETECTED_PROFILE="$HOME/.zshrc"
   115    elif [ "$SHELLTYPE" = "fish" ]; then
   116      DETECTED_PROFILE="$HOME/.config/fish/config.fish"
   117    fi
   118  
   119    if [ -z "$DETECTED_PROFILE" ]; then
   120      if [ -f "$HOME/.profile" ]; then
   121        DETECTED_PROFILE="$HOME/.profile"
   122      elif [ -f "$HOME/.bashrc" ]; then
   123        DETECTED_PROFILE="$HOME/.bashrc"
   124      elif [ -f "$HOME/.bash_profile" ]; then
   125        DETECTED_PROFILE="$HOME/.bash_profile"
   126      elif [ -f "$HOME/.zshrc" ]; then
   127        DETECTED_PROFILE="$HOME/.zshrc"
   128      elif [ -f "$HOME/.config/fish/config.fish" ]; then
   129        DETECTED_PROFILE="$HOME/.config/fish/config.fish"
   130      fi
   131    fi
   132  
   133    if [ ! -z "$DETECTED_PROFILE" ]; then
   134      echo "$DETECTED_PROFILE"
   135    fi
   136  }
   137  
   138  wasmer_link() {
   139  
   140    WASMER_PROFILE="$(wasmer_detect_profile)"
   141  
   142    LOAD_STR="\n# Wasmer\nexport WASMER_DIR=\"$INSTALL_DIRECTORY\"\n[ -s \"\$WASMER_DIR/wasmer.sh\" ] && source \"\$WASMER_DIR/wasmer.sh\"\n"
   143    SOURCE_STR="# Wasmer config\nexport WASMER_DIR=\"$INSTALL_DIRECTORY\"\nexport WASMER_CACHE_DIR=\"\$WASMER_DIR/cache\"\nexport PATH=\"\$WASMER_DIR/bin:\$PATH:\$WASMER_DIR/globals/wapm_packages/.bin\"\n"
   144  
   145    # We create the wasmer.sh file
   146    printf "$SOURCE_STR" >"$INSTALL_DIRECTORY/wasmer.sh"
   147  
   148    if [ -z "${WASMER_PROFILE-}" ]; then
   149      wasmer_error "Profile not found. Tried:\n* ${WASMER_PROFILE} (as defined in \$PROFILE)\n* ~/.bashrc\n* ~/.bash_profile\n* ~/.zshrc\n* ~/.profile.\n${reset}Append the following lines to the correct file yourself:\n${SOURCE_STR}"
   150      return 1
   151    else
   152      printf "Updating bash profile $WASMER_PROFILE\n"
   153      if ! grep -q 'wasmer.sh' "$WASMER_PROFILE"; then
   154        # if [[ $WASMER_PROFILE = *"fish"* ]]; then
   155        #   command fish -c 'set -U fish_user_paths $fish_user_paths ~/.wasmer/bin'
   156        # else
   157        command printf "$LOAD_STR" >>"$WASMER_PROFILE"
   158        # fi
   159        if [ "$WASMER_INSTALL_LOG" = "$WASMER_VERBOSE" ]; then
   160          printf "we've added the following to your $WASMER_PROFILE\n"
   161          echo "If you have a different profile please add the following:"
   162          printf "$dim$LOAD_STR$reset"
   163        fi
   164        wasmer_fresh_install=true
   165      else
   166        wasmer_warning "the profile already has Wasmer and has not been changed"
   167      fi
   168  
   169      version=$($INSTALL_DIRECTORY/bin/wasmer --version) || (
   170        wasmer_error "wasmer was installed, but doesn't seem to be working :("
   171        return 1
   172      )
   173  
   174      wasmer_install_status "check" "$version installed successfully ✓"
   175  
   176      if [ "$WASMER_INSTALL_LOG" = "$WASMER_VERBOSE" ]; then
   177        if [ "$wasmer_fresh_install" = true ]; then
   178          printf "wasmer & wapm will be available the next time you open the terminal.\n"
   179          printf "If you want to have the commands available now please execute:\n\nsource $INSTALL_DIRECTORY/wasmer.sh$reset\n"
   180        fi
   181      fi
   182    fi
   183    return 0
   184  }
   185  
   186  initArch() {
   187    ARCH=$(uname -m)
   188    case $ARCH in
   189    amd64) ARCH="amd64" ;;
   190    x86_64) ARCH="amd64" ;;
   191    aarch64) ARCH="aarch64" ;;
   192    riscv64) ARCH="riscv64" ;;
   193    arm64) ARCH="arm64" ;; # This is for the macOS M1 ARM chips
   194    *)
   195      wasmer_error "The system architecture (${ARCH}) is not yet supported by this installation script."
   196      exit 1
   197      ;;
   198    esac
   199    # echo "ARCH = $ARCH"
   200  }
   201  
   202  initOS() {
   203    OS=$(uname | tr '[:upper:]' '[:lower:]')
   204    case "$OS" in
   205    darwin) OS='darwin' ;;
   206    linux) OS='linux' ;;
   207    freebsd) OS='freebsd' ;;
   208    # mingw*) OS='windows';;
   209    # msys*) OS='windows';;
   210    *)
   211      printf "$red> The OS (${OS}) is not supported by this installation script.$reset\n"
   212      exit 1
   213      ;;
   214    esac
   215  }
   216  
   217  wasmer_install() {
   218    magenta1="${reset}\033[34;1m"
   219    magenta2=""
   220    magenta3=""
   221  
   222    if which wasmer >/dev/null; then
   223      printf "${reset}Welcome to the Wasmer bash installer!$reset\n"
   224    else
   225      printf "${reset}Welcome to the Wasmer bash installer!$reset\n"
   226      if [ "$WASMER_INSTALL_LOG" = "$WASMER_VERBOSE" ]; then
   227        printf "
   228  ${magenta1}               ww
   229  ${magenta1}               wwwww
   230  ${magenta1}        ww     wwwwww  w
   231  ${magenta1}        wwwww      wwwwwwwww
   232  ${magenta1}ww      wwwwww  w     wwwwwww
   233  ${magenta1}wwwww      wwwwwwwwww   wwwww
   234  ${magenta1}wwwwww  w      wwwwwww  wwwww
   235  ${magenta1}wwwwwwwwwwwwww   wwwww  wwwww
   236  ${magenta1}wwwwwwwwwwwwwww  wwwww  wwwww
   237  ${magenta1}wwwwwwwwwwwwwww  wwwww  wwwww
   238  ${magenta1}wwwwwwwwwwwwwww  wwwww  wwwww
   239  ${magenta1}wwwwwwwwwwwwwww  wwwww   wwww
   240  ${magenta1}wwwwwwwwwwwwwww  wwwww
   241  ${magenta1}   wwwwwwwwwwww   wwww
   242  ${magenta1}       wwwwwwww
   243  ${magenta1}           wwww
   244  ${reset}
   245  "
   246      fi
   247    fi
   248  
   249    wasmer_download $1 && wasmer_link
   250    wapm_download
   251    wasmer_reset
   252  }
   253  
   254  wasmer_reset() {
   255    unset -f wasmer_install semver_compare wasmer_reset wasmer_download_json wasmer_link wasmer_detect_profile wasmer_download_file wasmer_download wasmer_verify_or_quit
   256  }
   257  
   258  version() {
   259    echo "$@" | awk -F. '{ printf("%d%03d%03d%03d\n", $1,$2,$3,$4); }'
   260  }
   261  
   262  semverParseInto() {
   263    local RE='v?([0-9]+)[.]([0-9]+)[.]([0-9]+)([.0-9A-Za-z-]*)'
   264  
   265    # # strip word "v" if exists
   266    # version=$(echo "${1//v/}")
   267  
   268    #MAJOR
   269    eval $2=$(echo $1 | sed -E "s#$RE#\1#")
   270    #MINOR
   271    eval $3=$(echo $1 | sed -E "s#$RE#\2#")
   272    #MINOR
   273    eval $4=$(echo $1 | sed -E "s#$RE#\3#")
   274    #SPECIAL
   275    eval $5=$(echo $1 | sed -E "s#$RE#\4#")
   276  }
   277  
   278  ###
   279  # Code inspired (copied partially and improved) with attributions from:
   280  # https://github.com/cloudflare/semver_bash/blob/master/semver.sh
   281  # https://gist.github.com/Ariel-Rodriguez/9e3c2163f4644d7a389759b224bfe7f3
   282  ###
   283  semver_compare() {
   284    local version_a version_b
   285  
   286    local MAJOR_A=0
   287    local MINOR_A=0
   288    local PATCH_A=0
   289    local SPECIAL_A=0
   290  
   291    local MAJOR_B=0
   292    local MINOR_B=0
   293    local PATCH_B=0
   294    local SPECIAL_B=0
   295  
   296    semverParseInto $1 MAJOR_A MINOR_A PATCH_A SPECIAL_A
   297    semverParseInto $2 MAJOR_B MINOR_B PATCH_B SPECIAL_B
   298  
   299    # Check if our version is higher
   300    if [ $MAJOR_A -gt $MAJOR_B ]; then
   301      echo 1 && return 0
   302    fi
   303    if [ $MAJOR_A -eq $MAJOR_B ]; then
   304      if [ $MINOR_A -gt $MINOR_B ]; then
   305        echo 1 && return 0
   306      elif [ $MINOR_A -eq $MINOR_B ]; then
   307        if [ $PATCH_A -gt $PATCH_B ]; then
   308          echo 1 && return 0
   309        elif [ $PATCH_A -eq $PATCH_B ]; then
   310          if [ -n "$SPECIAL_A" ] && [ -z "$SPECIAL_B" ]; then
   311            # if the version we're targeting does not have a tag and our current
   312            # version does, we should upgrade because no tag > tag
   313            echo -1 && return 0
   314          elif [ "$SPECIAL_A" \> "$SPECIAL_B" ]; then
   315            echo 1 && return 0
   316          elif [ "$SPECIAL_A" = "$SPECIAL_B" ]; then
   317            # complete match
   318            echo 0 && return 0
   319          fi
   320        fi
   321      fi
   322    fi
   323  
   324    # if we're here we know that the target version cannot be less than or equal to
   325    # our current version, therefore we upgrade
   326  
   327    echo -1 && return 0
   328  }
   329  
   330  wasmer_download() {
   331    # identify platform based on uname output
   332    initArch || return 1
   333    initOS || return 1
   334  
   335    # assemble expected release artifact name
   336    BINARY="wasmer-${OS}-${ARCH}.tar.gz"
   337  
   338    # add .exe if on windows
   339    # if [ "$OS" = "windows" ]; then
   340    #     BINARY="$BINARY.exe"
   341    # fi
   342  
   343    wasmer_install_status "downloading" "wasmer-$OS-$ARCH"
   344    if [ $# -eq 0 ]; then
   345      # The version was not provided, assume latest
   346      wasmer_download_json LATEST_RELEASE "$RELEASES_URL/latest" || return 1
   347      WASMER_RELEASE_TAG=$(echo "${LATEST_RELEASE}" | tr -s '\n' ' ' | sed 's/.*"tag_name":"//' | sed 's/".*//')
   348      printf "Latest release: ${WASMER_RELEASE_TAG}\n"
   349    else
   350      WASMER_RELEASE_TAG="${1}"
   351      printf "Installing provided version: ${WASMER_RELEASE_TAG}\n"
   352    fi
   353  
   354    if which $INSTALL_DIRECTORY/bin/wasmer >/dev/null; then
   355      WASMER_VERSION=$($INSTALL_DIRECTORY/bin/wasmer --version | sed 's/wasmer //g')
   356      printf "Wasmer already installed in ${INSTALL_DIRECTORY} with version: ${WASMER_VERSION}\n"
   357  
   358      WASMER_COMPARE=$(semver_compare $WASMER_VERSION $WASMER_RELEASE_TAG)
   359      case $WASMER_COMPARE in
   360      # WASMER_VERSION = WASMER_RELEASE_TAG
   361      0)
   362        if [ $# -eq 0 ]; then
   363          wasmer_warning "wasmer is already installed in the latest version: ${WASMER_RELEASE_TAG}"
   364        else
   365          wasmer_warning "wasmer is already installed with the same version: ${WASMER_RELEASE_TAG}"
   366        fi
   367        printf "Do you want to force the installation?"
   368        wasmer_verify_or_quit || return 1
   369        ;;
   370        # WASMER_VERSION > WASMER_RELEASE_TAG
   371      1)
   372        wasmer_warning "the selected version (${WASMER_RELEASE_TAG}) is lower than current installed version ($WASMER_VERSION)"
   373        printf "Do you want to continue installing Wasmer $WASMER_RELEASE_TAG?"
   374        wasmer_verify_or_quit || return 1
   375        ;;
   376        # WASMER_VERSION < WASMER_RELEASE_TAG (we continue)
   377      -1) ;;
   378      esac
   379    fi
   380  
   381    # fetch the real release data to make sure it exists before we attempt a download
   382    wasmer_download_json RELEASE_DATA "$RELEASES_URL/tag/$WASMER_RELEASE_TAG" || return 1
   383  
   384    BINARY_URL="$RELEASES_URL/download/$WASMER_RELEASE_TAG/$BINARY"
   385    DOWNLOAD_FILE=$(mktemp -t wasmer.XXXXXXXXXX)
   386  
   387    printf "Downloading archive from ${BINARY_URL}\n"
   388  
   389    wasmer_download_file "$BINARY_URL" "$DOWNLOAD_FILE" || return 1
   390    # echo -en "\b\b"
   391    printf "\033[K\n\033[1A"
   392  
   393    # windows not supported yet
   394    # if [ "$OS" = "windows" ]; then
   395    #     INSTALL_NAME="$INSTALL_NAME.exe"
   396    # fi
   397  
   398    # echo "Moving executable to $INSTALL_DIRECTORY/$INSTALL_NAME"
   399  
   400    wasmer_install_status "installing" "${INSTALL_DIRECTORY}"
   401  
   402    mkdir -p $INSTALL_DIRECTORY
   403  
   404    # Untar the wasmer contents in the install directory
   405    tar -C $INSTALL_DIRECTORY -zxf $DOWNLOAD_FILE
   406    return 0
   407  }
   408  
   409  wapm_download() {
   410    # identify platform based on uname output
   411    initArch || return 1
   412    initOS || return 1
   413  
   414    if [ "$ARCH" = "arm64" ]; then
   415      ARCH="aarch64"
   416    fi
   417  
   418    # assemble expected release artifact name
   419    BINARY="wapm-cli-${OS}-${ARCH}.tar.gz"
   420  
   421    wasmer_install_status "downloading" "wapm-cli-$OS-$ARCH"
   422    # Download latest wapm version
   423    wasmer_download_json LATEST_RELEASE "$WAPM_RELEASES_URL/latest" || return 1
   424    WAPM_RELEASE_TAG=$(echo "${LATEST_RELEASE}" | tr -s '\n' ' ' | sed 's/.*"tag_name":"//' | sed 's/".*//' | sed 's/v//g')
   425    printf "Latest release: ${WAPM_RELEASE_TAG}\n"
   426  
   427    if which $INSTALL_DIRECTORY/bin/wapm >/dev/null; then
   428      WAPM_VERSION=$($INSTALL_DIRECTORY/bin/wapm --version | sed 's/wapm-cli //g')
   429      printf "WAPM already installed in ${INSTALL_DIRECTORY} with version: ${WAPM_VERSION}\n"
   430  
   431      WAPM_COMPARE=$(semver_compare $WAPM_VERSION $WAPM_RELEASE_TAG)
   432      case $WAPM_COMPARE in
   433      # WAPM_VERSION = WAPM_RELEASE_TAG
   434      0)
   435        if [ $# -eq 0 ]; then
   436          wasmer_warning "WAPM is already installed in the latest version: ${WAPM_RELEASE_TAG}"
   437        else
   438          wasmer_warning "WAPM is already installed with the same version: ${WAPM_RELEASE_TAG}"
   439        fi
   440        printf "Do you want to force the installation?"
   441        wasmer_verify_or_quit || return 1
   442        ;;
   443        # WAPM_VERSION > WAPM_RELEASE_TAG
   444      1)
   445        wasmer_warning "the selected version (${WAPM_RELEASE_TAG}) is lower than current installed version ($WAPM_VERSION)"
   446        printf "Do you want to continue installing WAPM $WAPM_RELEASE_TAG?"
   447        wasmer_verify_or_quit || return 1
   448        ;;
   449        # WAPM_VERSION < WAPM_RELEASE_TAG (we continue)
   450      -1) ;;
   451      esac
   452    fi
   453  
   454    # fetch the real release data to make sure it exists before we attempt a download
   455    wasmer_download_json RELEASE_DATA "$WAPM_RELEASES_URL/tag/v$WAPM_RELEASE_TAG" || return 1
   456  
   457    BINARY_URL="$WAPM_RELEASES_URL/download/v$WAPM_RELEASE_TAG/$BINARY"
   458    DOWNLOAD_FILE=$(mktemp -t wapm.XXXXXXXXXX)
   459  
   460    printf "Downloading archive from ${BINARY_URL}\n"
   461  
   462    wasmer_download_file "$BINARY_URL" "$DOWNLOAD_FILE" || return 1
   463  
   464    printf "\033[K\n\033[1A"
   465  
   466    wasmer_install_status "installing" "${INSTALL_DIRECTORY}"
   467  
   468    mkdir -p $INSTALL_DIRECTORY
   469  
   470    # Untar the WAPM contents in the install directory
   471    tar -C $INSTALL_DIRECTORY -zxf $DOWNLOAD_FILE
   472  }
   473  
   474  wasmer_error() {
   475    printf "$bold${red}error${white}: $1${reset}\n"
   476  }
   477  
   478  wasmer_install_status() {
   479    printf "$bold${green}${1}${white}: $2${reset}\n"
   480  }
   481  
   482  wasmer_warning() {
   483    printf "$bold${yellow}warning${white}: $1${reset}\n"
   484  }
   485  
   486  wasmer_verify_or_quit() {
   487    if [ -n "$BASH_VERSION" ]; then
   488      # If we are in bash, we can use read -n
   489      read -p "$1 [y/N] " -n 1 -r
   490      echo
   491      if [[ ! $REPLY =~ ^[Yy]$ ]]; then
   492        wasmer_error "installation aborted"
   493        return 1
   494      fi
   495      return 0
   496    fi
   497  
   498    read -p "$1 [y/N]" yn
   499    case $yn in
   500    [Yy]*) break ;;
   501    [Nn]*)
   502      wasmer_error "installation aborted"
   503      return 1
   504      ;;
   505    *) echo "Please answer yes or no." ;;
   506    esac
   507  
   508    return 0
   509  }
   510  
   511  # determine install directory if required
   512  if [ -z "$WASMER_DIR" ]; then
   513    # If WASMER_DIR is not present
   514    INSTALL_DIRECTORY="$HOME/.wasmer"
   515  else
   516    # If WASMER_DIR is present
   517    INSTALL_DIRECTORY="${WASMER_DIR}"
   518  fi
   519  
   520  wasmer_install $1 # $2