Class: Amp::Dispatch::Runner
Overview
This class runs Amp as a binary. Create a new instance with the arguments to use, and call run! to run Amp.
Instance Method Summary collapse
- #collect_options(arguments) ⇒ Object
-
#initialize(args, opts = {}) ⇒ Runner
constructor
A new instance of Runner.
-
#load_ampfile!(in_dir = Dir.pwd) ⇒ Object
Loads the ampfile (or whatever it’s specified as) from the current directory or a parent directory.
- #load_plugins! ⇒ Object
- #run! ⇒ Object
- #trim_argv_for_command(arguments, command) ⇒ Object
Constructor Details
#initialize(args, opts = {}) ⇒ Runner
Returns a new instance of Runner.
21 22 23 |
# File 'lib/amp-front/dispatch/runner.rb', line 21 def initialize(args, opts={}) @args, @opts = args, opts end |
Instance Method Details
#collect_options(arguments) ⇒ Object
75 76 77 78 79 80 81 82 83 |
# File 'lib/amp-front/dispatch/runner.rb', line 75 def (arguments) argv = arguments.dup _, hash = Trollop::(argv) do "Amp - some more crystal, sir?" version "Amp version #{Amp::VERSION} (#{Amp::VERSION_TITLE})" stop_on_unknown end [hash, argv] end |
#load_ampfile!(in_dir = Dir.pwd) ⇒ Object
Loads the ampfile (or whatever it’s specified as) from the current directory or a parent directory.
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/amp-front/dispatch/runner.rb', line 43 def load_ampfile!(in_dir = Dir.pwd) file = @opts[:ampfile] || 'ampfile' variations = [file, file[0,1].upcase + file[1..-1]] # include titlecase to_load = variations.find {|x| File.exist?(File.join(in_dir, x))} if to_load load to_load elsif File.dirname(in_dir) != in_dir load_ampfile! File.dirname(in_dir) end end |
#load_plugins! ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/amp-front/dispatch/runner.rb', line 54 def load_plugins! Amp::Plugins::Base.all_plugins.each do |plugin| instance = plugin.new(@opts) instance.load! Amp::Plugins::Base.loaded_plugins << instance end end |
#run! ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/amp-front/dispatch/runner.rb', line 25 def run! global_opts, arguments = (@args) load_ampfile! load_plugins! command_class = Amp::Command.for_name(arguments.join(' ')) if command_class.nil? command_class = Amp::Command::Help else arguments = trim_argv_for_command(arguments, command_class) end command = command_class.new opts, arguments = command.(arguments) command.call(opts.merge(global_opts), arguments) end |
#trim_argv_for_command(arguments, command) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/amp-front/dispatch/runner.rb', line 62 def trim_argv_for_command(arguments, command) argv = arguments.dup path_parts = command.inspect.gsub(/Amp::Command::/, '').gsub(/::/, ' ').split path_parts.each do |part| next_part = argv.shift if next_part.downcase != part.downcase raise ArgumentError.new( "Failed to parse command line option for: #{command.inspect}") end end argv end |