Module: GitHub

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

Overview

Starting simple.

$ github <command> <args>

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

Defined Under Namespace

Classes: Command, GitCommand, Helper

Constant Summary collapse

BasePath =
File.expand_path(File.dirname(__FILE__))

Instance Method Summary collapse

Instance Method Details

#activate(args) ⇒ Object



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

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



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

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



82
83
84
# File 'lib/github.rb', line 82

def commands
  @commands ||= {}
end

#debug(*messages) ⇒ Object



126
127
128
# File 'lib/github.rb', line 126

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

#debug?Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/github.rb', line 142

def debug?
  !!@debug
end

#desc(str) ⇒ Object



41
42
43
# File 'lib/github.rb', line 41

def desc(str)
  @next_description = str
end

#descriptionsObject



86
87
88
# File 'lib/github.rb', line 86

def descriptions
  @descriptions ||= {}
end

#find_command(name) ⇒ Object



77
78
79
80
# File 'lib/github.rb', line 77

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

#flag_descriptionsObject



90
91
92
# File 'lib/github.rb', line 90

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

#flags(hash) ⇒ Object



45
46
47
48
# File 'lib/github.rb', line 45

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

#helper(command, &block) ⇒ Object



55
56
57
58
# File 'lib/github.rb', line 55

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

#invoke(command, *args) ⇒ Object



71
72
73
74
75
# File 'lib/github.rb', line 71

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

#learn(message) ⇒ Object



130
131
132
133
134
135
136
# File 'lib/github.rb', line 130

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

#learn?Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/github.rb', line 138

def learn?
  !!@learn
end

#load(file) ⇒ Object



146
147
148
149
150
# File 'lib/github.rb', line 146

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

#optionsObject



98
99
100
# File 'lib/github.rb', line 98

def options
  @options
end

#original_argsObject



102
103
104
# File 'lib/github.rb', line 102

def original_args
  @@original_args ||= []
end

#parse_options(args) ⇒ Object



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

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



50
51
52
53
# File 'lib/github.rb', line 50

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

#usage_descriptionsObject



94
95
96
# File 'lib/github.rb', line 94

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