gem_bench 1.0.5 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,14 +2,37 @@ require "forwardable"
2
2
 
3
3
  module GemBench
4
4
  class Team
5
- EXCLUDE = [ 'bundler','gem_bench','i18n-airbrake','devise-async','km','vestal_versions','omniauth-facebook',
6
- 'flag_shih_tzu','pry-remote','koala','simple_form','thumbs_up','memoist','cancan','friendly_id',
7
- 'faker']
5
+ EXCLUDE = %w[
6
+ bundler
7
+ gem_bench
8
+ i18n-airbrake
9
+ devise-async
10
+ km
11
+ vestal_versions
12
+ omniauth-facebook
13
+ flag_shih_tzu
14
+ pry-remote
15
+ koala
16
+ simple_form
17
+ thumbs_up
18
+ memoist
19
+ cancan
20
+ friendly_id
21
+ faker
22
+ ]
8
23
  # A comment preceding the require: false anywhere on the line should not be considered an active require: false
9
24
  extend Forwardable
10
25
  def_delegators :@scout, :gem_paths, :gemfile_path, :check_gemfile?, :loaded_gems
11
26
  attr_reader :scout, :look_for_regex
12
- attr_accessor :all, :excluded, :starters, :benchers, :verbose, :gemfile_lines, :trash_lines, :current_gemfile_suggestions, :bad_ideas
27
+ attr_accessor :all,
28
+ :excluded,
29
+ :starters,
30
+ :benchers,
31
+ :verbose,
32
+ :gemfile_lines,
33
+ :trash_lines,
34
+ :current_gemfile_suggestions,
35
+ :bad_ideas
13
36
 
14
37
  def initialize(options = {})
15
38
  @look_for_regex = options[:look_for_regex]
@@ -20,94 +43,104 @@ module GemBench
20
43
  @scout = GemBench::Scout.new(check_gemfile: options[:check_gemfile] || benching?)
21
44
  @exclude_file_pattern_regex_proc = options[:exclude_file_pattern_regex_proc].respond_to?(:call) ? options[:exclude_file_pattern_regex_proc] : GemBench::EXCLUDE_FILE_PATTERN_REGEX_PROC
22
45
  # Among the loaded gems there may be some that did not need to be.
23
- @excluded, @all = @scout.loaded_gems.partition {|x| EXCLUDE.include?(x[0]) }
24
- exclusions = " + #{self.excluded.length} loaded gems which GemBench is configured to ignore.\n" if @excluded.length > 0
46
+ @excluded, @all = @scout.loaded_gems.partition { |x| EXCLUDE.include?(x[0]) }
47
+ exclusions = " + #{excluded.length} loaded gems which GemBench is configured to ignore.\n" if @excluded.length > 0
25
48
  @starters = []
26
49
  @benchers = []
27
50
  @current_gemfile_suggestions = []
28
51
  @verbose = options[:verbose]
29
- self.check_all
52
+ check_all
30
53
  @bad_ideas = if benching?
31
- options[:bad_ideas] ? true : check_gemfile? ? false : options[:bad_ideas] == false ? false : true
32
- else
33
- false
34
- end
35
- puts "[GemBench] Will search for gems in #{gem_paths.inspect}\n#{benching? ? @scout.check_gemfile? ? "[GemBench] Will check Gemfile at #{gemfile_path}.\n" : "[GemBench] No Gemfile found.\n" : ""}#{self.bad_ideas ? "[GemBench] Will show bad ideas. Be Careful.\n" : ''}[GemBench] Detected #{self.all.length} loaded gems#{exclusions}"
36
- self.compare_gemfile if benching? && @scout.check_gemfile?
37
- self.print if self.verbose
54
+ if options[:bad_ideas]
55
+ true
56
+ else
57
+ check_gemfile? ? false : !(options[:bad_ideas] == false)
58
+ end
59
+ else
60
+ false
61
+ end
62
+ puts "[GemBench] Will search for gems in #{gem_paths.inspect}\n#{if benching?
63
+ @scout.check_gemfile? ? "[GemBench] Will check Gemfile at #{gemfile_path}.\n" : "[GemBench] No Gemfile found.\n"
64
+ else
65
+ ""
66
+ end}#{bad_ideas ? "[GemBench] Will show bad ideas. Be Careful.\n" : ""}[GemBench] Detected #{all.length} loaded gems#{exclusions}"
67
+ compare_gemfile if benching? && @scout.check_gemfile?
68
+ self.print if verbose
38
69
  end
39
70
 
40
71
  def list_starters(format: :name)
41
- starters.map {|starter| starter.to_s(format)}
72
+ starters.map { |starter| starter.to_s(format) }
42
73
  end
