What
A Ruby tool to create dependency graph images from any kind of file.
Installing
- Install Graphviz from here.
- Install DepGraph:
In linux:
sudo gem install DepGraph
In windows:gem install DepGraph
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):
- Installed Gem dependencies
- Ruby requires statements
- C# projects (csproj files)
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 test.dot
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:
- A name for the dependency type.
- A file pattern filter.
- A regular expression to tell DepGraph how to find dependencies inside each file.
- The regular expression capture group index. Will be ignored if none is defined.
As an example, this is the csproj entry:
csproj: 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:- location: which is a setter that receives an array of strings representing the source in which the nodes can be found. This will be directories most of the time but it depends on your implementation. So they could be, for example, urls.
- get_nodes: this method returns an array of Node objects, the graph you want to display.
- the file must be named
[nodetype]_node_finder.rb - the class must be named
[Nodetype]NodeFinder. Notice the first letter in uppercase.
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 end def get_nodes #let's return a hardcoded graph with 2 nodes and #1 dependency between them node1 = Node.new('node1') node2 = Node.new('node2') node1.depends_on(node2) return [node1, node2] end end end end
Forum
http://groups.google.com/group/DepGraph
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://rubyforge.org/var/svn/depgraph/trunk for anonymous access.
License
This code is free to use under the terms of the MIT license.
Contact
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