giraffe 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README +31 -0
  2. data/bin/giraffe +12 -0
  3. data/lib/giraffe.rb +71 -0
  4. metadata +66 -0
data/README ADDED
@@ -0,0 +1,31 @@
1
+ = Giraffe
2
+
3
+ Generate graphs of the partials your Rails app uses
4
+
5
+ == Install
6
+
7
+ $ gem install giraffe
8
+
9
+ == Dependencies
10
+
11
+ * ruby-graphviz
12
+ * graphviz
13
+
14
+ == Usage
15
+
16
+ g = Giraffe.new("/path/to/app/views")
17
+ g.generate("graph.png")
18
+
19
+ Or, more succinctly:
20
+
21
+ Giraffe.generate("/path/to/app/views", "graph.png")
22
+
23
+ == giraffe commandline
24
+
25
+ $ giraffe /path/to/app/views/ graph.png
26
+
27
+ == Todo
28
+
29
+ It's pretty naive at the moment, doesn't deal with anything other than partials included by name etc...
30
+
31
+ Could do with being a bit more intelligent.
data/bin/giraffe ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'lib/giraffe.rb'
5
+
6
+ path, out = ARGV
7
+
8
+ abort "please specify a template path and output image (giraffe /path/to/templates out.png)" unless path && out
9
+
10
+ Giraffe.generate(path, out)
11
+
12
+ puts "done"
data/lib/giraffe.rb ADDED
@@ -0,0 +1,71 @@
1
+ require 'graphviz'
2
+
3
+ class Giraffe
4
+
5
+ PARTIAL_REGEX = /render\W+:partial\W+=>\W+"([\w\/]+)"/
6
+
7
+ def self.generate(path, outfile)
8
+ new(path).generate(outfile)
9
+ end
10
+
11
+ def initialize(path)
12
+ @path = path
13
+ @graph = GraphViz.new(:G, :type => :digraph)
14
+ @nodes = {}
15
+ end
16
+
17
+ def generate(outfile)
18
+ Dir.chdir(@path) do
19
+ Dir.glob("**/*.*").each do |file|
20
+ parse_file(file)
21
+ end
22
+ end
23
+ write_graph(outfile)
24
+ end
25
+
26
+ def write_graph(outfile)
27
+ @graph.output( :png => outfile )
28
+ end
29
+
30
+ def parse_file(path)
31
+ File.open(path, 'r').each_line do |line|
32
+ if match = line.match(PARTIAL_REGEX)
33
+ add_edge(normalize(path), find_template(partial_path(path, match[1])))
34
+ end
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ def partial_path(container, partial)
41
+ unless partial.include? "/"
42
+ normalize(File.join(File.dirname(container), "_#{partial}"))
43
+ else
44
+ normalize(File.join(File.dirname(partial), "_#{File.basename(partial)}"))
45
+ end
46
+ end
47
+
48
+ def normalize(path)
49
+ File.expand_path(path, "/")
50
+ end
51
+
52
+ def find_template(name)
53
+ "#{name}#{template_type(name)}"
54
+ end
55
+
56
+ def template_type(name)
57
+ full_path = File.join(@path, name)
58
+ if template = Dir.glob("#{full_path}.*")[0]
59
+ template.match(/(\..+$)/)[1]
60
+ end
61
+ end
62
+
63
+ def add_edge(template, partial)
64
+ @graph.add_edge(add_node(template), add_node(partial))
65
+ end
66
+
67
+ def add_node(path)
68
+ @nodes[path] ||= @graph.add_node(path)
69
+ end
70
+
71
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: giraffe
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Richard Livsey
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-21 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: ruby-graphviz
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: Generate graphs of your Rails app's partials
26
+ email: richard@livsey.org
27
+ executables:
28
+ - giraffe
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README
33
+ files:
34
+ - README
35
+ - lib/giraffe.rb
36
+ has_rdoc: true
37
+ homepage: http://github.com/rlivsey/giraffe
38
+ licenses: []
39
+
40
+ post_install_message:
41
+ rdoc_options:
42
+ - --main
43
+ - README
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.3.5
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: Rails template graphing
65
+ test_files: []
66
+