module Irbrc

Constants

BASE_DIR
VERSION

Public Class Methods

agree(msg, opts = {}) click to toggle source
# File lib/irbrc.rb, line 180
def agree msg, opts = {}
  # ask yes or no question and return true/false
  # optional 'default' arg

  default = if opts.has_key? :default
    opts[:default] ? 'y' : 'n'
  else
    ''
  end

  loop do
    puts "#{msg.chomp '?'}?  %s" % '[y/n]'.sub(default, default.upcase)
    res = gets.strip.downcase
    res = default if res.empty?
    if ['y', 'yes', 'n', 'no'].include? res
      return ['y', 'yes'].include? res
    else
      puts "\ninvalid response\n\n"
    end
  end
end
create_rc() click to toggle source
# File lib/irbrc.rb, line 54
def create_rc
  path = remote_rc || local_rc

  if File.exists? path
    raise Exception.new "rc file already exists: #{path}"
  end

  if remote_rc
    FileUtils.mkpath File.dirname remote_rc
  end

  msg = if repo = parse_repo
    "# IRBRC for #{parse_repo[:source]}:#{repo[:repo]}\n"
  else
    "# IRBRC"
  end

  File.open(path, 'w') do |fh|
    fh.write "#{msg}\n\n"
  end
end
git_cmd(cmd) click to toggle source
# File lib/irbrc.rb, line 220
def git_cmd cmd
  res = `git #{cmd} 2>/dev/null`.chomp
  res.empty? ? nil : res
end
git_env?() click to toggle source
# File lib/irbrc.rb, line 215
def git_env?
  !! git_cmd('status --short')
end
git_ignore() click to toggle source

Add rc file to git ignore.

# File lib/irbrc.rb, line 100
def git_ignore
  ignore_path = [
    project_root,
    '.git',
    'info',
    'exclude'
  ].join File::SEPARATOR

  add_ignore = if File.exists? ignore_path
    msg = "Add .irbrc to #{ignore_path}"
    File.read(ignore_path) !~ /\W\.irbrc\W/ and agree(msg, default: true)
  end

  if add_ignore
    File.open(ignore_path, 'a') do |fh|
      fh.write "\n.irbrc\n"
    end
  end
end
global_rc() click to toggle source
# File lib/irbrc.rb, line 170
def global_rc
  [ Dir.home, '.irbrc' ].join File::SEPARATOR
end
init() click to toggle source

set up or fix this project's rc file and symlink

# File lib/irbrc.rb, line 26
def init
  if File.symlink? local_rc
    if ! File.exists? local_rc
      # clean up bad symlink
      unlink local_rc
    end
  elsif File.exists? local_rc
    if remote_rc and agree("Move local rc: mv #{local_rc} #{remote_rc}")
      FileUtils.mkpath File.dirname remote_rc
      File.rename local_rc, remote_rc
    end
  elsif ! realpath remote_rc and agree('Create irbrc', default: true)
    # create new rc file
    create_rc
  end

  if ! File.exists? local_rc and realpath remote_rc
    # symlink remote rc
    File.symlink remote_rc, local_rc
  end

  init_global_rc
  git_ignore if git_env?

  nil
end
init_global_rc() click to toggle source

Ensure ~/.irbrc loads Irbrc upon irb start.

# File lib/irbrc.rb, line 78
def init_global_rc
  require_cmd = "require 'irbrc'"

  add_required = if File.exists? global_rc
    add_msg = "Add `#{require_cmd}` to #{global_rc}"
    File.read(global_rc) !~ /\W#{require_cmd}\W/ and agree(add_msg)
  else
    true
  end

  if add_required
    File.open(global_rc, 'a') do |fh|
      fh.write "\n"
      fh.write "# load per project .irbrc\n"
      fh.write "#{require_cmd}\n"
      fh.write "load_rc\n\n"
    end
  end
end
load_rc() click to toggle source
# File lib/irbrc.rb, line 15
def load_rc
  if File.exists? local_rc
    # avoid circular reload
    if local_rc != global_rc
      load local_rc
    end
  end
end
local_rc() click to toggle source
# File lib/irbrc.rb, line 162
def local_rc
  [
    project_root || Dir.pwd,
    '.irbrc'
  ].join File::SEPARATOR
end
localize() click to toggle source

use local rc file instead of symlink

# File lib/irbrc.rb, line 235
def localize
  if File.symlink? local_rc
    unlink local_rc
  end

  if File.exists? local_rc
    unlink local_rc if agree "Overwrite local rc: #{local_rc}"
  end

  File.rename remote_rc, local_rc unless File.exists? local_rc
end
parse_repo(str = nil) click to toggle source
# File lib/irbrc.rb, line 136
def parse_repo str = nil
  str = git_cmd "remote -v" unless str
  return unless str

  repos = str.split("\n").map(&:split).map do |line|
    next unless line.first == "origin"
    next unless line.last == "(fetch)"

    source, repo = line[1].split ':'
    source.sub!(/^.*@/, '')
    source.sub!(/\.(com|org)$/, '')

    {
      source: source,
      repo: repo,
    }
  end.compact.uniq

  if repos.count != 1
    raise Exception.new "parse error: #{str}"
  end

  repos.first
end
project_root() click to toggle source
# File lib/irbrc.rb, line 175
def project_root
   git_cmd("rev-parse --show-toplevel")
end
realpath(path) click to toggle source
# File lib/irbrc.rb, line 210
def realpath path
  File.realpath path if path and File.exists? path
end
remote_rc() click to toggle source
# File lib/irbrc.rb, line 121
def remote_rc
  repo = parse_repo

  if repo
    [
      BASE_DIR,
      repo[:source],
      repo[:repo].gsub(/#{File::SEPARATOR}/, '.') + '.rb',
    ].join File::SEPARATOR
  else
    nil
  end
end
remove() click to toggle source

remove rc file

# File lib/irbrc.rb, line 227
def remove
  if agree "remove rc file"
    unlink local_rc, remote_rc
  end
end