Class: Perennial::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/perennial/application.rb

Defined Under Namespace

Classes: CommandEnv

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Application

Returns a new instance of Application.



45
46
47
48
49
50
51
52
53
54
# File 'lib/perennial/application.rb', line 45

def initialize(opts = {})
  @options         = opts
  @commands        = {}
  @descriptions    = {}
  @option_parsers  = {}
  @option_parser   = nil
  @command_options = {}
  @command_env     = (opts[:command_env] || CommandEnv)
  @banners         = {}
end

Instance Attribute Details

Returns the value of attribute banner.



43
44
45
# File 'lib/perennial/application.rb', line 43

def banner
  @banner
end

#command_envObject

Returns the value of attribute command_env.



43
44
45
# File 'lib/perennial/application.rb', line 43

def command_env
  @command_env
end

#optionsObject

Returns the value of attribute options.



43
44
45
# File 'lib/perennial/application.rb', line 43

def options
  @options
end

Class Method Details

.processing(args, &blk) ⇒ Object



142
143
144
145
146
147
148
149
150
# File 'lib/perennial/application.rb', line 142

def self.processing(args, &blk)
  application = self.new
  if blk.arity == 1
    blk.call(application)
  else
    application.instance_eval(&blk)
  end
  application.execute args
end

Instance Method Details

#add(raw_command, description = nil, &blk) ⇒ Object

Raises:

  • (ArgumentError)


93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/perennial/application.rb', line 93

def add(raw_command, description = nil, &blk)
  raise ArgumentError, "You must provide a block with an #{self.class.name}#add" if blk.nil?
  raise ArgumentError, "Your block must accept atleast one argument (a hash of options)" if blk.arity == 0
  command, _ = raw_command.split(" ", 2)
  @banners[command] = raw_command
  @commands[command]     = blk
  @descriptions[command] = description if description.present?
  # Add the default help message for a command
  option_parser.add(:help, "Show this message") { help_for(command) }
  @option_parsers[command] = option_parser
  @option_parser = nil
end

#add_default_options!Object



60
61
62
# File 'lib/perennial/application.rb', line 60

def add_default_options!
  option_parser.add_defaults!
end

#attempt_showing_bannerObject



152
153
154
155
156
157
# File 'lib/perennial/application.rb', line 152

def attempt_showing_banner
  if banner.present?
    puts banner
    puts ""
  end
end

#controller!(controller, description, opts = {}) ⇒ Object



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

def controller!(controller, description, opts = {})
  return unless defined?(Loader)
  add_default_options!
  option :kill, "Kill any runninng instances"
  controller_name = controller.to_s.underscore
  controller = controller.to_sym
  command_name = controller_name.gsub("_", "-")
  add("#{command_name} #{"[PATH]" if !opts[:skip_path]}".strip, description) do |*args|
    options = args.extract_options!
    path = File.expand_path(args[0] || ".")
    Settings.root = path
    if options.delete(:kill)
      attempt_showing_banner
      puts "Attempting to kill processess for #{command_name}"
      Daemon.kill_all(controller)
    else
      Loader.run!(controller, options)
    end
  end
end

#execute(arguments) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/perennial/application.rb', line 106

def execute(arguments)
  return usage if arguments.empty?
  arguments = arguments.dup
  command = arguments.shift
  if @commands.has_key?(command)
    execute_command(command, arguments)
  else
    show_error "Unknown command '#{command}', please try again."
    usage(true)
  end
end

#generator!Object



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

def generator!
  if defined?(Generator)
    self.command_env = Generator::CommandEnv
  end
end

#help_for(command, skip_banner = false) ⇒ Object



132
133
134
135
136
137
138
139
140
# File 'lib/perennial/application.rb', line 132

def help_for(command, skip_banner = false)
  attempt_showing_banner unless skip_banner
  puts @descriptions[command]
  puts ""
  puts "Usage: #{$0} #{@banners[command]} [options]"
  puts "Options:"
  puts pad_left(@option_parsers[command].summary)
  exit
end

#option(name, description = nil, opts = {}) ⇒ Object



56
57
58
# File 'lib/perennial/application.rb', line 56

def option(name, description = nil, opts = {})
  option_parser.add(name, description, opts) { |v| @command_options[name] = v }
end

#usage(skip_banner = false) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/perennial/application.rb', line 118

def usage(skip_banner = false)
  attempt_showing_banner unless skip_banner
  puts "Usage:"
  max_width = @banners.values.map { |b| b.length }.max
  @commands.keys.sort.each do |command|
    next unless @descriptions.has_key?(command)
    formatted_command = "#{@banners[command]} [OPTIONS]".ljust(max_width + 10)
    command = "  %s - %s" % [formatted_command, @descriptions[command]]
    puts command
  end
  puts ""
  puts "Please note: you can pass -h / --help to any command for more specific help"
end