lib/bones/app.rb in bones-2.5.1 vs lib/bones/app.rb in bones-3.0.0

- old
+ new

@@ -1,60 +1,82 @@ -require 'fileutils' -require 'optparse' -require 'erb' +require 'net/http' +require 'uri' -module Bones -class App +module Bones::App + extend LittlePlugger(:path => 'bones/app', :module => Bones::App) + disregard_plugins :error, :main, :command, :file_manager - # Create a new instance of App, and run the +bones+ application given + Error = Class.new(StandardError) + + # Create a new instance of Main, and run the +bones+ application given # the command line _args_. # - def self.run( args ) - self.new.run args + def self.run( args = nil ) + args ||= ARGV.dup.map! { |v| v.dup } + ::Bones::App::Main.new.run args end - # Create a new main instance using _io_ for standard output and _err_ for - # error messages. - # - def initialize( out = STDOUT, err = STDERR ) - @out = out - @err = err - end + class Main + attr_reader :stdout + attr_reader :stderr - # Parse the desired user command and run that command object. - # - def run( args ) - cmd_str = args.shift - cmd = case cmd_str - when 'create'; CreateCommand.new(@out, @err) - when 'update'; UpdateCommand.new(@out, @err) - when 'freeze'; FreezeCommand.new(@out, @err) - when 'unfreeze'; UnfreezeCommand.new(@out, @err) - when 'info'; InfoCommand.new(@out, @err) - when nil, '-h', '--help' - help - when '-v', '--version' - @out.puts "Mr Bones #{::Bones::VERSION}" - nil - else - raise "Unknown command #{cmd_str.inspect}" - end + # Create a new Main application instance. Options can be passed to + # configuret he stdout and stderr IO streams (very useful for testing). + # + def initialize( opts = {} ) + opts[:stdout] ||= $stdout + opts[:stderr] ||= $stderr - cmd.run args if cmd + @opts = opts + @stdout = opts[:stdout] + @stderr = opts[:stderr] + end - rescue StandardError => err - @err.puts "ERROR: While executing bones ... (#{err.class})" - @err.puts " #{err.to_s}" - exit 1 - end + # Parse the desired user command and run that command object. + # + def run( args ) + commands = [] + @plugins = ::Bones::App.plugins + @plugins.each { |k,v| commands << k.to_s if v < ::Bones::App::Command } - # Show the toplevel Mr Bones help message. - # - def help - @out.puts <<-MSG + 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 "ERROR: While executing bones ..." + stderr.puts " #{err.message}" + exit 1 + rescue StandardError => err + stderr.puts "ERROR: While executing bones ... (#{err.class})" + stderr.puts " #{err.to_s}" + exit 1 + end + + # Show the toplevel Mr Bones help message. + # + 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: @@ -66,27 +88,34 @@ bones create new_project bones freeze -r git://github.com/fudgestudios/bort.git bort bones create -s bort new_rails_project Commands: - bones create create a new project from a skeleton - bones update copy Mr Bones tasks to a project - bones freeze create a new skeleton in ~/.mrbones/ - bones unfreeze remove a skeleton from ~/.mrbones/ - bones info show information about available skeletons + 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] + 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://codeforpeople.rubyforge.org/bones/ + http://github.com/TwP/bones - MSG - nil - end + MSG -end # class App -end # module Bones + stdout.puts msg + end -Bones.require_all_libs_relative_to(__FILE__) + end # class Main +end # module Bones::App # EOF