Class: OverridesTracker::MethodsCollector

Inherits:
Object
  • Object
show all
Defined in:
lib/overrides_tracker/methods_collector.rb

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.instanceObject (readonly)

Returns the value of attribute instance.



13
14
15
# File 'lib/overrides_tracker/methods_collector.rb', line 13

def instance
  @instance
end

Instance Method Details

#add_method_for_class(method_type, class_name, method_name, method_hash) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/overrides_tracker/methods_collector.rb', line 16

def add_method_for_class(method_type, class_name, method_name, method_hash)
  methods_collection(class_name)
  @methods_collection[class_name][method_type][method_name] = method_hash
  if !@methods_collection[class_name][:is_part_of_app] && @methods_collection[class_name][method_type][method_name][:is_part_of_app]
    @methods_collection[class_name][:is_part_of_app] = true
  end
end

#build_overrides_hashObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/overrides_tracker/methods_collector.rb', line 105

def build_overrides_hash
  total_classes = @methods_collection.size
  count = 0
  working_directory = Dir.pwd

  @methods_collection.each do |class_name, class_methods|
    unless class_name.nil?
      begin
        clazz = class_name.constantize
        build_overrides_hash_for_method_type(clazz, class_methods, :instance_methods, working_directory)
        build_overrides_hash_for_method_type(clazz, class_methods, :singleton_methods, working_directory)
      rescue Exception => e
        puts "Error processing #{class_name}".red
      end
    end
    count += 1
    puts "Processed #{class_name} #{count} / #{total_classes}"
  end
end

#build_overrides_hash_for_method_type(clazz, class_methods, methods_type, working_directory) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/overrides_tracker/methods_collector.rb', line 51

def build_overrides_hash_for_method_type(clazz, class_methods, methods_type, working_directory)
  methods = []
  if methods_type == :instance_methods
    methods = clazz.instance_methods(false)
    clazz.ancestors.each do |ancestor|
      break if ancestor.instance_of?(Class)

      methods += ancestor.instance_methods(false)
    end
  else
    methods = clazz.singleton_methods(false)
    clazz.ancestors.each do |ancestor|
      break if ancestor.instance_of?(Class)

      methods += ancestor.singleton_methods(false)
    end
    clazz.singleton_class.ancestors.each do |ancestor|
      break if ancestor.instance_of?(Class)
      methods += ancestor.instance_methods(false)
    end
  end

  methods.each do |method_name|
    next unless !method_name.nil? && method_name != :overrides_tracker_finished_file

    method_hash = class_methods[methods_type][method_name]

    begin
      method_to_check = if methods_type == :instance_methods
                          clazz.instance_method(method_name)
                        else
                          clazz.singleton_class.instance_method(method_name) || clazz.singleton_method(method_name)
                        end

      method_to_check_hash = OverridesTracker::Util.method_hash(method_to_check)

      unless method_to_check_hash[:location].nil?
        if !method_hash.nil?
          if method_to_check_hash[:location] != method_hash[:location]
            mark_method_as_override(methods_type, clazz.name, method_name, method_to_check, method_to_check_hash)
            puts "#{method_name} of class #{clazz.name} was overridden".green
          end
        elsif method_to_check_hash[:location][0].include? working_directory
          mark_method_as_added("added_#{methods_type}".to_sym, clazz.name, method_name, method_to_check,
                               method_to_check_hash)
          puts "#{method_name} of class #{clazz.name} was added".green
        end
      end
    rescue Exception => e
      # puts "Error processing #{method_name} of class #{clazz.name}".red
    end
  end
end

#load_from_file(file_name) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/overrides_tracker/methods_collector.rb', line 129

def load_from_file(file_name)
  file_path = File.join(Dir.pwd, "/overrides_tracker/#{file_name}")
  data = nil
  begin
    File.open(file_path) do |f|
      file_data = JSON.parse(f.read)
      data = file_data
    end
  rescue StandardError
    puts "Error processing #{file_path}"
  end
  data
end

#mark_method_as_added(method_type, class_name, method_name, overriding_method, method_hash) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/overrides_tracker/methods_collector.rb', line 39

def mark_method_as_added(method_type, class_name, method_name, overriding_method, method_hash)
  overridden_methods_collection(class_name)
  @overridden_methods_collection[class_name][method_type][method_name] = method_hash
  @overridden_methods_collection[class_name][method_type][method_name][:overriding_location] =
