shift-railroad 0.5.8

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
@@ -0,0 +1,166 @@
1
+ = RailRoad
2
+
3
+ RailRoad generates models and controllers diagrams in DOT language for a
4
+ Rails application.
5
+
6
+ This is a patched version based on the original v0.5.0.
7
+
8
+ I (Peter Hoeg) am not trying to hijack Javier's project, but since he hasn't released any
9
+ new versions since May '08, I figured I'd better put one out in order to make
10
+ railroad work with rails v2.3.
11
+
12
+ = Usage
13
+
14
+ Run RailRoad on the Rails application's root directory. You can redirect its
15
+ output to a .dot file or pipe it to the dot or neato utilities to produce a
16
+ graphic. Model diagrams are intended to be processed using dot and
17
+ controller diagrams are best processed using neato.
18
+
19
+ railroad [options] command
20
+
21
+ == Options
22
+
23
+ Common options:
24
+ -b, --brief Generate compact diagram
25
+ (no attributes nor methods)
26
+ -e, --exclude file1[,fileN] Exclude given files
27
+ -i, --inheritance Include inheritance relations
28
+ -l, --label Add a label with diagram information
29
+ (type, date, migration, version)
30
+ -o, --output FILE Write diagram to file FILE
31
+ -v, --verbose Enable verbose output
32
+ (produce messages to STDOUT)
33
+
34
+ Models diagram options:
35
+ -a, --all Include all models
36
+ (not only ActiveRecord::Base derived)
37
+ --hide-magic Hide magic field names
38
+ --hide-types Hide attributes type
39
+ -j, --join Concentrate edges
40
+ -m, --modules Include modules
41
+ -p, --plugins-models Include plugins models
42
+ -t, --transitive Include transitive associations
43
+ (through inheritance)
44
+
45
+ Controllers diagram options:
46
+ --hide-public Hide public methods
47
+ --hide-protected Hide protected methods
48
+ --hide-private Hide private methods
49
+
50
+ Other options:
51
+ -h, --help Show this message
52
+ --version Show version and copyright
53
+
54
+ == Commands
55
+
56
+ -M, --models Generate models diagram
57
+ -C, --controllers Generate controllers diagram
58
+ -A, --aasm Generate "acts as state machine" diagram
59
+
60
+
61
+ == Examples
62
+
63
+ railroad -o models.dot -M
64
+ Produces a models diagram to the file 'models.dot'
65
+ railroad -a -i -o full_models.dot -M
66
+ Models diagram with all classes showing inheritance relations
67
+ railroad -M | dot -Tsvg > models.svg
68
+ Model diagram in SVG format
69
+ railroad -C | neato -Tpng > controllers.png
70
+ Controller diagram in PNG format
71
+ railroad -h
72
+ Shows usage help
73
+
74
+
75
+ = Processing DOT files
76
+
77
+ To produce a PNG image from model diagram generated by RailRoad you can
78
+ issue the following command:
79
+
80
+ dot -Tpng models.dot > models.png
81
+
82
+ If you want to do the same with a controller diagram, use neato instead of
83
+ dot:
84
+
85
+ neato -Tpng controllers.dot > controllers.png
86
+
87
+ If you want to produce SVG (vectorial, scalable, editable) files, you can do
88
+ the following:
89
+
90
+ dot -Tsvg models.dot > models.svg
91
+ neato -Tsvg controllers.dot > controllers.svg
92
+
93
+ Important: There is a bug in Graphviz tools when generating SVG files that
94
+ cause a text overflow. You can solve this problem editing (with a text
95
+ editor, not a graphical SVG editor) the file and replacing around line 12
96
+ "font-size:14.00;" by "font-size:11.00;", or by issuing the following command
97
+ (see "man sed"):
98
+
99
+ sed -i 's/font-size:14.00/font-size:11.00/g' file.svg
100
+
101
+ Note: For viewing and editing SVG there is an excellent opensource tool
102
+ called Inkscape (similar to Adobe Illustrator. For DOT processing you can
103
+ also use Omnigraffle (on Mac OS X).
104
+
105
+ = RailRoad as a rake task
106
+
107
+ (Thanks to Thomas Ritz, http://www.galaxy-ritz.de ,for the code.)
108
+
109
+ In your Rails application, put the following rake tasks into 'lib/task/diagrams.rake':
110
+
111
+ namespace :doc do
112
+ namespace :diagram do
113
+ task :models do
114
+ sh "railroad -i -l -a -m -M | dot -Tsvg | sed 's/font-size:14.00/font-size:11.00/g' > doc/models.svg"
115
+ end
116
+
117
+ task :controllers do
118
+ sh "railroad -i -l -C | neato -Tsvg | sed 's/font-size:14.00/font-size:11.00/g' > doc/controllers.svg"
119
+ end
120
+ end
121
+
122
+ task :diagrams => %w(diagram:models diagram:controllers)
123
+ end
124
+
125
+ Then, 'rake doc:diagrams' produces 'doc/models.svg' and 'doc/controllers.svg'.
126
+
127
+ = Requirements
128
+
129
+ RailRoad has been tested with the following Ruby and Rails versions
130
+
131
+ == Ruby
132
+ * 1.8.5
133
+ * 1.8.7
134
+
135
+ == Rails
136
+ * 1.1.6 to 1.2.3
137
+ * 2.3.2
138
+
139
+ There are no additional requirements (nevertheless, all your Rails application
140
+ requirements must be installed).
141
+
142
+ In order to view/export the DOT diagrams, you'll need the processing tools
143
+ from Graphviz.
144
+
145
+ = Website and Project Home
146
+
147
+ http://railroad.rubyforge.org
148
+
149
+ = License
150
+
151
+ RailRoad is distributed under the terms of the GNU General Public License
152
+ as published by the Free Software Foundation; either version 2 of the
153
+ License, or (at your option) any later version.
154
+
155
+ See LICENSE for details.
156
+
157
+ == Copyright
158
+
159
+ Copyright (c) 2007-2008 Javier Smaldone
160
+ Copyright (c) 2009 Peter Hoeg
161
+
162
+ See LICENSE for details.
163
+
164
+ == Authors
165
+
166
+ See AUTHORS for details.
data/Rakefile ADDED
@@ -0,0 +1,46 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "railroad"
8
+ gem.executables = "railroad"
9
+ gem.email = ['peter@hoeg.com', 'p.hoeg@northwind.sg', 'javier@smaldone.com.ar']
10
+ gem.homepage = "http://github.com/peterhoeg/RailRoad"
11
+ gem.authors = ["Peter Hoeg", "Javier Smaldone"]
12
+ gem.summary = "A DOT diagram generator for Ruby on Rail applications"
13
+ gem.description = gem.summary
14
+ gem.files = FileList["[A-Z]*", "{autotest,bin,lib,spec}/**/*", ".document"]
15
+ gem.extra_rdoc_files = FileList["*.rdoc"]
16
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
+ end
18
+
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
21
+ end
22
+
23
+ require 'spec/rake/spectask'
24
+ Spec::Rake::SpecTask.new(:spec) do |spec|
25
+ spec.libs << 'lib' << 'spec'
26
+ spec.spec_files = FileList['spec/**/*_spec.rb']
27
+ end
28
+
29
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
30
+ spec.libs << 'lib' << 'spec'
31
+ spec.pattern = 'spec/**/*_spec.rb'
32
+ spec.rcov = true
33
+ end
34
+
35
+ task :default => :spec
36
+
37
+ require 'rake/rdoctask'
38
+ require 'lib/version'
39
+ Rake::RDocTask.new do |rdoc|
40
+ rdoc.rdoc_dir = 'rdoc'
41
+ rdoc.title = "railroad #{RailRoad::VERSION::STRING}"
42
+ rdoc.rdoc_files.include('README*')
43
+ rdoc.rdoc_files.include('CHANGELOG*')
44
+ rdoc.rdoc_files.include('AUTHORS*')
45
+ rdoc.rdoc_files.include('lib/**/*.rb')
46
+ end
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 5
4
+ :patch: 8
data/bin/railroad ADDED
@@ -0,0 +1,60 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # RailRoad - RoR diagrams generator
4
+ # http://railroad.rubyforge.org
5
+ #
6
+ # RailRoad generates models and controllers diagrams in DOT language
7
+ # for a Rails application.
8
+ #
9
+ # Copyright 2007-2008 - Javier Smaldone (http://www.smaldone.com.ar)
10
+ #
11
+ # This program is free software; you can redistribute it and/or modify
12
+ # it under the terms of the GNU General Public License as published by
13
+ # the Free Software Foundation; either version 2 of the License, or
14
+ # (at your option) any later version.
15
+ #
16
+
17
+ require File.dirname(__FILE__) + '/../lib/version'
18
+
19
+ APP_NAME = "railroad"
20
+ APP_HUMAN_NAME = "RailRoad"
21
+ APP_VERSION = [RailRoad::VERSION::MAJOR, RailRoad::VERSION::MINOR, RailRoad::VERSION::PATCH]
22
+ COPYRIGHT = "Copyright (C) 2007-2008 Javier Smaldone, 2009 Peter Hoeg"
23
+
24
+ if ARGV.first == '--version'
25
+ puts "#{APP_HUMAN_NAME} v#{RailRoad::VERSION::STRING}"
26
+ exit(0)
27
+ end
28
+
29
+ require 'options_struct'
30
+ require 'models_diagram'
31
+ require 'controllers_diagram'
32
+ require 'aasm_diagram'
33
+
34
+ options = OptionsStruct.new
35
+
36
+ options.parse ARGV
37
+
38
+ old_dir = Dir.pwd
39
+
40
+ Dir.chdir(options.root) if options.root != ''
41
+
42
+ if options.command == 'models'
43
+ diagram = ModelsDiagram.new options
44
+ elsif options.command == 'controllers'
45
+ diagram = ControllersDiagram.new options
46
+ elsif options.command == 'aasm'
47
+ diagram = AasmDiagram.new options
48
+ else
49
+ STDERR.print "#{APP_HUMAN_NAME} v#{RailRoad::VERSION::STRING}\n" +
50
+ "Error: You must supply a command\n" +
51
+ " (try #{APP_NAME} -h)\n\n"
52
+ exit 1
53
+ end
54
+
55
+ diagram.generate
56
+
57
+ Dir.chdir(old_dir)
58
+
59
+ diagram.print
60
+
@@ -0,0 +1,81 @@
1
+ # RailRoad - RoR diagrams generator
2
+ # http://railroad.rubyforge.org
3
+ #
4
+ # Copyright 2007-2008 - Javier Smaldone (http://www.smaldone.com.ar)
5
+ # See COPYING for more details
6
+
7
+ # AASM code provided by Ana Nelson (http://ananelson.com/)
8
+
9
+ require 'app_diagram'
10
+
11
+ # Diagram for Acts As State Machine
12
+ class AasmDiagram < AppDiagram
13
+
14
+ def initialize(options)
15
+ #options.exclude.map! {|e| e = "app/models/" + e}
16
+ super options
17
+ @graph.diagram_type = 'Models'
18
+ # Processed habtm associations
19
+ @habtm = []
20
+ end
21
+
22
+ # Process model files
23
+ def generate
24
+ STDERR.print "Generating AASM diagram\n" if @options.verbose
25
+ files = Dir.glob("app/models/**/*.rb")
26
+ files += Dir.glob("vendor/plugins/**/app/models/*.rb") if @options.plugins_models
27
+ files -= @options.exclude
28
+ files.each do |f|
29
+ process_class extract_class_name(f).constantize
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ # Load model classes
36
+ def load_classes
37
+ begin
38
+ disable_stdout
39
+ files = Dir.glob("app/models/**/*.rb")
40
+ files += Dir.glob("vendor/plugins/**/app/models/*.rb") if @options.plugins_models
41
+ files -= @options.exclude
42
+ files.each {|m| require m }
43
+ enable_stdout
44
+ rescue LoadError
45
+ enable_stdout
46
+ print_error "model classes"
47
+ raise
48
+ end
49
+ end # load_classes
50
+
51
+ # Process a model class
52
+ def process_class(current_class)
53
+
54
+ STDERR.print "\tProcessing #{current_class}\n" if @options.verbose
55
+
56
+ # Only interested in acts_as_state_machine models.
57
+ return unless current_class.respond_to?'states'
58
+
59
+ node_attribs = []
60
+ node_type = 'aasm'
61
+
62
+ current_class.states.each do |state_name|
63
+ state = current_class.read_inheritable_attribute(:states)[state_name]
64
+ node_shape = (current_class.initial_state === state_name) ? ", peripheries = 2" : ""
65
+ node_attribs << "#{current_class.name.downcase}_#{state_name} [label=#{state_name} #{node_shape}];"
66
+ end
67
+ @graph.add_node [node_type, current_class.name, node_attribs]
68
+
69
+ current_class.read_inheritable_attribute(:transition_table).each do |event_name, event|
70
+ event.each do |transition|
71
+ @graph.add_edge [
72
+ 'event',
73
+ current_class.name.downcase + "_" + transition.from.to_s,
74
+ current_class.name.downcase + "_" + transition.to.to_s,
75
+ event_name.to_s
76
+ ]
77
+ end
78
+ end
79
+ end # process_class
80
+
81
+ end # class AasmDiagram
@@ -0,0 +1,90 @@
1
+ # RailRoad - RoR diagrams generator
2
+ # http://railroad.rubyforge.org
3
+ #
4
+ # Copyright 2007-2008 - Javier Smaldone (http://www.smaldone.com.ar)
5
+ # See COPYING for more details
6
+
7
+ require 'diagram_graph'
8
+
9
+ # Root class for RailRoad diagrams
10
+ class AppDiagram
11
+
12
+ def initialize(options)
13
+ @options = options
14
+ @graph = DiagramGraph.new
15
+ @graph.show_label = @options.label
16
+
17
+ STDERR.print "Loading application environment\n" if @options.verbose
18
+ load_environment
19
+
20
+ STDERR.print "Loading application classes\n" if @options.verbose
21
+ load_classes
22
+ end
23
+
24
+ # Print diagram
25
+ def print
26
+ if @options.output
27
+ old_stdout = STDOUT.dup
28
+ begin
29
+ STDOUT.reopen(@options.output)
30
+ rescue
31
+ STDERR.print "Error: Cannot write diagram to #{@options.output}\n\n"
32
+ exit 2
33
+ end
34
+ end
35
+
36
+ if @options.xmi
37
+ STDERR.print "Generating XMI diagram\n" if @options.verbose
38
+ STDOUT.print @graph.to_xmi
39
+ else
40
+ STDERR.print "Generating DOT graph\n" if @options.verbose
41
+ STDOUT.print @graph.to_dot
42
+ end
43
+
44
+ if @options.output
45
+ STDOUT.reopen(old_stdout)
46
+ end
47
+ end # print
48
+
49
+ private
50
+
51
+ # Prevents Rails application from writing to STDOUT
52
+ def disable_stdout
53
+ @old_stdout = STDOUT.dup
54
+ STDOUT.reopen(PLATFORM =~ /mswin/ ? "NUL" : "/dev/null")
55
+ end
56
+
57
+ # Restore STDOUT
58
+ def enable_stdout
59
+ STDOUT.reopen(@old_stdout)
60
+ end
61
+
62
+
63
+ # Print error when loading Rails application
64
+ def print_error(type)
65
+ STDERR.print "Error loading #{type}.\n (Are you running " +
66
+ "#{APP_NAME} on the aplication's root directory?)\n\n"
67
+ end
68
+
69
+ # Load Rails application's environment
70
+ def load_environment
71
+ begin
72
+ disable_stdout
73
+ require "config/environment"
74
+ enable_stdout
75
+ rescue LoadError
76
+ enable_stdout
77
+ print_error "application environment"
78
+ raise
79
+ end
80
+ end
81
+
82
+ # Extract class name from filename
83
+ def extract_class_name(filename)
84
+ #filename.split('/')[2..-1].join('/').split('.').first.camelize
85
+ # Fixed by patch from ticket #12742
86
+ # File.basename(filename).chomp(".rb").camelize
87
+ filename.split('/')[2..-1].collect { |i| i.camelize }.join('::').chomp(".rb")
88
+ end
89
+
90
+ end # class AppDiagram