Class: Rage::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/rage/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name) ⇒ Object



259
260
261
262
263
264
265
266
267
268
# File 'lib/rage/cli.rb', line 259

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

Returns:

  • (Boolean)


66
67
68
# File 'lib/rage/cli.rb', line 66

def self.exit_on_failure?
  true
end

Instance Method Details

#consoleObject



176
177
178
179
180
181
182
183
184
185
186
# File 'lib/rage/cli.rb', line 176

def console
  return help("console") if options.help?

  set_env(options)

  require "irb"
  environment
  patch_fiber_for_irb
  ARGV.clear
  IRB.start
end

#events(*event_class_names) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/rage/cli.rb', line 199

def events(*event_class_names)
  return help("events") if options.help?

  environment
  Rage::Events.__eager_load_subscribers if Rage.env.development?

  event_classes = if event_class_names.any?
    event_class_names.flat_map { |name| name.split(",") }.map do |event_class_name|
      @last_event_class_name = event_class_name
      Object.const_get(event_class_name)
    end
  else
    registered_events = Rage::Events.__registered_subscribers.keys
    registered_events.reject do |event_class|
      registered_events.any? { |e| e.ancestors.include?(event_class) && e.ancestors.index(event_class) != 0 }
    end
  end

  event_classes.each { |event_class| print_event_subscribers_tree(event_class) }

rescue NameError
  if @last_event_class_name
    spell_checker = DidYouMean::SpellChecker.new(dictionary: Rage::Events.__registered_subscribers.keys)
    suggestion = DidYouMean.formatter.message_for(spell_checker.correct(@last_event_class_name))
    puts "Could not find the `#{@last_event_class_name}` event. #{suggestion}"
  else
    raise
  end
end

#middlewareObject



189
190
191
192
193
194
195
# File 'lib/rage/cli.rb', line 189

def middleware
  environment

  Rage.config.middleware.middlewares.each do |middleware|
    say "use #{middleware.first.name}"
  end
end

#new(path = nil) ⇒ Object



73
74
75
76
77
78
# File 'lib/rage/cli.rb', line 73

def new(path = nil)
  return help("new") if options.help? || path.nil?

  require "rage/all"
  CLINewAppGenerator.start([path, options[:database]])
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


270
271
272
# File 'lib/rage/cli.rb', line 270

def respond_to_missing?(method_name, include_private = false)
  linked_rake_tasks.any? { |task| task.name == method_name.to_s } || super
end

#routesObject



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
164
165
166
167
168
169
170
171
172
# File 'lib/rage/cli.rb', line 114

def routes
  return help("routes") if options.help?
  # the result would be something like this:
  # Verb  Path  Controller#Action
  # GET   /     application#index

  # load config/application.rb
  set_env(options)
  environment

  routes = Rage.__router.routes
  pattern = options[: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|
    meta = route[:constraints]
    meta.merge!(route[:defaults]) if route[:defaults]

    raw_handler = route[:meta][:raw_handler]
    handler = if raw_handler.respond_to?(:__rage_app_name)
      raw_handler.__rage_app_name
    else
      raw_handler
    end
    handler = "#{handler} #{meta}" unless meta&.empty?

    puts format("%-#{longest_method}s%-#{longest_path}s%s", route[:method], route[:path], handler)
    puts "\n" if i == 0
  end
end

#serverObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rage/cli.rb', line 86

def server
  return help("server") if options.help?

  set_env(options)

  app = ::Rack::Builder.parse_file(options[:config] || "config.ru")
  app = app[0] if app.is_a?(Array)

  server_options = { service: :http, handler: app }

  server_options[:port] = options[:port] || ENV["PORT"] || Rage.config.server.port
  server_options[:address] = options[:binding] || (Rage.env.production? ? "0.0.0.0" : "localhost")
  server_options[:timeout] = Rage.config.server.timeout
  server_options[:max_clients] = Rage.config.server.max_clients
  server_options[:public] = Rage.config.public_file_server.enabled ? Rage.root.join("public").to_s : nil

  ::Iodine.listen(**server_options)
  ::Iodine.threads = Rage.config.server.threads_count
  ::Iodine.workers = Rage.config.server.workers_count

  ::Iodine.start
end

#tasksObject



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/rage/cli.rb', line 243

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

#versionObject



230
231
232
# File 'lib/rage/cli.rb', line 230

def version
  puts Rage::VERSION
end