aidir 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7742d491ef0024bf96b42dc5702247322aa561cd
4
- data.tar.gz: 77c3484dff8c9d897fec772ab81c39824c2b5bfd
3
+ metadata.gz: 3c2de8e8218ddccab613724408ddc4ecd97fcefb
4
+ data.tar.gz: cbaedbc1e39afd759eb67dd1d03226b633ea91d7
5
5
  SHA512:
6
- metadata.gz: 8a567fe9d09b8326ba6458f25c6933564c1f831ab6deb118f5711a6fb387585fd70190d55544ed3f8e2cb0c6985194c0ddd7487497ac52242d0e8c31e818e800
7
- data.tar.gz: 4816f092e78393dac48ea363ef4dffea115d70583f71eb34926f8c44b4401172bfcc96a7a6a6facacf6bc6ace49e1d2a9e53346e0795fc00717fa480657dd88b
6
+ metadata.gz: 85e838414ca0856f4edc386fc3be3a006a7a5d1f40d32f70fe93c062939882a33ab91fbf27265b70c94e116d6590a5b704558b33b38e4c77fa5cd9f86b723ed4
7
+ data.tar.gz: b9c8f0cf400e9d4f2d1a1cc52fadb9b131576e692b2a1a9f0f90d0e8587547f480b0d6eee25dfbd0da4f7bd4d0aae3598f7bb61f8cb6b4eb07ad75698c2ef4f0
data/lib/aidir.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require_relative 'aidir/git'
2
2
  require_relative 'aidir/flog'
3
+ require_relative 'aidir/formatter'
3
4
  require_relative 'aidir/scoreboard'
4
5
  require 'open3'
5
6
 
@@ -28,8 +29,7 @@ class Aidir
28
29
  git.clear_cached_files
29
30
 
30
31
  scoreboard = Scoreboard.new(@results)
31
- output = scoreboard.results
32
- print output
32
+ print scoreboard.results
33
33
  end
34
34
 
35
35
  end
@@ -0,0 +1,105 @@
1
+ class Formatter
2
+
3
+ def caption(title)
4
+ "\n--- #{colors[:cyan]}#{title}#{color_end} ---\n"
5
+ end
6
+
7
+ def table_header(col1, col2, col3)
8
+ sprintf(format[:header], col1, col2, col3)
9
+ end
10
+
11
+ def method_row(method, info)
12
+ current = info[:current]
13
+ if info[:flag]
14
+ contents = preformat_flag(info[:flag])
15
+ else
16
+ contents = preformat_delta(info[:delta])
17
+ end
18
+ sprintf(format[:method_row], contents, preformat_current(current), method)
19
+ end
20
+
21
+ def file_row(metric, info)
22
+ filename = info[:file]
23
+ delta = info[:delta]
24
+ if metric == 'flog total'
25
+ current = preformat_file_total(info[:current])
26
+ elsif metric == 'flog/method average'
27
+ current = preformat_file_avg(info[:current])
28
+ end
29
+ sprintf(format[:method_row], preformat_delta(delta), current, filename)
30
+ end
31
+
32
+ private
33
+
34
+ # Preformatters
35
+
36
+ def preformat_current(score)
37
+ sprintf(format[:current], method_color(score), score, color_end)
38
+ end
39
+
40
+ def preformat_delta(score)
41
+ sprintf(format[:delta], delta_color(score), score, color_end)
42
+ end
43
+
44
+ def preformat_flag(flag)
45
+ sprintf(format[:flag], flag)
46
+ end
47
+
48
+ def preformat_file_total(score)
49
+ sprintf(format[:current], '', score, '')
50
+ end
51
+
52
+ def preformat_file_avg(score)
53
+ sprintf(format[:current], method_color(score), score, color_end)
54
+ end
55
+
56
+ # Format strings
57
+
58
+ def format
59
+ @format ||= {
60
+ header: "%18s%10s %-90s\n",
61
+ delta: "%s%18.1f%s",
62
+ current: "%s%10.1f%s",
63
+ method_row: "%s%s %-90s\n",
64
+ flag: "%18s",
65
+ }
66
+ end
67
+
68
+ # Score coloring logic
69
+
70
+ def delta_color(score)
71
+ delta_score_thresholds = [0, 5]
72
+ score_color(score, delta_score_thresholds)
73
+ end
74
+
75
+ def method_color(score)
76
+ method_score_thresholds = [20, 40]
77
+ score_color(score, method_score_thresholds)
78
+ end
79
+
80
+ def score_color(score, thresholds)
81
+ if score < thresholds[0]
82
+ colors[:green]
83
+ elsif score < thresholds[1]
84
+ colors[:yellow]
85
+ else
86
+ colors[:red]
87
+ end
88
+ end
89
+
90
+ # ASCII rainbows and unicorns go here
91
+
92
+ def colors
93
+ @colors ||= {
94
+ cyan: "\033[36m",
95
+ red: "\033[31m",
96
+ yellow: "\033[33m",
97
+ green: "\033[32m"
98
+ }
99
+ end
100
+
101
+ def color_end
102
+ @color_end ||= "\033[0m"
103
+ end
104
+
105
+ end
@@ -2,16 +2,17 @@ class Scoreboard
2
2
 