43
74
 
44
75
  def print
45
- string = ''
46
- if self.all.empty?
76
+ string = ""
77
+ if all.empty?
47
78
  string << nothing
48
- elsif self.starters.empty?
79
+ elsif starters.empty?
49
80
  string << if benching?
50
- "[GemBench] Found no gems that need to load at boot time.\n"
51
- else
52
- "[GemBench] Found no gems containing #{look_for_regex} in Ruby code.\n"
53
- end
54
- else
55
- if self.starters.length > 0
56
- string << "\n#{GemBench::USAGE}" unless check_gemfile?
57
- string << if benching?
58
- "[GemBench] We found a Rails::Railtie or Rails::Engine in the following files. However, it is possible that there are false positives, so you may want to verify that this is the case.\n\n"
59
- else
60
- "[GemBench] We found #{look_for_regex} in the following files.\n\n"
61
- end
62
- self.starters.each do |starter|
63
- string << "\t#{starter}:\n"
64
- starter.stats.each do |stat|
65
- string << "\t\t#{stat[0]}:#{stat[1]}\n"
66
- end
67
- end
68
- string << "[GemBench] If you want to check for false positives, the files to check for Railties and Engines are listed above.\n" if benching?
69
- string << if benching?
70
- "[GemBench] #{self.starters.length} out of #{self.all.length} evaluated gems actually need to be loaded at boot time. They are:\n"
71
- else
72
- "[GemBench] #{self.starters.length} out of #{self.all.length} evaluated gems contain #{look_for_regex}. They are:\n"
73
- end
74
- self.starters.each_with_index do |starter, index|
75
- string << "#{starter.info(index + 1)}\n"
76
- end
77
- if extra_verbose? && !benching? && self.benchers.length > 0
78
- string << "[GemBench] #{self.benchers.length} out of #{self.all.length} evaluated gems did not contain #{look_for_regex}. They are:\n"
79
- self.benchers.each_with_index do |bencher, index|
80
- string << "#{bencher.info(index + 1)}\n"
81
- end
81
+ "[GemBench] Found no gems that need to load at boot time.\n"
82
+ else
83
+ "[GemBench] Found no gems containing #{look_for_regex} in Ruby code.\n"
84
+ end
85
+ elsif starters.length > 0
86
+ string << "\n#{GemBench::USAGE}" unless check_gemfile?
87
+ string << if benching?
88
+ "[GemBench] We found a Rails::Railtie or Rails::Engine in the following files. However, it is possible that there are false positives, so you may want to verify that this is the case.\n\n"
89
+ else
90
+ "[GemBench] We found #{look_for_regex} in the following files.\n\n"
91
+ end
92
+ starters.each do |starter|
93
+ string << "\t#{starter}:\n"
94
+ starter.stats.each do |stat|
95
+ string << "\t\t#{stat[0]}:#{stat[1]}\n"
82
96
  end
97
+ end
98
+ if benching?
99
+ string << "[GemBench] If you want to check for false positives, the files to check for Railties and Engines are listed above.\n"
100
+ end
101
+ string << if benching?
102
+ "[GemBench] #{starters.length} out of #{all.length} evaluated gems actually need to be loaded at boot time. They are:\n"
83
103
  else
84
- string << "[GemBench] Congrats! All gems appear clean.\n"
85
- string << "\n#{GemBench::USAGE}" unless check_gemfile?
104
+ "[GemBench] #{starters.length} out of #{all.length} evaluated gems contain #{look_for_regex}. They are:\n"
105
+ end
106
+ starters.each_with_index do |starter, index|
107
+ string << "#{starter.info(index + 1)}\n"
86
108
  end
109
+ if extra_verbose? && !benching? && benchers.length > 0
110
+ string << "[GemBench] #{benchers.length} out of #{all.length} evaluated gems did not contain #{look_for_regex}. They are:\n"
111
+ benchers.each_with_index do |bencher, index|
112
+ string << "#{bencher.info(index + 1)}\n"
113
+ end
114
+ end
115
+ else
116
+ string << "[GemBench] Congrats! All gems appear clean.\n"
117
+ string << "\n#{GemBench::USAGE}" unless check_gemfile?
87
118
  end
88
119
  if check_gemfile? && benching?
89
- if self.current_gemfile_suggestions.length > 0
90
- string << "[GemBench] Evaluated #{self.all.length} gems and Gemfile at #{gemfile_path}.\n[GemBench] Here are #{self.current_gemfile_suggestions.length} suggestions for improvement:\n"
91
- self.current_gemfile_suggestions.each_with_index do |player, index|
120
+ if current_gemfile_suggestions.length > 0
121
+ string << "[GemBench] Evaluated #{all.length} gems and Gemfile at #{gemfile_path}.\n[GemBench] Here are #{current_gemfile_suggestions.length} suggestions for improvement:\n"
122
+ current_gemfile_suggestions.each_with_index do |player, index|
92
123
  string << "#{player.suggest(index + 1)}\n"
