Class: Sorbet::Private::RequireEverything

Inherits:
Object
  • Object
show all
Defined in:
lib/require_everything.rb

Class Method Summary collapse

Class Method Details

.load_bundlerObject



32
33
34
35
36
37
38
39
40
# File 'lib/require_everything.rb', line 32

def self.load_bundler
  return unless File.exist?('Gemfile')
  begin
    require 'bundler'
  rescue LoadError
    return
  end
  Sorbet::Private::GemLoader.require_all_gems
end

.load_railsObject



23
24
25
26
27
28
29
30
# File 'lib/require_everything.rb', line 23

def self.load_rails
  return unless rails?
  require './config/application'
  rails = Object.const_get(:Rails)
  rails.application.require_environment!
  rails.application.eager_load!
  true
end

.my_require(abs_path, numerator, denominator) ⇒ Object



82
83
84
85
86
# File 'lib/require_everything.rb', line 82

def self.my_require(abs_path, numerator, denominator)
  rel_path = Pathname.new(abs_path).relative_path_from(Pathname.new(Dir.pwd)).to_s
  Sorbet::Private::Status.say("[#{numerator}/#{denominator}] require_relative './#{rel_path}'")
  require_relative abs_path
end

.patch_kernelObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/require_everything.rb', line 88

def self.patch_kernel
  Kernel.send(:define_method, :exit) do |*|
    puts 'Kernel#exit was called while requiring ruby source files'
    raise ExitCalledError.new
  end

  Kernel.send(:define_method, :at_exit) do |&block|
    if File.split($0).last == 'rake'
      # Let `rake test` work
      super
      return proc {}
    end
    # puts "Ignoring at_exit: #{block}"
    proc {}
  end
end

.require_all_filesObject



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
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/require_everything.rb', line 42

def self.require_all_files
  excluded_paths = Set.new
  excluded_paths += excluded_rails_files if rails?

  abs_paths = rb_file_paths
  errors = []
  abs_paths.each_with_index do |abs_path, i|
    # Executable files are likely not meant to be required.
    # Some things we're trying to prevent against:
    # - misbehaving require-time side effects (removing files, reading from stdin, etc.)
    # - extra long runtime (making network requests, running a benchmark)
    # While this isn't a perfect heuristic for these things, it's pretty good.
    next if File.executable?(abs_path)
    next if excluded_paths.include?(abs_path)

    # Skip db/schema.rb, as requiring it can wipe the database. This is left
    # out of exclude_rails_files, as it is possible to use the packages that
    # generate it without using the whole rails ecosystem.
    next if /db\/schema.rb$/.match(abs_path)

    begin
      my_require(abs_path, i+1, abs_paths.size)
    rescue LoadError, NoMethodError, SyntaxError
      next
    rescue
      errors << abs_path
      next
    end
  end
  # one more chance for order dependent things
  errors.each_with_index do |abs_path, i|
    begin
      my_require(abs_path, i+1, errors.size)
    rescue
    end
  end

  Sorbet::Private::Status.done
end

.require_everythingObject

Goes through the most common ways to require all your userland code



14
15
16
17
18
19
20
21
# File 'lib/require_everything.rb', line 14

def self.require_everything
  return if @already_ran
  @already_ran = true
  patch_kernel
  load_rails
  load_bundler # this comes second since some rails projects fail `Bundler.require' before rails is loaded
  require_all_files
end