Module: Appli

Extended by:
Appli
Included in:
Appli
Defined in:
lib/appli.rb,
lib/appli/command.rb

Defined Under Namespace

Classes: Command, Error, MustBeInRepositoryError, NotConfiguredError

Constant Summary collapse

VERSION =
"0.0.12"

Instance Method Summary collapse

Instance Method Details

#alias(a, b) ⇒ Object



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

def alias(a, b)
  @aliases = Hash.new if @aliases.nil?
  @aliases[a] = b
end

#application_commandsObject



73
74
75
# File 'lib/appli.rb', line 73

def application_commands
  @application_commands
end

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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/appli.rb', line 49

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

#commandsObject



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

def commands
  global_commands.merge(application_commands)
end

#desc(value) ⇒ Object



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

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

#flags(key, value) ⇒ Object



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

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

#global_commandsObject



69
70
71
# File 'lib/appli.rb', line 69

def global_commands
  @global_commands
end

#load_commandsObject



100
101
102
103
104
# File 'lib/appli.rb', line 100

def load_commands
  Dir[File.join(File.dirname(__FILE__), 'commands', '*.rb')].each do |path|
    Appli.module_eval File.read(path), path
  end
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/appli.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

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



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

def run(command, args = [])
  load_commands
  command = 'help' if command.nil?
  
  if @global_commands[command]
    run_command(command, :global, args)
  elsif @application_commands[args.first]
    run_command(args.shift, :app, ([command] + args))
  else
    puts "usage: appli [command]"
    puts "Command not found. Check 'appli help' for full information."
    Process.exit(1)
  end  
end

#run_command(command, type, args = []) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/appli.rb', line 34

def run_command(command, type, args = [])
  array = (type == :global ? @global_commands : @application_commands)
  
  options = parse_options(args)    
  options.merge!({:application => args.shift}) if type == :app

  if args.size < array[command][:required_args].to_i
    puts "error: #{array[command][:usage]}"
    puts "See 'cb help #{command}' for usage."
    Process.exit(1)
  end

  array[command][:block].call(options, *args)
end

#usage(value) ⇒ Object



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

def usage(value)
  @next_usage = value
end