Sha256: 21e1a237d848fb416109fa8e05ec0a47b7cf1d8d6c65d81541aa3a1870f5d614

Contents?: true

Size: 1.53 KB

Versions: 5

Compression:

Stored size: 1.53 KB

Contents

require 'gem/release/cmds/runner'

module Gem
  module Release
    module GemCommand
      class Setup < Struct.new(:name, :cmd)
        def run
          cmd.summary      = const.summary
          cmd.description  = const.description
          cmd.usage        = usage
          cmd.arguments    = arguments
          cmd.defaults_str = ''

          opts.each do |args|
            cmd.add_option(*args.first) do |*a|
              cmd.instance_exec(*a, &args.last)
            end
          end
        end

        def opts
          const.opts + Cmds::Base.opts
        end

        def const
          Cmds[name] || raise("Unknown command #{name}")
        end

        def usage
          args = const.args.map(&:first)
          [:gem, name, args.map { |arg| "[#{arg}]" }.join(' ')].join(' ')
        end

        def arguments
          arg  = const.args.map(&:first).max_by(&:size)
          args = const.args.map { |name, summary| [name.to_s.ljust(arg.size), summary] }
          args.map { |pair| pair.join(' - ') }.join("\n")
        end
      end

      def self.included(const)
        const.singleton_class.send(:attr_accessor, :cmd)
        const.send(:alias_method, :opts, :options)
      end

      attr_accessor :arguments, :usage, :defaults_str, :description

      def initialize
        super(cmd)
        Setup.new(cmd, self).run
      end

      def execute
        Cmds::Runner.new(cmd, opts.delete(:args), opts).run
      rescue Abort => ex
        abort(ex.message)
      end

      def cmd
        self.class.cmd
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
gem-release-2.2.2 lib/gem/release/support/gem_command.rb
gem-release-2.2.1 lib/gem/release/support/gem_command.rb
gem-release-2.2.0 lib/gem/release/support/gem_command.rb
gem-release-2.1.1 lib/gem/release/support/gem_command.rb
gem-release-2.1.0 lib/gem/release/support/gem_command.rb