lib/boson.rb in boson-0.4.0 vs lib/boson.rb in boson-1.0.0

- old
+ new

@@ -1,91 +1,83 @@ -%w{hirb alias}.each {|e| require e } -%w{runner runners/console_runner repo manager loader inspector library}.each {|e| require "boson/#{e}" } -%w{argument method comment}.each {|e| require "boson/inspectors/#{e}_inspector" } -# order of library subclasses matters -%w{module file gem require local_file}.each {|e| require "boson/libraries/#{e}_library" } -(%w{namespace view command util commands option_parser options} + - %w{index repo_index scientist option_command pipe pipes version}).each {|e| require "boson/#{e}" } +require 'boson/bare_runner' +require 'boson/manager' +require 'boson/loader' +require 'boson/inspector' +require 'boson/library' +require 'boson/method_inspector' +require 'boson/runner_library' +require 'boson/command' +require 'boson/util' +require 'boson/option_parser' +require 'boson/options' +require 'boson/scientist' +require 'boson/option_command' +require 'boson/version' -# This module stores the libraries, commands, repos and main object used throughout Boson. +# This module stores the libraries, commands and the main_object. # # Useful documentation links: -# * Boson::BinRunner - Runs the boson executable -# * Boson::ConsoleRunner - Runs Boson from the ruby console -# * Boson::Repo.config - Explains main config file # * Boson::Library - All about libraries -# * Boson::FileLibrary - Explains creating libraries as files # * Boson::Loader - Explains library module callbacks # * Boson::OptionParser - All about options module Boson - # Module which is extended by Boson.main_object to give it command functionality. - module Universe; include Commands::Namespace; end - NAMESPACE = '.' # Delimits namespace from command extend self + + # Module which is extended by Boson.main_object to give it command functionality. + module Universe; end + # Module under which most library modules are evaluated. + module Commands; end + + # Default config + CONFIG = {libraries: {}, command_aliases: {}, option_underscore_search: true} + # The object which holds and executes all command functionality attr_accessor :main_object - attr_accessor :commands, :libraries alias_method :higgs, :main_object + attr_accessor :commands, :libraries, :config + # Prints debugging info when set + attr_accessor :debug + # Returns true if commands are being executed from a non-ruby shell i.e. bash + # Returns nil/false if in a ruby shell i.e. irb. + attr_accessor :in_shell + # Returns true if in commandline with verbose flag or if set explicitly. + # Plugins should use this to display more info. + attr_accessor :verbose + # Array of loaded Boson::Library objects. def libraries @libraries ||= Array.new end # Array of loaded Boson::Command objects. def commands @commands ||= Array.new end - # The main required repository which defaults to ~/.boson. - def repo - @repo ||= Repo.new("#{Util.find_home}/.boson") + # Global config used by most classes + def config + @config ||= CONFIG end - # An optional local repository which defaults to ./lib/boson or ./.boson. - def local_repo - @local_repo ||= begin - ignored_dirs = (repo.config[:ignore_directories] || []).map {|e| File.expand_path(e) } - dir = ["lib/boson", ".boson"].find {|e| File.directory?(e) && - File.expand_path(e) != repo.dir && !ignored_dirs.include?(File.expand_path('.')) } - Repo.new(dir) if dir - end - end - - # The array of loaded repositories containing the main repo and possible local and global repos - def repos - @repos ||= [repo, local_repo, global_repo].compact - end - - # Optional global repository at /etc/boson - def global_repo - File.exists?('/etc/boson') ? Repo.new('/etc/boson') : nil - end - - def main_object=(value) #:nodoc: + # Sets main_object and extends it with commands from Universe + def main_object=(value) @main_object = value.extend(Universe) end - def library(query, attribute='name') #:nodoc: + # Finds first library that has a value of attribute + def library(query, attribute='name') libraries.find {|e| e.send(attribute) == query } end - # Start Boson by loading repositories and their configured libraries. - # See ConsoleRunner.start for its options. - def start(options={}) - ConsoleRunner.start(options) - end - # Invoke an action on the main object. def invoke(*args, &block) main_object.send(*args, &block) end - # Invoke command string even with namespaces - def full_invoke(cmd, args) #:nodoc: - command, subcommand = cmd.include?(NAMESPACE) ? cmd.split(NAMESPACE, 2) : [cmd, nil] - dispatcher = subcommand ? Boson.invoke(command) : Boson.main_object - dispatcher.send(subcommand || command, *args) + # Similar to invoke but accepts args as an array + def full_invoke(cmd, args) + main_object.send(cmd, *args) end # Boolean indicating if the main object can invoke the given method/command. def can_invoke?(meth, priv=true) Boson.main_object.respond_to? meth, priv