Skip to content

Instantly share code, notes, and snippets.

@robbkidd
Last active November 16, 2016 06:50
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robbkidd/30d6ef0da23431b84932 to your computer and use it in GitHub Desktop.
Save robbkidd/30d6ef0da23431b84932 to your computer and use it in GitHub Desktop.
Use `direnv` to control your environments.

brew install direnv ruby-install chruby

Currently have Ruby switching and declaring Chef DK use only in directories that you specify.

Recommend adding .direnv/ to your Ruby projects' .gitignore

Massive nods to schisamo for the Chef DK piece! And to Steve Tooke for most of the Ruby solution.

# add these functions to ~/.direnvrc
use_ruby() {
source /usr/local/opt/chruby/share/chruby/chruby.sh
local ver=$1
if [[ -z $ver ]] && [[ -f .ruby-version ]]; then
ver=$(cat .ruby-version)
fi
if [[ -z $ver ]]; then
echo Unknown ruby version
exit 1
fi
# switch to the desired ruby
chruby $ver
# Sets the GEM_HOME environment variable to `$PWD/.direnv/ruby/RUBY_VERSION`.
# This forces the installation of any gems into the project’s sub-folder. If
# you’re using bundler it will create wrapper programs that can be invoked
# directly instead of using the `bundle exec` prefix.
layout_ruby
}
use_chefdk() {
# include this function in `~/.direnvrc`
EXPANDED_HOME=`expand_path ~`
# Override the GEM environment
log_status "Overriding default Ruby environment to use ChefDK"
export GEM_ROOT="/opt/chefdk/embedded/lib/ruby/gems/2.1.0"
export GEM_HOME="$EXPANDED_HOME/.chefdk/gem/ruby/2.1.0"
export GEM_PATH="$EXPANDED_HOME/.chefdk/gem/ruby/2.1.0:/opt/chefdk/embedded/lib/ruby/gems/2.1.0"
# Ensure ChefDK and it's embedded tools are first in the PATH
log_status "Ensuring ChefDK and it's embedded tools are first in the PATH"
PATH_add $EXPANDED_HOME/.chefdk/gem/ruby/2.1.0/bin/
PATH_add /opt/chefdk/embedded/bin
PATH_add /opt/chefdk/bin
}
# add this to an `.envrc` file in your cookbook project's root directory
use chefdk
# add this to an `.envrc` file in your Ruby project's root directory
use ruby 2.1.8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment