Module: GitHub

Extended by:
GitHub
Included in:
GitHub
Defined in:
lib/github.rb,
lib/github/ui.rb,
lib/github/helper.rb,
lib/github/command.rb,
lib/github/version.rb

Overview

Starting simple.

$ github <command> <args>

GitHub.command <command> do |*args|
  whatever
end

Defined Under Namespace

Modules: UI Classes: Command, GitCommand, Helper

Constant Summary collapse

BasePath =
File.expand_path(File.dirname(__FILE__))
VERSION =
"0.7.2"

Instance Method Summary collapse

Instance Method Details

#activate(args) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/github.rb', line 62

def activate(args)
  @@original_args = args.clone
  @options = parse_options(args)
  @debug = @options.delete(:debug)
  @learn = @options.delete(:learn)
  Dir[BasePath + '/commands/*.rb'].each do |command|
    load command
  end
  invoke(args.shift, *args)
end

#command(command, options = {}, &block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/github.rb', line 28

def command(command, options = {}, &block)
  command = command.to_s
  debug "Registered `#{command}`"
  descriptions[command] = @next_description if @next_description
  @next_description = nil
  flag_descriptions[command].update @next_flags if @next_flags
  usage_descriptions[command] = @next_usage if @next_usage
  @next_flags = nil
  @next_usage = []
  commands[command] = Command.new(block)
  Array(options[:alias] || options[:aliases]).each do |command_alias|
    commands[command_alias.to_s] = commands[command.to_s]
  end
end

#commandsObject



84
85
86
# File 'lib/github.rb', line 84

def commands
  @commands ||= {}
end

#debug(*messages) ⇒ Object



128
129
130
# File 'lib/github.rb', line 128

def debug(*messages)
  puts *messages.map { |m| "== #{m}" } if debug?
end

#debug?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/github.rb', line 144

def debug?
  !!@debug
end

#desc(str) ⇒ Object



43
44
45
# File 'lib/github.rb', line 43

def desc(str)
  @next_description = str
end

#descriptionsObject



88
89
90
# File 'lib/github.rb', line 88

def descriptions
  @descriptions ||= {}
end

#find_command(name) ⇒ Object



79
80
81
82
# File 'lib/github.rb', line 79

def find_command(name)
  name = name.to_s
  commands[name] || (commands[name] = GitCommand.new(name)) || commands['default']
end

#flag_descriptionsObject



92
93
94
# File 'lib/github.rb', line 92

def flag_descriptions
  @flagdescs ||= Hash.new { |h, k| h[k] = {} }
end

#flags(hash) ⇒ Object



47
48
49
50
# File 'lib/github.rb', line 47

def flags(hash)
  @next_flags ||= {}
  @next_flags.update hash
end

#helper(command, &block) ⇒ Object



57
58
59
60
# File 'lib/github.rb', line 57

def helper(command, &block)
  debug "Helper'd `#{command}`"
  Helper.send :define_method, command, &block
end

#invoke(command, *args) ⇒ Object



73
74
75
76
77
# File 'lib/github.rb', line 73

def invoke(command, *args)
  block = find_command(command)
  debug "Invoking `#{command}`"
  block.call(*args)
end

#learn(message) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/github.rb', line 132

def learn(message)
  if learn?
    puts "== " + Color.yellow(message)
  else
    debug(message)
  end
end

#learn?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/github.rb', line 140

def learn?
  !!@learn
end

#load(file) ⇒ Object



148
149
150
151
152
# File 'lib/github.rb', line 148

def load(file)
  file[0] =~ /^\// ? path = file : path = BasePath + "/commands/#{File.basename(file)}"
  data = File.read(path)
  GitHub.module_eval data, path
end

#optionsObject



100
101
102
# File 'lib/github.rb', line 100

def options
  @options
end

#original_argsObject



104
105
106
# File 'lib/github.rb', line 104

def original_args
  @@original_args ||= []
end

#parse_options(args) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/github.rb', line 108

def parse_options(args)
  idx = 0
  args.clone.inject({}) do |memo, arg|
    case arg
    when /^--(.+?)=(.*)/
      args.delete_at(idx)
      memo.merge($1.to_sym => $2)
    when /^--(.+)/
      args.delete_at(idx)
      memo.merge($1.to_sym => true)
    when "--"
      args.delete_at(idx)
      return memo
    else
      idx += 1
      memo
    end
  end
end

#usage(string) ⇒ Object



52
53
54
55
# File 'lib/github.rb', line 52

def usage(string)
  @next_usage ||= []
  @next_usage << string
end

#usage_descriptionsObject



96
97
98
# File 'lib/github.rb', line 96

def usage_descriptions
  @usage_descriptions ||= Hash.new { |h, k| h[k] = [] }
end