Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output when require happens, not after #4

Open
janpio opened this issue Sep 27, 2018 · 2 comments
Open

Output when require happens, not after #4

janpio opened this issue Sep 27, 2018 · 2 comments

Comments

@janpio
Copy link

janpio commented Sep 27, 2018

While looking into #2 I noticed that (of course) the callback for a require is only fired after all the requires inside are taken care of.

Is there a way to execute something when the require starts, so it is easier to understand in what order things are required?

@janpio
Copy link
Author

janpio commented Sep 27, 2018

After looking at the code, it seems this would require some changes:

def require(feature, options=nil)
result = require_without_callback(feature)
if result
Kernel.required(feature)
Kernel.backloaded(feature, :require=>true, :load=>false)
end
result
end

Before result = ... we could add something like this:

  Kernel.will_require(feature) 
  Kernel.will_backload(feature, :require=>true, :load=>false) # or similar

... and add those "stubs" as well so they can easily be implemented.
Correct?

@janpio janpio changed the title Output when Output when require happens, not after Sep 27, 2018
@janpio
Copy link
Author

janpio commented Sep 27, 2018

Well, here we go with a super crappy overload implementation of that:

require 'backload'
require 'backload/require_relative'

module Kernel
  def self.will_require(path)
  end
private
  #
  # Alias backload's Kernel#require method.
  #
  alias_method :backload_require, :require
  #
  # Redefine Kernel#require with will_.
  #
  def require(feature, options=nil)
    Kernel.will_require(feature)
	backload_require(feature)
  end
  class << self
    #
    # Alias original Kernel.require method.
    #
    alias_method :backload_require, :require
    #
    # Redefine Kernel.require with callback.
    #
    def require(feature)
	  Kernel.will_require(feature)
      backload_require(feature)
    end
  end
end

module Kernel
  def self.will_require_relative(path)
  end
private
  #
  # Alias backload's Kernel#require_relative method.
  #
  alias_method :backload_require_relative, :require_relative
  #
  # Redefine Kernel#require_relative with will_.
  #
  def require_relative(feature)
    Kernel.will_require_relative(feature)
	#backload_require_relative feature
	## We have to reimplement #require_relative instead of calling
    ## the alias to ensure the proper path is used. (*sad*)
    result = (
      c = caller.first
      fail "Can't parse #{c}" unless c.rindex(/:\d+(:in `.*')?$/)
      file = $` # File.dirname(c)
      if /\A\((.*)\)/ =~ file # eval, etc.
        raise LoadError, "require_relative is called in #{$1}"
      end
      #options[:relative] = File.dirname(file)
      absolute = File.expand_path(feature, File.dirname(file))
      backload_require_relative(absolute)
    )
  end

  class << self
    #
    # Alias original Kernel.require_relative method.
    #
    alias_method :backload_require_relative, :require_relative
    #
    # Redefine Kernel.require_relative with will_.
    #
    def require_relative(feature)
	  Kernel.will_require_relative(feature)
	  #backload_require_relative feature
	## We have to reimplement #require_relative instead of calling
    ## the alias to ensure the proper path is used. (*sad*)
    result = (
      c = caller.first
      fail "Can't parse #{c}" unless c.rindex(/:\d+(:in `.*')?$/)
      file = $` # File.dirname(c)
      if /\A\((.*)\)/ =~ file # eval, etc.
        raise LoadError, "require_relative is called in #{$1}"
      end
      #options[:relative] = File.dirname(file)
      absolute = File.expand_path(feature, File.dirname(file))
      backload_require_relative(absolute)
    )
	end
  end
end

def Kernel.will_require(feature)
  puts "will require '#{feature}'"
end

def Kernel.will_require_relative(feature)
  puts "will require_relative '#{feature}'"
end

Could really easily be turned into a feature of this, but we should probably talk about the naming then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant