module Nydp

with thanks to @liwp ( blog.lauripesonen.com/ ) at stackoverflow.com/questions/2065923/irb-history-not-working

Constants

COMMENT_RX
GENERATED_CLASS_PREFIX
GENERATED_CLASS_PREFIX_REGEXP
NIL
PLUGINS
T
VERSION

Attributes

logger[RW]

Public Class Methods

all_files() click to toggle source
# File lib/nydp/plugin.rb, line 65
def self.all_files ; PLUGINS.each_with_object([]) { |plg, list|  plg.loadfiles.each { |f| list << f } } ; end
apply_function(ns, name, *args ;) click to toggle source
# File lib/nydp.rb, line 11
def self.apply_function      ns, name, *args ; ns.apply name, *args                                 ; end
base_gen_path() click to toggle source
# File lib/nydp/plugin.rb, line 49
def self.base_gen_path   ; File.expand_path("rubycode/")       ; end
build_nydp(&block) click to toggle source
# File lib/nydp/plugin.rb, line 67
def self.build_nydp &block
  rc = base_gen_path
  $LOAD_PATH.unshift rc unless $LOAD_PATH.include?(rc)

  digest   = Digest::SHA256.hexdigest(all_files.map { |f| f.read }.join("\n"))
  mname    = "Manifest_#{digest}"

  ns = ::Nydp::Namespace.new
  setup(ns)

  digest = ""

  PLUGINS.each { |plugin|
    digest = install_plugin ns, plugin, digest, &block
  }

  ns
end
enhance_backtrace(bt) click to toggle source
# File lib/nydp/plugin.rb, line 45
def self.enhance_backtrace bt
  bt.map { |s| nydp_from_backtrace s }
end
eval_src(ns, src_txt, name=nil ;) click to toggle source
# File lib/nydp.rb, line 13
def self.eval_src      ns, src_txt, name=nil ; eval_with Nydp::Runner, ns, src_txt, name            ; end
eval_with(runner, ns, src_txt, name ;) click to toggle source
# File lib/nydp.rb, line 14
def self.eval_with runner, ns, src_txt, name ; runner.new(ns, reader(name, src_txt), nil, name).run ; end
handle_run_error(e, indent="") click to toggle source
# File lib/nydp.rb, line 23
def self.handle_run_error e, indent=""
  puts "#{indent}#{e.class.name}"
  puts "#{indent_message indent, e.message}"
  if e.cause
    puts "#{indent}#{e.backtrace.first}"
    puts "\n#{indent}Caused by:"
    handle_run_error e.cause, "#{indent}    "
  else
    Nydp.enhance_backtrace(e.backtrace).each do |b|
      puts "#{indent}#{b}"
    end
  end
end
indent_message(indent, msg) click to toggle source
# File lib/nydp.rb, line 19
def self.indent_message indent, msg
  msg.split(/\n/).map { |line| "#{indent}#{line}" }.join("\n")
end
indent_text(txt) click to toggle source
# File lib/nydp/error.rb, line 2
def self.indent_text txt
  txt.split(/\n/).map { |line| "  #{line}"}.join("\n")
end
install_plugin(ns, plugin, digest, &block) click to toggle source
# File lib/nydp/plugin.rb, line 86
def self.install_plugin ns, plugin, digest, &block
  f0       = plugin.loadfiles.map { |f| f.read }.join("\n")
  f1       = plugin.testfiles.map { |f| f.read }.join("\n")
  digest   = Digest::SHA256.hexdigest([digest, f0, f1].join("\n"))
  mname    = "Manifest_#{digest}"

  if Nydp.logger
    Nydp.logger.info "manifest name for plugin #{plugin.name.inspect} is #{mname.inspect}"
  end

  begin
    require mname
    const_get(mname).build ns

  rescue LoadError => e
    manifest = []
    loadall ns, plugin, plugin.loadfiles, manifest, &block
    loadall ns, plugin, plugin.testfiles, manifest, &block
    Nydp::Evaluator.mk_manifest "Manifest_#{digest}", manifest
  end

  digest
