我正在编写脚本以从Rails项目的gemfile中获取所有gem。脚本的内容类似于此
@gem_file_path=File.expand_path('../Gemfile', __FILE__) || ENV['BUNDLE_GEMFILE'] || Dir.pwd + "/Gemfile"
File.open(gem_file_path).read.each_line do |i|
i=i.split
if i[0] == "gem" && i[0].start_with?("gem")
name = i[1].gsub(",","")
gem_name= eval name
author_email_id=Gem::Specification.find_by_name(gem_name.to_s).email
author_name=Gem::Specification.find_by_name(gem_name).author
end
end
有更好的方法吗?因为当在gemfile中提到以下类似内容时,我也会遇到问题]
gem 'rspec-rails',:group => [:test,:development]
谢谢
使用类似这样的内容:
repos = Gem.loaded_specs.values
repos.each do |repo|
g_em = repo.name
s_ource = repo.source
end
Gemnasium-parser可能是一个很好的解决方案
https://github.com/gemnasium/gemnasium-parser
您也可以使用Bundler直接执行此操作,但是冒着评估您不知道的Ruby代码的风险。
您还可以使用Bundler解析Gemfile.lock,这避免了评估问题。
对于几年后像我这样绊脚石的人,我发现您可以通过执行以下操作来访问Gemfile中引用的gem,而无需实际运行gemfile:
Bundler::Definition.build('Gemfile', nil, {}).dependencies
'Gemfile'这里应该是当前文件中Gemfile的路径。
信用归功于Troy Verbrugge,以研究捆绑程序代码。