Reputation: 987
How can I write a rake task to run a bundle install in a different project? I have create a project with just rake, and written tasks like this
task :bundle do
projects.each do |name, repo|
if Dir.exists?("../#{name}")
exec("cd ../#{name} && bin/bundle install")
end
end
end
But when I run this I get:
Using rake 10.3.2
Using bundler 1.9.6
Bundle complete! 1 Gemfile dependency, 2 gems now installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.
Which looks ok at first, but is in fact the bundle install
for the current rake-only project and not the target rails project.
I also tried back ticks
puts `cd ../#{name} && bin/bundle install`
But it did the same. I also tried just bundle install
instead of bin/bundle install
, but it didn't work.
When I run it on the command like directly it does what I expect:
Using rake 10.4.2
Using CFPropertyList 2.3.1
...
...
Using turbolinks 2.5.3
Using uglifier 2.7.1
Bundle complete! 34 Gemfile dependencies, 120 gems now installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.
How do I get it to do the right bundle install
?
Upvotes: 2
Views: 2036
Reputation:
Couple of things to note on this
bin/bundle
will only work if the bundle
files exists in in bin
directory
exec('echo "Hello World"')
- runs the command and replaces the current process with it.system('echo "Hello World"')
- runs the command in a subshell and captures the exit status code`echo "hello world"`
- runs the command in a subshell and captures all output to the STDOUT and STDERR but not the exit status code.To the why it didn't work
You need to provide bundler with a clean environment so it knows that you are bundling a different project. surround the commands within your rake task with Bundler.with_clean_env
block as per below
task :bundle do
Bundler.with_clean_env do
projects.each do |name, repo|
if Dir.exists?("../#{name}")
exec("cd ../#{name} && bundle install")
end
end
end
end
Upvotes: 5