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



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

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
# File 'lib/github.rb', line 26

def command(command, options = {}, &block)
  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.to_s] = Command.new(block)
  Array(options[:alias] || options[:aliases]).each do |command_alias|
    commands[command_alias.to_s] = commands[command.to_s]
  end
end

#commandsObject



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

def commands
  @commands ||= {}
end

#debug(*messages) ⇒ Object



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

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

#debug?Boolean

Returns:

  • (Boolean)


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

def debug?
  !!@debug
end

#desc(str) ⇒ Object



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

def desc(str)
  @next_description = str
end

#descriptionsObject



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

def descriptions
  @descriptions ||= {}
end

#find_command(name) ⇒ Object



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

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

#flag_descriptionsObject



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

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

#flags(hash) ⇒ Object



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

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

#helper(command, &block) ⇒ Object



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

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

#invoke(command, *args) ⇒ Object



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

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

#learn(message) ⇒ Object



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

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

#learn?Boolean

Returns:

  • (Boolean)


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

def learn?
  !!@learn
end

#load(file) ⇒ Object



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

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

#optionsObject



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

def options
  @options
end

#original_argsObject



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

def original_args
  @@original_args ||= []
end

#parse_options(args) ⇒ Object



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

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



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

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

#usage_descriptionsObject



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

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