3
3
  def initialize(raw_data)
4
4
  @raw_data = raw_data
5
- @diff_data = {}
5
+ @delta_data = {}
6
6
  @file_data = {}
7
7
  @method_data = {}
8
8
  @output = ''
9
+ @formatter = Formatter.new
9
10
  end
10
11
 
11
12
  def results
12
13
  get_relevant_scores
13
14
  if @method_data.empty?
14
- print_no_diffs
15
+ print_no_deltas_found
15
16
  return
16
17
  end
17
18
  print_method_board
@@ -21,144 +22,40 @@ class Scoreboard
21
22
 
22
23
  private
23
24
 
25
+ def file_keys
26
+ @file_keys ||= [
27
+ 'flog total',
28
+ 'flog/method average'
29
+ ]
30
+ end
31
+
24
32
  def get_relevant_scores
25
- get_diffs
33
+ get_deltas
26
34
  split_scores_by_type
27
35
  sort_scores
28
36
  end
29
37
 
30
- def diff_format
31
- @diff_format ||= "%s%18.1f%s"
32
- end
33
-
34
- def current_format
35
- @current_format ||= "%s%10.1f%s"
36
- end
37
-
38
- def diff_output(score)
39
- sprintf(diff_format, diff_prefix(score), score, color_end)
40
- end
41
-
42
- def current_output(score)
43
- sprintf(current_format, current_prefix(score), score, color_end)
44
- end
45
-
46
- def total_current_output(score)
47
- sprintf(current_format, "", score, "")
48
- end
49
-
50
- def avg_current_output(score)
51
- sprintf(current_format, current_prefix(score), score, color_end)
52
- end
53
-
54
- def head_format
55
- @head_format ||= "%18s%10s %-90s\n"
56
- end
57
-
58
- def print_method_board
59
- @output << "--- #{cyan_start}METHOD SCORES#{color_end} ---\n"
60
-
61
- @output << sprintf(head_format, "Diff from master", "Current", "Method")
62
- format = "%s%s %-90s\n"
63
-
64
- @method_data.each do |method, info|
65
- current = info[:current]
66
- unless info[:flag]
67
- diff = info[:diff]
68
- @output << sprintf(format, diff_output(diff), current_output(current), method)
69
- else
70
- flag_output = sprintf("%18s", info[:flag].to_s)
71
- @output << sprintf(format, flag_output, current_output(current), method)
72
- end
73
- end
74
- end
75
-
76
- def print_file_board
77
- @output << "--- #{cyan_start}FILE SCORES#{color_end} ---\n"
78
-
79
- @output << sprintf(head_format, "Diff from master", "Current", "Method")
80
- format = "%s%s %-90s\n"
81
-
82
- @file_data.each do |metric, files|
83
- @output << sprintf("%-18s\n", "#{cyan_start}#{metric}#{color_end}")
84
- files.each do |file|
85
- filename = file[:file]
86
- diff = file[:diff]
87
- current = file[:current]
88
- if metric == "flog total"
89
- @output << sprintf(format, diff_output(diff), total_current_output(current), filename)
90
- else
91
- @output << sprintf(format, diff_output(diff), avg_current_output(current), filename)
92
- end
93
- end
94
- end
95
- end
96
-
97
- def print_no_diffs
98
- @output << "#{cyan_start}No changes detected#{color_end}\n"
99
- end
100
-
101
- def diff_prefix(score)
102
- if score < 0
103
- green_start
104
- elsif score < 5
105
- yellow_start
106
- else
107
- red_start
108
- end
109
- end
110
-
111
- def current_prefix(score)
112
- if score < 20
113
- green_start
114
- elsif score < 40
115
- yellow_start
116
- else
117
- red_start
118
- end
119
- end
120
-
121
- def cyan_start
122
- "\033[36m"
123
- end
124
-
125
- def red_start
126
- "\033[31m"
127
- end
128
-
129
- def yellow_start
130
- "\033[33m"
131
- end
132
-
133
- def green_start
134
- "\033[32m"
135
- end
136
-
137
- def color_end
138
- "\033[0m"
139
- end
140
-
141
38
  def split_scores_by_type
