Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replacing ruby-build with ruby-install. #205

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
ruby-build
ruby-install
ruby-install-source
31 changes: 24 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,46 @@ Ruby plugin for [asdf](https://github.com/asdf-vm/asdf) version manager
asdf plugin-add ruby https://github.com/asdf-vm/asdf-ruby.git
```

Please make sure you have the required [system dependencies](https://github.com/rbenv/ruby-build/wiki#suggested-build-environment) installed before trying to install Ruby.
Please make sure you have the required [system dependencies](https://github.com/postmodern/ruby-install#requirements) installed before trying to install Ruby.

By default, asdf-ruby will try to rely as much as it can on your [package manager](https://github.com/postmodern/ruby-install#features) for both using ruby-install as well as fetching necessary dependencies to build from source.

## Use

Check [asdf](https://github.com/asdf-vm/asdf) readme for instructions on how to install & manage versions of Ruby.

When installing Ruby using `asdf install`, you can pass custom configure options with the [env vars supported by ruby-build](https://github.com/rbenv/ruby-build#custom-build-configuration).
When installing Ruby using `asdf install`, you can pass custom configure options with the [env vars supported by ruby-install](https://github.com/postmodern/ruby-install#synopsis).

Under the hood, asdf-ruby uses [ruby-build](https://github.com/rbenv/ruby-build) to build and install Ruby, check its [README](https://github.com/rbenv/ruby-build/blob/master/README.md) for more information about build options and the [troubleshooting](https://github.com/rbenv/ruby-build/wiki#troubleshooting) wiki section for any issues encountered during installation of ruby versions.
Under the hood, asdf-ruby uses [ruby-install](https://github.com/postmodern/ruby-install) to build and install Ruby, check its [README](https://github.com/postmodern/ruby-install) for more information about build options and the [requirements](https://github.com/postmodern/ruby-install#requirements) section for any issues encountered during installation of ruby versions.

You may also apply custom patches before building with `RUBY_APPLY_PATCHES`, e.g.

```
```sh
RUBY_APPLY_PATCHES=$'dir/1.patch\n2.patch\nhttp://example.com/3.patch' asdf install ruby 2.4.1
RUBY_APPLY_PATCHES=$(curl -s https://raw.githubusercontent.com/rvm/rvm/master/patchsets/ruby/2.1.1/railsexpress) asdf install ruby 2.1.1
RUBY_APPLY_PATCHES="https://raw.githubusercontent.com/rvm/rvm/master/patchsets/ruby/2.1.1/railsexpress" asdf install ruby 2.1.1
```

By default asdf-ruby uses the latest release of ruby-build, but you can choose your own branch/tag through the `ASDF_RUBY_BUILD_VERSION` variable:
Although unecessary with ruby-install, you can still pass custom options

```sh
RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix [email protected])" asdf install ruby 2.4.0
```
ASDF_RUBY_BUILD_VERSION=master asdf install ruby 2.6.4

```sh
RUBY_CONFIGURE_OPTS="--disable-install-doc" asdf install ruby 3.0.0
```

By default asdf-ruby uses the latest release of ruby-install, but you can choose your own branch/tag through the `ASDF_RUBY_INSTALL_VERSION` variable:

```sh
ASDF_RUBY_INSTALL_VERSION="v0.8.0" asdf install ruby 2.6.4
```

```sh
ASDF_RUBY_INSTALL_VERSION="master" asdf install ruby 2.6.4
```


## Default gems

asdf-ruby can automatically install a set of default gems right after
Expand Down
7 changes: 5 additions & 2 deletions bin/exec-env
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
current_script_path=${BASH_SOURCE[0]}
ruby_plugin_dir=$(dirname "$(dirname "$current_script_path")")

load_os=("$install_path/lib/ruby/$version/"*"-darwin19")
export LOAD_PATH="${load_os[@]}:$install_path/lib/ruby/$version:$LOAD_PATH"

if [ "$RUBYLIB" = "" ]; then
export RUBYLIB="$ruby_plugin_dir/rubygems-plugin"
export RUBYLIB="$LOAD_PATH:$ruby_plugin_dir/rubygems-plugin"
else
export RUBYLIB="$ruby_plugin_dir/rubygems-plugin:$RUBYLIB"
export RUBYLIB="$LOAD_PATH:$ruby_plugin_dir/rubygems-plugin:$RUBYLIB"
fi

export PATH=$install_path/bin:$PATH
122 changes: 76 additions & 46 deletions bin/install
Original file line number Diff line number Diff line change
Expand Up @@ -4,70 +4,100 @@ set -euo pipefail

# shellcheck source=../lib/utils.sh
source "$(dirname $0)/../lib/utils.sh"
ensure_ruby_install_setup

install_ruby() {
ensure_ruby_build_setup

local install_type=$1
local version=$2
local install_path=$3

if [ "$install_type" != "version" ]; then
echoerr "Cannot install specific ref from source, sorry."
echoerr "For a list of available versions, see \`asdf list-all ruby\`"
if [ "$ASDF_INSTALL_TYPE" != "version" ]; then
printerr "Cannot install specific ref from source, sorry."
printerr "For a list of available versions, see \`asdf list-all ruby\`"
exit 1
fi

if [ -z "${RUBY_APPLY_PATCHES:-}" ] && [ -z "${RUBY_CONFIGURE_OPTS:-}"] && [ -z "${ASDF_RUBY_FROM_SOURCE:-}" ] && os_name="$(get_macos_marketing_name)"; then
if download_url="$(macos_get_download_url $os_name)"; then
macos_install_from_prebuilt "$download_url"
else
printf 'Fallback to building from source. \n'
install_from_source
fi
else
install_from_source
fi


}

install_from_source() {
local opts=""
local patches=""
local patches=" "

if [[ -n "${RUBY_APPLY_PATCHES:-}" ]]; then
opts="$opts --patch"
patches=$(fetch_patches "$RUBY_APPLY_PATCHES")
patches=" --patch $RUBY_APPLY_PATCHES "
fi

if [[ -n "${RUBY_BUILD_OPTS:-}" ]]; then
opts="$opts $RUBY_BUILD_OPTS"
if [[ -n "${RUBY_CONFIGURE_OPTS:-}" ]]; then
opts="-- $RUBY_CONFIGURE_OPTS"
fi

# shellcheck disable=SC2086
echo "$patches" | $(ruby_build_path) ${opts} "$version" "$install_path"
local command="$(ruby_install_executable) --no-reinstall --cleanup --src-dir "$ASDF_DOWNLOAD_PATH" --install-dir "$ASDF_INSTALL_PATH""$patches""$ASDF_INSTALL_VERSION" "$opts""
echo "$command"
echo $($command)
}

fetch_patches() {
while read -r line; do
if [ "$line" = "" ]; then continue; fi
if [[ "$line" =~ ^[Hh][Tt][Tt][Pp][Ss]?:// ]]; then
>&2 echo "Using patch from URL: $line"
curl -fSs "$line" || exit 1
else
local abs_path
abs_path="$(get_absolute_path "$line")"
>&2 echo "Using local patch: $abs_path"
cat "$abs_path" || exit 1
fi
done <<< "$@"
}
macos_install_from_prebuilt() {
#macos_check_homebrew_setup
mkdir -p "$ASDF_INSTALL_PATH"
mkdir -p "$ASDF_DOWNLOAD_PATH"


local tmp_path="$HOME/.asdf/tmp/$(plugin_name)/extracted/$ASDF_INSTALL_VERSION"
mkdir -p "$tmp_path"


local download_url="$1"

printf 'Downloading %s. \n' "$download_url"

curl -L "$download_url" | tar xzf - -C "$tmp_path" --strip-components=2


# We're not keeping the same directory structure as Homebrew.
local replacement_cellar_pattern="@@HOMEBREW_CELLAR@@/ruby/$ASDF_INSTALL_VERSION"

# Replace the homebrew URL with something compatible with asdf
grep -rlI --null "$replacement_cellar_pattern" "$tmp_path" | xargs -0 sed -i '' "s:$replacement_cellar_pattern:$ASDF_INSTALL_PATH:g"

local replacement_prefix_pattern="@@HOMEBREW_PREFIX@@"

# Replace the homebrew URL with something compatible with asdf
grep -rlI --null "$replacement_prefix_pattern" "$tmp_path" | xargs -0 sed -i '' "s:$replacement_prefix_pattern:$ASDF_INSTALL_PATH:g"

local pattern="@@HOMEBREW_CELLAR@@"
local linked_libs=()
while IFS= read -r -d '' file_path; do
linked_libs+=("$file_path")
done < <(grep -rl --null "$pattern" "$tmp_path")

macos_update_linked_paths "${linked_libs[@]}"

mv -f "$tmp_path/"* "$ASDF_INSTALL_PATH"

local lib_rubies=("$ASDF_INSTALL_PATH/lib/libruby."*".dylib")

for lib_ruby in "${lib_rubies[@]}"; do
echo "$lib_ruby"
$(install_name_tool -id "$lib_ruby" "$lib_ruby")
cp "$lib_ruby" "$lib_ruby.backup"
codesign -s - -f -vvvvvv "$lib_ruby.backup"
mv -f "$lib_ruby.backup" "$lib_ruby"
done

get_absolute_path() {
local start_dir
local rel_path
local rel_dir
local rel_base

start_dir=$(pwd)
rel_path=$1
rel_dir=$(dirname "$rel_path")
rel_base=$(basename "$rel_path")

(
cd "$start_dir" \
&& cd "$rel_dir" 2>/dev/null \
&& echo "$(pwd)/$rel_base" \
|| echo "$rel_path"
)
}



install_default_gems() {
local args=()
local default_gems="${HOME}/.default-gems"
Expand Down Expand Up @@ -111,5 +141,5 @@ install_default_gems() {
done < "$default_gems"
}

install_ruby "$ASDF_INSTALL_TYPE" "$ASDF_INSTALL_VERSION" "$ASDF_INSTALL_PATH"
install_ruby
install_default_gems
6 changes: 2 additions & 4 deletions bin/list-all
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ set -euo pipefail

# shellcheck source=../lib/utils.sh
source "$(dirname $0)/../lib/utils.sh"
ensure_ruby_install_setup

list_versions() {
ensure_ruby_build_setup

# This sed command was a quick and dirty solution to remove topaz-dev from
# the version list, since it doesn't exist
"$(ruby_build_path)" --definitions | grep -v "topaz-dev" | paste -sd " " -
echo $($(ruby_install_executable))
}

list_versions
Loading