Sha256: 293a3963219bfad10a64623faeb6bdf6a0a52f43ed0581105253b7c81347aa1f

Contents?: true

Size: 986 Bytes

Versions: 1

Compression:

Stored size: 986 Bytes

Contents

require 'rack/app'
require 'optparse'
class Rack::App::CLI

  require 'rack/app/cli/command'

  def self.start(argv)
    @argv = Rack::App::Utils.deep_dup(argv)

    context = {}
    Kernel.__send__(:define_method, :run) { |app, *_| context[:app]= app }
    config_ru_file_path = Rack::App::Utils.pwd('config.ru')
    load(config_ru_file_path) if File.exist?(config_ru_file_path)

    context[:app].cli.start(argv)
  end

  def start(argv)
    command_name = argv.shift
    command = find_command_for(command_name)
    command && command.start(argv)
  end

  def merge!(cli)
    commands.push(*cli.commands)
    self
  end

  protected

  def find_command_for(command_name)
    commands.find { |command| command.name == command_name }
  end

  def commands
    @commands ||= []
  end

  def command(name, &block)
    command_prototype = Rack::App::Utils.deep_dup(Rack::App::CLI::Command)
    command_prototype.instance_exec(&block)
    commands << command_prototype.new(name)
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rack-app-3.0.0.delta lib/rack/app/cli.rb