Class: Rage::CLI
- Inherits:
-
Thor
- Object
- Thor
- Rage::CLI
- Defined in:
- lib/rage/cli.rb
Class Method Summary collapse
Instance Method Summary collapse
- #console ⇒ Object
- #method_missing(method_name) ⇒ Object
- #middleware ⇒ Object
- #new(path = nil) ⇒ Object
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
- #routes ⇒ Object
- #server ⇒ Object
- #tasks ⇒ Object
- #version ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name) ⇒ Object
215 216 217 218 219 220 221 222 223 224 |
# File 'lib/rage/cli.rb', line 215 def method_missing(method_name, *, &) set_env({}) if respond_to?(method_name) Rake::Task[method_name].invoke else suggestions = linked_rake_tasks.map(&:name) raise UndefinedCommandError.new(method_name.to_s, suggestions, nil) end end |
Class Method Details
.exit_on_failure? ⇒ Boolean
64 65 66 |
# File 'lib/rage/cli.rb', line 64 def self.exit_on_failure? true end |
Instance Method Details
#console ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/rage/cli.rb', line 167 def console return help("console") if .help? set_env() require "irb" environment patch_fiber_for_irb ARGV.clear IRB.start end |
#middleware ⇒ Object
180 181 182 183 184 185 186 |
# File 'lib/rage/cli.rb', line 180 def middleware environment Rage.config.middleware.middlewares.each do |middleware| say "use #{middleware.first.name}" end end |
#new(path = nil) ⇒ Object
71 72 73 74 75 76 |
# File 'lib/rage/cli.rb', line 71 def new(path = nil) return help("new") if .help? || path.nil? require "rage/all" CLINewAppGenerator.start([path, [:database]]) end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
226 227 228 |
# File 'lib/rage/cli.rb', line 226 def respond_to_missing?(method_name, include_private = false) linked_rake_tasks.any? { |task| task.name == method_name.to_s } || super end |
#routes ⇒ Object
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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/rage/cli.rb', line 110 def routes return help("routes") if .help? # the result would be something like this: # Verb Path Controller#Action # GET / application#index # load config/application.rb set_env() environment routes = Rage.__router.routes pattern = [:grep] routes.unshift({ method: "Verb", path: "Path", meta: { raw_handler: "Controller#Action" } }) grouped_routes = routes.each_with_object({}) do |route, memo| if pattern && !memo.empty? next unless route[:path].match?(pattern) || route[:meta][:raw_handler].to_s.match?(pattern) || route[:method].match?(pattern) end key = [route[:path], route[:meta][:raw_handler]] if route[:meta][:mount] memo[key] = route.merge(method: "") unless route[:path].end_with?("*") next end if memo[key] memo[key][:method] += "|#{route[:method]}" else memo[key] = route end end longest_path = longest_method = 0 grouped_routes.each do |_, route| longest_path = route[:path].length if route[:path].length > longest_path longest_method = route[:method].length if route[:method].length > longest_method end margin = 3 longest_path += margin longest_method += margin grouped_routes.each_with_index do |(_, route), i| = route[:constraints] .merge!(route[:defaults]) if route[:defaults] handler = route[:meta][:raw_handler] handler = "#{handler} #{}" unless &.empty? puts format("%-#{longest_method}s%-#{longest_path}s%s", route[:method], route[:path], handler) puts "\n" if i == 0 end end |
#server ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/rage/cli.rb', line 84 def server return help("server") if .help? set_env() app = ::Rack::Builder.parse_file([:config] || "config.ru") app = app[0] if app.is_a?(Array) = { service: :http, handler: app } [:port] = [:port] || ENV["PORT"] || Rage.config.server.port [:address] = [:binding] || (Rage.env.production? ? "0.0.0.0" : "localhost") [:timeout] = Rage.config.server.timeout [:max_clients] = Rage.config.server.max_clients [:public] = Rage.config.public_file_server.enabled ? Rage.root.join("public").to_s : nil ::Iodine.listen(**) ::Iodine.threads = Rage.config.server.threads_count ::Iodine.workers = Rage.config.server.workers_count ::Iodine.start end |
#tasks ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/rage/cli.rb', line 199 def tasks require "io/console" tasks = linked_rake_tasks return if tasks.empty? _, max_width = IO.console.winsize max_task_name = tasks.max_by { |task| task.name.length }.name.length + 2 max_comment = max_width - max_task_name - 8 tasks.each do |task| comment = task.comment.length <= max_comment ? task.comment : "#{task.comment[0...max_comment - 5]}..." puts sprintf("rage %-#{max_task_name}s # %s", task.name, comment) end end |