93
124
  end
94
125
  else
95
- string << self.strike_out
126
+ string << strike_out
96
127
  end
97
128
  end
98
129
 
99
- if benching? && self.bad_ideas
130
+ if benching? && bad_ideas
100
131
  # Only bad ideas if you are evaluating an actual Gemfile. If just evaluating loaded gems, then info is fine.
101
- string << self.prepare_bad_ideas
132
+ string << prepare_bad_ideas
102
133
  end
103
134
 
104
135
  puts string
105
136
  end
106
137
 
107
138
  def strike_out
108
- check_gemfile? ?
109
- "[GemBench] Evaluated #{self.all.length} gems against your Gemfile but found no primary dependencies which can safely skip require on boot (require: false).\n" :
110
- "[GemBench] Evaluated #{self.all.length} gems but found none which can safely skip require on boot (require: false).\n"
139
+ if check_gemfile?
140
+ "[GemBench] Evaluated #{all.length} gems against your Gemfile but found no primary dependencies which can safely skip require on boot (require: false).\n"
141
+ else
142
+ "[GemBench] Evaluated #{all.length} gems but found none which can safely skip require on boot (require: false).\n"
143
+ end
111
144
  end
112
145
 
113
146
  def nothing
@@ -115,40 +148,46 @@ module GemBench
115
148
  end
116
149
 
117
150
  def prepare_bad_ideas
118
- string = ''
119
- if self.benchers.length > 0
120
- gemfile_instruction = check_gemfile? ? '' : "To safely evaluate a Gemfile:\n\t1. Make sure you are in the root of a project with a Gemfile\n\t2. Make sure the gem is actually a dependency in the Gemfile\n"
121
- string << "[GemBench] Evaluated #{self.all.length} loaded gems and found #{self.benchers.length} which may be able to skip boot loading (require: false).\n*** => WARNING <= ***: Be careful adding non-primary dependencies to your Gemfile as it is generally a bad idea.\n#{gemfile_instruction}"
122
- self.benchers.each_with_index do |player, index|
151
+ string = ""
152
+ if benchers.length > 0
153
+ gemfile_instruction = check_gemfile? ? "" : "To safely evaluate a Gemfile:\n\t1. Make sure you are in the root of a project with a Gemfile\n\t2. Make sure the gem is actually a dependency in the Gemfile\n"
154
+ string << "[GemBench] Evaluated #{all.length} loaded gems and found #{benchers.length} which may be able to skip boot loading (require: false).\n*** => WARNING <= ***: Be careful adding non-primary dependencies to your Gemfile as it is generally a bad idea.\n#{gemfile_instruction}"
155
+ benchers.each_with_index do |player, index|
123
156
  string << "#{player.careful(index + 1)}\n"
124
157
  end
125
158
  else
126
- string << self.strike_out
159
+ string << strike_out
127
160
  end
128
161
  string
129
162
  end
130
163
 
131
164
  def compare_gemfile
132
- self.benchers.each do |player|
165
+ benchers.each do |player|
133
166
  scout.gemfile_lines.each do |line|
134
167
  found = (line =~ player.gemfile_regex)
135
- if found
136
- # remove the found line from the array, because no sane person has more than one gem dependency per line... right?
137
- line = scout.gemfile_lines.delete_at(scout.gemfile_lines.index(line))
138
- # does the line already have require: false?
139
- self.current_gemfile_suggestions << self.benchers.delete_at(self.benchers.index(player)) unless line =~ GemBench::REQUIRE_FALSE_REGEX
140
- break # outside of the inner loop
168
+ next unless found
169
+
170
+ # remove the found line from the array, because no sane person has more than one gem dependency per line... right?
171
+ line = scout.gemfile_lines.delete_at(scout.gemfile_lines.index(line))
172
+ # does the line already have require: false?
173
+ unless line =~ GemBench::REQUIRE_FALSE_REGEX
174
+ current_gemfile_suggestions << benchers.delete_at(benchers.index(player))
141
175
  end
176
+ break # outside of the inner loop
142
177
  end
143
178
  end
144
179
  end
145
180
 
146
181
  def check_all
147
- self.all.each do |player_data|
182
+ all.each do |player_data|
148
183
  exclude_file_pattern = @exclude_file_pattern_regex_proc.call(player_data[0])