overriding_method.source_location
  if @overridden_methods_collection[class_name][method_type][method_name][:overriding_location][0].include? Dir.pwd
    @overridden_methods_collection[class_name][method_type][method_name][:overriding_is_part_of_app] = true
  else
    @overridden_methods_collection[class_name][method_type][method_name][:overriding_is_part_of_app] = false
  end
end

#mark_method_as_override(method_type, class_name, method_name, overriding_method, method_hash) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/overrides_tracker/methods_collector.rb', line 24

def mark_method_as_override(method_type, class_name, method_name, overriding_method, method_hash)
  overridden_methods_collection(class_name)
  @overridden_methods_collection[class_name][method_type][method_name] =
@methods_collection[class_name][method_type][method_name]
  @overridden_methods_collection[class_name][method_type][method_name][:overriding_location] =
overriding_method.source_location
  @overridden_methods_collection[class_name][method_type][method_name][:overriding_body] = method_hash[:body]
  @overridden_methods_collection[class_name][method_type][method_name][:overriding_sha] = method_hash[:sha]
  if @overridden_methods_collection[class_name][method_type][method_name][:overriding_location][0].include? Dir.pwd
    @overridden_methods_collection[class_name][method_type][method_name][:overriding_is_part_of_app] = true
  else
    @overridden_methods_collection[class_name][method_type][method_name][:overriding_is_part_of_app] = false
  end
end

#overridden_methodsObject



125
126
127
# File 'lib/overrides_tracker/methods_collector.rb', line 125

def overridden_methods
  @overridden_methods_collection
end

#report(api_token) ⇒ Object



384
385
386
# File 'lib/overrides_tracker/methods_collector.rb', line 384

def report(api_token)
  OverridesTracker::Api.report_build(api_token, branch_name_to_report, last_commit_id, last_commit_name_to_report, path_to_report_file)
end

#save_to_fileObject



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/overrides_tracker/methods_collector.rb', line 195

def save_to_file
  file_data = {}
  file_data[:version] = OverridesTracker::VERSION
  file_data[:branch_name] = branch_name
  file_data[:author_name] = author_name
  file_data[:committer_name] = committer_name
  file_data[:branch_name_to_report] = branch_name_to_report
  file_data[:last_commit_id] = last_commit_id
  file_data[:last_commit_name] = last_commit_name
  file_data[:last_commit_name_to_report] = last_commit_name_to_report
  file_data[:working_directory] = Dir.pwd
  file_data[:bundle_path] = Bundler.bundle_path.to_s
  file_data[:methods_collection] = @overridden_methods_collection.sort.to_h

  classes_with_overrides = @methods_collection.select do |_key, val|
    !val[:instance_methods].nil? || !val[:singleton_methods].nil?
  end
  classes_with_overrides_transformed = classes_with_overrides.map do |k, v|
    [k, v[:instance_methods], v[:singleton_methods]]
  end

  file_data[:number_of_methods] = classes_with_overrides_transformed.sum { |a| a[1].size + a[2].size }
  file_data[:number_of_methods_in_app_path] = classes_with_overrides_transformed.sum do |a|
    a[1].sum do |b|
      (b[1][:is_part_of_app] || b[1][:overriding_is_part_of_app]) ? 1 : 0 
    end + a[2].sum do |b|
      (b[1][:is_part_of_app] || b[1][:overriding_is_part_of_app]) ? 1 : 0
    end
  end

  file_data[:number_of_classes] = @methods_collection.size
  file_data[:number_of_classes_in_app_path] = @methods_collection.select { |_k, v| v[:is_part_of_app] }.size

  File.open(path_to_report_file, 'w') do |f|
    f << file_data.to_json
  end
  puts '  '
  puts '==========='
  puts "Report saved to #{path_to_report_file}."
end

#show_override(class_name, method_name, method_hash, separator = '#', word_choice = 'overridden') ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/overrides_tracker/methods_collector.rb', line 166

