Reputation: 434
Im writing a script to get all gems from the gemfile from a rails project.Script goes something like this
@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
Is there a better way to do it ? Because i also get issues when something like below is mentioned in gemfile and other possibilities too
gem 'rspec-rails',:group => [:test,:development]
Thanks
Upvotes: 3
Views: 2909
Reputation: 724
Just in case you want to parse from Gemfile.lock given. Then the below is a tip for me:
Bundler::LockfileParser.new(Bundler.read_file("Gemfile.lock"))
Upvotes: 1
Reputation: 85
For anyone stumbling across this question years later like myself, I found that you can get access to the gems referenced in the Gemfile without actually running the gemfile by doing something like the following:
Bundler::Definition.build('Gemfile', nil, {}).dependencies
'Gemfile' here should be the path to the Gemfile from the current file.
Credit goes to Troy Verbrugge for digging into the bundler code.
Upvotes: 6
Reputation: 682
use something like this:
repos = Gem.loaded_specs.values
repos.each do |repo|
g_em = repo.name
s_ource = repo.source
end
Upvotes: 2
Reputation: 19879
Gemnasium-parser might be a good solution
https://github.com/gemnasium/gemnasium-parser
You can also use Bundler to do it directly, but you run the risk of evaluating ruby code you don't know is in there.
You can also use Bundler to parse the Gemfile.lock which avoids the eval issue.
Upvotes: 0