#!/usr/bin/env bash install_ruby() { local install_type=$1 local version=$2 local install_path=$3 local ruby_type=$(get_ruby_type $version) if [ "$TMPDIR" = "" ]; then local tmp_download_dir=$(mktemp -d) else local tmp_download_dir=$TMPDIR fi # path to the tar file local source_path=$(get_download_file_path $install_type $version $tmp_download_dir) download_source $install_type $version $source_path # running this in a subshell # because we don't want to disturb current working dir ( cd $(dirname $source_path) if [ "${ruby_type}" = "jruby" ]; then tar zxf $source_path -C $(dirname $install_path) || exit 1 exit 0 else tar zxf $source_path || exit 1 fi # we use untar path because the extracted dir maybe diff # ideally should be using tar with --strip-components 1 cd $(untar_path $install_type $version $tmp_download_dir) if [ "$ASDF_PKG_MISSING" != "" ]; then echo "WARNING: Might use OS-provided pkgs for the following: $ASDF_PKG_MISSING" fi local configure_options="$(construct_configure_options $install_path)" # set in os_based_configure_options # we unset it here because echo-ing changes the return value of the function unset ASDF_PKG_MISSING echo "Building with options: $configure_options" ./configure $configure_options || exit 1 make || exit 1 make install || exit 1 ) } construct_configure_options() { local install_path=$1 if [ "$RUBY_CONFIGURE_OPTIONS" = "" ]; then local configure_options="$(os_based_configure_options) --prefix=$install_path" if [ "$RUBY_EXTRA_CONFIGURE_OPTIONS" != "" ]; then configure_options="$configure_options $RUBY_EXTRA_CONFIGURE_OPTIONS" fi else local configure_options="$RUBY_CONFIGURE_OPTIONS --prefix=$install_path" fi echo "$configure_options" } homebrew_package_path() { local package_name=$1 if [ "$(brew ls --versions $package_name)" = "" ]; then echo "" else echo "$(brew --prefix $package_name)" fi } exit_if_homebrew_not_installed() { if [ "$(brew --version 2>/dev/null)" = "" ]; then echo "ERROR: Please install homebrew for OSX" exit 1 fi } os_based_configure_options() { local operating_system=$(uname -a) local configure_options="" if [[ "$operating_system" =~ "Darwin" ]]; then exit_if_homebrew_not_installed local openssl_path=$(homebrew_package_path openssl) local libyaml_path=$(homebrew_package_path libyaml) local readline_path=$(homebrew_package_path readline) else local openssl_path=/usr local libyaml_path=/usr fi if [ "$openssl_path" = "" ]; then export ASDF_PKG_MISSING="openssl" else configure_options="--with-openssl-dir=$openssl_path" fi if [ "$libyaml_path" = "" ]; then export ASDF_PKG_MISSING="$ASDF_PKG_MISSING libyaml" else configure_options="$configure_options --with-libyaml-dir=$libyaml_path" fi if [ "$readline_path" != "" ]; then configure_options="$configure_options --with-readline-dir=$readline_path" fi configure_options="$configure_options --enable-shared --disable-install-doc" echo $configure_options } download_source() { local install_type=$1 local version=$2 local download_path=$3 local download_url=$(get_download_url $install_type $version) if [ -f $download_path ]; then rm $download_path fi curl -Lo $download_path $download_url } get_download_file_path() { local install_type=$1 local version=$2 local tmp_download_dir=$3 local ruby_type=$(get_ruby_type $version) local ruby_version=$(get_ruby_version $version) if [ "${ruby_type}" = "ruby" ]; then local pkg_name="ruby-${ruby_version}.tar.gz" elif [ "${ruby_type}" = "jruby" ]; then local pkg_name="jruby-bin-${ruby_version}.tar.gz" fi echo "$tmp_download_dir/$pkg_name" } untar_path() { local install_type=$1 local version=$2 local tmp_download_dir=$3 local ruby_type=$(get_ruby_type $version) local ruby_version=$(get_ruby_version $version) if [ "${ruby_type}" = "ruby" ]; then local dir_name="ruby-${ruby_version}" elif [ "${ruby_type}" = "jruby" ]; then local dir_name="jruby-${ruby_version}" fi echo "$tmp_download_dir/$dir_name" } get_download_url() { local install_type=$1 local version=$2 local ruby_type=$(get_ruby_type $version) local ruby_version=$(get_ruby_version $version) if [ "${ruby_type}" = "ruby" ]; then echo "http://cache.ruby-lang.org/pub/ruby/ruby-${ruby_version}.tar.gz" elif [ "${ruby_type}" = "jruby" ]; then echo "https://s3.amazonaws.com/jruby.org/downloads/${ruby_version}/jruby-bin-${ruby_version}.tar.gz" fi } get_ruby_version() { IFS='-' read -a version_info <<< "$1" if [ "${version_info[0]}" = "jruby" ]; then # jruby if [ "${#version_info[@]}" -eq 2 ]; then echo "${version_info[1]}" else echo "${version_info[1]}-${version_info[2]}" fi else # ruby if [ "${#version_info[@]}" -eq 1 ]; then echo "${version_info[0]}" else echo "${version_info[0]}-${version_info[1]}" fi fi } get_ruby_type() { IFS='-' read -a version_info <<< "$1" if [ "${version_info[0]}" = "jruby" ]; then echo "jruby" else echo "ruby" fi } install_ruby $ASDF_INSTALL_TYPE $ASDF_INSTALL_VERSION $ASDF_INSTALL_PATH