Running brew inside rake task

We wanted to run a brew command inside a rake to check if certain package is installed and then run the install.

This is the code to do it would like this:

namespace :phantomjs do
desc “Installs Phantomjs on Mac”
 task :install do |task_name|
log_task(task_name)
if system(“brew list | grep phantomjs”)
puts “#{package} is already installed. Skipping”
else
system(“brew install phantomjs”)
end
end
end

But this failed on my machine with the following error:

# rake phantomjs:install
/Users/uday/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/bundler-1.10.6/lib/bundler/spec_set.rb:92:in `block in materialize’: Could not find rake-10.4.2 in any of the sources (Bundler::GemNotFound)
from /Users/uday/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/bundler-1.10.6/lib/bundler/spec_set.rb:85:in `map!’
from /Users/uday/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/bundler-1.10.6/lib/bundler/spec_set.rb:85:in `materialize’
from /Users/uday/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/bundler-1.10.6/lib/bundler/definition.rb:140:in `specs’
from /Users/uday/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/bundler-1.10.6/lib/bundler/definition.rb:185:in `specs_for’
from /Users/uday/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/bundler-1.10.6/lib/bundler/definition.rb:174:in `requested_specs’
from /Users/uday/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/bundler-1.10.6/lib/bundler/environment.rb:18:in `requested_specs’
from /Users/uday/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/bundler-1.10.6/lib/bundler/runtime.rb:13:in `setup’
from /Users/uday/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/bundler-1.10.6/lib/bundler.rb:127:in `setup’
from /Users/uday/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/bundler-1.10.6/lib/bundler/setup.rb:18:in `<top (required)>’
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require’
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require’
Could not find rake-10.4.2 in any of the sources
Run `bundle install` to install missing gems.

After analysing it, found out that the reason for this error is because brew is also written in ruby and bundler environment is inherited by the sub-shell we are opening with system command.

It seems the correct way of running a sub process in a rake is to use Bundler.with_clean_env. And the rake task will look below now:

namespace :phantomjs do
desc “Installs Phantomjs on Mac”
task :install do |task_name|
log_task(task_name)
   Bundler.with_clean_env do
if system(“brew list | grep phantomjs”)
puts “#{package} is already installed. Skipping”
else
system(“brew install phantomjs”)
end
end
end
end

More explanation about this issue can be found at: http://www.rake.build/fascicles/003-clean-environment.html

And check the “Shelling out” section in the bundle-exec documentation: http://bundler.io/v1.10/man/bundle-exec.1.html