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



194
195
196
197
198
199
200
201
202
203
# File 'lib/rage/cli.rb', line 194

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)


44
45
46
# File 'lib/rage/cli.rb', line 44

def self.exit_on_failure?
  true
end

Instance Method Details

#consoleObject



147
148
149
150
151
152
153
154
155
156
# File 'lib/rage/cli.rb', line 147

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

  set_env(options)

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

#middlewareObject



159
160
161
162
163
164
165
# File 'lib/rage/cli.rb', line 159

def middleware
  environment

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

#new(path = nil) ⇒ Object



51
52
53
54
55
56
# File 'lib/rage/cli.rb', line 51

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)


205
206
207
# File 'lib/rage/cli.rb', line 205

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

#routesObject



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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/rage/cli.rb', line 90

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



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rage/cli.rb', line 64

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



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/rage/cli.rb', line 178

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



168
169
170
# File 'lib/rage/cli.rb', line 168

def version
  puts Rage::VERSION
end