Get Version



A Ruby tool to create dependency graph images from any kind of file.


How it works

DepGraph uses Graphviz to create a dependency graph from a list of nodes.

The list of nodes depends on the required dependency type that is choosen at the command line. A dependency type is just a name associated to the knowledge needed to get this list of nodes. Dependency types and its associated knowledge can be extended through a yaml file or through the addition of classes that implement a simple interface.

The current version of DepGraph includes three types of sample dependencies (see dependency_types.yaml and the nodefinders directory):

The program is very extensible. You can create your own dependency types and send them to me!!

Demonstration of usage

Let’s see some examples of how you can use DepGraph from the command line.

You can always type depgraph --help to see a summary of the available options.

Example I

Create the dependency graph of all the installed gems. We just need to tell DepGraph that we will use the gems dependency type:

depgraph -type gems

Example II

Create the dependency graph of all the C# projects found under the myprojects directory:

cd myprojects
depgraph -type csproj

This will create the file dependency_graph.png under the myprojects directory. The generated image will look similar to this one:

We can simplify the graph by applying a transitive reduction like this:

depgraph -type csproj -trans

This is very useful when we need to test a software system, as it let’s us see what components can get broken when we change some code or detect a bug.

Example III

Create a dependency graph for all the requires dependencies found in all Ruby files found in directories dir1 and dir2/subdir1:

depgraph -type ruby_requires -dirs "dir1, dir2/subdir1"

Example IV

Show all the C# projects with a name starting with BusinessLayer that have dependencies to projects that end with DataLayer:

depgraph -type csproj -from "^BusinessLayer" -to "DataLayer$"

As you can see, you can define this filters using regular expressions.

Note that the DepGraph built in dependency types (C# projects and Ruby require statements) identify projects by name, not by the complete path. So we assume that project names are unique.

Example V

Create a dependency graph for all the requires dependencies of Ruby files excluding nodes named client and server:

depgraph -type ruby_requires -exc "client, server"

Example VI

Create a dot file output. This is very useful if we want to do further processing with Graphviz command line filters:

depgraph -type csproj -output

Extending DepGraph

Regexp based dependency types

You can create your own regexp based dependency type just by inserting the appropiate entry in the dependency_types.yaml file that can be found in the gem source directory. This entry must have the following structure:

As an example, this is the csproj entry:

  file_name_pattern: "*.csproj"
  dependable_regexp: !ruby/regexp /"([^"]*\\)?([^\\"]+?)\.(csproj|dll)"/
  capture_group_index: 1

Dynamically loaded dependency types

If you find out that the regexp based dependency types are not powerful enough for your needs, you can always extend DepGraph by creating a new NodeFinder class under the nodefinders directory. This class has to implement two methods: Besides this, you must obey the naming convention:

If you follow this rules you can type

depgraph -type [nodetype]
and you create your brand new custom dependency type!!

Here you have an example that is included with DepGraph, just check the lib/nodefinders folder:

require 'node'

module DepGraph
  module NodeFinders
    class TestNodeFinder
      def location=(loc)
        #we will ignore location in this example
      def get_nodes
        #let's return a hardcoded graph with 2 nodes and
        #1 dependency between them
        node1 ='node1')
        node2 ='node2')
        return [node1, node2] 


How to submit patches

Read the 8 steps for fixing other people’s code and for section 8b: Submit patch to Google Groups, use the Google Group above. Please include rspec tests.

The trunk repository is svn:// for anonymous access.


This code is free to use under the terms of the MIT license.


Comments are welcome. Send an email to Daniel Cadenas Nión via the forum.

Daniel Cadenas Nión, 21st March 2008
Theme extended from Paul Battley