142
- @method_data = @diff_data
39
+ @method_data = @delta_data
143
40
  file_keys.each do |key|
144
41
  @file_data[key] = @method_data.delete(key)
145
42
  end
146
43
  end
147
44
 
148
- def get_diffs
45
+ def get_deltas
149
46
  @raw_data.each do |file, lines|
150
47
  lines.each do |metric, scores|
151
- score = diff(scores[:branch], scores[:master])
48
+ score = calc_diff(scores[:branch], scores[:master])
152
49
  next if score == 0.0
153
- @diff_data[metric] ||= []
50
+ @delta_data[metric] ||= []
154
51
  flag = nil
155
52
  if [:new, :deleted].include?(score)
156
53
  flag = score
157
54
  score = 0.0
158
55
  end
159
- @diff_data[metric] << {
56
+ @delta_data[metric] << {
160
57
  file: file,
161
- diff: score,
58
+ delta: score,
162
59
  current: scores[:branch],
163
60
  flag: flag
164
61
  }
@@ -169,13 +66,13 @@ class Scoreboard
169
66
  def sort_scores
170
67
  return if @method_data.empty?
171
68
  file_keys.each do |key|
172
- @file_data[key].sort_by! { |f| -f[:diff] }
69
+ @file_data[key].sort_by! { |f| -f[:delta] }
173
70
  end
174
- sorted_methods = @method_data.sort_by { |_, data| -data[0][:diff] }.flatten
71
+ sorted_methods = @method_data.sort_by { |_, data| -data[0][:delta] }.flatten
175
72
  @method_data = Hash[*sorted_methods]
176
73
  end
177
74
 
178
- def diff(branch, master)
75
+ def calc_diff(branch, master)
179
76
  if master and branch
180
77
  (branch - master).round(2)
181
78
  elsif branch
@@ -185,11 +82,27 @@ class Scoreboard
185
82
  end
186
83
  end
187
84
 
188
- def file_keys
189
- [
190
- 'flog total',
191
- 'flog/method average'
192
- ]
85
+ def print_method_board
86
+ @output << @formatter.caption('METHOD SCORES')
87
+ @output << @formatter.table_header('Diff from master', 'Current', 'Method')
88
+
89
+ @method_data.each do |method, info|
90
+ @output << @formatter.method_row(method, info)
91
+ end
92
+ end
93
+
94
+ def print_file_board
95
+ @file_data.each do |metric, files|
96
+ @output << @formatter.caption("FILE #{metric} SCORES")
97
+ @output << @formatter.table_header('Diff from master', 'Current', 'File')
98
+ files.each do |file_info|
99
+ @output << @formatter.file_row(metric, file_info)
100
+ end
101
+ end
102
+ end
103
+
104
+ def print_no_deltas_found
105
+ print @formatter.caption('No changes detected')
193
106
  end
194
107
 
195
108
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aidir
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adomas Sliužinskas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-13 00:00:00.000000000 Z
11
+ date: 2013-10-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: flog
@@ -38,9 +38,9 @@ dependencies:
38
38
  - - ~>
39
39
  - !ruby/object:Gem::Version
40
40
  version: 4.1.2
41
- description: 'aidir - Am I Doing It Right: track and improve your Flog (http://ruby.sadi.st/Flog.html)
42
- score before merging code to master by getting Flog score differences between current
43
- branch and master branch'
41
+ description: 'aidir - Am I Doing It Right: track and improve your Flog score before
42
+ merging code to master by getting Flog score differences between current branch
43
+ and master branch'
44
44
  email: adomas.sliuzinskas@gmail.com
45
45
  executables:
46
46
  - aidir
@@ -49,6 +49,7 @@ extra_rdoc_files: []
49
49
  files:
50
50
  - lib/aidir.rb
51
51
  - lib/aidir/flog.rb
52
+ - lib/aidir/formatter.rb
52
53
  - lib/aidir/git.rb
53
54
  - lib/aidir/scoreboard.rb
54
55
  - bin/aidir