ruby-codegraph 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 Jan Xie (jan.h.xie@gmail.com)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,25 @@
1
+ Graph For Your Ruby Code
2
+ ========================
3
+
4
+ Use `graphviz` to generate class inheritance graph for your ruby code.
5
+
6
+ Usage
7
+ -----
8
+
9
+ Basic usage:
10
+
11
+ ```shell
12
+ > codegraph <dir>
13
+ > open output.png
14
+ ```
15
+
16
+ Get help:
17
+
18
+ ```shell
19
+ > codegraph -h
20
+ ```
21
+
22
+ Copyright
23
+ ---------
24
+
25
+ [MIT License](https://github.com/janx/codegraph/blob/master/LICENSE)
data/bin/codegraph ADDED
@@ -0,0 +1,58 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $:.push File.expand_path("../../lib", __FILE__)
4
+
5
+ require 'optparse'
6
+ require 'codegraph'
7
+
8
+ options = {
9
+ graph_name: 'Code Graph',
10
+ engine: 'dot',
11
+ type: 'inheritance',
12
+ output: 'output',
13
+ format: 'png'
14
+ }
15
+
16
+ OptionParser.new do |opts|
17
+ opts.banner = <<USAGE
18
+ Usage:
19
+
20
+ codegraph [options] <directory>
21
+
22
+ USAGE
23
+
24
+ opts.separator "Options:"
25
+
26
+ opts.on("-n", "--name [NAME]", "Specify graph name") do |v|
27
+ options[:graph_name] = v
28
+ end
29
+
30
+ opts.on("-o", "--output [FILE]", "Specify a output file") do |v|
31
+ options[:output] = v
32
+ end
33
+
34
+ opts.on("-f", "--format [FORMAT]", "Specify the format of output, default to PNG") do |v|
35
+ options[:format] = v
36
+ end
37
+
38
+ opts.on("-t", "--type [TYPE]", %w(inheritance), "Graph type (inheritance). Default to inheritance") do |v|
39
+ options[:type] = v
40
+ end
41
+
42
+ opts.on("-e", "--engine [ENGINE]", %w(dot neato twopi fdp circo), "Layout engine for graphviz (dot, neato, twopi, fdp, circo), default to dot") do |v|
43
+ options[:engine] = v
44
+ end
45
+
46
+ opts.on("-v", "--version", "Show version") do |v|
47
+ puts "CodeGraph v#{CodeGraph::VERSION}"
48
+ end
49
+ end.parse!
50
+
51
+ if ARGV.size < 1
52
+ STDERR.puts "Error: You must supply a directory. Type 'codegraph -h' to get more information."
53
+ exit 1
54
+ end
55
+
56
+ klass = CodeGraph.const_get(options[:type].capitalize)
57
+ graph = klass.new(ARGV[0], options)
58
+ graph.generate
@@ -0,0 +1,72 @@
1
+ module CodeGraph
2
+
3
+ class Inheritance
4
+
5
+ CLASS_REGEXP = /class\s+([A-Z][\w:]*)(?:\s*<\s*([A-Z][\w:]*))?/
6
+
7
+ def initialize(dir, options={})
8
+ @dir = dir
9
+ @options = options
10
+ @nodes = {}
11
+ @edges = {}
12
+
13
+ @g = GraphViz.new(
14
+ @options[:graph_name],
15
+ label: label,
16
+ type: :digraph,
17
+ use: @options[:engine],
18
+ rankdir: 'RL'
19
+ )
20
+ end
21
+
22
+ def generate
23
+ directory_scan
24
+ output
25
+ end
26
+
27
+ private
28
+
29
+ def label
30
+ "#{@options[:graph_name]} for #{File.expand_path @dir} at #{Time.now}"
31
+ end
32
+
33
+ def directory_scan
34
+ Dir["#{@dir}/**/*.rb"].each {|rb| class_scan(rb)}
35
+ end
36
+
37
+ def class_scan(file)
38
+ File.readlines(file).each do |line|
39
+ _, klass, parent = line.match(CLASS_REGEXP).to_a
40
+ if klass
41
+ add_node(klass)
42
+ if parent
43
+ add_node(parent)
44
+ add_edge(klass, parent)
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ def add_node(name)
51
+ if !@nodes.has_key?(name)
52
+ @nodes[name] = @g.add_nodes(name, shape: 'box')
53
+ end
54
+ end
55
+
56
+ def add_edge(from, to)
57
+ @edges[from] ||= {}
58
+ from_node, to_node = @nodes[from], @nodes[to]
59
+ if from_node && to_node && !@edges[from].has_key?(to)
60
+ @edges[from][to] = @g.add_edges(from_node, to_node, arrowhead: 'onormal')
61
+ end
62
+ end
63
+
64
+ def output
65
+ output_file = @options[:output]
66
+ output_file = "#{output_file}.#{@options[:format]}" if output_file !~ /\.#{@options[:format]}$/
67
+ @g.output(@options[:format] => output_file)
68
+ end
69
+
70
+ end
71
+
72
+ end
@@ -0,0 +1,3 @@
1
+ module CodeGraph
2
+ VERSION = "0.0.1"
3
+ end
data/lib/codegraph.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'graphviz'
2
+ require 'codegraph/version'
3
+ require 'codegraph/inheritance'
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-codegraph
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jan Xie
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: ruby-graphviz
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.0.0
30
+ description: Use codegraph to generate all kinds of graphviz graphs for you code,
31
+ to visualize things like class hierarchy.
32
+ email:
33
+ - jan.h.xie@gmail.com
34
+ executables:
35
+ - codegraph
36
+ extensions: []
37
+ extra_rdoc_files: []
38
+ files:
39
+ - lib/codegraph.rb
40
+ - lib/codegraph/version.rb
41
+ - lib/codegraph/inheritance.rb
42
+ - bin/codegraph
43
+ - LICENSE
44
+ - README.markdown
45
+ homepage: https://github.com/janx/codegraph
46
+ licenses: []
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubyforge_project:
65
+ rubygems_version: 1.8.23
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Generate graphviz graphs for your code.
69
+ test_files: []