erb_dependencies 0.0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dc07ebee019626b20102e4266270200607e81a6b
4
+ data.tar.gz: 206eb82a962f29f6dc6fd9f9fa82baadd7e56296
5
+ SHA512:
6
+ metadata.gz: 0f0d6a6d3eb9dea2a71580921990b5a93b168f58395af5d6b188c8900769cc3ce4051861b739a677038383aae778fd749fd662b68b2f91f91b0bf101048d392d
7
+ data.tar.gz: 76274edee1a5d080f6c3675635f58f0882d7dd16e0267a2dc638dee7af4b5ada42296d02dac1094eb43e7d7e6a4db242313283592a3f4976a157b3603ecd4654
@@ -0,0 +1,22 @@
1
+ #Remove tags from erb file and output only an array of pure ruby code
2
+
3
+ class ErbTagsRemover
4
+
5
+ def remove_erb_tags(text)
6
+ all_tagged_chunks = text.scan(/(?<=\<%)(.*?)(?=\%>)/)
7
+ all_tagged_chunks.each do |tagged_chunks|
8
+ tagged_chunks.each do |tagged_chunk|
9
+ if tagged_chunk[0] == '='
10
+ tagged_chunk.slice!(0)
11
+ end
12
+ if tagged_chunk[-1] == '-'
13
+ tagged_chunk.slice!(-1)
14
+ end
15
+ if tagged_chunk[0] == '#'
16
+ tagged_chunks - [tagged_chunk]
17
+ end
18
+ end
19
+ end
20
+ all_tagged_chunks.join("\n")
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ #Call erb_tags_remover on the input file, then calls ruby_parser to parse the code into ast, calls method_controller_visitor
2
+ #to get all calls to possible controllers and output them in a file
3
+
4
+ class ControllerGrabber
5
+
6
+ require_relative '../ErbFileAnalyser/Erb_tags_remover'
7
+ require_relative '../ErbFileAnalyser/find_controller_calls'
8
+ require_relative '../Util/file_manager'
9
+ require_relative '../Util/ruby_parser'
10
+ require_relative '../Util/output_model'
11
+ require 'ast/node'
12
+
13
+ def grab_controllers(file_path)
14
+ output_value = ""
15
+ file_manager = File_manager.new
16
+ file_text = file_manager.read_file(file_path)
17
+ code = ErbTagsRemover.new.remove_erb_tags(file_text)
18
+ parsed_code = Ruby_parser.new.parse_code(code)
19
+ output_array = Find_controller_calls.new([],"","").find_controllers(parsed_code)
20
+ end
21
+
22
+ end
@@ -0,0 +1,219 @@
1
+ #Visits parse tree looking for calls to controllers, when found, insert them on an array
2
+ class Find_controller_calls
3
+
4
+ require 'ast/node'
5
+ require_relative '../Util/transform_into'
6
+
7
+ def initialize(array, instanceVar, localVar)
8
+ $output_array = array
9
+ $instance_variable = instanceVar
10
+ $lvar_derived_from_ivar = localVar
11
+ end
12
+
13
+ $link_to = :link_to
14
+ $str = :str
15
+ $ivar = :ivar
16
+ $lvar = :lvar
17
+ $submit = :submit
18
+ $form_for = :form_for
19
+ $each = :each
20
+ $label = :label
21
+ $hash = :hash
22
+ $confirm = :confirm
23
+ $block = :block
24
+ $send = :send
25
+ $render = :render
26
+ $form_tag = :form_tag
27
+
28
+ def find_controllers(code)
29
+ look_for_instance_variable(code)
30
+ look_for_loop_argument(code)
31
+ code.children.each do |code_children|
32
+ if is_still_a_node(code_children)
33
+ look_for_link_to_calls(code_children)
34
+ look_for_submit_calls(code_children, $instance_variable)
35
+ look_for_auto_gen_methods(code_children,$instance_variable,$lvar_derived_from_ivar)
36
+ look_for_form_for_action(code_children,$instance_variable)
37
+ look_for_render_call(code_children,$instance_variable)
38
+ look_for_form_tag_call(code_children, $instance_variable)
39
+ find_controllers(code_children)
40
+ end
41
+ end
42
+ $output_array
43
+ end
44
+
45
+ def insert_outputs_on_array(name, receiver)
46
+ output_model = Output_model.new
47
+ output_model.name = name
48
+ output_model.receiver = receiver
49
+ $output_array.push output_model
50
+ end
51
+
52
+ end
53
+
54
+ def look_for_link_to_calls(code)
55
+ method_name = code.children[1]
56
+ if method_name == $link_to
57
+ found_confirm_call = look_for_confirm_call(code)
58
+ if !found_confirm_call
59
+ method_argument_type = code.children[3].type
60
+ if method_argument_type == $ivar || method_argument_type == $lvar
61
+ method_argument_value = code.children[3].children[0]
62
+ insert_outputs_on_array(Transform_into.var_into_method(method_argument_value), "")
63
+ else
64
+ method_inside_link_to_has_params = code.children[3].children[1].nil?
65
+ if !method_inside_link_to_has_params
66
+ method_inside_link_to_param = code.children[3].children[1]
67
+ insert_outputs_on_array(method_inside_link_to_param, "")
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+
74
+ def look_for_submit_calls(code, instance_variable)
75
+ method_name = code.children[1]
76
+ if method_name == $submit
77
+ method_argument_type = code.children[2].type
78
+ if method_argument_type == $str
79
+ method_argument = code.children[2].children[0]
80
+ else
81
+ if method_argument_type == $send
82
+ method_argument = code.children[3].children[0].children[1].children[0]
83
+ end
84
+ end
85
+ insert_outputs_on_array("#{method_argument}".downcase,Transform_into.var_into_controller(instance_variable))
86
+ end
87
+ end
88
+
89
+ def look_for_auto_gen_methods(code, instance_variable,lvar_derived_from_ivar)
90
+ method_name = code.children[1]
91
+ if method_name == $label
92
+ method_argument_value = code.children[2].children[0]
93
+ if method_argument_value.is_a?(Parser::AST::Node)
94
+ method_argument_value = code.children[2].children[0].children[0].children[2].children[0]
95
+ insert_outputs_on_array(method_argument_value, instance_variable)
96
+ else
97
+ insert_outputs_on_array(method_argument_value, instance_variable)
98
+ end
99
+ end
100
+ if is_still_a_node(code.children[0])
101
+ variable_type = code.children[0].type
102
+ variable_calls_method = !code.children[1].nil?
103
+ if variable_type == $lvar && variable_calls_method
104
+ method_argument = code.children[0].children[0]
105
+ if method_argument == lvar_derived_from_ivar
106
+ insert_outputs_on_array(method_name, instance_variable)
107
+ end
108
+ end
109
+ end
110
+ end
111
+
112
+ def look_for_loop_argument(code)
113
+ if $lvar_derived_from_ivar == ""
114
+ code.children.each do |code_children|
115
+ if is_still_a_node(code_children)
116
+ if code_children.type == $block
117
+ if code_children.children[0].type == $send
118
+ loop_type = code_children.children[0].children[1]
119
+ if loop_type == $each
120
+ loop_argument_variable = code_children.children[1].children[0].children[0]
121
+ $lvar_derived_from_ivar = loop_argument_variable
122
+ end
123
+ end
124
+ end
125
+ look_for_loop_argument(code_children)
126
+ end
127
+ end
128
+ else
129
+ $lvar_derived_from_ivar
130
+ end
131
+ end
132
+
133
+ def look_for_instance_variable(code)
134
+ if $instance_variable == ""
135
+ code.children.each do |code_children|
136
+ if is_still_a_node(code_children)
137
+ loop_type = code_children.children[1]
138
+ if loop_type == $form_for && code_children.children[2].type == $ivar
139
+ loop_variable_value = code_children.children[2].children[0]
140
+ $instance_variable = loop_variable_value
141
+ elsif loop_type == $each
142
+ loop_variable_value = code_children.children[0].children[0]
143
+ if loop_variable_value.to_s[0] == '@'
144
+ $instance_variable = loop_variable_value
145
+ end
146
+ end
147
+ look_for_instance_variable(code_children)
148
+ end
149
+ end
150
+ else
151
+ $instance_variable = Transform_into.singular("#{$instance_variable}")
152
+ end
153
+ end
154
+
155
+ def look_for_confirm_call(code)
156
+ has_aditional_call = !code.children[4].nil?
157
+ if has_aditional_call
158
+ link_to_type = code.children[4].type
159
+ has_confirm_call = code.children[4].children[0].children[0].children[0]
160
+ if link_to_type == $hash && has_confirm_call == $confirm
161
+ link_to_redirect_name = code.children[2].children[0]
162
+ link_to_argument_variable = code.children[3].children[0]
163
+ insert_outputs_on_array("#{link_to_redirect_name}".downcase,Transform_into.var_into_controller(link_to_argument_variable))
164
+ true
165
+ end
166
+ else
167
+ false
168
+ end
169
+ end
170
+
171
+ def look_for_form_for_action(code, instance_variable)
172
+ if is_still_a_node(code)
173
+ if code.type == $send
174
+ loop_type = code.children[1]
175
+ if loop_type == $form_for
176
+ has_hash = !code.children[3].nil?
177
+ if has_hash
178
+ possible_hash = code.children[3].children[1].children[1].type
179
+ if possible_hash == $hash
180
+ loop_action = code.children[3].children[1].children[1].children[1].children[1].children[0]
181
+ insert_outputs_on_array(loop_action, instance_variable)
182
+ end
183
+ end
184
+ end
185
+ end
186
+ end
187
+ end
188
+
189
+ def look_for_render_call(code, instance_variable)
190
+ method_name = code.children[1]
191
+ if method_name == $render
192
+ method_argument = code.children[2].children[0]
193
+ insert_outputs_on_array(method_argument, instance_variable)
194
+ end
195
+ end
196
+
197
+ def look_for_form_tag_call(code, instance_variable)
198
+ method_name = code.children[1]
199
+ if method_name == $form_tag
200
+ possible_hash = code.children[2].type
201
+ if possible_hash == $hash
202
+ method_argument = code.children[2].children[1].children[1].children[0]
203
+ controller_called = code.children[2].children[0].children[1].children[0]
204
+ insert_outputs_on_array(method_argument, controller_called)
205
+ else
206
+ method_argument = code.children[2].children[1]
207
+ insert_outputs_on_array(method_argument, instance_variable)
208
+ end
209
+
210
+ end
211
+ end
212
+
213
+ def is_still_a_node(code)
214
+ if code.is_a?(Parser::AST::Node)
215
+ true
216
+ else
217
+ false
218
+ end
219
+ end
@@ -0,0 +1,31 @@
1
+ class File_manager
2
+
3
+ require 'fileutils'
4
+
5
+ def create_file(path, extension)
6
+ dir = File.dirname(path)
7
+
8
+ unless File.directory?(dir)
9
+ FileUtils.mkdir_p(dir)
10
+ end
11
+
12
+ path << ".#{extension}"
13
+ File.new(path, 'w')
14
+ end
15
+
16
+ def read_file(path)
17
+ File.open(path, 'rb') { |file| file.read }
18
+ end
19
+
20
+ def write_on_file(text, path)
21
+ File.open(path, 'w') do |f|
22
+ f.write text
23
+ end
24
+ end
25
+
26
+ def get_file_name(file_path)
27
+ name_with_extension = /(?!.*\\)(.*)$/.match(file_path).to_s
28
+ name = /.*(?=\.)/.match(name_with_extension).to_s
29
+ end
30
+
31
+ end
@@ -0,0 +1,6 @@
1
+ class Output_model
2
+
3
+ attr_accessor :name
4
+ attr_accessor :receiver
5
+
6
+ end
@@ -0,0 +1,12 @@
1
+ #Parse ruby code into ast
2
+
3
+ require 'parser/current'
4
+ require 'ast'
5
+
6
+ class Ruby_parser
7
+
8
+ Parser::Builders::Default.emit_lambda = true # opt-in to most recent AST format
9
+ def parse_code(code)
10
+ p Parser::CurrentRuby.parse(code)
11
+ end
12
+ end
@@ -0,0 +1,29 @@
1
+ require 'active_support/inflector'
2
+
3
+ class Transform_into
4
+
5
+ def self.var_into_controller(var)
6
+ if var == ''
7
+
8
+ else
9
+ if var[-1] == 'y'
10
+ var = "#{var[0...-1]}ies_controller"
11
+ else
12
+ var = "#{var}s_controller"
13
+ end
14
+ end
15
+ end
16
+
17
+ def self.var_into_method(var)
18
+ if var.to_s[0] == '@'
19
+ "#{(var.to_s)[1..-1]}_path"
20
+ else
21
+ var = "#{var}_path"
22
+ end
23
+ end
24
+
25
+ def self.singular(var)
26
+ var.singularize
27
+ end
28
+
29
+ end
@@ -0,0 +1,8 @@
1
+ require_relative 'ErbFileAnalyser/Grab_controller_from_erb_file'
2
+ class Erb_dependencies
3
+
4
+ def grab_controllers(file_path)
5
+ ControllerGrabber.new.grab_controllers(file_path)
6
+ end
7
+
8
+ end
@@ -0,0 +1,8 @@
1
+ [name: 'uuid', receiver: '@otml_file']
2
+ [name: 'otml_category_id', receiver: '@otml_file']
3
+ [name: 'name', receiver: '@otml_file']
4
+ [name: 'path', receiver: '@otml_file']
5
+ [name: 'content', receiver: '@otml_file']
6
+ [name: 'update', receiver: '@otml_files_controller']
7
+ [name: 'otml_file_path', receiver: '']
8
+ [name: 'otml_files_path', receiver: '']
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: erb_dependencies
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Joao Pedro de Medeiros Santos
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-11-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Tool made for finding possible controller calls inside .erb views
14
+ email: jpms2@cin.ufpe.br
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/ErbFileAnalyser/Erb_tags_remover.rb
20
+ - lib/ErbFileAnalyser/Grab_controller_from_erb_file.rb
21
+ - lib/ErbFileAnalyser/find_controller_calls.rb
22
+ - lib/Util/file_manager.rb
23
+ - lib/Util/output_model.rb
24
+ - lib/Util/ruby_parser.rb
25
+ - lib/Util/transform_into.rb
26
+ - lib/erb_dependencies.rb
27
+ - lib/outputs/sample1.html_output.txt
28
+ homepage: http://rubygems.org/gems/erb_dependencies
29
+ licenses:
30
+ - MIT
31
+ metadata: {}
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubyforge_project:
48
+ rubygems_version: 2.6.7
49
+ signing_key:
50
+ specification_version: 4
51
+ summary: erb_dependencies
52
+ test_files: []