问题
I have a gem that exists simply to collect several engines together for ease of implementation, as well as to provide a few utility methods to all of the included engines. One such utility method is a rake task to release new versions of all of the collected engines.
My problem is that, when I run code that should execute in the context of one of the collected engines it all (git commands, file system manipulation, etc.) works, except for the rake build
command. For some reason, that command is somehow running in the context of the umbrella gem, and is picking up the version number from its Gemfile.lock.
Using this code for an example:
Dir.chdir( PATH_TO_COLLECTED_ENGINE ) do
# Below lie all of my failed build attempts, all of which failed in the same way...
#p sh( 'bundle' , 'exec' , 'rake' , 'build' )
#p sh( 'bundle exec rake build' )
#p `bundle exec rake build`
#thr = Thread.new {
# p `bundle exec rake build`
#}
#thr.join
#load File.join(Dir.pwd, 'Rakefile')
#Rake::Task['build'].invoke
#p `gem build #{ PATH_TO_COLLECTED_ENGINE_GEMSPEC_FILE }`
end
When run from a standard Ruby file, I get the desired output of collected_engine_a 3.12.9 built to pkg/collected_engine_a-3.12.9.gem
, but when running from a rake task in the umbrella gem, I get the puzzling output of collected_engine_a 3.12.9 built to pkg/collected_engine_a-3.12.2.gem
. It seems that the version is being derived from the Gemfile.lock of the umbrella gem (If I adjust the version there, it affects the output).
I've tried both with and without a bundle exec
preface, with basically the same result.
Can anyone think of a way to get this to pick up the correct context, or am I stuck moving these out of a rakefile, and into a standard script (renaming the output file is a poor option that will not be considered)?
回答1:
bundle exec
has a specific behavior when launched in a subshell. You can see the following note in bundle help exec
:
make sure that if bundler is invoked in the subshell, it uses the same Gemfile (by setting BUNDLE_GEMFILE)
So in your case, you would do:
bundle exec rake build BUNDLE_GEMFILE=#{PATH_TO_COLLECTED_ENGINE}/Gemfile
来源:https://stackoverflow.com/questions/16554397/rake-build-is-operating-in-an-incorrect-context