class Unbundler::CLI

Public Class Methods

start() click to toggle source
# File lib/unbundler/cli.rb, line 7
def self.start
  cli = CLI.new
  cli.run(cli.parse_args)
end

Public Instance Methods

parse_args() click to toggle source
# File lib/unbundler/cli.rb, line 20
def parse_args
  opts = { :action => :unbundle }
  case ARGV.first
  when "show", "list"
    opts[:action] = :show
  when "edit_keep_list"
    opts[:action] = :edit_keep_list
  end
  opts.merge( Trollop::options do
                version "unbundler v#{Unbundler::VERSION}"
                opt :keep, "List of gems to keep", :type => :strings
                opt :plain, "Plain output for unbundle list"
                opt :interactive, "Interactive unbundle(ask about each gem)"
                opt :quiet, "Produce no output"
              end)
end
run(opts) click to toggle source
# File lib/unbundler/cli.rb, line 12
def run(opts)
  @interactive = opts[:interactive]
  @plain = opts[:plain]
  @quiet = opts[:quiet]
  @gems = GemList.new(opts[:keep])
  self.send(opts[:action])
end

Private Instance Methods

edit_keep_list() click to toggle source
# File lib/unbundler/cli.rb, line 52
def edit_keep_list
  exec "${FCEDIT:-${VISUAL:-${EDITOR:-vi}}} ~/.unbundler_keep"
end
get_char() click to toggle source

stackoverflow.com/a/14527475/162459

# File lib/unbundler/cli.rb, line 57
def get_char
  state = `stty -g`
  `stty raw -echo -icanon isig`
  STDIN.getc.chr
ensure
  `stty #{state}`
end
keep(gem) click to toggle source
# File lib/unbundler/cli.rb, line 65
def keep(gem)
  return false unless @interactive
  while true do
    puts "Delete #{gem} (y/n/a)"
    case get_char
    when 'y', 'Y'
      return false
    when 'n', 'N'
      return true
    when 'a', 'A'
      @interactive = false
      return false
    else
      puts "y/n/a expected"
    end
  end
end
show() click to toggle source
# File lib/unbundler/cli.rb, line 45
def show
  puts "Gems to be unbundled:" unless @plain
  @gems.each do |gem|
    puts @plain ? gem : "  * #{gem}"
  end
end
unbundle() click to toggle source
# File lib/unbundler/cli.rb, line 39
def unbundle
  @gems.each do |gem|
    uninstall_gem(gem) unless keep(gem)
  end
end
uninstall_gem(gem) click to toggle source
# File lib/unbundler/cli.rb, line 83
def uninstall_gem(gem)
  `gem uninstall -x #{gem}`
end