class Bones::App::Main

Attributes

stderr[R]
stdout[R]

Public Class Methods

new( opts = {} ) click to toggle source

Create a new Main application instance. Options can be passed to configuret he stdout and stderr IO streams (very useful for testing).

# File lib/bones/app.rb, line 27
def initialize( opts = {} )
  opts[:stdout] ||= $stdout
  opts[:stderr] ||= $stderr

  @opts = opts
  @stdout = opts[:stdout]
  @stderr = opts[:stderr]
end

Public Instance Methods

help() click to toggle source

Show the toplevel Mr Bones help message.

# File lib/bones/app.rb, line 73
    def help
      msg = <<-MSG
NAME
  bones v#{::Bones.version}

DESCRIPTION
  Mr Bones is a handy tool that builds a skeleton for your new Ruby
  projects. The skeleton contains some starter code and a collection of
  rake tasks to ease the management and deployment of your source code.

  Usage:
    bones -h/--help
    bones -v/--version
    bones command [options] [arguments]

  Examples:
    bones create new_project
    bones freeze -r git://github.com/fudgestudios/bort.git bort
    bones create -s bort new_rails_project

  Commands:
      MSG

     fmt = lambda { |cmd|
             if @plugins[cmd] < ::Bones::App::Command
               msg << "    bones %-15s %s\n" % [cmd, @plugins[cmd].summary]
             end
           }

     ary = [:create, :freeze, :unfreeze, :info, :plugins]
     ary.each(&fmt)
     (@plugins.keys - ary).each(&fmt)

      msg.concat <<-MSG

  Further Help:
    Each command has a '--help' option that will provide detailed
    information for that command.

    http://github.com/TwP/bones

      MSG

    stdout.puts msg
    end
run( args ) click to toggle source

Parse the desired user command and run that command object.

# File lib/bones/app.rb, line 38
def run( args )
  commands = []
  @plugins = ::Bones::App.plugins
  @plugins.each { |k,v| commands << k.to_s if v < ::Bones::App::Command }

  cmd_str = args.shift
  cmd = case cmd_str
    when *commands
      key = cmd_str.to_sym
      @plugins[key].new @opts
    when nil, '-h', '--help'
      help
    when '-v', '--version'
      stdout.puts "Mr Bones v#{::Bones.version}"
    else
      raise Error, "Unknown command #{cmd_str.inspect}"
    end

  if cmd
    cmd.parse args
    cmd.run
  end

rescue Bones::App::Error => err
  stderr.puts "#{colorize('ERROR', :white, :on_red)}:  While executing bones ..."
  stderr.puts "    #{err.message.split("\n").join("\n    ")}"
  exit 1
rescue StandardError => err
  stderr.puts "#{colorize('ERROR', :white, :on_red)}:  While executing bones ... (#{colorize(err.class, :red)})"
  stderr.puts "    #{err.to_s}"
  exit 1
end