Rails Gemfile 中分环境选择 source

tomlive · January 14, 2014 · Last by luikore replied at January 29, 2014 · 2511 hits

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

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
You need to Sign in before reply, if you don't have an account, please Sign up first.