class Spring::Client::Binstub

def self.call(args)

def self.call(args)
  require "spring/commands"
  super
end

def self.description

def self.description
  "Generate spring based binstubs. Use --all to generate a binstub for all known commands. Use --remove to revert."
end

def self.rails_command

def self.rails_command
  @rails_command ||= CommandWrapper.new("rails")
end

def call

def call
  case @mode
  when :add
    bindir.mkdir unless bindir.exist?
    File.write(spring_binstub, SPRING)
    spring_binstub.chmod 0755
    items.each(&:add)
  when :remove
    spring_binstub.delete if @all
    items.each(&:remove)
  else
    raise ArgumentError
  end
end

def find_commands(name)

def find_commands(name)
  case name
  when "--all"
    @all = true
    commands = Spring.commands.dup
    commands.delete_if { |command_name, _| command_name.start_with?("rails_") }
    commands.values + [self.class.rails_command]
  when "--remove"
    @mode = :remove
    []
  when "rails"
    [self.class.rails_command]
  else
    if command = Spring.commands[name]
      [command]
    else
      $stderr.puts "The '#{name}' command is not known to spring."
      exit 1
    end
  end
end

def initialize(args)

def initialize(args)
  super
  @bindir = env.root.join("bin")
  @all    = false
  @mode   = :add
  @items  = args.drop(1)
                .map { |name| find_commands name }
                .inject(Set.new, :|)
                .map { |command| Item.new(command) }
end

def spring_binstub

def spring_binstub
  bindir.join("spring")
end