Class: Rage::CLI::App

Inherits:
Base
  • 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



211
212
213
214
215
216
217
218
219
220
# File 'lib/rage/cli.rb', line 211

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)


15
16
17
# File 'lib/rage/cli.rb', line 15

def self.exit_on_failure?
  true
end

Instance Method Details

#consoleObject



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/rage/cli.rb', line 125

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



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
173
174
175
176
# File 'lib/rage/cli.rb', line 148

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



138
139
140
141
142
143
144
# File 'lib/rage/cli.rb', line 138

def middleware
  environment

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

#new(path = nil) ⇒ Object



22
23
24
25
26
27
# File 'lib/rage/cli.rb', line 22

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

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

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

Returns:

  • (Boolean)


222
223
224
# File 'lib/rage/cli.rb', line 222

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

#routesObject



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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/rage/cli.rb', line 63

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rage/cli.rb', line 35

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



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/rage/cli.rb', line 195

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



179
180
181
# File 'lib/rage/cli.rb', line 179

def version
  puts Rage::VERSION
end