Module: Codebase

Extended by:
Codebase
Included in:
Codebase
Defined in:
lib/codebase.rb,
lib/codebase/command.rb

Defined Under Namespace

Classes: Command, Error, MustBeInRepositoryError, NotConfiguredError

Constant Summary collapse

VERSION =
"4.0.11"

Instance Method Summary collapse

Instance Method Details

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



35
36
37
38
39
40
41
42
43
44
# File 'lib/codebase.rb', line 35

def command(command, options = {}, &block)
  @commands = Hash.new if @commands.nil?
  @commands[command] = Hash.new
  @commands[command][:description] = @next_description
  @commands[command][:usage] = @next_usage
  @commands[command][:flags] = @next_flags
  @commands[command][:required_args] = (options[:required_args] || 0)
  @commands[command][:block] = Command.new(block)
  @next_usage, @next_description, @next_flags = nil, nil, nil
end

#commandsObject



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

def commands
  @commands
end

#desc(value) ⇒ Object



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

def desc(value)
  @next_description = Array.new if @next_description.nil?
  @next_description << value
end

#flags(key, value) ⇒ Object



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

def flags(key, value)
  @next_flags = Hash.new if @next_flags.nil?
  @next_flags[key] = value
end

#load_commandsObject



64
65
66
67
68
# File 'lib/codebase.rb', line 64

def load_commands
  Dir[File.join(File.dirname(__FILE__), 'commands', '*.rb')].each do |path|
    Codebase.module_eval File.read(path), path
  end
end

#parse_options(args) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/codebase.rb', line 70

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

#run(command, args = []) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/codebase.rb', line 19

def run(command, args = [])
  load_commands
  command = 'help' if command.nil?
  if @commands[command]
    options = parse_options(args)
    if args.size < @commands[command][:required_args].to_i
      puts "error: #{@commands[command][:usage]}"
      puts "See 'cb help #{command}' for usage."
      Process.exit(1)
    end
    @commands[command][:block].call(options, *args)
  else
    puts "Command not found. Check 'cb help' for full information."
  end
end

#usage(value) ⇒ Object



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

def usage(value)
  @next_usage = value
end