ruby_traverser 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Kristian Mandrup
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,195 @@
1
+ # Ruby traverser ##
2
+
3
+ A DSL for traversing ruby code as an object model (graph), which lets you find parts of the ruby code of interest.
4
+ The traverser leverages `ripper2ruby`, which leverages `ripper`, which comes with ruby 1.9. Ruby 1.9 is thus required.
5
+
6
+ See the unit tests in the test directory for examples of use.
7
+
8
+ ## Finders ##
9
+
10
+ * find_module(name)
11
+ * find_class(name, options = {})
12
+ * find_call(name, options = {})
13
+ * find_block(name, options = {})
14
+ * find_def(name, options = {})
15
+ * find_variable(name, options = {})
16
+ * find_assignment(name, options = {})
17
+
18
+ ## Find module ##
19
+
20
+ <pre>
21
+ src = %q{
22
+ module Xyz::Xxx::Blip
23
+ 2
24
+ end
25
+ }
26
+
27
+ code = Ripper::RubyBuilder.build(src)
28
+ module_node = code.find_module('Xyz::Xxx::Blip')
29
+ assert_equal Ruby::Module, module_node.class
30
+ </pre>
31
+
32
+ ## Find class ##
33
+
34
+ <pre>
35
+ # class Monty::Python ... end
36
+ clazz_node = code.find_class('Monty::Python')
37
+ </pre>
38
+
39
+ Find class inheriting from a certain superclass
40
+ <pre>
41
+ # class Monty < Abc::Blip ... end
42
+ clazz_node = code.find_class('Monty', :superclass => 'Abc::Blip')
43
+ </pre>
44
+
45
+ ## Find call ##
46
+
47
+ <pre>
48
+ # gem 'ripper', :src => 'github'
49
+ gem_call = code.find_call('gem', :args => ['ripper', {:src => 'github'}])
50
+ </pre>
51
+
52
+
53
+ ## Find block ##
54
+
55
+ <pre>
56
+ # my_block do ... end
57
+ block_node = code.find_block('my_block')
58
+ </pre>
59
+
60
+ <pre>
61
+ # my_block do |v| ... end
62
+ block_node = code.find_block('my_block', :block_params => ['v'])
63
+ </pre>
64
+
65
+ <pre>
66
+ # my_block 7, 'a' do ... end
67
+ block_node = code.find_block('my_block', :args => [7, 'a'])
68
+ </pre>
69
+
70
+ <pre>
71
+ # my_block 7, 'a', :k => 32 do |v| ... end
72
+ block_node = code.find_block('my_block', :args => [7, 'a', {:k => 32}], :block_params => ['v'])
73
+ </pre>
74
+
75
+ <pre>
76
+ # my_block :a => 7, b => 3 do |v| ... end
77
+ block_node = code.find_block('my_block', :args => [{:a => 7, 'b' => 3}])
78
+ </pre>
79
+
80
+ <pre>
81
+ # my_block ['a', 'b'] do |v| ... end
82
+ block_node = code.find_block('my_block', :args => [{:array =>['a', 'b']}])
83
+ </pre>
84
+
85
+
86
+ ## Find variable ##
87
+
88
+ Source code:
89
+ <pre>
90
+ def hello_world(a)
91
+ my_var
92
+ end
93
+ </pre>
94
+
95
+ Ruby code find DSL:
96
+ <pre>
97
+ code = Ripper::RubyBuilder.build(src)
98
+ code.inside_def('hello_world', :params => ['a']) do |b|
99
+ call_node = b.find_variable('my_var')
100
+ assert_equal Ruby::Variable, call_node.class
101
+ puts call_node.to_ruby
102
+ end
103
+ </pre>
104
+
105
+
106
+ ## Find assignment ##
107
+
108
+ Source code:
109
+ <pre>
110
+ def hello_world(a)
111
+ my_var = 2
112
+ end
113
+ </pre>
114
+
115
+ Ruby code find DSL:
116
+ <pre>
117
+ code = Ripper::RubyBuilder.build(src)
118
+ code.inside_def('hello_world', :params => ['a']) do |b|
119
+ call_node = b.find_assignment('my_var')
120
+ end
121
+ </pre>
122
+
123
+ ## Inside ##
124
+
125
+ The following finder methods have corresponding `inside_` functions, which support block DSL constructs as shown below.
126
+
127
+ * inside_module
128
+ * inside_class
129
+ * inside_def
130
+ * inside_block
131
+
132
+ <pre>
133
+ # source code
134
+ src = %q{
135
+ gem 'ripper', :src => 'github', :blip => 'blap'
136
+ group :test do
137
+ gem 'ripper', :src => 'github'
138
+ end
139
+ }
140
+ </pre>
141
+
142
+ <pre>
143
+ code = Ripper::RubyBuilder.build(src)
144
+ # chaining finders using 'inside__' DSL block constructs
145
+ code.inside_block('group', :args => [:test]) do |b|
146
+ call_node = b.find_call('gem', :args => ['ripper', {:src => 'github'}])
147
+ assert_equal Ruby::Call, call_node.class
148
+ puts call_node.to_ruby # output ruby code as string for found node
149
+ end
150
+ </pre>
151
+
152
+ <pre>
153
+ src = %q{
154
+ def hello_world(b)
155
+ 3
156
+ end
157
+
158
+ def hello_world(a)
159
+ gem 'ripper', :src => 'github'
160
+ end
161
+
162
+ }
163
+ </pre>
164
+
165
+ <pre>
166
+ # chaining finders using 'inside__' DSL block constructs
167
+ code = Ripper::RubyBuilder.build(src)
168
+ code.inside_def('hello_world', :params => ['a']) do |b|
169
+ call_node = b.find_call('gem', :args => ['ripper', {:src => 'github'}])
170
+ assert_equal Ruby::Call, call_node.class
171
+ puts call_node.to_ruby # output ruby code as string for found node
172
+ end
173
+ </pre>
174
+
175
+ ## Code Mutation API ##
176
+
177
+ The API now also supports a wide variety of code mutations using a DSL.
178
+ More information will soon be available here or on the github wiki.
179
+ Check the test/mutate folder for test demonstrating what is currently possible.
180
+
181
+ Note: The mutation API code was developed in a test-driven fashion, but is in need of a major refactoring overhaul sometime soon...
182
+
183
+ ## Note on Patches/Pull Requests ##
184
+
185
+ * Fork the project.
186
+ * Make your feature addition or bug fix.
187
+ * Add tests for it. This is important so I don't break it in a
188
+ future version unintentionally.
189
+ * Commit, do not mess with rakefile, version, or history.
190
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
191
+ * Send me a pull request. Bonus points for topic branches.
192
+
193
+ ## Copyright ##
194
+
195
+ Copyright (c) 2010 Kristian Mandrup. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,48 @@
1
+ # require 'rubygems'
2
+ # require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "ruby_traverser"
8
+ gem.summary = %Q{traverse and mutate ruby code using a nice DSL}
9
+ gem.description = %Q{traverse a ruby code model and optionally mutate it along the way using a nice rubyish DSL}
10
+ gem.email = "kmandrup@gmail.com"
11
+ gem.homepage = "http://github.com/kristianmandrup/ruby_trav"
12
+ gem.authors = ["Kristian Mandrup"]
13
+ gem.add_development_dependency "rspec", ">= 2.0.0"
14
+ gem.add_dependency "ripper2ruby", "> 0.0.2"
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+
17
+ # add more gem options here
18
+ end
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
21
+ end
22
+
23
+ # require 'rspec/rake_task'
24
+ # Rspec::Core::RakeTask.new(:spec) do |spec|
25
+ # # spec.libs << 'lib' << 'spec'
26
+ # # spec.spec_files = FileList['spec/**/*_spec.rb']
27
+ # end
28
+ #
29
+ # require 'rspec/rake_task'
30
+ # Rspec::Core::RakeTask.new(:rcov) do |spec|
31
+ # # spec.libs << 'lib' << 'spec'
32
+ # # spec.pattern = 'spec/**/*_spec.rb'
33
+ # # spec.rcov = true
34
+ # end
35
+ #
36
+ # task :spec => :check_dependencies
37
+ #
38
+ # task :default => :spec
39
+ #
40
+ # require 'rake/rdoctask'
41
+ # Rake::RDocTask.new do |rdoc|
42
+ # version = File.exist?('VERSION') ? File.read('VERSION') : ""
43
+ #
44
+ # rdoc.rdoc_dir = 'rdoc'
45
+ # rdoc.title = "ruby_trav #{version}"
46
+ # rdoc.rdoc_files.include('README*')
47
+ # rdoc.rdoc_files.include('lib/**/*.rb')
48
+ # end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/lib/mutate/api.rb ADDED
@@ -0,0 +1,124 @@
1
+ require 'mutate/replacer'
2
+
3
+ class String
4
+ # Returns an indented string, all lines of string will be indented with count of chars
5
+ def indent(char, count)
6
+ (char * count) + gsub(/(\n+)/) { $1 + (char * count) }
7
+ end
8
+ end
9
+
10
+
11
+ module RubyAPI
12
+ module Mutator
13
+ include Replacer
14
+
15
+ def append_code(code)
16
+ return append_code_simple(code) if !elemental?
17
+ obj = object
18
+ indentation = obj.last_indent
19
+ code = "\n#{code}\n".indent(' ', indentation)
20
+ ruby_code = Ripper::RubyBuilder.build(code)
21
+ inject_code = ruby_code.elements[0]
22
+ obj.get_elements << inject_code
23
+ inject_code
24
+ end
25
+
26
+ def append_code_simple(code)
27
+ indentation = position.col
28
+ code = "\n#{code}\n".indent(' ', indentation)
29
+ ruby_code = Ripper::RubyBuilder.build(code)
30
+ inject_code = ruby_code.elements[0]
31
+ index = parent.find_index(self)
32
+ parent.get_elements.insert(index+1, inject_code)
33
+ inject_code
34
+ end
35
+
36
+ def find_index(obj)
37
+ get_elements.each_with_index do |elem, i|
38
+ if elem == obj
39
+ return i
40
+ end
41
+ end
42
+ end
43
+
44
+ def prepend_code(code)
45
+ obj = object
46
+ indentation = obj.first_indent
47
+ code = "\n#{code}\n".indent(' ', indentation)
48
+ ruby_code = Ripper::RubyBuilder.build(code)
49
+ inject_code = ruby_code.elements[0]
50
+ obj.get_elements.insert(0, inject_code)
51
+ obj
52
+ end
53
+
54
+
55
+ def append_comment(text)
56
+ append_code("# #{text}")
57
+ end
58
+
59
+ def elemental?
60
+ respond_to?(:body) || respond_to?(:elements)
61
+ end
62
+
63
+ def get_elements
64
+ case self
65
+ when Ruby::Class
66
+ body.elements
67
+ else
68
+ elements
69
+ end
70
+ end
71
+
72
+
73
+ protected
74
+
75
+ def last_indent
76
+ case self
77
+ when Ruby::Block, Ruby::Class
78
+ last_position(get_elements)
79
+ else
80
+ puts "unknown: #{obj.class}"
81
+ end
82
+ end
83
+
84
+ def last_position(elements)
85
+ last_element = elements.last
86
+ return position.col
87
+ return position.col if simple_pos?
88
+ return last_element.identifier.position.col if elements && elements.size > 0
89
+ inside_indent
90
+ end
91
+
92
+ def simple_pos?
93
+ [Ruby::Token, Ruby::Variable].include?(last_element.class)
94
+ end
95
+
96
+ def first_indent
97
+ case self
98
+ when Ruby::Block, Ruby::Class
99
+ first_position(get_elements)
100
+ else
101
+ puts "unknown: #{obj.class}"
102
+ end
103
+ end
104
+
105
+ def first_position(elements)
106
+ first_element = elements.first
107
+ return position.col if simple_pos?
108
+ return first_element.identifier.position.col if elements && elements.size > 0
109
+ inside_indent
110
+ end
111
+
112
+ def object
113
+ self.respond_to?(:block) ? self.block : self
114
+ end
115
+
116
+ end
117
+ end
118
+
119
+ module Ruby
120
+ class Node
121
+ include RubyAPI::Mutator
122
+ end
123
+ end
124
+
@@ -0,0 +1,129 @@
1
+ module PositionReplacer
2
+ def replace_position_arg(options)
3
+ pos = position_arg?(options[:arg])
4
+ self.arguments.elements[pos.to_i].replace_pos_argument(options)
5
+ end
6
+
7
+ def replace_pos_argument(options)
8
+ case self.arg
9
+ when Ruby::String
10
+ replace_arg_token(options[:replace_arg])
11
+ end
12
+ end
13
+
14
+ def position_arg?(arg)
15
+ return arg[1] if arg && arg[0] == '#'
16
+ nil
17
+ end
18
+ end
19
+
20
+ module TokenReplacer
21
+ def replace_arg_token(replacement)
22
+ self.arg.elements[0].token = replacement
23
+ end
24
+
25
+ def matching_string_arg?(txt)
26
+ self.arg.elements[0].token == txt
27
+ end
28
+ end
29
+
30
+ module HashReplacer
31
+ def hash_arg?(arg)
32
+ case arg
33
+ when Hash
34
+ matching_hash_arg?(arg)
35
+ when Symbol
36
+ matching_symbol_arg?(arg)
37
+ end
38
+ end
39
+
40
+ def replace_hash_arg(options)
41
+ src = options[:replace_code]
42
+ code = Ripper::RubyBuilder.build(src)
43
+ code.set_ldelim(self.arg)
44
+ self.arg = code
45
+ end
46
+
47
+ def set_ldelim(arg)
48
+ if arg.respond_to? :elements
49
+ self.ldelim = arg.elements[0].key.ldelim
50
+ self.ldelim.token = ''
51
+ else
52
+ self.ldelim = arg.ldelim
53
+ end
54
+ end
55
+
56
+ def matching_hash_arg?(arg)
57
+ if self.arg.respond_to? :elements
58
+ if self.arg.elements[0].class == Ruby::Assoc
59
+ key = self.arg.elements[0].key
60
+ value = self.arg.elements[0].value
61
+ arg_key = arg.first[0]
62
+ arg_value = arg.first[1]
63
+ return key.identifier.token.to_sym == arg_key && value.elements[0].token == arg_value
64
+ end
65
+ end
66
+ false
67
+ end
68
+
69
+ def matching_symbol_arg?(arg)
70
+ if self.arg.respond_to? :elements
71
+ if self.arg.elements[0].class == Ruby::Assoc
72
+ return self.arg.elements[0].key.identifier.token.to_sym == arg
73
+ end
74
+ else
75
+ # remove ':' token from symbol
76
+ self.arg.ldelim.token = ''
77
+ return self.arg.identifier.token.to_sym == arg
78
+ end
79
+ false
80
+ end
81
+ end
82
+
83
+ module ValueReplacer
84
+ def replace_value(options)
85
+ if self.class == Ruby::Assignment
86
+ self.right.token = options[:value]
87
+ end
88
+ end
89
+ end
90
+
91
+ module RubyAPI
92
+ module Mutator
93
+ module Replacer
94
+ include PositionReplacer
95
+ include TokenReplacer
96
+ include HashReplacer
97
+ include ValueReplacer
98
+
99
+ # :arg => 'ripper', :replace_arg => 'rapper'
100
+ def replace(options)
101
+ if options[:value]
102
+ return replace_value(options)
103
+ end
104
+
105
+ if position_arg?(options[:arg])
106
+ return replace_position_arg(options)
107
+ end
108
+
109
+ self.arguments.elements.each_with_index do |elem, i|
110
+ case elem
111
+ when Ruby::Arg
112
+ if elem.hash_arg?(options[:arg])
113
+ return elem.replace_hash_arg(options)
114
+ end
115
+ elem.replace_argument(options)
116
+ end
117
+ end
118
+ end
119
+
120
+ def replace_argument(options)
121
+ case self.arg
122
+ when Ruby::String
123
+ replace_arg_token(options[:replace_arg]) if matching_string_arg?(options[:arg])
124
+ end
125
+ end
126
+
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,23 @@
1
+ module RubyAPI
2
+ module Rails
3
+ module Gemfile
4
+ def inside_group(name, &block)
5
+ inside_block('group', :args => [:"#{name}"], :extend => RubyAPI::Rails::Gemfile, &block)
6
+ end
7
+
8
+ def add_gem(name)
9
+ append_code("gem '#{name}'")
10
+ end
11
+
12
+ def replace_gem(name, replace_name)
13
+ found = find_gem(name)
14
+ puts "gem:#{found}"
15
+ found.replace(:arg => name, :replace_arg => replace_name) if found
16
+ end
17
+
18
+ def find_gem(name, options = nil)
19
+ find_call('gem', :args => ["#{name}", options])
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,4 @@
1
+ require 'ripper2ruby'
2
+ require 'traversal/mixin'
3
+ require 'traversal/api/traversal'
4
+ require 'mutate/api'
@@ -0,0 +1,41 @@
1
+ require 'yaml'
2
+
3
+ module RubyAPI
4
+ module Finders
5
+ def find_module(name)
6
+ get_obj.select(Ruby::Module, :identifier => name).first
7
+ end
8
+
9
+ def find_class(name, options = {})
10
+ options.merge!(:identifier => name)
11
+ get_obj.select(Ruby::Class, options).first
12
+ end
13
+
14
+ def find_variable(name, options = {})
15
+ options.merge!(:token => name)
16
+ get_obj.select(Ruby::Variable, options).first
17
+ end
18
+
19
+ def find_assignment(name, options = {})
20
+ options.merge!(:left_token => name)
21
+ get_obj.select(Ruby::Assignment, options).first
22
+ end
23
+
24
+ def find_call(name, options = {})
25
+ options.merge!(:identifier => name)
26
+ get_obj.select(Ruby::Call, options).first
27
+ end
28
+
29
+ def find_block(name, options = {})
30
+ options.merge!(:identifier => name)
31
+ options.merge!(:block => true) if !options.has_key?(:block_params)
32
+ get_obj.select(Ruby::Call, options).first
33
+ end
34
+
35
+
36
+ def find_def(name, options = {})
37
+ options.merge!(:identifier => name)
38
+ get_obj.select(Ruby::Method, options).first
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,36 @@
1
+ module RubyAPI
2
+ module Inside
3
+ def inside_block(name, options = {}, &block)
4
+ call_block(find_block(name, options), options, &block)
5
+ end
6
+
7
+ def inside_module(name, &block)
8
+ call_block(find_module(name), options, &block)
9
+ end
10
+
11
+ def inside_class(name, options = {}, &block)
12
+ call_block(find_class(name, options), options, &block)
13
+ end
14
+
15
+ def inside_def(name, options = {}, &block)
16
+ call_block(find_def(name, options), options, &block)
17
+ end
18
+
19
+ protected
20
+
21
+ def call_block(s, options, &block)
22
+ # inc_inside_indent
23
+ s.extend(options[:extend]) if options[:extend]
24
+ block.arity < 1 ? s.instance_eval(&block) : block.call(s)
25
+ end
26
+
27
+ def inside_indent
28
+ if self.class == Ruby::Class
29
+ pos = ldelim.position.col
30
+ return pos
31
+ end
32
+ 2
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,20 @@
1
+ require 'traversal/api/finders'
2
+ require 'traversal/api/inside'
3
+
4
+ module RubyAPI
5
+ include Finders
6
+ include Inside
7
+
8
+ protected
9
+
10
+ def get_obj(options = {})
11
+ return self.block if self.class == Ruby::Method
12
+ self
13
+ end
14
+ end
15
+
16
+ module Ruby
17
+ class Node
18
+ include RubyAPI
19
+ end
20
+ end