def show_override(class_name, method_name, method_hash, separator = '#', word_choice = 'overridden')
  
  puts ""
  puts "==========================================================================================="
  puts ""

  puts "#{class_name}#{separator}#{method_name} was #{word_choice}."
  unless method_hash[:body].nil?
    puts "-------------------------------------------------------------------------------------------".pink
    puts ''
    puts 'Original:'.italic
    puts ''
    puts "#{method_hash[:body]}".pink
    puts ''
    puts "in #{method_hash[:location][0]}:#{method_hash[:location][1]}".italic
  end
  puts ''
  puts ''
  unless method_hash[:overriding_body].nil?
    puts "-------------------------------------------------------------------------------------------".blue
    puts ''
    puts 'Override:'.italic
    puts ''
    puts "#{method_hash[:overriding_body]}".blue
    puts ''
    puts "in: #{method_hash[:overriding_location][0]}:#{method_hash[:overriding_location][1]}".italic
  end
end

#summarize_overridesObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/overrides_tracker/methods_collector.rb', line 143

def summarize_overrides
  puts ""
  puts "==========================================================================================="
  puts ""
  puts ""
  puts "SUMMARY"
  puts ""
  @overridden_methods_collection.each do |class_name, class_methods|
    class_methods[:instance_methods].each do |method_name, method_hash|
      show_override(class_name, method_name, method_hash, '#', 'overridden')
    end
    class_methods[:singleton_methods].each do |method_name, method_hash|
      show_override(class_name, method_name, method_hash, '.', 'overridden')
    end
    class_methods[:added_instance_methods].each do |method_name, method_hash|
      show_override(class_name, method_name, method_hash, '#', 'added')
    end
    class_methods[:added_singleton_methods].each do |method_name, method_hash|      
      show_override(class_name, method_name, method_hash, '.', 'added')
    end
  end
end

#write_html_reportObject



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/overrides_tracker/methods_collector.rb', line 236

def write_html_report
  require 'fileutils'
  source = File.join(Gem::Specification.find_by_name("overrides_tracker").gem_dir(), '/lib/html')
  target = File.join(Dir.pwd, '/overrides_tracker')
  FileUtils.copy_entry source, target
  
  file_path = File.join(Dir.pwd, "/overrides_tracker/summary.html")
  html_text = nil
  begin
    File.open(file_path) do |f|
      html_text = f.read
    end
  rescue StandardError
    puts "Error processing #{file_path}"
  end

  overrides_inside_codebase = ''
  added_methods_inside_codebase = ''
  overrides_outside_codebase = ''

  number_of_overrides_inside = 0
  number_of_overrides_outside = 0
  number_of_added_methods = 0

  @overridden_methods_collection.sort.to_h.each do |class_name, class_methods|
    class_methods[:instance_methods].each do |method_name, method_hash|
      if method_hash[:is_part_of_app] || method_hash[:overriding_is_part_of_app]
        overrides_inside_codebase += write_override_html(class_name, method_name, method_hash, '#', 'overridden')
        number_of_overrides_inside+=1
      else
        overrides_outside_codebase += write_override_html(class_name, method_name, method_hash, '#', 'overridden')
        number_of_overrides_outside+=1
      end
    end
    class_methods[:singleton_methods].each do |method_name, method_hash|
      if method_hash[:is_part_of_app] || method_hash[:overriding_is_part_of_app]
        overrides_inside_codebase += write_override_html(class_name, method_name, method_hash, '.', 'overridden')
        number_of_overrides_inside+=1
      else
        overrides_outside_codebase += write_override_html(class_name, method_name, method_hash, '.', 'overridden')
        number_of_overrides_outside+=1
      end
    end
    class_methods[:added_instance_methods].each do |method_name, method_hash|
      added_methods_inside_codebase += write_override_html(class_name, method_name, method_hash, '#', 'added')
      number_of_added_methods+=1
    end
    class_methods[:added_singleton_methods].each do |method_name, method_hash|      
      added_methods_inside_codebase += write_override_html(class_name, method_name, method_hash, '.', 'added')
      number_of_added_methods+=1
    end
  end

  html_text.gsub!('<!--DATE-->', DateTime.now.strftime('%d/%m/%Y %H:%M:%S'))
  html_text.gsub!('<!--BRANCH_NAME-->', branch_name)
  html_text.gsub!('<!--BRANCH_NAME_TO_REPORT-->', branch_name_to_report)
  html_text.gsub!('<!--LAST_COMMIT_ID-->', last_commit_id)
  html_text.gsub!('<!--LAST_COMMIT_NAME-->', last_commit_name)
 
  classes_with_overrides = @methods_collection.select do |_key, val|
    !val[:instance_methods].nil? || !val[:singleton_methods].nil?
  end
  classes_with_overrides_transformed = classes_with_overrides.map do |k, v|
    [k, v[:instance_methods], v[:singleton_methods]]
  end
      
  html_text.gsub!('<!--NUMBER_OF_CLASSES_INVESTIGATED-->', @methods_collection.size.to_s)
  html_text.gsub!('<!--NUMBER_OF_METHODS_INVESTIGATED-->', classes_with_overrides_transformed.sum { |a| a[1].size + a[2].size }.to_s)

  html_text.gsub!('<!--NUMBER_OF_OVERRIDES_INSIDE-->', number_of_overrides_inside.to_s)
  html_text.gsub!('<!--NUMBER_OF_ADDED_METHODS-->',  number_of_added_methods.to_s)
  html_text.gsub!('<!--NUMBER_OF_OVERRIDES_OUTSIDE-->', number_of_overrides_outside.to_s)

  html_text.gsub!('<!--OVERRIDES_INSIDE_CODEBASE-->', overrides_inside_codebase)
  html_text.gsub!('<!--ADDED_METHODS_INSIDE_CODEBASE-->', added_methods_inside_codebase)
  html_text.gsub!('<!--OVERRIDES_OUTSIDE_CODEBASE-->', overrides_outside_codebase)

  File.open(file_path, 'w') do |f|
    f << html_text
  end

  puts '  '
  puts '==========='
  puts "Find your all your overrides here:"
  puts "#{file_path}"
