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

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__))

Instance Method Summary collapse

Instance Method Details

#activate(args) ⇒ Object



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

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



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

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



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

def commands
  @commands ||= {}
end

#debug(*messages) ⇒ Object



130
131
132
# File 'lib/github.rb', line 130

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

#debug?Boolean

Returns:

  • (Boolean)


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

def debug?
  !!@debug
end

#desc(str) ⇒ Object



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

def desc(str)
  @next_description = str
end

#descriptionsObject



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

def descriptions
  @descriptions ||= {}
end

#find_command(name) ⇒ Object



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

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

#flag_descriptionsObject



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

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

#flags(hash) ⇒ Object



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

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

#helper(command, &block) ⇒ Object



59
60
61
62
# File 'lib/github.rb', line 59

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

#invoke(command, *args) ⇒ Object



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

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

#learn(message) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/github.rb', line 134

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

#learn?Boolean

Returns:

  • (Boolean)


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

def learn?
  !!@learn
end

#load(file) ⇒ Object



150
151
152
153
154
# File 'lib/github.rb', line 150

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

#optionsObject



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

def options
  @options
end

#original_argsObject



106
107
108
# File 'lib/github.rb', line 106

def original_args
  @@original_args ||= []
end

#parse_options(args) ⇒ Object



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

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



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

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

#usage_descriptionsObject



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

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