Class: Sorbet::Private::RequireEverything
- Inherits:
-
Object
- Object
- Sorbet::Private::RequireEverything
- Defined in:
- lib/require_everything.rb
Class Method Summary collapse
- .load_bundler ⇒ Object
- .load_rails ⇒ Object
- .my_require(abs_path, numerator, denominator) ⇒ Object
- .patch_kernel ⇒ Object
- .require_all_files ⇒ Object
-
.require_everything ⇒ Object
Goes through the most common ways to require all your userland code.
Class Method Details
.load_bundler ⇒ Object
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_rails ⇒ Object
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
78 79 80 81 82 |
# File 'lib/require_everything.rb', line 78 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_kernel ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/require_everything.rb', line 84 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_files ⇒ Object
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 |
# 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 = Dir.glob("#{Dir.pwd}/**/*.rb") 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) next if /^# +typed: +ignore$/.match(File.read(abs_path).scrub) 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_everything ⇒ Object
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 |