Class: GemBench::Team
- Inherits:
-
Object
- Object
- GemBench::Team
- Extended by:
- Forwardable
- Defined in:
- lib/gem_bench/team.rb
Constant Summary collapse
- EXCLUDE =
[ 'bundler','gem_bench','i18n-airbrake','devise-async','km','vestal_versions','omniauth-facebook', 'flag_shih_tzu','pry-remote','koala','simple_form','thumbs_up','memoist','cancan','friendly_id', 'faker']
Instance Attribute Summary collapse
-
#all ⇒ Object
Returns the value of attribute all.
-
#bad_ideas ⇒ Object
Returns the value of attribute bad_ideas.
-
#benchers ⇒ Object
Returns the value of attribute benchers.
-
#current_gemfile_suggestions ⇒ Object
Returns the value of attribute current_gemfile_suggestions.
-
#excluded ⇒ Object
Returns the value of attribute excluded.
-
#gemfile_lines ⇒ Object
Returns the value of attribute gemfile_lines.
-
#look_for_regex ⇒ Object
readonly
Returns the value of attribute look_for_regex.
-
#scout ⇒ Object
readonly
Returns the value of attribute scout.
-
#starters ⇒ Object
Returns the value of attribute starters.
-
#trash_lines ⇒ Object
Returns the value of attribute trash_lines.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
Instance Method Summary collapse
- #add_to_roster(player) ⇒ Object
- #check(player) ⇒ Object
- #check_all ⇒ Object
- #compare_gemfile ⇒ Object
-
#initialize(options = {}) ⇒ Team
constructor
A new instance of Team.
- #list_starters(format: :name) ⇒ Object
- #nothing ⇒ Object
- #prepare_bad_ideas ⇒ Object
- #print ⇒ Object
- #strike_out ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Team
Returns a new instance of Team.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/gem_bench/team.rb', line 14 def initialize( = {}) @look_for_regex = [:look_for_regex] # find: Find gems containing specific strings in code # bench: Find gems that can probably be benched (require: false) in the Gemfile @check_type = @look_for_regex ? :find : :bench @benching = @check_type == :bench @scout = GemBench::Scout.new(check_gemfile: [:check_gemfile] || benching?) @exclude_file_pattern_regex_proc = [:exclude_file_pattern_regex_proc].respond_to?(:call) ? [:exclude_file_pattern_regex_proc] : GemBench::EXCLUDE_FILE_PATTERN_REGEX_PROC # Among the loaded gems there may be some that did not need to be. @excluded, @all = @scout.loaded_gems.partition {|x| EXCLUDE.include?(x[0]) } exclusions = " + #{self.excluded.length} loaded gems which GemBench is configured to ignore.\n" if @excluded.length > 0 @starters = [] @benchers = [] @current_gemfile_suggestions = [] @verbose = [:verbose] self.check_all @bad_ideas = if benching? [:bad_ideas] ? true : check_gemfile? ? false : [:bad_ideas] == false ? false : true else false end 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}" self.compare_gemfile if benching? && @scout.check_gemfile? self.print if self.verbose end |
Instance Attribute Details
#all ⇒ Object
Returns the value of attribute all.
12 13 14 |
# File 'lib/gem_bench/team.rb', line 12 def all @all end |
#bad_ideas ⇒ Object
Returns the value of attribute bad_ideas.
12 13 14 |
# File 'lib/gem_bench/team.rb', line 12 def bad_ideas @bad_ideas end |
#benchers ⇒ Object
Returns the value of attribute benchers.
12 13 14 |
# File 'lib/gem_bench/team.rb', line 12 def benchers @benchers end |
#current_gemfile_suggestions ⇒ Object
Returns the value of attribute current_gemfile_suggestions.
12 13 14 |
# File 'lib/gem_bench/team.rb', line 12 def current_gemfile_suggestions @current_gemfile_suggestions end |
#excluded ⇒ Object
Returns the value of attribute excluded.
12 13 14 |
# File 'lib/gem_bench/team.rb', line 12 def excluded @excluded end |
#gemfile_lines ⇒ Object
Returns the value of attribute gemfile_lines.
12 13 14 |
# File 'lib/gem_bench/team.rb', line 12 def gemfile_lines @gemfile_lines end |
#look_for_regex ⇒ Object (readonly)
Returns the value of attribute look_for_regex.
11 12 13 |
# File 'lib/gem_bench/team.rb', line 11 def look_for_regex @look_for_regex end |
#scout ⇒ Object (readonly)
Returns the value of attribute scout.
11 12 13 |
# File 'lib/gem_bench/team.rb', line 11 def scout @scout end |
#starters ⇒ Object
Returns the value of attribute starters.
12 13 14 |
# File 'lib/gem_bench/team.rb', line 12 def starters @starters end |
#trash_lines ⇒ Object
Returns the value of attribute trash_lines.
12 13 14 |
# File 'lib/gem_bench/team.rb', line 12 def trash_lines @trash_lines end |
#verbose ⇒ Object
Returns the value of attribute verbose.
12 13 14 |
# File 'lib/gem_bench/team.rb', line 12 def verbose @verbose end |
Instance Method Details
#add_to_roster(player) ⇒ Object
167 168 169 170 171 172 173 |
# File 'lib/gem_bench/team.rb', line 167 def add_to_roster(player) if player.starter? self.starters << player else self.benchers << player end end |
#check(player) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/gem_bench/team.rb', line 155 def check(player) gem_paths.each do |path| glob_path = "#{path}/#{player.file_path_glob}" file_paths = Dir.glob("#{glob_path}") puts "[GemBench] checking #{player} at #{glob_path} (#{file_paths.length} files)" if extra_verbose? file_paths.each do |file_path| player.set_starter(file_path, line_match: look_for_regex) return if player.starter? end end end |
#check_all ⇒ Object
146 147 148 149 150 151 152 153 |
# File 'lib/gem_bench/team.rb', line 146 def check_all self.all.each do |player_data| exclude_file_pattern = @exclude_file_pattern_regex_proc.call(player_data[0]) player = GemBench::Player.new({name: player_data[0], version: player_data[1], exclude_file_pattern: exclude_file_pattern}) self.check(player) self.add_to_roster(player) end end |
#compare_gemfile ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/gem_bench/team.rb', line 131 def compare_gemfile self.benchers.each do |player| scout.gemfile_lines.each do |line| found = (line =~ player.gemfile_regex) if found # remove the found line from the array, because no sane person has more than one gem dependency per line... right? line = scout.gemfile_lines.delete_at(scout.gemfile_lines.index(line)) # does the line already have require: false? self.current_gemfile_suggestions << self.benchers.delete_at(self.benchers.index(player)) unless line =~ GemBench::REQUIRE_FALSE_REGEX break # outside of the inner loop end end end end |
#list_starters(format: :name) ⇒ Object
40 41 42 |
# File 'lib/gem_bench/team.rb', line 40 def list_starters(format: :name) starters.map {|starter| starter.to_s(format)} end |
#nothing ⇒ Object
113 114 115 |
# File 'lib/gem_bench/team.rb', line 113 def nothing "[GemBench] No gems were evaluated by GemBench.\n#{GemBench::USAGE}" end |
#prepare_bad_ideas ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/gem_bench/team.rb', line 117 def prepare_bad_ideas string = '' if self.benchers.length > 0 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" 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}" self.benchers.each_with_index do |player, index| string << "#{player.careful(index + 1)}\n" end else string << self.strike_out end string end |
#print ⇒ Object
44 45 46 47 48 49 50 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 104 105 |
# File 'lib/gem_bench/team.rb', line 44 def print string = '' if self.all.empty? string << nothing elsif self.starters.empty? string << if benching? "[GemBench] Found no gems that need to load at boot time.\n" else "[GemBench] Found no gems containing #{look_for_regex} in Ruby code.\n" end else if self.starters.length > 0 string << "\n#{GemBench::USAGE}" unless check_gemfile? string << if benching? "[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" else "[GemBench] We found #{look_for_regex} in the following files.\n\n" end self.starters.each do |starter| string << "\t#{starter}:\n" starter.stats.each do |stat| string << "\t\t#{stat[0]}:#{stat[1]}\n" end end string << "[GemBench] If you want to check for false positives, the files to check for Railties and Engines are listed above.\n" if benching? string << if benching? "[GemBench] #{self.starters.length} out of #{self.all.length} evaluated gems actually need to be loaded at boot time. They are:\n" else "[GemBench] #{self.starters.length} out of #{self.all.length} evaluated gems contain #{look_for_regex}. They are:\n" end self.starters.each_with_index do |starter, index| string << "#{starter.info(index + 1)}\n" end if extra_verbose? && !benching? && self.benchers.length > 0 string << "[GemBench] #{self.benchers.length} out of #{self.all.length} evaluated gems did not contain #{look_for_regex}. They are:\n" self.benchers.each_with_index do |bencher, index| string << "#{bencher.info(index + 1)}\n" end end else string << "[GemBench] Congrats! All gems appear clean.\n" string << "\n#{GemBench::USAGE}" unless check_gemfile? end end if check_gemfile? && benching? if self.current_gemfile_suggestions.length > 0 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" self.current_gemfile_suggestions.each_with_index do |player, index| string << "#{player.suggest(index + 1)}\n" end else string << self.strike_out end end if benching? && self.bad_ideas # Only bad ideas if you are evaluating an actual Gemfile. If just evaluating loaded gems, then info is fine. string << self.prepare_bad_ideas end puts string end |
#strike_out ⇒ Object
107 108 109 110 111 |
# File 'lib/gem_bench/team.rb', line 107 def strike_out check_gemfile? ? "[GemBench] Evaluated #{self.all.length} gems against your Gemfile but found no primary dependencies which can safely skip require on boot (require: false).\n" : "[GemBench] Evaluated #{self.all.length} gems but found none which can safely skip require on boot (require: false).\n" end |