是否可以从ruby系统调用中运行bundle install ?
我正在尝试安装gems并在另一个路径下为项目运行测试…
例如:
"cd /some/other/project && bundle install && gem list && rspec spec"
理想情况下,我想只是通过一个项目中的rake文件运行测试,同时确保该项目的相关宝石安装。
cd似乎工作正常,如果我运行:
"cd /some/other/project && pwd"
它给出了正确的路径。但是如果我要bundle install &&在gem环境中,它似乎为当前文件夹安装gems,而不使用来自其他项目的Gemfile,随后rspec规范不起作用。
总而言之,在rakefile中的另一个项目中运行'rspec spec'的最佳方式是什么?
实际上,实现这种行为的官方方式看起来是这样的:
Bundler.with_clean_env do
system "shell out"
end
我在google groups中找到了答案:https://groups.google.com/d/msg/ruby-bundler/UufhzrliWfo/d51B_zARksUJ
编辑:我想我有办法了。看看这是否适合你:
#@pwd is the "working directory of the execution...
Dir.chdir @pwd do
so = ""
vars = {
"BUNDLE_GEMFILE" => nil,
"BUNDLE_BIN_PATH" => nil,
"RUBYOPT" => nil,
"rvm_" => nil,
"RACK_ENV" => nil,
"RAILS_ENV" => nil,
"PWD" => @pwd
}
options = {
:chdir=>@pwd
}
Open3.popen3(vars, cmd, options) do |stdin, stdout, stderr|
stdin.close_write
so = stdout.read
so = stderr.read if so.nil? || so == ""
end
so
end
原始文章:我要把头发都扯掉了。我认为这与bundle exec|install|update设置环境变量有关,当你启动应用程序时,我已经尝试过
bash -c "cd ../other/; bundle install; and it fails"
我试过使用open3。Popen ("bundle install",:chdir=>"./other")
如果这是什么安慰你没有疯,但我似乎不知道如何解决它。
我也试过open3。Popen ("bundle install", {:chdir=>"../other",:unsetenv_others => false})但这最终会从系统路径中删除RVM;
除了kangguru的回答,您可能还需要做
bundle install --deployment
所以Bundler。With_clean_env不会被rvm弄乱。这会将所有gem的副本安装到项目根目录下的.vendor/bundle中,然后由Bundler拾取。with_clean_env命令。
(我本来想把这个作为评论,但是我没有50+的声誉)