Module: Kernel

Defined in:
lib/amp/dependencies/zip/ziprequire.rb,
lib/amp/commands/hooks.rb,
lib/amp/support/ruby_19_compatibility.rb,
lib/amp/commands/commands/workflows/hg/bisect.rb,
lib/amp/support/support.rb

Overview

Now for some helpers!

Instance Method Summary collapse

Instance Method Details

#abort(str) ⇒ Object



261
262
263
# File 'lib/amp/support/support.rb', line 261

def abort(str)
  AbortError.new str
end

#already_loaded?(moduleName) ⇒ Boolean

Returns:



78
79
80
81
# File 'lib/amp/dependencies/zip/ziprequire.rb', line 78

def already_loaded?(moduleName)
  moduleRE = Regexp.new("^"+moduleName+"(\.rb|\.so|\.dll|\.o)?$")
  $".detect { |e| e =~ moduleRE } != nil
end

#amp_pauseObject



255
256
257
258
259
# File 'lib/amp/support/support.rb', line 255

def amp_pause
  Amp::UI.say "Amp is paused. Type 'go' and hit enter to continue"
  until gets.strip == 'go'
  end
end

#bisect_command(name, opts = {}) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
# File 'lib/amp/commands/commands/workflows/hg/bisect.rb', line 242

def bisect_command(name, opts={})
  command name.to_sym do |c|
    
    # set the default options as passed in
    opts.each do |k, v|
      c.default k, v
    end
    
    yield self if block_given?
  end
end

#ensure_rb_extension(aString) ⇒ Object



83
84
85
# File 'lib/amp/dependencies/zip/ziprequire.rb', line 83

def ensure_rb_extension(aString)
  aString.sub(/(\.rb)?$/i, ".rb")
end

#full_backtrace_please { ... } ⇒ Object

The built-in Ruby 1.8.x implementation will only show a certain number of context lines at the start and end of its backtrace when an exception is raised. All other levels of the stack will be labeled “… 15 levels …” Sadly, sometimes some important information is in those 15 levels, and without patching the interpreter, there’s no way to just disable that abbreviation.

So, we simply catch all exceptions, print their full backtrace, and then exit!

Yields:

  • The block is run, and any exceptions raised print their full backtrace.



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/amp/support/support.rb', line 237

def full_backtrace_please
  begin
    yield
  rescue AbortError => e
    Amp::UI.say "Operation aborted."
    raise
  rescue StandardError => e
    message = ["***** Left engine failure *****",
               "***** Ejection system error *****",
               "***** Vaccuum in booster engine *****"
              ][rand(3)]
    Amp::UI.say message
    Amp::UI.say e.to_s
    e.backtrace.each {|err| Amp::UI.say "\tfrom #{err}" }
    return
  end
end

#get_resource(resourceName, &aProc) ⇒ Object



73
74
75
76
# File 'lib/amp/dependencies/zip/ziprequire.rb', line 73

def get_resource(resourceName, &aProc)
  zl = ZipList.new($:.grep(/\.zip$/))
  zl.get_input_stream(resourceName, &aProc)
end

#hook(names) { ... } ⇒ Object

Adds a hook to each of the provided hook entry points. Requires a block.

Parameters:

  • names (Symbol)

    the hook entry points for which to add the block as a hook

Yields:

  • The block provided is the code that will be run as a hook at a later time.



78
79
80
# File 'lib/amp/commands/hooks.rb', line 78

def hook(names, &block)
  Amp::Hook.new(names, &block)
end

#ignore_missing_files { ... } ⇒ Object

Allows any code called within the block to access non-existent files without raising an exception. Only “file not found” exceptions are ignored - all other exceptions will be raised as normal.

Yields:

  • The block is run with all missing-file exceptions caught and ignored.



218
219
220
221
222
223
224
225
# File 'lib/amp/support/support.rb', line 218

def ignore_missing_files
  begin
    yield
  rescue Errno::ENOENT
  rescue StandardError
    raise
  end
end

#oldRequireObject



56
# File 'lib/amp/dependencies/zip/ziprequire.rb', line 56

alias :oldRequire :require

#require(moduleName) ⇒ Object



58
59
60
# File 'lib/amp/dependencies/zip/ziprequire.rb', line 58

def require(moduleName)
  zip_require(moduleName) || oldRequire(moduleName)
end

#ruby_19?Boolean

Returns:



2
# File 'lib/amp/support/ruby_19_compatibility.rb', line 2

def ruby_19?; (RUBY_VERSION >= "1.9"); end

#zip_require(moduleName) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/amp/dependencies/zip/ziprequire.rb', line 62

def zip_require(moduleName)
  return false if already_loaded?(moduleName)
  get_resource(ensure_rb_extension(moduleName)) { 
    |zis| 
    eval(zis.read); $" << moduleName 
  }
  return true
rescue Errno::ENOENT => ex
  return false
end