Class: GemBench::Team

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/gem_bench/team.rb

Constant Summary collapse

EXCLUDE =
%w[
  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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Team

Returns a new instance of Team.



37
38
39
40
41
42
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
# File 'lib/gem_bench/team.rb', line 37

def initialize(options = {})
  @look_for_regex = options[: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: options[:check_gemfile] || benching?)
  @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
  # 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 = " + #{excluded.length} loaded gems which GemBench is configured to ignore.\n" if @excluded.length > 0
  @starters = []
  @benchers = []
  @current_gemfile_suggestions = []
  @verbose = options[:verbose]
  check_all
  @bad_ideas = if benching?
    if options[:bad_ideas]
      true
    else
      check_gemfile? ? false : !(options[:bad_ideas] == false)
    end
  else
    false
  end
  puts "[GemBench] Will search for gems in #{gem_paths.inspect}\n#{if benching?
                                                                     @scout.check_gemfile? ? "[GemBench] Will check Gemfile at #{gemfile_path}.\n" : "[GemBench] No Gemfile found.\n"
                                                                   else
                                                                     ""
                                                                   end}#{bad_ideas ? "[GemBench] Will show bad ideas.  Be Careful.\n" : ""}[GemBench] Detected #{all.length} loaded gems#{exclusions}"
  compare_gemfile if benching? && @scout.check_gemfile?
  self.print if verbose
end

Instance Attribute Details

#allObject

Returns the value of attribute all.



27
28
29
# File 'lib/gem_bench/team.rb', line 27

def all
  @all
end

#bad_ideasObject

Returns the value of attribute bad_ideas.



27
28
29
# File 'lib/gem_bench/team.rb', line 27

def bad_ideas
  @bad_ideas
end

#benchersObject

Returns the value of attribute benchers.



27
28
29
# File 'lib/gem_bench/team.rb', line 27

def benchers
  @benchers
end

#current_gemfile_suggestionsObject

Returns the value of attribute current_gemfile_suggestions.



27
28
29
# File 'lib/gem_bench/team.rb', line 27

def current_gemfile_suggestions
  @current_gemfile_suggestions
end

#excludedObject

Returns the value of attribute excluded.



27
28
29
# File 'lib/gem_bench/team.rb', line 27

def excluded
  @excluded
end

#gemfile_linesObject

Returns the value of attribute gemfile_lines.



27
28
29
# File 'lib/gem_bench/team.rb', line 27

def gemfile_lines
  @gemfile_lines
end

#look_for_regexObject (readonly)

Returns the value of attribute look_for_regex.



26
27
28
# File 'lib/gem_bench/team.rb', line 26

def look_for_regex
  @look_for_regex
end

#scoutObject (readonly)

Returns the value of attribute scout.



26
27
28
# File 'lib/gem_bench/team.rb', line 26

def scout
  @scout
end

#startersObject

Returns the value of attribute starters.



27
28
29
# File 'lib/gem_bench/team.rb', line 27

def starters
  @starters
end

#trash_linesObject

Returns the value of attribute trash_lines.



27
28
29
# File 'lib/gem_bench/team.rb', line 27

def trash_lines
  @trash_lines
end

#verboseObject

Returns the value of attribute verbose.



27
28
29
# File 'lib/gem_bench/team.rb', line 27

def verbose
  @verbose
end

Instance Method Details

#add_to_roster(player) ⇒ Object



206
207
208
209
210
211
212
# File 'lib/gem_bench/team.rb', line 206

def add_to_roster(player)
  if player.starter?
    starters << player
  else
    benchers << player
  end
end

#check(player) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
# File 'lib/gem_bench/team.rb', line 194

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_allObject



181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/gem_bench/team.rb', line 181

def check_all
  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,
    })
    check(player)
    add_to_roster(player)
  end
end

#compare_gemfileObject



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/gem_bench/team.rb', line 164

def compare_gemfile
  benchers.each do |player|
    scout.gemfile_lines.each do |line|
      found = (line =~ player.gemfile_regex)
      next unless 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?
      unless line =~ GemBench::REQUIRE_FALSE_REGEX
        current_gemfile_suggestions << benchers.delete_at(benchers.index(player))
      end
      break # outside of the inner loop
    end
  end
end

#list_starters(format: :name) ⇒ Object



71
72
73
# File 'lib/gem_bench/team.rb', line 71

def list_starters(format: :name)
  starters.map { |starter| starter.to_s(format) }
end

#nothingObject



146
147
148
# File 'lib/gem_bench/team.rb', line 146

def nothing
  "[GemBench] No gems were evaluated by GemBench.\n#{GemBench::USAGE}"
end

#prepare_bad_ideasObject



150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/gem_bench/team.rb', line 150

def prepare_bad_ideas
  string = ""
  if 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 #{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}"
    benchers.each_with_index do |player, index|
      string << "#{player.careful(index + 1)}\n"
    end
  else
    string << strike_out
  end
  string
end


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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/gem_bench/team.rb', line 75

def print
  string = ""
  if all.empty?
    string << nothing
  elsif 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
  elsif 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
    starters.each do |starter|
      string << "\t#{starter}:\n"
      starter.stats.each do |stat|
        string << "\t\t#{stat[0]}:#{stat[1]}\n"
      end
    end
    if benching?
      string << "[GemBench] If you want to check for false positives, the files to check for Railties and Engines are listed above.\n"
    end
    string << if benching?
      "[GemBench] #{starters.length} out of #{all.length} evaluated gems actually need to be loaded at boot time. They are:\n"
    else
      "[GemBench] #{starters.length} out of #{all.length} evaluated gems contain #{look_for_regex}. They are:\n"
    end
    starters.each_with_index do |starter, index|
      string << "#{starter.info(index + 1)}\n"
    end
    if extra_verbose? && !benching? && benchers.length > 0
      string << "[GemBench] #{benchers.length} out of #{all.length} evaluated gems did not contain #{look_for_regex}. They are:\n"
      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
  if check_gemfile? && benching?
    if current_gemfile_suggestions.length > 0
      string << "[GemBench] Evaluated #{all.length} gems and Gemfile at #{gemfile_path}.\n[GemBench] Here are #{current_gemfile_suggestions.length} suggestions for improvement:\n"
      current_gemfile_suggestions.each_with_index do |player, index|
        string << "#{player.suggest(index + 1)}\n"
      end
    else
      string << strike_out
    end
  end

  if benching? && bad_ideas
    # Only bad ideas if you are evaluating an actual Gemfile. If just evaluating loaded gems, then info is fine.
    string << prepare_bad_ideas
  end

  puts string
end

#strike_outObject



138
139
140
141
142
143
144
# File 'lib/gem_bench/team.rb', line 138

def strike_out
  if check_gemfile?
    "[GemBench] Evaluated #{all.length} gems against your Gemfile but found no primary dependencies which can safely skip require on boot (require: false).\n"
  else
    "[GemBench] Evaluated #{all.length} gems but found none which can safely skip require on boot (require: false).\n"
  end
end