end
load_rake_tasks() click to toggle source
# File lib/nydp/plugin.rb, line 51
def self.load_rake_tasks ; PLUGINS.each &:load_rake_tasks      ; end
loadall(ns, plugin, files, manifest) { |name| ... } click to toggle source
# File lib/nydp/plugin.rb, line 55
def self.loadall ns, plugin, files, manifest
  ns.apply :"script-run", :"plugin-start", plugin.name if plugin
  files.each { |f|
    Nydp::Runner.new(ns, f, nil, f.name, manifest).run
    yield f.name if block_given?
  }
ensure
  ns.apply :"script-run", :"plugin-end", plugin.name if plugin
end
ms(t1, t0 ;) click to toggle source
# File lib/nydp.rb, line 15
def self.ms                           t1, t0 ; ((t1 - t0) * 1000).to_i                              ; end
new_parser() click to toggle source
# File lib/nydp.rb, line 17
def self.new_parser                          ; Nydp::Parser.new                                     ; end
new_tokeniser(reader ;) click to toggle source
# File lib/nydp.rb, line 16
def self.new_tokeniser                reader ; Nydp::Tokeniser.new reader                           ; end
nydp_from_backtrace(str) click to toggle source
# File lib/nydp/plugin.rb, line 21
def self.nydp_from_backtrace str
  file, original_line, meth = str.split(/:/)
  line = original_line.to_i - 2 # -1 to convert from 1-based index to zero-based index ; -1 to start looking backwards from previous line
  filepath = File.expand_path file

  if filepath =~ Nydp::GENERATED_CLASS_PREFIX_REGEXP
    code = File.read filepath
    lines = code.split /\n/

    while line > 0 && !(lines[line] =~ COMMENT_RX)
      line = line - 1
    end

    if (line >= 0 && (lines[line] =~ COMMENT_RX))
      comment = lines[line].sub(COMMENT_RX, '').gsub(/\\n/, "\n")
      return [(filepath.sub(base_gen_path + '/', '') + ":" + original_line), comment].join("\n")
    else
      return str
    end
  end

  str
end
plug_in(plugin ;) click to toggle source
# File lib/nydp/plugin.rb, line 50
def self.plug_in  plugin ; PLUGINS << plugin                   ; end
plugin_names() click to toggle source
# File lib/nydp/plugin.rb, line 53
def self.plugin_names    ; PLUGINS.map(&:name)                 ; end
reader(name, txt ;) click to toggle source
# File lib/nydp.rb, line 12
def self.reader                    name, txt ; Nydp::StringReader.new name, txt                     ; end
repl(options={ }) click to toggle source
# File lib/nydp.rb, line 45
def self.repl options={ }
  launch_time      = Time.now
  silent           = options.delete :silent
  ns               = options.delete :ns
  last_script_time = Time.now
  puts "welcome to nydp #{options.inspect}" unless silent
  reader = Nydp::ReadlineReader.new $stdin, "nydp > "
  ns   ||= build_nydp do |script|
    this_script_time = Time.now
    puts "script #{script} time #{ms this_script_time, last_script_time}ms" if options[:verbose]
    last_script_time = this_script_time
  end
  load_time = Time.now
  puts "nydp v#{Nydp::VERSION} repl ready in #{ms(load_time, launch_time)}ms" unless silent
  puts "^D to exit" unless silent
  while !options[:exit]
    toplevel do
      Nydp::Runner.new(ns, reader, $stdout, "<stdin>").run
      options[:exit] = true
    end
  end
  # Nydp::Invocation.whazzup
end
setup(ns ;) click to toggle source
# File lib/nydp/plugin.rb, line 52
def self.setup        ns ; PLUGINS.each { |plg| plg.setup ns } ; end
tests(*options) click to toggle source
# File lib/nydp.rb, line 69
def self.tests *options
  toplevel do
    verbose = options.include?(:verbose) ? true : nil
    puts "welcome to nydp : running tests"
    build_nydp.apply :"run-all-tests", verbose
  end
end
toplevel() { || ... } click to toggle source
# File lib/nydp.rb, line 37
def self.toplevel
  begin
    yield
  rescue StandardError => e
    handle_run_error e
  end
end