开发者

ruby: code to install gem if missing

is there some ruby code I can use to install a gem from a local file, if that gem is not installed?

i'm thinking it would look something like:

if !gem_installed("some gem name")
  system "gem install -l local_copy.gem"
end

i don't know if anything exists that lets me check for gem开发者_如何学JAVAs like this or not...


Checking availability is covered in this previous StackOverflow Quesiton

begin
  gem "somegem"
  # with requirements
  gem "somegem", ">=2.0"
rescue Gem::LoadError
  # not installed
end

or

matches = Gem.source_index.find_name(gem.name, gem.version_requirements)

As for the install, it looks like rails uses the system for gem install also

 puts %x(#{cmd})


This is my way of doing this

['json','date','mail'].each { |req|
    begin
        gem req
    rescue Gem::LoadError
        puts " -> install gem " + req
        Gem.install(req)
        gem req
    end
    require req
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