class Bundler::CLI

def self.printable_tasks

def self.printable_tasks
  tasks = super.dup
  nodoc = /^bundle (cache)/
  tasks.reject!{|t| t.first =~ nodoc }
  tasks
end

def cache

def cache
  Bundler.load.cache
  Bundler.load.prune_cache unless options[:no_prune]
rescue GemNotFound => e
  Bundler.ui.error(e.message)
  Bundler.ui.warn "Run `bundle install` to install missing gems."
  exit 128
end

def check

def check
  not_installed = Bundler.definition.missing_specs
  if not_installed.any?
    Bundler.ui.error "The following gems are missing"
    not_installed.each { |s| Bundler.ui.error " * #{s.name} (#{s.version})" }
    Bundler.ui.warn "Install missing gems with `bundle install`"
    exit 1
  else
    Bundler.ui.info "The Gemfile's dependencies are satisfied"
  end
end

def console(group = nil)

def console(group = nil)
  require 'bundler/setup'
  group ? Bundler.require(:default, group) : Bundler.require
  ARGV.clear
  require 'irb'
  IRB.start
end

def exec(*)

def exec(*)
  ARGV.delete("exec")
  # Set PATH
  paths = (ENV["PATH"] || "").split(File::PATH_SEPARATOR)
  paths.unshift "#{Bundler.bundle_path}/bin"
  ENV["PATH"] = paths.uniq.join(File::PATH_SEPARATOR)
  # Set BUNDLE_GEMFILE
  ENV["BUNDLE_GEMFILE"] = Bundler::SharedHelpers.default_gemfile.to_s
  # Set RUBYOPT
  rubyopt = [ENV["RUBYOPT"]].compact
  if rubyopt.empty? || rubyopt.first !~ /-rbundler\/setup/
    rubyopt.unshift "-rbundler/setup"
    rubyopt.unshift "-I#{File.expand_path('../..', __FILE__)}"
    ENV["RUBYOPT"] = rubyopt.join(' ')
  end
  begin
    # Run
    Kernel.exec(*ARGV)
  rescue Errno::EACCES
    Bundler.ui.error "bundler: not executable: #{ARGV.first}"
  rescue Errno::ENOENT
    Bundler.ui.error "bundler: command not found: #{ARGV.first}"
    Bundler.ui.warn  "Install missing gem binaries with `bundle install`"
  end
end

def init

def init
  opts = options.dup
  if File.exist?("Gemfile")
    Bundler.ui.error "Gemfile already exists at #{Dir.pwd}/Gemfile"
    exit 1
  end
  if opts[:gemspec]
    gemspec = File.expand_path(opts[:gemspec])
    unless File.exist?(gemspec)
      Bundler.ui.error "Gem specification #{gemspec} doesn't exist"
      exit 1
    end
    spec = Gem::Specification.load(gemspec)
    puts "Writing new Gemfile to #{Dir.pwd}/Gemfile"
    File.open('Gemfile', 'w') do |file|
      file << "# Generated from #{gemspec}\n"
      file << spec.to_gemfile
    end
  else
    puts "Writing new Gemfile to #{Dir.pwd}/Gemfile"
    FileUtils.cp(File.expand_path('../templates/Gemfile', __FILE__), 'Gemfile')
  end
end

def initialize(*)

def initialize(*)
  super
  use_shell = options["no-color"] ? Thor::Shell::Basic.new : shell
  Bundler.ui = UI::Shell.new(use_shell)
  Gem::DefaultUserInteraction.ui = UI::RGProxy.new(Bundler.ui)
end

def install(path = nil)

def install(path = nil)
  opts = options.dup
  opts[:without] ||= []
  opts[:without].map! { |g| g.to_sym }
  # Can't use Bundler.settings for this because settings needs gemfile.dirname
  ENV['BUNDLE_GEMFILE'] = opts[:gemfile] if opts[:gemfile]
  Bundler.settings[:path] = path if path
  Bundler.settings[:bin] = 'bin' if opts[:binstubs]
  Bundler.settings[:disable_shared_gems] = '1' if options["disable-shared-gems"] || path
  Bundler.settings.without = opts[:without]
  Bundler.ui.be_quiet! if opts[:quiet]
  Installer.install(Bundler.root, Bundler.definition, opts)
  cache if Bundler.root.join("vendor/cache").exist?
  Bundler.ui.confirm "Your bundle is complete! " +
    "Use `bundle show [gemname]` to see where a bundled gem is installed."
rescue GemNotFound => e
  if Bundler.definition.no_sources?
    Bundler.ui.warn "Your Gemfile doesn't have any sources. You can add one with a line like 'source :gemcutter'"
  end
  raise e
end

def locate_gem(name)

def locate_gem(name)
  spec = Bundler.load.specs.find{|s| s.name == name }
  raise GemNotFound, "Could not find gem '#{name}' in the current bundle." unless spec
  spec.full_gem_path
end

def lock

def lock
  Bundler.ui.warn "Lock is deprecated. Your bundle is now locked whenever you run `bundle install`."
end

def open(name)

def open(name)
  editor = [ENV['BUNDLER_EDITOR'], ENV['VISUAL'], ENV['EDITOR']].find{|e| !e.nil? && !e.empty? }
  if editor
    command = "#{editor} #{locate_gem(name)}"
    success = system(command)
    Bundler.ui.info "Could not run '#{command}'" unless success
  else
    Bundler.ui.info("To open a bundled gem, set $EDITOR or $BUNDLER_EDITOR")
  end
end

def package

def package
  install
  # TODO: move cache contents here now that all bundles are locked
  cache
end

def show(gem_name = nil)

def show(gem_name = nil)
  if gem_name
    Bundler.ui.info locate_gem(gem_name)
  else
    Bundler.ui.info "Gems included by the bundle:"
    Bundler.load.specs.sort_by { |s| s.name }.each do |s|
      Bundler.ui.info "  * #{s.name} (#{s.version}#{s.git_version})"
    end
  end
end

def unlock

def unlock
  Bundler.ui.warn "Unlock is deprecated. To update to newer gem versions, use `bundle update`."
end

def update(*gems)

def update(*gems)
  sources = Array(options[:source])
  if gems.empty? && sources.empty?
    # We're doing a full update
    FileUtils.rm_f Bundler.root.join("Gemfile.lock")
  else
    Bundler.definition(:gems => gems, :sources => sources)
  end
  Installer.install Bundler.root, Bundler.definition
  cache if Bundler.root.join("vendor/cache").exist?
  Bundler.ui.confirm "Your bundle is updated! " +
    "Use `bundle show [gemname]` to see where a bundled gem is installed."
end

def version

def version
  Bundler.ui.info "Bundler version #{Bundler::VERSION}"
end

def viz

def viz
  output_file = File.expand_path(options[:file])
  graph = Graph.new( Bundler.load )
  begin
    graph.viz(output_file, options[:version], options[:requirements])
    Bundler.ui.info output_file
  rescue LoadError => e
    Bundler.ui.error e.inspect
    Bundler.ui.warn "Make sure you have the graphviz ruby gem. You can install it with:"
    Bundler.ui.warn "`gem install ruby-graphviz`"
  rescue StandardError => e
    if e.message =~ /GraphViz not installed or dot not in PATH/
      Bundler.ui.error e.message
      Bundler.ui.warn "The ruby graphviz gem requires GraphViz to be installed"
    else
      raise
    end
  end
end