class IRuby::Command

Public Class Methods

new(args) click to toggle source
# File lib/iruby/command.rb, line 6
def initialize(args)
  @args = args

  ipython_dir = ENV['IPYTHONDIR'] || '~/.ipython'
  @args.each do |arg|
    ipython_dir = $1 if arg =~ /\A--ipython-dir=(.*)\Z/
  end
  @kernel_dir = File.join(File.expand_path(ipython_dir), 'kernels', 'ruby')
  @kernel_file = File.join(@kernel_dir, 'kernel.json')
  @iruby_path = File.expand_path $0
end

Public Instance Methods

run() click to toggle source
# File lib/iruby/command.rb, line 18
def run
  case @args.first
  when 'version', '-v', '--version'
    require 'iruby/version'
    puts "IRuby #{IRuby::VERSION}, Ruby #{RUBY_VERSION}"
  when 'help', '-h', '--help'
    print_help
  when 'register'
    if registered_iruby_path && !@args.include?('--force')
      STDERR.puts "#{@kernel_file} already exists!\nUse --force to force a register."
      exit 1
    end
    register_kernel
  when 'unregister'
    unregister_kernel
  when 'kernel'
    run_kernel
  else
    run_ipython
  end
end

Private Instance Methods

check_registered_kernel() click to toggle source
# File lib/iruby/command.rb, line 95
    def check_registered_kernel
      if kernel = registered_iruby_path
        STDERR.puts "#{@iruby_path} differs from registered path #{registered_iruby_path}.
This might not work. Run 'iruby register --force' to fix it." if @iruby_path != kernel
      else
        register_kernel
      end
    end
check_version() click to toggle source
# File lib/iruby/command.rb, line 75
def check_version
  required = '3.0.0'
  version = `ipython --version`.chomp
  if version < required
    STDERR.puts "Your IPython version #{version} is too old, at least #{required} is required"
    exit 1
  end
end
print_help() click to toggle source
register_kernel() click to toggle source
# File lib/iruby/command.rb, line 104
def register_kernel
  FileUtils.mkpath(@kernel_dir)
  unless RUBY_PLATFORM =~ /mswin(?!ce)|mingw|cygwin/
    File.write(@kernel_file, MultiJson.dump(argv: [ @iruby_path, 'kernel', '{connection_file}' ],
                                          display_name: "Ruby #{RUBY_VERSION}", language: 'ruby'))
  else
    ruby_path, iruby_path = [RbConfig.ruby, @iruby_path].map{|path| path.gsub('/', '\\\\')}
    File.write(@kernel_file, MultiJson.dump(argv: [ ruby_path, iruby_path, 'kernel', '{connection_file}' ],
                                            display_name: "Ruby #{RUBY_VERSION}", language: 'ruby'))
  end

  FileUtils.copy(Dir[File.join(__dir__, 'assets', '*')], @kernel_dir) rescue nil
end
registered_iruby_path() click to toggle source
# File lib/iruby/command.rb, line 118
def registered_iruby_path
  File.exist?(@kernel_file) && MultiJson.load(File.read(@kernel_file))['argv'].first
end
run_ipython() click to toggle source
# File lib/iruby/command.rb, line 84
def run_ipython
  # If no command is given, we use the console to launch the whole 0MQ-client-server stack
  @args = %w(console) + @args if @args.first.to_s !~ /\A\w/
  @args += %w(--kernel ruby) if %w(console qtconsole).include? @args.first

  check_version
  check_registered_kernel

  Kernel.exec('ipython', *@args)
end
run_kernel() click to toggle source
# File lib/iruby/command.rb, line 56
def run_kernel
  require 'iruby/logger'
  IRuby.logger = MultiLogger.new(*Logger.new(STDOUT))
  @args.reject! {|arg| arg =~ /\A--log=(.*)\Z/ && IRuby.logger.loggers << Logger.new($1) }
  IRuby.logger.level = @args.delete('--debug') ? Logger::DEBUG : Logger::INFO

  raise(ArgumentError, 'Not enough arguments to the kernel') if @args.size < 2 || @args.size > 4
  config_file, boot_file, working_dir = @args[1..-1]
  Dir.chdir(working_dir) if working_dir

  require boot_file if boot_file

  require 'iruby'
  Kernel.new(config_file).run
rescue Exception => e
  IRuby.logger.fatal "Kernel died: #{e.message}\n#{e.backtrace.join("\n")}"
  raise
end
unregister_kernel() click to toggle source
# File lib/iruby/command.rb, line 122
def unregister_kernel
  FileUtils.rm_rf(@kernel_dir)
end