Rails Gemfile 中分环境选择 source

tomlive · 2014年01月14日 · 最后由 luikore 回复于 2014年01月29日 · 2511 次阅读

之前在一些开源项目中这样的写法:

if ENV['TRAVIS']
  source 'https://rubygems.org'
else
  source 'http://ruby.taobao.org'
end

请教一下各位,如果需要在 development, test, production 中选择不同的 source,不知是否有可能?谢谢

用 ENV['RAILS_ENV'] 试试?

我怎么感觉不对啊,这个 source 只在 bundle install 时候才用到,而 bundle install 时候是不启动 Rails 的,如何判断 development、test、production 呢?

可以,Gemfile 是普通 ruby 代码,通过环境变量甚至读文件通过系统或者用户做判断都可以

@luikore 能给一段 demo 的代码么

#4 楼 @tomlive ... 例

env = ENV['RACK_ENV'] || ENV['RAILS_ENV']
case env
when /test/i
  source 'http://ruby.taobao.org'
when /production/i
  source 'http://my.source.com'
else
  source 'https://rubygems.org'
end
需要 登录 后方可回复, 如果你还没有账号请 注册新账号