public
Rubygem
Description: The official `github` command line helper for simplifying your GitHub experience.
Homepage: http://github.com
Clone URL: git://github.com/defunkt/github-gem.git
defunkt (author)
Tue May 06 01:13:08 -0700 2008
commit  8261bb0b1a577f24047f76409f114820937a89ac
tree    ceff7ed189c6f488a799959567a32af002338889
parent  808c2c3563c2cc33944324cd0201de596d995c56
github-gem / lib / github.rb
100644 100 lines (83 sloc) 1.97 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
$:.unshift File.dirname(__FILE__)
require 'github/command'
require 'github/helper'
 
##
# Starting simple.
#
# $ github <command> <args>
#
# GitHub.register <command> do |*args|
# whatever
# end
#
# We'll probably want to use the `choice` gem for concise, tasty DSL
# arg parsing action.
#
 
module GitHub
  extend self
 
  BasePath = File.expand_path(File.dirname(__FILE__) + '/..')
 
  def register(command, &block)
    debug "Registered `#{command}`"
    commands[command.to_s] = Command.new(block)
  end
 
  def describe(hash)
    descriptions.update hash
  end
 
  def helper(command, &block)
    debug "Helper'd `#{command}`"
    Helper.send :define_method, command, &block
  end
 
  def activate(args)
    @options = parse_options(args)
    load 'helpers.rb'
    load 'commands.rb'
    invoke(args.shift, *args)
  end
 
  def invoke(command, *args)
    block = commands[command.to_s] || commands['default']
    debug "Invoking `#{command}`"
    block.call(*args)
  end
 
  def commands
    @commands ||= {}
  end
 
  def descriptions
    @descriptions ||= {}
  end
 
  def options
    @options
  end
 
  def parse_options(args)
    @debug = args.delete('--debug')
    args.inject({}) do |memo, arg|
      if arg =~ /^--([^=]+)=(.+)/
        args.delete(arg)
        memo.merge($1.to_sym => $2)
      elsif arg =~ /^--(.+)/
        args.delete(arg)
        memo.merge($1.to_sym => true)
      else
        memo
      end
    end
  end
 
  def load(file)
    file[0] == ?/ ? super : super(BasePath + "/commands/#{file}")
  end
 
  def debug(*messages)
    puts *messages.map { |m| "== #{m}" } if debug?
  end
 
  def debug?
    !!@debug
  end
end
 
GitHub.register :default do
  puts "Usage: github command <space separated arguments>", ''
  puts "Available commands:", ''
  longest = GitHub.descriptions.map { |d,| d.to_s.size }.max
  GitHub.descriptions.each do |command, desc|
    command = "%-#{longest}s" % command
    puts " #{command} => #{desc}"
  end
  puts
end