Class: GemBench::Team
- Inherits:
-
Object
- Object
- GemBench::Team
- 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.
-
#check_gemfile ⇒ Object
Returns the value of attribute check_gemfile.
-
#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.
-
#gemfile_path ⇒ Object
Returns the value of attribute gemfile_path.
-
#paths ⇒ Object
Returns the value of attribute paths.
-
#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.
- #nothing ⇒ Object
- #prepare_bad_ideas ⇒ Object
- #print ⇒ Object
- #strike_out ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Team
Returns a new instance of Team.
12 13 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 39 40 41 |
# File 'lib/gem_bench/team.rb', line 12 def initialize( = {}) possibles = [Bundler.rubygems.gem_dir, Bundler.rubygems.gem_path] @paths = possibles.flatten.compact.uniq.map {|x| x.to_s }.reject { |p| p.empty? }.map {|x| "#{x}/gems" } begin self.paths << "#{Bundler.install_path}" self.paths << "#{Bundler.bundle_path}/gems" @check_gemfile = true @gemfile_path = "#{Dir.pwd}/Gemfile" rescue Bundler::GemfileNotFound => e # Don't fail here ensure @check_gemfile ||= false @gemfile_path ||= nil end self.paths.uniq! # Gem.loaded_specs are the gems that have been loaded / required. # Among these there may be some that did not need to be. totes = Gem.loaded_specs.values.map {|x| [x.name, x.version.to_s] } @excluded, @all = totes.partition {|x| EXCLUDE.include?(x[0]) } exclusions = "\t(excluding the #{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 = [:bad_ideas] ? true : self.check_gemfile ? false : [:bad_ideas] == false ? false : true puts "[GemBench] Will search for gems in #{self.paths.inspect}\n#{self.check_gemfile ? "[GemBench] Will check Gemfile at #{self.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\n#{exclusions}" self.compare_gemfile if self.check_gemfile self.print if self.verbose end |
Instance Attribute Details
#all ⇒ Object
Returns the value of attribute all.
10 11 12 |
# File 'lib/gem_bench/team.rb', line 10 def all @all end |
#bad_ideas ⇒ Object
Returns the value of attribute bad_ideas.
10 11 12 |
# File 'lib/gem_bench/team.rb', line 10 def bad_ideas @bad_ideas end |
#benchers ⇒ Object
Returns the value of attribute benchers.
10 11 12 |
# File 'lib/gem_bench/team.rb', line 10 def benchers @benchers end |
#check_gemfile ⇒ Object
Returns the value of attribute check_gemfile.
10 11 12 |
# File 'lib/gem_bench/team.rb', line 10 def check_gemfile @check_gemfile end |
#current_gemfile_suggestions ⇒ Object
Returns the value of attribute current_gemfile_suggestions.
10 11 12 |
# File 'lib/gem_bench/team.rb', line 10 def current_gemfile_suggestions @current_gemfile_suggestions end |
#excluded ⇒ Object
Returns the value of attribute excluded.
10 11 12 |
# File 'lib/gem_bench/team.rb', line 10 def excluded @excluded end |
#gemfile_lines ⇒ Object
Returns the value of attribute gemfile_lines.
10 11 12 |
# File 'lib/gem_bench/team.rb', line 10 def gemfile_lines @gemfile_lines end |
#gemfile_path ⇒ Object
Returns the value of attribute gemfile_path.
10 11 12 |
# File 'lib/gem_bench/team.rb', line 10 def gemfile_path @gemfile_path end |
#paths ⇒ Object
Returns the value of attribute paths.
10 11 12 |
# File 'lib/gem_bench/team.rb', line 10 def paths @paths end |
#starters ⇒ Object
Returns the value of attribute starters.
10 11 12 |
# File 'lib/gem_bench/team.rb', line 10 def starters @starters end |
#trash_lines ⇒ Object
Returns the value of attribute trash_lines.
10 11 12 |
# File 'lib/gem_bench/team.rb', line 10 def trash_lines @trash_lines end |
#verbose ⇒ Object
Returns the value of attribute verbose.
10 11 12 |
# File 'lib/gem_bench/team.rb', line 10 def verbose @verbose end |
Instance Method Details
#add_to_roster(player) ⇒ Object
151 152 153 154 155 156 157 |
# File 'lib/gem_bench/team.rb', line 151 def add_to_roster(player) if player.starter? self.starters << player else self.benchers << player end end |
#check(player) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/gem_bench/team.rb', line 139 def check(player) self.paths.each do |path| glob_path = "#{path}/#{player.path_glob}" file_paths = Dir.glob("#{glob_path}") puts "[GemBench] checking #{player} at #{glob_path} (#{file_paths.length} files)" if self.verbose == 'extra' file_paths.each do |file_path| player.set_starter(file_path) return if player.starter? end end end |
#check_all ⇒ Object
131 132 133 134 135 136 137 |
# File 'lib/gem_bench/team.rb', line 131 def check_all self.all.each do |player_data| player = GemBench::Player.new({name: player_data[0], version: player_data[1]}) self.check(player) self.add_to_roster(player) end end |
#compare_gemfile ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/gem_bench/team.rb', line 111 def compare_gemfile f = File.open(self.gemfile_path) # Get all lines as an array all_lines = f.readlines # Remove all the commented || blank lines self.trash_lines, self.gemfile_lines = all_lines.partition {|x| x =~ GemBench::TRASH_REGEX} self.benchers.each_with_index do |player, index| self.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 = self.gemfile_lines.delete_at(self.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 |
#nothing ⇒ Object
93 94 95 |
# File 'lib/gem_bench/team.rb', line 93 def nothing "[GemBench] No gems were evaluated by GemBench.\n#{GemBench::USAGE}" end |
#prepare_bad_ideas ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/gem_bench/team.rb', line 97 def prepare_bad_ideas string = '' if self.benchers.length > 0 gemfile_instruction = self.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
43 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 |
# File 'lib/gem_bench/team.rb', line 43 def print string = '' if self.all.empty? string << nothing elsif self.starters.empty? string << "[GemBench] Found no gems that need to load at boot time.\n" else if self.starters.length > 0 string << "\n#{GemBench::USAGE}" unless self.check_gemfile self.starters.each do |starter| string << "[GemBench] You might want to verify that #{starter} really has a Rails::Railtie or Rails::Engine. Check these files:\n" starter.stats.each do |stat| string << "\t#{stat}\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" string << "[GemBench] #{self.starters.length} out of #{self.all.length} evaluated gems actually need to be loaded at boot time. They are:\n" self.starters.each_with_index do |starter, index| string << "#{starter.suggest(index + 1)}\n" end else string << "[GemBench] Congrats! No gems to load at boot.\n" string << "\n#{GemBench::USAGE}" unless self.check_gemfile end end if self.check_gemfile if self.current_gemfile_suggestions.length > 0 string << "[GemBench] Evaluated #{self.all.length} gems and Gemfile at #{self.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 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
87 88 89 90 91 |
# File 'lib/gem_bench/team.rb', line 87 def strike_out self.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 |