Module: Kernel

Defined in:
lib/require-hooks/mode/kernel_patch.rb

Overview

Patch Kernel to hijack require/require_relative/load

Class Method Summary collapse

Class Method Details

.load(path, wrap = false) ⇒ Object



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/require-hooks/mode/kernel_patch.rb', line 277

def load(path, wrap = false)
  if wrap
    warn "RequireHooks does not support `load(smth, wrap: ...)`. Falling back to original `Kernel#load`" if RequireHooks.print_warnings
    return load_without_require_hooks(path, wrap)
  end

  path = path.to_path if path.respond_to?(:to_path)
  raise TypeError unless path.respond_to?(:to_str)

  path = path.to_str

  raise TypeError unless path.is_a?(::String)

  realpath =
    if path =~ /^\.\.?\//
      path
    else
      RequireHooks::KernelPatch::Features.feature_path(path, implitic_ext: false)
    end

  return load_without_require_hooks(path, wrap) unless realpath

  RequireHooks::KernelPatch.load(realpath)
rescue Errno::ENOENT, Errno::EACCES
  raise LoadError, "cannot load such file -- #{path}"
rescue LoadError => e
  warn "RuquireHooks failed to load '#{path}': #{e.message}" if RequireHooks.print_warnings
  load_without_require_hooks(path)
end

.load_without_require_hooksObject



276
# File 'lib/require-hooks/mode/kernel_patch.rb', line 276

alias_method :load_without_require_hooks, :load

.require(path) ⇒ Object

Raises:

  • (TypeError)


203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/require-hooks/mode/kernel_patch.rb', line 203

def require(path)
  path = path.to_path if path.respond_to?(:to_path)
  raise TypeError unless path.respond_to?(:to_str)

  path = path.to_str

  raise TypeError unless path.is_a?(::String)

  realpath = nil
  feature = path

  # if extname == ".rb" => lookup feature -> resolve feature -> load
  # if extname != ".rb" => append ".rb" - lookup feature -> resolve feature -> lookup orig (no ext) -> resolve orig (no ext) -> load
  if File.extname(path) != ".rb"
    realpath = RequireHooks::KernelPatch::Features.feature_path(path + ".rb")

    if realpath
      feature = path + ".rb"
    end
  end

  realpath ||= RequireHooks::KernelPatch::Features.feature_path(path)

  return require_without_require_hooks(path) unless realpath

  ctx = RequireHooks.context_for(realpath)

  return require_without_require_hooks(path) if ctx.empty?

  return false if $LOADED_FEATURES.include?(realpath)

  begin
    RequireHooks::KernelPatch::Features::LOCK.lock_feature(feature) do |loaded|
      return false if loaded

      $LOADED_FEATURES << realpath
      RequireHooks::KernelPatch.load(realpath, ctx: ctx)
      true
    end
  rescue LoadError => e
    $LOADED_FEATURES.delete(realpath) if realpath
    warn "RequireHooks failed to require '#{path}' from '#{realpath}': #{e.message}" if RequireHooks.print_warnings
    require_without_require_hooks(path)
  rescue Errno::ENOENT, Errno::EACCES
    raise LoadError, "cannot load such file -- #{path}"
  rescue
    $LOADED_FEATURES.delete(realpath) if realpath
    raise
  end
end

.require_relative(path) ⇒ Object

Raises:

  • (TypeError)


255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/require-hooks/mode/kernel_patch.rb', line 255

def require_relative(path)
  path = path.to_path if path.respond_to?(:to_path)
  raise TypeError unless path.respond_to?(:to_str)
  path = path.to_str

  raise TypeError unless path.is_a?(::String)

  return require(path) if Pathname.new(path).absolute?

  loc = caller_locations(1..1).first
  from = loc.absolute_path || loc.path || File.join(Dir.pwd, "main")
  realpath = File.absolute_path(
    File.join(
      File.dirname(File.absolute_path(from)),
      path
    )
  )

  require(realpath)
end

.require_relative_without_require_hooksObject



254
# File 'lib/require-hooks/mode/kernel_patch.rb', line 254

alias_method :require_relative_without_require_hooks, :require_relative

.require_without_require_hooksObject



201
# File 'lib/require-hooks/mode/kernel_patch.rb', line 201

alias_method :require_without_require_hooks, :require