p8-metric_fu 0.9.0 → 0.9.0.1

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.
@@ -1,129 +0,0 @@
1
- module MetricFu
2
-
3
- def self.generate_flog_report
4
- Flog::Generator.generate_report
5
- system("open #{Flog::Generator.metric_dir}/index.html") if open_in_browser?
6
- end
7
-
8
- module Flog
9
- class Generator < Base::Generator
10
- def generate_report
11
- @base_dir = self.class.metric_dir
12
- pages = []
13
- flog_results.each do |filename|
14
- page = Base.parse(open(filename, "r") { |f| f.read })
15
- if page
16
- page.filename = filename
17
- pages << page
18
- end
19
- end
20
- generate_pages(pages)
21
- end
22
-
23
- def generate_pages(pages)
24
- pages.each do |page|
25
- unless MetricFu::MD5Tracker.file_already_counted?(page.filename)
26
- generate_page(page)
27
- end
28
- end
29
- save_html(ERB.new(File.read(template_file)).result(binding))
30
- end
31
-
32
- def generate_page(page)
33
- save_html(page.to_html, page.path)
34
- end
35
-
36
- def flog_results
37
- Dir.glob("#{@base_dir}/**/*.txt")
38
- end
39
-
40
- def self.template_name
41
- "flog"
42
- end
43
- end
44
-
45
- SCORE_FORMAT = "%0.2f"
46
-
47
- class Base
48
- METHOD_LINE_REGEX = /([A-Za-z]+#.*):\s\((\d+\.\d+)\)/
49
- OPERATOR_LINE_REGEX = /\s+(\d+\.\d+):\s(.*)$/
50
-
51
- class << self
52
-
53
- def parse(text)
54
- score = text[/\w+ = (\d+\.\d+)/, 1]
55
- return nil unless score
56
- page = Page.new(score)
57
-
58
- text.each_line do |method_line|
59
- if match = method_line.match(METHOD_LINE_REGEX)
60
- page.scanned_methods << ScannedMethod.new(match[1], match[2])
61
- end
62
-
63
- if match = method_line.match(OPERATOR_LINE_REGEX)
64
- return if page.scanned_methods.empty?
65
- page.scanned_methods.last.operators << Operator.new(match[1], match[2])
66
- end
67
- end
68
- page
69
- end
70
-
71
- end
72
- end
73
-
74
- class Page < MetricFu::Base::Generator
75
- attr_accessor :filename, :score, :scanned_methods
76
-
77
- def initialize(score, scanned_methods = [])
78
- @score = score.to_f
79
- @scanned_methods = scanned_methods
80
- end
81
-
82
- def path
83
- @path ||= File.basename(filename, ".txt") + '.html'
84
- end
85
-
86
- def to_html
87
- ERB.new(File.read(template_file)).result(binding)
88
- end
89
-
90
- def average_score
91
- return 0 if scanned_methods.length == 0
92
- sum = 0
93
- scanned_methods.each do |m|
94
- sum += m.score
95
- end
96
- sum / scanned_methods.length
97
- end
98
-
99
- def highest_score
100
- scanned_methods.inject(0) do |highest, m|
101
- m.score > highest ? m.score : highest
102
- end
103
- end
104
-
105
- def template_name
106
- 'flog_page'
107
- end
108
- end
109
-
110
- class Operator
111
- attr_accessor :score, :operator
112
-
113
- def initialize(score, operator)
114
- @score = score.to_f
115
- @operator = operator
116
- end
117
- end
118
-
119
- class ScannedMethod
120
- attr_accessor :name, :score, :operators
121
-
122
- def initialize(name, score, operators = [])
123
- @name = name
124
- @score = score.to_f
125
- @operators = operators
126
- end
127
- end
128
- end
129
- end
@@ -1,52 +0,0 @@
1
- require 'digest/md5'
2
- require 'fileutils'
3
-
4
- module MetricFu
5
- class MD5Tracker
6
-
7
- @@unchanged_md5s = []
8
-
9
- class << self
10
- def md5_dir(path_to_file, base_dir)
11
- File.join(base_dir,
12
- path_to_file.split('/')[0..-2].join('/'))
13
- end
14
-
15
- def md5_file(path_to_file, base_dir)
16
- File.join(md5_dir(path_to_file, base_dir),
17
- path_to_file.split('/').last.sub(/\.[a-z]+/, '.md5'))
18
- end
19
-
20
- def track(path_to_file, base_dir)
21
- md5 = Digest::MD5.hexdigest(File.read(path_to_file))
22
- FileUtils.mkdir_p(md5_dir(path_to_file, base_dir), :verbose => false)
23
- f = File.new(md5_file(path_to_file, base_dir), "w")
24
- f.puts(md5)
25
- f.close
26
- md5
27
- end
28
-
29
- def file_changed?(path_to_file, base_dir)
30
- orig_md5_file = md5_file(path_to_file, base_dir)
31
- return !!track(path_to_file, base_dir) unless File.exist?(orig_md5_file)
32
-
33
- current_md5 = ""
34
- file = File.open(orig_md5_file, 'r')
35
- file.each_line { |line| current_md5 << line }
36
- file.close
37
- current_md5.chomp!
38
-
39
- new_md5 = Digest::MD5.hexdigest(File.read(path_to_file))
40
- new_md5.chomp!
41
-
42
- @@unchanged_md5s << path_to_file if new_md5 == current_md5
43
-
44
- return new_md5 != current_md5
45
- end
46
-
47
- def file_already_counted?(path_to_file)
48
- return @@unchanged_md5s.include?(path_to_file)
49
- end
50
- end
51
- end
52
- end