lib/trinidad/command_line_parser.rb in trinidad-1.2.3 vs lib/trinidad/command_line_parser.rb in trinidad-1.3.0

- old
+ new

@@ -7,44 +7,52 @@ def self.parse(argv) CommandLineParser.new.parse!(argv) end def initialize - @default_options = { - :port => 3000, - :environment => 'development', - :context_path => '', - :libs_dir => 'lib', - :classes_dir => 'classes', - :ssl_port => 8443, - :ajp_port => 8009, - :config => 'config/trinidad.yml' - } + @default_options = {} end def parse!(argv) begin options_parser.parse!(argv) rescue OptionParser::InvalidOption => e p e, options_parser exit(1) end - if default_options.has_key?(:config) - require 'yaml' - require 'erb' - default_options[:config] = File.expand_path(default_options[:config], default_options[:web_app_dir] || Dir.pwd) + load_configuration(default_options) + end - if File.exist?(default_options[:config]) - config_options = YAML.load(ERB.new(File.read(default_options[:config])).result(binding)) - default_options.deep_merge!(config_options.symbolize!) + def load_configuration(options) + base_dir = options[:web_app_dir] || Dir.pwd + config = options.delete(:config) || Dir.glob(File.join(base_dir, 'config', 'trinidad.{yml,rb}')).first + if config and config = File.expand_path(config, base_dir) + if yaml_configuration?(config) + require 'yaml' + require 'erb' + config_options = YAML.load(ERB.new(File.read(config)).result(binding)) + options.deep_merge!(config_options.symbolize!) end end - default_options + Trinidad.configure(options) + if ruby_configuration?(config) + load config + end + + options end + def yaml_configuration?(config) + config && File.exist?(config) && config =~ /\.yml$/ + end + + def ruby_configuration?(config) + config && File.exist?(config) && config =~ /\.rb$/ + end + def options_parser @parser ||= OptionParser.new do |opts| opts.banner = 'Trinidad server default options:' opts.separator '' @@ -77,23 +85,23 @@ "default: #{default_options[:classes_dir]}") do |v| default_options[:classes_dir] = v end opts.on('-s', '--ssl [SSL_PORT]', 'Enable secure socket layout', - "default port: #{default_options[:ssl_port]}") do |v| - ssl_port = v.nil? ? default_options.delete(:ssl_port) : v.to_i + "default port: 8443") do |v| + ssl_port = v.nil? ? 8443 : v.to_i default_options[:ssl] = {:port => ssl_port} end opts.on('-a', '--ajp [AJP_PORT]', 'Enable ajp connections', - "default port: #{default_options[:ajp_port]}") do |v| - ajp_port = v.nil? ? default_options.delete(:ajp_port) : v.to_i + "default port: 8009") do |v| + ajp_port = v.nil? ? 8009 : v.to_i default_options[:ajp] = {:port => ajp_port} end opts.on('-f', '--config [CONFIG_FILE]', 'Configuration file', - "default: #{default_options[:config]}") do |file| - default_options[:config] = file unless file.nil? + "default: config/trinidad.yml") do |file| + default_options[:config] = file || 'config/trinidad.yml' end opts.on('-r', '--rackup [RACKUP_FILE]', 'Rackup configuration file', 'default: config.ru') do |v| default_options[:rackup] = v || 'config.ru'