Class: VhostGenerator::Application

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

Overview

VhostGenerator main application object. When invoking vhost-generator from the command line, a VhostGenerator::Application object is created and run.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output_stream = $stdout) ⇒ Application

Returns a new instance of Application.



16
17
18
19
# File 'lib/vhost_generator/application.rb', line 16

def initialize(output_stream=$stdout)
  @name = File.basename($0 || 'vhost-generator')
  @output_stream = output_stream
end

Instance Attribute Details

#config(configurator = VhostGenerator::VhostConfiguration) ⇒ Object



31
32
33
# File 'lib/vhost_generator/application.rb', line 31

def config(configurator=VhostGenerator::VhostConfiguration)
  @config ||= configurator.new # XXX
end

Instance Method Details

#application_optionsObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/vhost_generator/application.rb', line 94

def application_options
  [
    ['-a', '--application APPLICATION',
       %q{Unique name of your application (e.g. myapp)},
       lambda { |value| config.application = value }],
    ['-f', '--static-folder STATIC_FOLDER',
            %q{Path of your application's static folder (e.g. public/)},
            lambda { |value| config.static_folder = value }],
    ['-l', '--listen SERVER_PORTS',
            %q{Public ports to listen on (e.g. 80,81)},
            lambda { |value| config.server_ports = value }],
    ['-s', '--server-name SERVER_NAMES',
            %q{Server names to listen on (e.g. localhost,example.com)},
            lambda { |value| config.server_names = value }],
    ['-p', '--instance-port INSTANCE_PORTS',
            %q{Internal ports where instances listen on (e.g. 5000,5001)},
            lambda { |value| config.instance_ports = value }],
    ['-r', '--relative-root RAILS_RELATIVE_URL_ROOT',
           lambda { |value| config.relative_root = value }],
  ]
end

#display_error_message(ex) ⇒ Object

Display the error message that caused the exception.



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

def display_error_message(ex)
  $stderr.puts "#{@name} aborted!"
  $stderr.puts ex.message
  $stderr.puts ex.backtrace.join("\n")
end

#generator_optionsObject



116
117
118
119
120
121
122
123
124
125
# File 'lib/vhost_generator/application.rb', line 116

def generator_options
  [
    ['-g', '--generator GENERATOR',
           %q{Generator to use to output virtualhost configuration file},
           lambda { |value| config.generator = value }],
    ['-o', '--generator-options GENERATOR_OPTIONS',
            %q{Generator options as comma-separated list of key=value},
            lambda { |value| config.generator_options = value }],
  ].freeze
end

#handle_env(env) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/vhost_generator/application.rb', line 35

def handle_env(env)
  if app = env['APPLICATION']
    config.application = app
  end
  if path = env['STATIC_FOLDER']
    config.static_folder = path
  end
  if ports = env['SERVER_PORTS']
    config.server_ports = ports
  end
  if names = env['SERVER_NAMES']
    config.server_names = names
  end
  if ports = env['INSTANCE_PORTS']
    config.instance_ports = ports
  end
  if root = env['RAILS_RELATIVE_URL_ROOT']
    config.relative_root = root
  end
  if generator = env['GENERATOR']
    config.generator = generator
  end
  if options = env['GENERATOR_OPTIONS']
    config.generator_options = options
  end
end

#handle_options(argv) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/vhost_generator/application.rb', line 62

def handle_options(argv)
  OptionParser.new do |opts|
    opts.banner = "Usage: #{@name} [options]"

    opts.separator ""
    opts.separator ['Note: all command-line options below also exist as ' \
                    'environment variables.', 'You may try to setenv all ' \
                    'uppercase names in the rest of this summary, eg.',
                    '`export RAILS_RELATIVE_URL_ROOT=/myapp`.'].join($/)

    opts.separator ""
    opts.separator "Application options:"
    application_options.each { |args| opts.on(*args) }

    opts.separator ""
    opts.separator "Generator options:"
    generator_options.each { |args| opts.on(*args) }

    opts.separator ""

    opts.on_tail("-v", "--version", "Display the program version.") do
      @output_stream.puts "#{@name}, version #{VhostGenerator::VERSION}"
      exit(true)
    end

    opts.on_tail("-h", "--help", "-H", "Display this help message.") do
      @output_stream.puts opts
      exit(true)
    end
  end.parse(argv)
end

#runObject

Run the VhostGenerator application.



22
23
24
25
26
27
28
29
# File 'lib/vhost_generator/application.rb', line 22

def run
  standard_exception_handling do
    handle_env(ENV)
    handle_options(ARGV)
    config.cmdline = CmdlineBuilder.new(config, Dir.pwd, $0, ENV)
    @output_stream.puts config.output
  end
end

#standard_exception_handlingObject

Provide standard exception handling for the given block.



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/vhost_generator/application.rb', line 128

def standard_exception_handling
  begin
    yield
  rescue SystemExit => ex
    # Exit silently with current status
    raise
  rescue OptionParser::InvalidOption, ArgumentError => ex
    $stderr.puts ex.message
    exit(false)
  rescue Exception => ex
    # Exit with error message
    display_error_message(ex)
    exit(false)
  end
end