Module: Loops::CLI::Options
- Included in:
- Loops::CLI
- Defined in:
- lib/loops/cli/options.rb
Overview
Contains methods to parse startup options, bootstrap application, and prepare #Loops::CLI class to run.
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- COMMANDS_HELP =
<<-HELP Available commands: list List available loops (based on config file) start Start all loops except ones marked with disabled:true in config start loop1 [loop2] Start only loops specified stop Stop daemonized loops monitor stats Print loops memory statistics debug loop Debug specified loop help Show this message HELP
- SPLIT_HELP_LINE =
"\n#{' ' * 37}"
Instance Attribute Summary collapse
-
#engine ⇒ Engine
readonly
The loops engine instance.
-
#options ⇒ Hash<Symbol, Object>
readonly
The hash of (parsed) command-line options.
Class Method Summary collapse
Instance Method Summary collapse
-
#bootstrap! ⇒ String
Application bootstrap.
-
#extract_command! ⇒ String
Extracts command name from arguments.
-
#guess_root_dir ⇒ String
Detect the application root directory (contatining “app” subfolder).
-
#option_parser ⇒ OptionParser
Returns an option parser configured with all options available.
-
#parse_options! ⇒ Hash
Parses startup options, bootstraps application, starts loops engine.
-
#start_engine! ⇒ Engine
Initializes a loops engine instance.
Instance Attribute Details
#engine ⇒ Engine (readonly)
Returns The loops engine instance.
40 41 42 |
# File 'lib/loops/cli/options.rb', line 40 def engine @engine end |
#options ⇒ Hash<Symbol, Object> (readonly)
Returns The hash of (parsed) command-line options.
36 37 38 |
# File 'lib/loops/cli/options.rb', line 36 def @options end |
Class Method Details
.included(base) ⇒ Object
14 15 16 |
# File 'lib/loops/cli/options.rb', line 14 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#bootstrap! ⇒ String
Application bootstrap.
Checks framework option passed and load application stratup files conrresponding to its value. Also intitalizes the Loops.default_logger variable with the framework’s default logger value.
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/loops/cli/options.rb', line 211 def bootstrap! loops_env = ENV['LOOPS_ENV'] = [:environment] if [:environment] [:require].each do |library| require library end case [:framework] when 'rails' ENV['RAILS_ENV'] = loops_env # Bootstrap Rails require Loops.root + 'config/boot' require Loops.root + 'config/environment' # Loops default logger Loops.default_logger = Rails.logger when 'merb' require 'merb-core' ENV['MERB_ENV'] = loops_env # Bootstrap Merb Merb.start_environment(:adapter => 'runner', :environment => ENV['MERB_ENV'] || 'development') # Loops default logger Loops.default_logger = Merb.logger when 'none' then # Plain ruby loops Loops.default_logger = Loops::Logger.new($stdout) else raise InvalidFrameworkError, "Invalid framework name: #{[:framework]}. Valid values are: none, rails, merb." end .delete(:environment) .delete(:framework) end |
#extract_command! ⇒ String
Extracts command name from arguments.
Other parameters are stored in the :args
option of the #options hash.
153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/loops/cli/options.rb', line 153 def extract_command! [:command], *[:args] = args if [:command].nil? || [:command] == 'help' puts option_parser exit end unless command = find_command([:command]) STDERR << option_parser exit end command end |
#guess_root_dir ⇒ String
Detect the application root directory (contatining “app” subfolder).
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/loops/cli/options.rb', line 173 def guess_root_dir # Check for environment variable LOOP_ROOT containing # the application root folder return [:root] = ENV['LOOPS_ROOT'] if ENV['LOOPS_ROOT'] # Check root parameter return [:root] if [:root] # Try to detect root dir (should contain app subfolder) current_dir = Dir.pwd loop do if File.directory?(File.join(current_dir, 'app')) # Found it! return [:root] = current_dir end # Move up the FS hierarhy pwd = File.(File.join(current_dir, '..')) break if pwd == current_dir # if changing the directory made no difference, then we're at the top current_dir = pwd end # Oops, not app folder found. Use the current dir as the root current_dir = Dir.pwd [:root] = current_dir end |
#option_parser ⇒ OptionParser
Returns an option parser configured with all options available.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 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 93 |
# File 'lib/loops/cli/options.rb', line 48 def option_parser @option_parser ||= OptionParser.new do |opt| opt. = "Usage: #{File.basename($0)} command [arg1 [arg2]] [options]" opt.separator '' opt.separator COMMANDS_HELP opt.separator '' opt.separator 'Specific options:' opt.on('-c', '--config=file', 'Configuration file') do |config_file| [:config_file] = config_file end opt.on('-d', '--daemonize', 'Daemonize when all loops started') do |value| [:daemonize] = true end opt.on('-e', '--environment=env', 'Set RAILS_ENV (MERB_ENV) value') do |env| [:environment] = env end opt.on('-f', '--framework=name', "Bootstraps Rails (rails - default value) or Merb (merb) before#{SPLIT_HELP_LINE}starting loops. Use \"none\" for plain ruby loops.") do |framework| [:framework] = framework end opt.on('-l', '--loops=dir', 'Root directory with loops classes') do |loops_root| [:loops_root] = loops_root end opt.on('-p', '--pid=file', 'Override loops.yml pid_file option') do |pid_file| [:pid_file] = pid_file end opt.on('-r', '--root=dir', 'Root directory which will be used as a loops home dir (chdir)') do |root| [:root] = root end opt.on('-Rlibrary', '--require=library', 'require the library before executing the script') do |library| [:require] << library end opt.on_tail("-h", '--help', 'Show this message') do puts(opt) exit(0) end end end |
#parse_options! ⇒ Hash
Parses startup options, bootstraps application, starts loops engine.
Method exits process when unknown option passed or invalid value specified.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/loops/cli/options.rb', line 103 def @options = { :daemonize => false, :config_file => 'config/loops.yml', :environment => nil, :framework => 'rails', :loops_root => 'app/loops', :pid_file => nil, :root => nil, :require => [], } begin option_parser.parse!(args) rescue OptionParser::ParseError => e STDERR.puts e. STDERR << "\n" << option_parser exit end # Root directory guess_root_dir Loops.root = .delete(:root) Dir.chdir(Loops.root) # Config file Loops.config_file = .delete(:config_file) # Loops root Loops.loops_root = .delete(:loops_root) @command = extract_command! [:framework] = 'none' unless @command.requires_bootstrap? bootstrap! start_engine! # Pid file Loops.pid_file = .delete(:pid_file) @options end |
#start_engine! ⇒ Engine
Initializes a loops engine instance.
Method loads and parses loops config file, and then initializes pid file path.
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/loops/cli/options.rb', line 256 def start_engine! # Start loops engine @engine = Loops::Engine.new # If pid file option is not passed, get if from loops config ... unless [:pid_file] ||= @engine.global_config['pid_file'] # ... or try Rails' tmp/pids folder ... [:pid_file] = if Loops.root.join('tmp/pids').directory? 'tmp/pids/loops.pid' else # ... or use global system pids folder '/var/run/loops.pid' end end @engine end |