Skip to content
This repository
Browse code

add back boson executable :fireworks:

  • Loading branch information...
commit f8a2026bf90ed0e58ca145b82224a0e55dccf685 1 parent 5b1bcd5
Gabriel Horner authored
1  .gemspec
@@ -11,6 +11,7 @@ Gem::Specification.new do |s|
11 11 s.summary = "A command/task framework similar to rake and thor that opens your ruby universe to the commandline and irb."
12 12 s.description = "Boson is a modular command/task framework. Thanks to its rich set of plugins, it differentiates itself from rake and thor by being usable from irb and the commandline, having optional automated views generated by hirb and allowing libraries to be written as plain ruby. Works with ruby >= 1.9.2"
13 13 s.required_rubygems_version = ">= 1.3.6"
  14 + s.executables = ['boson']
14 15 s.add_development_dependency 'mocha', '~> 0.10.4'
15 16 s.add_development_dependency 'bacon', '>= 1.1.0'
16 17 s.add_development_dependency 'mocha-on-bacon'
6 bin/boson
... ... @@ -0,0 +1,6 @@
  1 +#!/usr/bin/env ruby
  2 +
  3 +require 'boson'
  4 +require 'boson/bin_runner'
  5 +
  6 +Boson::BinRunner.start
5 lib/boson/bare_runner.rb
@@ -4,10 +4,7 @@ class BareRunner
4 4 DEFAULT_LIBRARIES = []
5 5 # Default options for parse_args
6 6 GLOBAL_OPTIONS = {
7   - help: {
8   - type: :boolean,
9   - desc: "Displays this help message or a command's help if given a command"
10   - }
  7 + help: { type: :boolean, desc: "Displays this help message" }
11 8 }
12 9
13 10 module API
114 lib/boson/bin_runner.rb
... ... @@ -0,0 +1,114 @@
  1 +module Boson
  2 + # This class handles the boson executable.
  3 + #
  4 + # Usage for the boson shell command looks like this:
  5 + # boson [GLOBAL OPTIONS] [COMMAND] [ARGS] [COMMAND OPTIONS]
  6 + #
  7 + # The boson executable comes with several global options: :version, :execute,
  8 + # :ruby_debug, :debug, and :load_path.
  9 + class BinRunner < BareRunner
  10 + GLOBAL_OPTIONS.update(
  11 + version: {type: :boolean, desc: "Prints the current version"},
  12 + execute: {type: :string,
  13 + desc: "Executes given arguments as a one line script"},
  14 + ruby_debug: {type: :boolean, desc: "Sets $DEBUG", alias: 'D'},
  15 + debug: {type: :boolean, desc: "Prints debug info for boson"},
  16 + load_path: {type: :string, desc: "Add to front of $LOAD_PATH", alias: 'I'}
  17 + )
  18 +
  19 + module API
  20 + attr_accessor :command
  21 +
  22 + # Executes functionality from either an option or a command
  23 + def execute_option_or_command(options, command, args)
  24 + options[:execute] ? eval_execute_option(options[:execute]) :
  25 + execute_command(command, args)
  26 + end
  27 +
  28 + # Evaluates :execute option.
  29 + def eval_execute_option(str)
  30 + Boson.main_object.instance_eval str
  31 + end
  32 +
  33 + # Returns true if an option does something and exits early
  34 + def early_option?(args)
  35 + if @options[:version]
  36 + puts("boson #{Boson::VERSION}")
  37 + true
  38 + elsif args.empty? || (@command.nil? && !@options[:execute])
  39 + print_usage
  40 + true
  41 + else
  42 + false
  43 + end
  44 + end
  45 +
  46 + # Determines verbosity of this class
  47 + def verbose
  48 + false
  49 + end
  50 +
  51 + # Handles no method errors
  52 + def no_method_error_message(err)
  53 + @command = @command.to_s
  54 + if err.backtrace.grep(/`(invoke|full_invoke)'$/).empty? ||
  55 + !err.message[/undefined method `(\w+\.)?#{command_name(@command)}'/]
  56 + default_error_message($!)
  57 + else
  58 + command_not_found?(@command) ?
  59 + "Error: Command '#{@command}' not found" : default_error_message(err)
  60 + end
  61 + end
  62 +
  63 + # Determine command name given full command name. Overridden by namespaces
  64 + def command_name(cmd)
  65 + cmd
  66 + end
  67 +
  68 + # Determines if a NoMethodError is a command not found error
  69 + def command_not_found?(cmd)
  70 + cmd[/\w+/]
  71 + end
  72 +
  73 + # Constructs error message
  74 + def default_error_message(err)
  75 + "Error: #{err.message}"
  76 + end
  77 +
  78 + def print_usage_header
  79 + puts "boson [GLOBAL OPTIONS] [COMMAND] [ARGS] [COMMAND OPTIONS]\n\n"
  80 + end
  81 +
  82 + # prints full usage
  83 + def print_usage
  84 + print_usage_header
  85 + @option_parser.print_usage_table
  86 + end
  87 + end
  88 + extend API
  89 +
  90 + # Starts, processes and ends a commandline request.
  91 + def self.start(args=ARGV)
  92 + super
  93 + @command, @options, @args = parse_args(args)
  94 +
  95 + $:.unshift(*options[:load_path].split(":")) if options[:load_path]
  96 + Boson.debug = true if options[:debug]
  97 + $DEBUG = true if options[:ruby_debug]
  98 + return if early_option?(args)
  99 + Boson.in_shell = true
  100 +
  101 + init
  102 + execute_option_or_command(@options, @command, @args)
  103 + rescue NoMethodError
  104 + abort_with no_method_error_message($!)
  105 + rescue
  106 + abort_with default_error_message($!)
  107 + end
  108 +
  109 + # Hash of global options passed in from commandline
  110 + def self.options
  111 + @options ||= {}
  112 + end
  113 + end
  114 +end

0 comments on commit f8a2026

Please sign in to comment.
Something went wrong with that request. Please try again.