end

#write_override_html(class_name, method_name, method_hash, separator = '#', word_choice = 'overridden') ⇒ Object



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/overrides_tracker/methods_collector.rb', line 323

def write_override_html(class_name, method_name, method_hash, separator = '#', word_choice = 'overridden')
  output ='<div class="col-12">'
  output +='<h4 class="break-all-words">'
  output +="#{class_name}#{separator}#{method_name}"
  output +='</h4>'
  output +='</div>'
  output +='<div class="row">'
  output +='<div class="col-12 col-md-6">'
  output +='<h6 class="break-all-words">'
  output +='Original source'
  output +='</h6>'
  unless method_hash[:body].nil?
    output +='<p class="text-break text-muted">'
    output +="#{method_hash[:location][0]}:#{method_hash[:location][1]}"
    output +='<button class="btn btn-primary btn-sm clipboard-btn ms-2" data-clipboard-action="copy" data-clipboard-text="'+"#{method_hash[:location][0]}:#{method_hash[:location][1]}"+'" type="button">'
    output +='<i class="mdi mdi-content-copy"></i>'
    output +='</button>'
    output +='</p>'
    output +='<div id="block">'
    output +='<pre><code class="codeblock javascript" data-mark-lines="" style="counter-reset: line-numbering 6;">'
    output += method_hash[:body]
    output +='</code>'
    output +='</pre>'
  else
    output +='<p class="text-break text-muted">'
    output +="Not found"
    output +='</p'
  end
  output +='</div>'
  output +='</div>'
  output +='<div class="col-12 col-md-6">'
  output +='<h6>'
  output +='Override'
  output +='</h6>'
  unless method_hash[:overriding_body].nil?
    output +='<p class="text-break text-muted">'
    output +="#{method_hash[:overriding_location][0]}:#{method_hash[:overriding_location][1]}"
    output +='<button class="btn btn-primary btn-sm clipboard-btn ms-2" data-clipboard-action="copy" data-clipboard-text="'+"#{method_hash[:overriding_location][0]}:#{method_hash[:overriding_location][1]}"+'" type="button">'
    output +='<i class="mdi mdi-content-copy"></i>'
    output +='</button>'
    output +='</p>'
    output +='<div id="block">'
    output +='<pre><code class="codeblock javascript" data-mark-lines="" style="counter-reset: line-numbering 28;">'
    output += method_hash[:overriding_body]
    output +='</code>'
    output +='</pre>'
   
  else
    output +='<p class="text-break text-muted">'
    output +="Not found"
    output +='</p'
  end      
  output +='</div>'
  output +='</div>'
  output +='</div>'
  output +='<hr/>'

  return output
end