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

Add support for .ruby-variant #5

Merged
merged 2 commits into from Oct 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -102,6 +102,14 @@ operating system will do the following:
* Run the shim named `rake`, which in turn passes the command along to
rbenv

### .ruby-version and .ruby-variant

Typically most projects will commit a `.ruby-version` file to their projects git repository. This causes conflicts when you are using a different variant of Ruby on the server.

For example, lets say that in development you have not installed fullstaq-rbenv and you have defined `2.6.3` in your `.ruby-version` file. On the server you have installed fullstaq-ruby and only installed `2.6.3-jemalloc`. Rbenv will complain that `2.6.3` is not installed as that is what your `.ruby-version` has defined.

The solution to this is to add a `.ruby-variant` file to your project. For example if you are using the jemalloc variant simply put `jemalloc` in this file. Only when you are using fullstaq-rbenv will it combine the contents of `.ruby-version` and `.ruby-variant` to a single version string, ie. `2.6.3-jemalloc`.

### Choosing the Ruby Version

When you execute a shim, rbenv determines which Ruby version to use by
Expand Down
15 changes: 15 additions & 0 deletions libexec/rbenv-version-file-read
Expand Up @@ -14,6 +14,21 @@ if [ -e "$VERSION_FILE" ]; then
if [ "$version" = ".." ] || [[ $version == */* ]]; then
echo "rbenv: invalid version in \`$VERSION_FILE'" >&2
elif [ -n "$version" ]; then
variant_file=""

if [[ "$(basename "$VERSION_FILE")" == ".ruby-version" ]]; then
variant_file="$(dirname "$VERSION_FILE")/.ruby-variant"
fi

if [[ -e "$variant_file" ]]; then
variant_words=( $(cat "$variant_file") )
variant="${variant_words[0]}"

if [ -n "$variant" ] && [[ "$version" != *-$variant ]]; then
version="${version}-${variant}"
fi
fi

echo "$version"
exit
fi
Expand Down
22 changes: 22 additions & 0 deletions test/version-file-read.bats
Expand Up @@ -86,3 +86,25 @@ IN
run rbenv-version-file-read my-version
assert_failure "rbenv: invalid version in \`my-version'"
}

@test "appends text from .ruby-variant file" {
echo "2.6.3" > .ruby-version
echo "jemalloc" > .ruby-variant

run rbenv-version-file-read .ruby-version
assert_success "2.6.3-jemalloc"

rm .ruby-variant
echo "jemalloc malloctrim" > .ruby-variant

run rbenv-version-file-read .ruby-version
assert_success "2.6.3-jemalloc"
}

@test "doesnt append .ruby-variant if .ruby-version contains variant" {
echo "2.6.3-jemalloc" > .ruby-version
echo "jemalloc" > .ruby-variant

run rbenv-version-file-read .ruby-version
assert_success "2.6.3-jemalloc"
}