Skip to content

Instantly share code, notes, and snippets.

@Chocksy
Last active April 9, 2020 16:26
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Chocksy/a00307aabfe96b684b92d5ffd471e3e7 to your computer and use it in GitHub Desktop.
Save Chocksy/a00307aabfe96b684b92d5ffd471e3e7 to your computer and use it in GitHub Desktop.
Gemfile local that will allow for special gems only on local env

Differences between team gemfile and your preferences

The solution for the above problem is to have a different gemfile that you run your rails app against. It can be easily done by adding some terminal aliases to use a different gemfile file. I made that in the current configuration and it works well.

# ~/.gitignore_global
# Ignore Gemfile.local
/Gemfile.local
/Gemfile.local.lock
# ~/.zshrc or ~/.bashrc
# this works to allow you to write commands like this:
# - $ be rails s # this will run the rails server using gemfile.local
# - $ be rspec spec # this will run rspec using gemfile.local
# - $ b # this will run bundle install but using gemfile.local
# if you run any of the usual commands then gemfile.local is ignored
alias restart_rails='kill -9 `cat tmp/pids/server.pid`; rails server -d'
alias kill_rails='kill -9 `cat tmp/pids/server.pid`'
__bundle_custom () {
if [ -f Gemfile.local ]
then
echo 'Doing regular bundle'
bundle $@
echo 'Copy the regular gemfile.lock file to gemfile.local.lock'
__bundlelock_copy_to_local
echo 'Run bundle with gemfile.local'
BUNDLE_GEMFILE="Gemfile.local" bundle $@
else
bundle $@
fi
}
__bundle_exec_custom () {
if [ -f Gemfile.local ]
then
BUNDLE_GEMFILE="Gemfile.local" bundle exec $@
else
bundle exec $@
fi
}
__bundle_exec_rails_custom () {
if [ -f Gemfile.local ]
then
BUNDLE_GEMFILE="Gemfile.local" bundle exec rails $@
else
bundle exec rails $@
fi
}
__bundlelock_copy_to_local () {
if [ ! -f Gemfile.lock ]
then
echo "File Gemfile.lock does not exists"
exit 1
fi
# copy file
cp Gemfile.lock Gemfile.local.lock
if [ $? != 0 ]
then
echo 'Problem copying file'
else
echo 'File copied successfully'
fi
}
# Rails aliases
alias b='__bundle_custom'
alias be='__bundle_exec_custom'
alias brails='__bundle_exec_rails_custom'
alias copy_local_lock='__bundlelock_copy_to_local'
# Add this file in the root of your rails app.
gemfile = File.join(File.dirname(__FILE__), 'Gemfile')
if File.readable?(gemfile)
puts "Loading #{gemfile}..." if $DEBUG
instance_eval(File.read(gemfile))
end
group :development,:test do
gem "binding_of_caller"
gem 'better_errors'
gem 'pry'
gem 'dotenv-rails'
gem "thin"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment