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



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

Returns:

  • (Boolean)


64
65
66
# File 'lib/rage/cli.rb', line 64

def self.exit_on_failure?
  true
end

Instance Method Details

#consoleObject



167
168
169
170
171
172
173
174
175
176
177
# File 'lib/rage/cli.rb', line 167

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

  set_env(options)

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

#middlewareObject



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 options.help? || path.nil?

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

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

Returns:

  • (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

#routesObject



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 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]

    handler = route[:meta][:raw_handler]
    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



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 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



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

#versionObject



189
190
191
# File 'lib/rage/cli.rb', line 189

def version
  puts Rage::VERSION
end