utils.sh 1.9 KB
Newer Older
R
Rojo 已提交
1
RUBY_BUILD_VERSION="${ASDF_RUBY_BUILD_VERSION:-v20200115}"
2
RUBY_BUILD_TAG="$RUBY_BUILD_VERSION"
3

P
Patrick Oscity 已提交
4 5 6 7
echoerr() {
  >&2 echo -e "\033[0;31m$1\033[0m"
}

8 9 10 11 12
ensure_ruby_build_setup() {
  ensure_ruby_build_installed
}

ensure_ruby_build_installed() {
13 14
    local ruby_build_version

15 16 17
    if [ ! -f "$(ruby_build_path)" ]; then
        download_ruby_build
    else
18
        current_ruby_build_version="$("$(ruby_build_path)" --version | cut -d ' ' -f2)"
19 20 21 22 23
        # If ruby-build version does not start with 'v',
        # add 'v' to beginning of version
        if [ ${current_ruby_build_version:0:1} != "v" ]; then
          current_ruby_build_version="v$current_ruby_build_version"
        fi
24
        if [ "$current_ruby_build_version" != "$RUBY_BUILD_VERSION" ]; then
25 26 27 28 29 30 31 32 33 34
            # If the ruby-build directory already exists and the version does not
            # match, remove it and download the correct version
            rm -rf "$(ruby_build_dir)"
            download_ruby_build
        fi
    fi
}

download_ruby_build() {
    # Print to stderr so asdf doesn't assume this string is a list of versions
35 36 37 38 39
    echoerr "Downloading ruby-build..."
    local build_dir="$(ruby_build_source_dir)"

    # Remove directory in case it still exists from last download
    rm -rf $build_dir
40 41 42

    # Clone down and checkout the correct ruby-build version
    git clone https://github.com/rbenv/ruby-build.git $build_dir >&2 >/dev/null
43
    (cd $build_dir; git checkout $RUBY_BUILD_TAG >&2 >/dev/null)
44 45

    # Install in the ruby-build dir
46
    PREFIX="$(ruby_build_dir)" $build_dir/install.sh
47 48

    # Remove ruby-build source dir
49
    rm -rf $build_dir
50 51
}

52 53 54
asdf_ruby_plugin_path() {
    echo "$(dirname "$(dirname "$0")")"
}
55
ruby_build_dir() {
56
    echo "$(asdf_ruby_plugin_path)/ruby-build"
57
}
58 59 60 61 62

ruby_build_source_dir() {
    echo "$(asdf_ruby_plugin_path)/ruby-build-source"
}

63 64
ruby_build_path() {
    echo "$(ruby_build_dir)/bin/ruby-build"
P
Patrick Oscity 已提交
65
}