How to See the Dependency Tree Just from Gemfile

How to see the dependency tree just from Gemfile?

gem dependency (with no args) should show you all gems from current system with their dependencies.

bundle exec gem dependency will show you for the current Gemfile

Edit:

You can also do gem dependency -R (or just dep instead of dependency) if you want to find out which gems use specific (or all) gems.

For deeper dependencies I'd parse output (regex maybe?) of first gem dependencies, pick gem's names and call gem dep on each of them, but that's just a loose idea.

How do I find out which gem has a specific dependency?

In the bash shell you can do:

gem dependency name_of_the_gem --reverse-dependencies

For instance:

$ gem dependency activesupport --reverse-dependencies                        
Gem activesupport-2.3.14
Used by
actionpack-2.3.14 (activesupport (= 2.3.14))
activerecord-2.3.14 (activesupport (= 2.3.14))
activeresource-2.3.14 (activesupport (= 2.3.14))

How to trace and check dependencies in bundled Ruby gems

To see a visual representation of the dependency tree run bundle viz:

apt-get install graphviz && gem install ruby-graphviz && bundle viz

It will generate a PNG file of the tree.

How to list ruby production only dependencies using Gemfile.lock and LockfileParser class

I will leave my other answer in case it helps someone else: Here is the revised version based on what I think you want

require 'bundler'

class DependencyTree
attr_reader :definition
def initialize(gemfile,lockfile)
@gemfile = gemfile
@definition = Bundler::Definition.build(gemfile,lockfile,nil)
end

def all_dependencies
return @all_dependencies if @all_dependencies
collect_dependencies
end

def inspect
"#<#{self.class.name}:#{self.object_id} Gemfile: #{Pathname.new(@gemfile).expand_path} >"
end

def lock_file
@definition.locked_gems
end

def to_h
lock_file.specs.each_with_object(Hash.new {|h,k| h[k] = []}) do |lock,obj|
gem_file_dep = all_dependencies.detect {|dep| dep[:name] == lock.name} || {group: :unknown}
name = lock.full_name.dup
name << " (#{gem_file_dep[:error]})" if gem_file_dep[:error]
obj[gem_file_dep[:group]] << name
end
end

private
def groupify(dep)
dep.groups.map do |g|
a = [{group: g, name: dep.name}]
begin
a << runtime_dependencies(g,dep.to_spec)
rescue Gem::LoadError => e
a[-1] = {group: g, name: dep.name,error: 'NOT INSTALLED'}
end
end
end
def collect_dependencies
@all_dependencies = @definition.dependencies.map do |dep|
groupify(dep)
end.flatten
group_missing
@all_dependencies.uniq!
end
def runtime_dependencies(group,spec)
spec.dependencies.select { |dep| dep.type == :runtime}.map do |dep|
a = {group: group, name: dep.name}
dep.to_spec.dependencies.empty? ? a : [a] << runtime_dependencies(group,dep.to_spec)
end
end
def group_missing
all_locks.cycle(2) do |a|
deep_dep = @all_dependencies.find_all {|h| a.include?(h[:name])}.uniq
a.each do |k|
deep_dep.each do |h|
all_dependencies << {group: h[:group], name: k, error: 'NOT INSTALLED'}
end
end
end
end
def all_locks
lock_file.specs.map do |spec|
spec.to_lock.delete(' ').split("\n").map do |s|
s.slice(/^[\w\-]+/)
end
end
end
end

the usage is:

 dt = DependencyTree.new('Gemfile','Gemfile.lock') 
dt.to_h

output Snippet:

{:default=>
["actionmailer-4.2.5.2 (NOT INSTALLED)",
"actionpack-4.2.5.2",
"actionview-4.2.5.2",
"activejob-4.2.5.2 (NOT INSTALLED)",
"activemodel-4.2.5.2",
"activerecord-4.2.5.2",
"activerecord-sqlserver-adapter-4.2.17",
"activesupport-4.2.5.2",
"arel-6.0.3 (NOT INSTALLED)",
"axlsx-2.0.1 (NOT INSTALLED)",
"binding_of_caller-0.7.2 (NOT INSTALLED)",
"builder-3.2.3",
"coffee-rails-4.1.1 (NOT INSTALLED)",
"coffee-script-2.4.1 (NOT INSTALLED)",
"coffee-script-source-1.12.2 (NOT INSTALLED)",
"concurrent-ruby-1.0.4",
"debug_inspector-0.0.2 (NOT INSTALLED)",
"erubis-2.7.0",
"execjs-2.7.0"],
:development=>
["airbrussh-1.1.2",
"byebug-9.0.6 (NOT INSTALLED)",
"capistrano-3.7.2"],
:doc => ["sdoc-0.4.2 (NOT INSTALLED)"]}

production gems will be in :default development gems would be :default + :development

How can I find out what gem is dependent on termios in my Gemfile?

Check your Gemfile.lock - it has all the gems and their dependencies listed in it. As long as you've been able to install these gems in the past, you'll be able to tell where that dependency is coming from.

How do I figure out which gem(s) caused a particular gem to be bundled?

Normally Gemfile.lock documents which dependencies were generated from other dependencies, it's listed in a rough tree form, but you may have to do some digging to get to the right spot.

Can I use a Gemfile to manage dependencies without generating a Gemfile.lock file?

AFAIK, it's not possible - but I do believe that it is essentially an optimization and snapshot tool. After Bundler resolves all the versions required to satisfy your gemset, it will write the dependency tree to Gemfile.lock, so you know exactly what set you're working with. I believe you can just blow away the Gemfile.lock file, but you will then require Bundler to do the dependency resolution anew every time, and, if dependency versions change, you may find your dependencies messed up because you don't have a snapshot (lock) of a known working state.

In other words, if you're happy with your current state, Gemfile.lock will record your state so you know what you're dealing with - but I don't think it's required for bundler to do its dependency management thing. Blow it away if you don't want it.

Find out which gems require native c extensions from a Gemfile?

You can use JRuby Lint for that. It will will check for some gems requiring C extension and even list alternative (based on this list).



Related Topics



Leave a reply



Submit