149
- player = GemBench::Player.new({name: player_data[0], version: player_data[1], exclude_file_pattern: exclude_file_pattern})
150
- self.check(player)
151
- self.add_to_roster(player)
184
+ player = GemBench::Player.new({
185
+ name: player_data[0],
186
+ version: player_data[1],
187
+ exclude_file_pattern: exclude_file_pattern,
188
+ })
189
+ check(player)
190
+ add_to_roster(player)
152
191
  end
153
192
  end
154
193
 
@@ -166,16 +205,16 @@ module GemBench
166
205
 
167
206
  def add_to_roster(player)
168
207
  if player.starter?
169
- self.starters << player
208
+ starters << player
170
209
  else
171
- self.benchers << player
210
+ benchers << player
172
211
  end
173
212
  end
174
213
 
175
214
  private
176
215
 
177
216
  def extra_verbose?
178
- self.verbose == "extra"
217
+ verbose == "extra"
179
218
  end
180
219
 
181
220
  def benching?
@@ -1,3 +1,5 @@
1
1
  module GemBench
2
- VERSION = "1.0.5"
2
+ module Version
3
+ VERSION = "2.0.1"
4
+ end
3
5
  end
data/lib/gem_bench.rb CHANGED
@@ -1,5 +1,8 @@
1
- require "gem_bench/version"
1
+ # external gems
2
+ require "version_gem"
2
3
  require "bundler" # This gem utilizes bundler as a tool.
4
+
5
+ require "gem_bench/version"
3
6
  require "gem_bench/scout"
4
7
  require "gem_bench/player"
5
8
  require "gem_bench/team"
@@ -10,19 +13,20 @@ require "gem_bench/strict_version_requirement"
10
13
  module GemBench
11
14
  USAGE = "[GemBench] Usage: Require another gem in this session to evaluate it.\n\tExample:\n\t\trequire 'rails'\n\t\tGemBench.check({verbose: true})\n"
12
15
  RAILTIE_REGEX = /Rails::Engine|Rails::Railtie/
13
- TRASH_REGEX = /^(\s*)([#]+.*)?$/
14
- REQUIRE_FALSE_REGEX = /^[^#]+require(([:]\s*)|(\s*=>\s*))false.*/
16
+ TRASH_REGEX = /^(\s*)(\#+.*)?$/
17
+ REQUIRE_FALSE_REGEX = /^[^#]+require((:\s*)|(\s*=>\s*))false.*/
15
18
  DEPENDENCY_REGEX_PROC = ->(name) { /^\s*[^#]*\s*gem\s+['"]{1}#{name}['"]{1}/ }
16
19
  PATH_GLOB = ->(name) { "#{name}*/lib/**/*.rb" }
17
- EXCLUDE_FILE_PATTERN_REGEX_PROC = ->(name) { /#{name}\/test|features|spec|bin|exe/ }
20
+ EXCLUDE_FILE_PATTERN_REGEX_PROC = ->(name) { %r{#{name}/test|features|spec|bin|exe} }
18
21
  DO_NOT_SCAN = []
19
22
  PLAYER_STATES = {
20
- starter: :starter,
21
- bench: :bench
23
+ starter: :starter,
24
+ bench: :bench,
22
25
  }
23
26
 
24
27
  class << self
25
28
  attr_accessor :roster
29
+
26
30
  def check(verbose: false)
27
31
  @roster = GemBench::Team.new({verbose: verbose})
28
32
  end
@@ -37,8 +41,17 @@ module GemBench
37
41
  @roster.list_missing_version_constraints
38
42
  end
39
43
 
40
- def find(look_for_regex: GemBench::RAILTIE_REGEX, exclude_file_pattern_regex_proc: GemBench::EXCLUDE_FILE_PATTERN_REGEX_PROC, verbose: false)
41
- @roster = GemBench::Team.new({look_for_regex: look_for_regex, exclude_file_pattern_regex_proc: exclude_file_pattern_regex_proc, verbose: verbose})
44
+ def find(look_for_regex: GemBench::RAILTIE_REGEX,
45
+ exclude_file_pattern_regex_proc: GemBench::EXCLUDE_FILE_PATTERN_REGEX_PROC, verbose: false)
46
+ @roster = GemBench::Team.new({
47
+ look_for_regex: look_for_regex,
48
+ exclude_file_pattern_regex_proc: exclude_file_pattern_regex_proc,
49
+ verbose: verbose,
50
+ })
42
51
  end
43
52
  end
44
53
  end
54
+
55
+ GemBench::Version.class_eval do
56
+ extend VersionGem::Basic
57
+ end
data.tar.gz.sig ADDED
Binary file